home · contact · privacy
978683292af5b76ab902e8cb5f3525b540bd9d1c
[plomrogue] / src / server / map_object_actions.c
1 /* src/server/map_object_actions.c */
2
3 #include "map_object_actions.h"
4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t */
6 #include <stdio.h> /* sprintf() */
7 #include <stdlib.h> /* free() */
8 #include <string.h> /* strlen(), strcmp(), memcpy(), strncmp() */
9 #include "../common/rexit.h" /* exit_err() */
10 #include "../common/try_malloc.h" /* try_malloc() */
11 #include "../common/yx_uint8.h" /* struct yx_uint8 */
12 #include "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
13                           * set_object_position(), own_map_object(),
14                           * get_map_object_def()
15                           */
16 #include "map.h" /* is_passable() */
17 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
18 #include "world.h" /* global world */
19
20
21
22 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
23 static void update_log(char * text);
24
25 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
26  * reach zero in the process, killing it. Generates appropriate log message.
27  */
28 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
29
30 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
31  * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
32  * (match_dir() is just a little helper to playerbonus_move().)
33  */
34 static void playerbonus_wait();
35 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
36 static void playerbonus_move(char d, uint8_t passable);
37 static void playerbonus_drop(uint8_t owns_none);
38 static void playerbonus_pick(uint8_t picked);
39 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
40
41
42
43 static void update_log(char * text)
44 {
45     char * f_name = "update_log()";
46     uint16_t len_new = strlen(text);
47     uint16_t len_old = 0;
48     if (world.log)
49     {
50         len_old = strlen(world.log);
51         uint16_t last_nl = len_old - 1;
52         while (last_nl != 0)
53         {
54             if ('\n' == world.log[last_nl])
55             {
56                 break;
57             }
58             last_nl--;
59         }
60         uint16_t last_stop = len_old - 1;
61         while (last_stop != 0)
62         {
63             if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
64             {
65                 break;
66             }
67             last_stop--;
68         }
69         if (   (last_stop + 1) - last_nl == (uint16_t) strlen(text)
70             && 0 == strncmp(world.log + last_nl, text, strlen(text)))
71         {
72             text = ".";
73         }
74     }
75     uint16_t len_whole = len_old + len_new + 1;
76     char * new_text = try_malloc(len_whole, f_name);
77     memcpy(new_text, world.log, len_old);
78     sprintf(new_text + len_old, "%s", text);
79     free(world.log);
80     world.log = new_text;
81 }
82
83
84
85 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
86 {
87     struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
88     struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
89     struct MapObj * player = get_player();
90     char * msg1 = "You";
91     char * msg2 = "wound";
92     char * msg3 = "you";
93     if      (player != hitter)
94     {
95         msg1 = mod_hitter->name;
96         msg2 = "wounds";
97     }
98     if (player != hitted)
99     {
100         msg3 = mod_hitted->name;
101     }
102     uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
103     char msg[len];
104     sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
105     update_log(msg);
106     hitted->lifepoints--;
107     if (0 == hitted->lifepoints)
108     {
109         hitted->type = mod_hitted->corpse_id;
110         if (player == hitted)
111         {
112             update_log(" You die.");
113             return;
114         }
115         update_log(" It dies.");
116     }
117 }
118
119
120
121 static void playerbonus_wait()
122 {
123         update_log("\nYou wait.");
124 }
125
126
127
128 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
129 {
130     if (d == match)
131     {
132         * dsc_d = dsc_match;
133         return 1;
134     }
135     return 0;
136 }
137
138
139
140 static void playerbonus_move(char d, uint8_t passable)
141 {
142     char * dsc_dir = "north-east";
143     if (   match_dir(d, &dsc_dir, 'd', "east")
144         || match_dir(d, &dsc_dir, 'c', "south-east")
145         || match_dir(d, &dsc_dir, 'x', "south-west")
146         || match_dir(d, &dsc_dir, 's', "west")
147         || match_dir(d, &dsc_dir, 'w', "north-west"))
148     {
149         ;
150     }
151     char * dsc_move = "You move ";
152     if (0 == passable)
153     {
154         dsc_move = "You fail to move ";
155     }
156     char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
157     sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
158     update_log(msg);
159 }
160
161
162
163 static void playerbonus_drop(uint8_t owns_none)
164 {
165     if (0 != owns_none)
166     {
167         update_log("\nYou try to drop an object, but you own none.");
168         return;
169     }
170     update_log("\nYou drop an object.");
171 }
172
173
174
175 static void playerbonus_pick(uint8_t picked)
176 {
177     if (picked)
178     {
179         update_log("\nYou pick up an object.");
180         return;
181     }
182     update_log("\nYou try to pick up an object, but there is none.");
183 }
184
185
186
187 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
188 {
189     if      (no_object)
190     {
191         update_log("\nYou try to use an object, but you own none.");
192         return;
193     }
194     else if (wrong_object)
195     {
196         update_log("\nYou try to use this object, but fail.");
197         return;
198     }
199     update_log("\nYou consume MAGIC MEAT.");
200 }
201
202
203
204 extern void free_map_object_actions(struct MapObjAct * moa)
205 {
206     if (NULL == moa)
207     {
208         return;
209     }
210     free(moa->name);
211     free_map_object_actions(moa->next);
212     free(moa);
213 }
214
215
216
217 extern uint8_t get_moa_id_by_name(char * name)
218 {
219     struct MapObjAct * moa = world.map_obj_acts;
220     while (NULL != moa)
221     {
222         if (0 == strcmp(moa->name, name))
223         {
224             break;
225         }
226         moa = moa->next;
227     }
228     exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
229     return moa->id;
230 }
231
232
233
234 extern void actor_wait(struct MapObj * mo)
235 {
236     if (mo == get_player())
237     {
238         playerbonus_wait();
239     }
240 }
241
242
243
244 extern void actor_move(struct MapObj * mo)
245 {
246     char d = mo->arg;
247     struct yx_uint8 target = mv_yx_in_dir(d, mo->pos);
248     struct MapObj * other_mo;
249     for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
250     {
251         if (0 == other_mo->lifepoints || other_mo == mo)
252         {
253             continue;
254         }
255         if (yx_uint8_cmp(&target, &other_mo->pos))
256         {
257             actor_hits_actor(mo, other_mo);
258             return;
259         }
260     }
261     uint8_t passable = is_passable(target);
262     if (passable)
263     {
264         set_object_position(mo, target);
265     }
266     if (mo == get_player())
267     {
268         playerbonus_move(d, passable);
269     }
270 }
271
272
273
274 extern void actor_drop(struct MapObj * mo)
275 {
276     uint8_t owns_none = (NULL == mo->owns);
277     if (!owns_none)
278     {
279         uint8_t select = mo->arg;
280         struct MapObj * owned = mo->owns;
281         uint8_t i = 0;
282         for (; i != select; i++, owned = owned->next);
283         own_map_object(&world.map_objs, &mo->owns, owned->id);
284     }
285     if (mo == get_player())
286     {
287         playerbonus_drop(owns_none);
288     }
289 }
290
291
292
293 extern void actor_pick(struct MapObj * mo)
294 {
295     struct MapObj * picked = NULL;
296     struct MapObj * mo_i;
297     for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next)
298     {
299         if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos))
300         {
301             picked = mo_i;
302         }
303     }
304     if (NULL != picked)
305     {
306         own_map_object(&mo->owns, &world.map_objs, picked->id);
307         set_object_position(picked, mo->pos);
308     }
309     if (mo == get_player())
310     {
311         playerbonus_pick(NULL != picked);
312     }
313 }
314
315
316
317 extern void actor_use(struct MapObj * mo)
318 {
319     uint8_t wrong_object = 1;
320     uint8_t no_object = (NULL == mo->owns);
321     if (!no_object)
322     {
323         uint8_t select = mo->arg;
324         uint8_t i = 0;
325         struct MapObj * selected = mo->owns;
326         for (; i != select; i++, selected = selected->next);
327         struct MapObjDef * mod = get_map_object_def(selected->type);
328         if (mod->consumable)
329         {
330             wrong_object = 0;
331             struct MapObj * next = selected->next;
332             free(selected);
333             if (0 < select)
334             {
335                 select--;
336                 selected = mo->owns;
337                 for (i = 0; i != select; i++, selected = selected->next);
338                 selected->next = next;
339             }
340             else
341             {
342                 mo->owns = next;
343             }
344             mo->lifepoints = mo->lifepoints + mod->consumable;
345         }
346     }
347     if (mo == get_player())
348     {
349         playerbonus_use(no_object, wrong_object);
350     }
351 }