home · contact · privacy
6f94ce18b410eb898f1450b7fe0f44babba12275
[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, UINT8_MAx */
6 #include <stdio.h> /* sprintf(), ungetc() */
7 #include <stdlib.h> /* free(), atoi() */
8 #include <string.h> /* strlen(), strcmp(), memcpy(), strncmp() */
9 #include "../common/err_try_fgets.h" /* err_try_fgets(), err_line(),
10                                       * reset_err_try_fgets_counter()
11                                       */
12 #include "../common/readwrite.h" /* textfile_sizes(), try_fopen(), try_fclose(),
13                                   * try_fgetc()
14                                   */
15 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
16 #include "../common/try_malloc.h" /* try_malloc() */
17 #include "../common/yx_uint8.h" /* struct yx_uint8 */
18 #include "cleanup.h" /* set_cleanup_flag() */
19 #include "map_objects.h" /* structs MapObj, MapObjDef, get_player(),
20                           * set_object_position(), own_map_object(),
21                           * get_map_object_def()
22                           */
23 #include "map.h" /* is_passable() */
24 #include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */
25 #include "world.h" /* global world */
26
27
28
29 /* Append "text" to game log, or a "." if "text" is the same as the last one. */
30 static void update_log(char * text);
31
32 /* If "name" fits "moa"->name, set "moa"->func to "func". */
33 static uint8_t try_func_name(struct MapObjAct * moa,
34                              char * name, void (* func) (struct MapObj *));
35
36 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
37  * reach zero in the process, killing it. Generates appropriate log message.
38  */
39 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted);
40
41 /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log
42  * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0.
43  * (match_dir() is just a little helper to playerbonus_move().)
44  */
45 static void playerbonus_wait();
46 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match);
47 static void playerbonus_move(char d, uint8_t passable);
48 static void playerbonus_drop(uint8_t owns_none);
49 static void playerbonus_pick(uint8_t picked);
50 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object);
51
52
53
54 static void update_log(char * text)
55 {
56     char * f_name = "update_log()";
57     uint16_t len_new = strlen(text);
58     uint16_t len_old = 0;
59     if (world.log)
60     {
61         len_old = strlen(world.log);
62         uint16_t last_nl = len_old - 1;
63         while (last_nl != 0)
64         {
65             if ('\n' == world.log[last_nl])
66             {
67                 break;
68             }
69             last_nl--;
70         }
71         uint16_t last_stop = len_old - 1;
72         while (last_stop != 0)
73         {
74             if ('.' == world.log[last_stop] && '.' != world.log[last_stop - 1])
75             {
76                 break;
77             }
78             last_stop--;
79         }
80         if (   (last_stop + 1) - last_nl == strlen(text)
81             && 0 == strncmp(world.log + last_nl, text, strlen(text)))
82         {
83             text = ".";
84         }
85     }
86     uint16_t len_whole = len_old + len_new + 1;
87     char * new_text = try_malloc(len_whole, f_name);
88     memcpy(new_text, world.log, len_old);
89     sprintf(new_text + len_old, "%s", text);
90     free(world.log);
91     world.log = new_text;
92 }
93
94
95
96 static uint8_t try_func_name(struct MapObjAct * moa,
97                              char * name, void (* func) (struct MapObj *))
98 {
99     if (0 == strcmp(moa->name, name))
100     {
101         moa->func = func;
102         return 1;
103     }
104     return 0;
105 }
106
107
108
109 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
110 {
111     struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
112     struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
113     struct MapObj * player = get_player();
114     char * msg1 = "You";
115     char * msg2 = "wound";
116     char * msg3 = "you";
117     if      (player != hitter)
118     {
119         msg1 = mod_hitter->name;
120         msg2 = "wounds";
121     }
122     if (player != hitted)
123     {
124         msg3 = mod_hitted->name;
125     }
126     uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
127     char msg[len];
128     sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
129     update_log(msg);
130     hitted->lifepoints--;
131     if (0 == hitted->lifepoints)
132     {
133         hitted->type = mod_hitted->corpse_id;
134         if (player == hitted)
135         {
136             update_log(" You die.");
137             return;
138         }
139         update_log(" It dies.");
140         if (player == hitter)
141         {
142             world.score = world.score + mod_hitted->lifepoints;
143         }
144     }
145 }
146
147
148
149 static void playerbonus_wait()
150 {
151         update_log("\nYou wait.");
152 }
153
154
155
156 static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match)
157 {
158     if (d == match)
159     {
160         * dsc_d = dsc_match;
161         return 1;
162     }
163     return 0;
164 }
165
166
167
168 static void playerbonus_move(char d, uint8_t passable)
169 {
170     char * dsc_dir = "north";
171     if (   match_dir(d, &dsc_dir, '6', "east")
172         || match_dir(d, &dsc_dir, '2', "south")
173         || match_dir(d, &dsc_dir, '4', "west")
174         || match_dir(d, &dsc_dir, '7', "north-west")
175         || match_dir(d, &dsc_dir, '9', "north-east")
176         || match_dir(d, &dsc_dir, '1', "south-west")
177         || match_dir(d, &dsc_dir, '3', "south-east"));
178     char * dsc_move = "You move ";
179     if (0 == passable)
180     {
181         dsc_move = "You fail to move ";
182     }
183     char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
184     sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
185     update_log(msg);
186 }
187
188
189
190 static void playerbonus_drop(uint8_t owns_none)
191 {
192     if (0 != owns_none)
193     {
194         update_log("\nYou try to drop an object, but you own none.");
195         return;
196     }
197     update_log("\nYou drop an object.");
198 }
199
200
201
202 static void playerbonus_pick(uint8_t picked)
203 {
204     if (picked)
205     {
206         update_log("\nYou pick up an object.");
207         return;
208     }
209     update_log("\nYou try to pick up an object, but there is none.");
210 }
211
212
213
214 static void playerbonus_use(uint8_t no_object, uint8_t wrong_object)
215 {
216     if      (no_object)
217     {
218         update_log("\nYou try to use an object, but you own none.");
219         return;
220     }
221     else if (wrong_object)
222     {
223         update_log("\nYou try to use this object, but fail.");
224         return;
225     }
226     update_log("\nYou consume MAGIC MEAT.");
227 }
228
229
230
231 extern void init_map_object_actions()
232 {
233     char * f_name = "init_map_object_actions()";
234     FILE * file = try_fopen(world.path_map_obj_acts, "r", f_name);
235     uint32_t linemax = textfile_sizes(file, NULL);
236     char line[linemax + 1];
237     struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts;
238     char * context = "Failed reading map object actions config file. ";
239     char * err_toolarge = "Value is too large.";
240     char * err_uniq     = "Declaration of ID already used.";
241     reset_err_try_fgets_counter();
242     while (1)
243     {
244         int test_for_end = try_fgetc(file, f_name);
245         if (EOF == test_for_end || '\n' == test_for_end)
246         {
247             break;
248         }
249         exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()");
250         struct MapObjAct * moa = try_malloc(sizeof(struct MapObjAct), f_name);
251         err_try_fgets(line, linemax, file, context, "nfi");
252         err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
253         moa->id = atoi(line);
254         struct MapObjAct * moa_test = world.map_obj_acts;
255         for (; NULL != moa_test; moa_test = moa_test->next)
256         {
257             err_line(moa->id == moa_test->id, line, context, err_uniq);
258         }
259         err_try_fgets(line, linemax, file, context, "0nfi");
260         err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge);
261         moa->effort = atoi(line);
262         err_try_fgets(line, linemax, file, context, "0nf");
263         line[strlen(line) - 1] = '\0';
264         uint8_t len_name = strlen(line) + 1;
265         moa->name = try_malloc(len_name, f_name);
266         memcpy(moa->name, line, len_name);
267         if (!(   try_func_name(moa, "move", actor_move)
268               || try_func_name(moa, "pick_up", actor_pick)
269               || try_func_name(moa, "drop", actor_drop)
270               || try_func_name(moa, "use", actor_use)))
271         {
272             moa->func = actor_wait;
273         }
274         moa->next = NULL;
275         * moa_ptr_ptr = moa;
276         moa_ptr_ptr = &moa->next;
277         err_try_fgets(line, linemax, file, context, "d");
278     }
279     try_fclose(file, f_name);
280     set_cleanup_flag(CLEANUP_MAP_OBJECT_ACTS);
281 }
282
283
284
285 extern void free_map_object_actions(struct MapObjAct * moa)
286 {
287     if (NULL == moa)
288     {
289         return;
290     }
291     free(moa->name);
292     free_map_object_actions(moa->next);
293     free(moa);
294 }
295
296
297
298 extern uint8_t get_moa_id_by_name(char * name)
299 {
300     struct MapObjAct * moa = world.map_obj_acts;
301     while (NULL != moa)
302     {
303         if (0 == strcmp(moa->name, name))
304         {
305             break;
306         }
307         moa = moa->next;
308     }
309     exit_err(NULL==moa, "get_moa_id_by_name() did not find map object action.");
310     return moa->id;
311 }
312
313
314
315 extern void actor_wait(struct MapObj * mo)
316 {
317     if (mo == get_player())
318     {
319         playerbonus_wait();
320     }
321 }
322
323
324
325 extern void actor_move(struct MapObj * mo)
326 {
327     char d = mo->arg;
328     struct yx_uint8 target = mv_yx_in_dir(d, mo->pos);
329     struct MapObj * other_mo;
330     for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next)
331     {
332         if (0 == other_mo->lifepoints || other_mo == mo)
333         {
334             continue;
335         }
336         if (yx_uint8_cmp(&target, &other_mo->pos))
337         {
338             actor_hits_actor(mo, other_mo);
339             return;
340         }
341     }
342     uint8_t passable = is_passable(target);
343     if (passable)
344     {
345         set_object_position(mo, target);
346     }
347     if (mo == get_player())
348     {
349         playerbonus_move(d, passable);
350     }
351 }
352
353
354
355 extern void actor_drop(struct MapObj * mo)
356 {
357     uint8_t owns_none = (NULL == mo->owns);
358     if (!owns_none)
359     {
360         uint8_t select = mo->arg;
361         struct MapObj * owned = mo->owns;
362         uint8_t i = 0;
363         for (; i != select; i++, owned = owned->next);
364         own_map_object(&world.map_objs, &mo->owns, owned->id);
365     }
366     if (mo == get_player())
367     {
368         playerbonus_drop(owns_none);
369     }
370 }
371
372
373
374 extern void actor_pick(struct MapObj * mo)
375 {
376     struct MapObj * picked = NULL;
377     struct MapObj * mo_i;
378     for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next)
379     {
380         if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos))
381         {
382             picked = mo_i;
383         }
384     }
385     if (NULL != picked)
386     {
387         own_map_object(&mo->owns, &world.map_objs, picked->id);
388         set_object_position(picked, mo->pos);
389     }
390     if (mo == get_player())
391     {
392         playerbonus_pick(NULL != picked);
393     }
394 }
395
396
397
398 extern void actor_use(struct MapObj * mo)
399 {
400     uint8_t wrong_object = 1;
401     uint8_t no_object = (NULL == mo->owns);
402     if (!no_object)
403     {
404         uint8_t select = mo->arg;
405         uint8_t i = 0;
406         struct MapObj * selected = mo->owns;
407         for (; i != select; i++, selected = selected->next);
408         struct MapObjDef * mod = get_map_object_def(selected->type);
409         if (!strcmp("MAGIC MEAT", mod->name))
410         {
411             wrong_object = 0;
412             struct MapObj * next = selected->next;
413             free(selected);
414             if (0 < select)
415             {
416                 select--;
417                 selected = mo->owns;
418                 for (i = 0; i != select; i++, selected = selected->next);
419                 selected->next = next;
420             }
421             else
422             {
423                 mo->owns = next;
424             }
425             mo->lifepoints++;
426         }
427     }
428     if (mo == get_player())
429     {
430         playerbonus_use(no_object, wrong_object);
431     }
432 }