Debian Codegraph @

types/transport/transport.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 Transport class of objects.
17 * A transport is basically little more than a container in which players
18 * can enter, and that can be moved around on the map.
19 */
20
21 #include "global.h"
22
23 #include <stdlib.h>
24
25 #include "ob_methods.h"
26 #include "ob_types.h"
27 #include "shop.h"
28 #include "sounds.h"
29 #include "sproto.h"
30
31 static method_ret transport_type_apply(ob_methods *context, object *op, object *applier, int aflags);
32 static method_ret transport_type_process(ob_methods *context, object *op);
33
34 /**
35 * Initializer for the TRANSPORT object type.
36 */
37 void init_type_transport(void) {
38 register_apply(TRANSPORT, transport_type_apply);
39 register_process(TRANSPORT, transport_type_process);
40 }
41
42 /**
43 * Player is trying to use a transport. This returns same values as
44 * apply_manual() does. This function basically checks to see if
45 * the player can use the transport, and if so, sets up the appropriate
46 * pointers.
47 * @param context The method context
48 * @param op The Transport to apply
49 * @param applier The object attempting to apply the Transport
50 * @param aflags Special flags (always apply/unapply)
51 */
52 static method_ret transport_type_apply(ob_methods *context, object *op, object *applier, int aflags) {
53 object *old_transport = applier->contr->transport;
54 object *inv;
55 char name_op[MAX_BUF], name_old[MAX_BUF];
56
57 /* Only players can use transports right now */
58 if (applier->type != PLAYER)
59 return METHOD_UNHANDLED;
60
61 query_name(op, name_op, MAX_BUF);
62
63 /* If player is currently on a transport but not this transport, they need
64 * to exit first. Perhaps transport to transport transfers should be
65 * allowed.
66 */
67 if (old_transport && old_transport != op) {
68 query_name(old_transport, name_old, MAX_BUF);
69 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
70 "You must exit %s before you can board %s.",
71 name_old, name_op);
72 return METHOD_SILENT_ERROR;
73 }
74
75 /* player is currently on a transport. This must mean he
76 * wants to exit.
77 */
78 if (old_transport) {
79 /* Should we print a message if the player only wants to
80 * apply?
81 */
82 if (aflags&AP_APPLY)
83 return 1;
84
85 query_name(old_transport, name_old, MAX_BUF);
86 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_UNAPPLY,
87 "You disembark from %s.",
88 name_old);
89 object_remove(applier);
90 if (applier->contr == old_transport->contr)
91 old_transport->contr = NULL;
92
93 applier->contr->transport = NULL;
94 object_insert_in_map_at(applier, old_transport->map, applier, 0, old_transport->x, old_transport->y);
95 if ( applier->map ) applier->map->players--; // Incremented player count incorrectly; adjust to correct
96 object_sum_weight(old_transport);
97
98 /* Possible for more than one player to be using a transport.
99 * if that is the case, we don't want to reset the face, as the
100 * transport is still occupied.
101 */
102 inv = object_find_by_type(old_transport, PLAYER);
103 if (!inv) {
104 old_transport->face = old_transport->arch->clone.face;
105 old_transport->animation = old_transport->arch->clone.animation;
106 } else {
107 old_transport->contr = inv->contr;
108 draw_ext_info_format(NDI_UNIQUE, 0, inv, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
109 "%s has disembarked. You are now the captain of %s",
110 applier->name, name_old);
111 }
112 return METHOD_OK;
113 } else {
114 /* player is trying to board a transport */
115 int pc = 0, p_limit;
116 const char *kv;
117 int16_t ox, oy;
118
119 if (aflags&AP_UNAPPLY)
120 return 1;
121
122 /* Can this transport hold the weight of this player? */
123 if (!transport_can_hold(op, applier, 1)) {
124 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
125 "The %s is unable to hold your weight!",
126 name_op);
127 return METHOD_SILENT_ERROR;
128 }
129
130 /* If the player is holding the transport, drop it. */
131 if (op->env == applier) {
132 old_transport = op;
133 /* Don't drop transports in shops. */
134 if (!shop_contains(applier)) {
135 op = drop_object(applier, op, 1);
136 } else {
137 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
138 "You cannot drop the %s in a shop to use it.",
139 name_old);
140 return METHOD_SILENT_ERROR;
141 }
142 /* Did it fail to drop? */
143 if (!op) {
144 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
145 "You need to drop the %s to use it.",
146 name_old);
147 return METHOD_SILENT_ERROR;
148 }
149 }
150
151 /* Does this transport have space for more players? */
152 FOR_INV_PREPARE(op, inv)
153 if (inv->type == PLAYER)
154 pc++;
155 FOR_INV_FINISH();
156
157 kv = object_get_value(op, "passenger_limit");
158 if (!kv)
159 p_limit = 1;
160 else
161 p_limit = atoi(kv);
162 if (pc >= p_limit) {
163 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_ERROR,
164 "The %s does not have space for any more people",
165 name_op);
166 return METHOD_SILENT_ERROR;
167 }
168
169 /* Everything checks out OK - player can get on the transport */
170 applier->contr->transport = op;
171
172 if (op->contr) {
173 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
174 "The %s's captain is currently %s",
175 name_op, op->contr->ob->name);
176 } else {
177 draw_ext_info_format(NDI_UNIQUE, 0, applier, MSG_TYPE_APPLY, MSG_TYPE_APPLY_SUCCESS,
178 "You're the %s's captain",
179 name_op);
180 op->contr = applier->contr;
181 }
182
183 if ( applier->map ) applier->map->players++; // About to subtract the player incorrectly
184 object_remove(applier);
185 /* object_insert_in_ob clear applier->x and applier->y, so store them away */
186 ox = applier->x;
187 oy = applier->y;
188 object_insert_in_ob(applier, op);
189 object_sum_weight(op);
190 applier->map = op->map;
191 if (ox != op->x || oy != op->y) {
192 esrv_map_scroll(&applier->contr->socket, (ox-op->x), (oy-op->y));
193 }
194 applier->contr->socket.update_look = 1;
195 applier->contr->socket.look_position = 0;
196 applier->x = op->x;
197 applier->y = op->y;
198
199 /* Might need to update face, animation info */
200 if (!pc) {
201 const char *str;
202
203 str = object_get_value(op, "face_full");
204 if (str)
205 op->face = find_face(str, op->face);
206 str = object_get_value(op, "anim_full");
207 if (str)
208 op->animation = find_animation(str);
209 }
210
211 /* Does speed of this object change based on weight? */
212 kv = object_get_value(op, "weight_speed_ratio");
213 if (kv) {
214 int wsr = atoi(kv);
215 float base_speed;
216
217 kv = object_get_value(op, "base_speed");
218 if (kv)
219 base_speed = atof(kv);
220 else
221 base_speed = op->arch->clone.speed;
222
223 op->speed = base_speed-(base_speed*op->carrying*wsr)/(op->weight_limit*100);
224
225 /* Put some limits on min/max speeds */
226 if (op->speed < 0.10)
227 op->speed = 0.10;
228 if (op->speed > 1.0)
229 op->speed = 1.0;
230 }
231 } /* else if player is boarding the transport */
232 return METHOD_OK;
233 }
234
235 /**
236 * Processes a Transport.
237 * @param context The method context
238 * @param op The Transport to process
239 * @retval 0 If the remaining speed of the transport was > 0.0
240 * @retval 1 If the remaining speed of the transport was < 0.0
241 */
242 static method_ret transport_type_process(ob_methods *context, object *op) {
243 /* Transports are directed by players - thus, there
244 * speed is reduced when the player moves them about.
245 * So give them back there speed here, since process_objects()
246 * has decremented it.
247 */
248 if (op->speed_left < 0.0) {
249 op->speed_left += 1.0;
250 return 1;
251 }
252 return 0;
253 }