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