Debian Codegraph @

types/exit/exit.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2014 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 /**
15 * @file
16 * The implementation of the Exit class of objects.
17 */
18 #include "global.h"
19
20 #include <string.h>
21
22 #include "ob_methods.h"
23 #include "ob_types.h"
24 #include "sounds.h"
25 #include "sproto.h"
26
27 static method_ret exit_type_move_on(ob_methods *context, object *trap, object *victim, object *originator);
28 static method_ret exit_type_apply(ob_methods *context, object *exit, object *op, int autoapply);
29
30 /**
31 * Initializer for the EXIT object type.
32 */
33 void init_type_exit(void) {
34 register_move_on(EXIT, exit_type_move_on);
35 register_apply(EXIT, exit_type_apply);
36 }
37
38 /**
39 * Move on this Exit object.
40 * @param context The method context
41 * @param trap The Exit we're moving on
42 * @param victim The object moving over this one
43 * @param originator The object that caused the move_on event
44 * @return METHOD_OK
45 */
46 static method_ret exit_type_move_on(ob_methods *context, object *trap, object *victim, object *originator) {
47 if (common_pre_ob_move_on(trap, victim, originator) == METHOD_ERROR)
48 return METHOD_OK;
49 if (victim->type == PLAYER && EXIT_PATH(trap)) {
50 /* Basically, don't show exits leading to random maps the
51 * players output.
52 */
53 if (trap->msg
54 && strncmp(EXIT_PATH(trap), "/!", 2)
55 && strncmp(EXIT_PATH(trap), "/random/", 8))
56 draw_ext_info(NDI_NAVY, 0, victim, MSG_TYPE_APPLY, MSG_TYPE_APPLY_TRAP, trap->msg);
57 enter_exit(victim, trap);
58 }
59 common_post_ob_move_on(trap, victim, originator);
60 return METHOD_OK;
61 }
62
63 /**
64 * This fonction return true if the exit is not a 2 ways one
65 * or it is 2 ways, valid exit.
66 * A valid 2 way exit means:
67 * - You can come back (there is another exit at the other side)
68 * - You are
69 * - the owner of the exit
70 * - or in the same party as the owner
71 *
72 * @note
73 * a owner in a 2 way exit is saved as the owner's name
74 * in the field exit->name cause the field exit->owner doesn't
75 * survive in the swapping (in fact the whole exit doesn't survive).
76 *
77 * @param op
78 * player to check for.
79 * @param exit
80 * exit object.
81 * @return
82 * 1 if exit is not 2 way, 0 else.
83 */
84 static int is_legal_2ways_exit(object *op, object *exit) {
85 object *tmp;
86 object *exit_owner;
87 player *pp;
88 mapstruct *exitmap;
89
90 if (exit->stats.exp != 1)
91 return 1; /*This is not a 2 way, so it is legal*/
92 if (!has_been_loaded(EXIT_PATH(exit)) && exit->race)
93 return 0; /* This is a reset town portal */
94 /* To know if an exit has a correspondent, we look at
95 * all the exits in destination and try to find one with same path as
96 * the current exit's position */
97 if (!strncmp(EXIT_PATH(exit), settings.localdir, strlen(settings.localdir)))
98 exitmap = ready_map_name(EXIT_PATH(exit), MAP_PLAYER_UNIQUE);
99 else
100 exitmap = ready_map_name(EXIT_PATH(exit), 0);
101 if (exitmap) {
102 tmp = GET_MAP_OB(exitmap, EXIT_X(exit), EXIT_Y(exit));
103 if (!tmp)
104 return 0;
105 for ((tmp = GET_MAP_OB(exitmap, EXIT_X(exit), EXIT_Y(exit))); tmp; tmp = tmp->above) {
106 if (tmp->type != EXIT)
107 continue; /*Not an exit*/
108 if (!EXIT_PATH(tmp))
109 continue; /*Not a valid exit*/
110 if ((EXIT_X(tmp) != exit->x) || (EXIT_Y(tmp) != exit->y))
111 continue; /*Not in the same place*/
112 if (strcmp(exit->map->path, EXIT_PATH(tmp)) != 0)
113 continue; /*Not in the same map*/
114
115 /* From here we have found the exit is valid. However
116 * we do here the check of the exit owner. It is important
117 * for the town portals to prevent strangers from visiting
118 * your apartments
119 */
120 if (!exit->race)
121 return 1; /*No owner, free for all!*/
122 exit_owner = NULL;
123 for (pp = first_player; pp; pp = pp->next) {
124 if (!pp->ob)
125 continue;
126 if (pp->ob->name != exit->race)
127 continue;
128 exit_owner = pp->ob; /*We found a player which correspond to the player name*/
129 break;
130 }
131 if (!exit_owner)
132 return 0; /* No more owner*/
133 if (exit_owner->contr == op->contr)
134 return 1; /*It is your exit*/
135 if (exit_owner /*There is a owner*/
136 && (op->contr) /*A player tries to pass */
137 && ((exit_owner->contr->party == NULL) /*No pass if controller has no party*/
138 || (exit_owner->contr->party != op->contr->party))) /* Or not the same as op*/
139 return 0;
140 return 1;
141 }
142 }
143 return 0;
144 }
145
146 /**
147 * Handles applying an exit.
148 * @param context The method context
149 * @param exit The exit applied
150 * @param op The object applying the exit
151 * @param autoapply Set this to 1 to automatically apply the sign
152 * @return METHOD_OK unless op is not a player, in which case METHOD_ERROR
153 */
154 static method_ret exit_type_apply(ob_methods *context, object *exit, object *op, int autoapply) {
155 if (op->type != PLAYER)
156 return METHOD_ERROR;
157 if (!EXIT_PATH(exit) || !is_legal_2ways_exit(op, exit)) {
158 char name[MAX_BUF];
159
160 query_name(exit, name, MAX_BUF);
161 draw_ext_info_format(NDI_UNIQUE, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_FAILURE,
162 "The %s is closed.", name);
163 } else {
164 /* Don't display messages for random maps. */
165 if (exit->msg
166 && strncmp(EXIT_PATH(exit), "/!", 2)
167 && strncmp(EXIT_PATH(exit), "/random/", 8))
168 draw_ext_info(NDI_NAVY, 0, op, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS, exit->msg);
169 enter_exit(op, exit);
170 }
171 return METHOD_OK;
172 }