1 /* map_object_actions.c */
3 #include "map_object_actions.h"
4 #include <stdlib.h> /* for free() */
5 #include <string.h> /* for strlen() */
6 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
7 #include "misc.h" /* for update_log(), turn_over(), try_malloc() */
8 #include "map.h" /* for Map struct */
9 #include "main.h" /* for World struct */
10 #include "map_objects.h" /* for map object (definition) structs */
11 #include "rrand.h" /* for rrand() */
12 #include "command_db.h" /* for get_command_id() */
16 /* Log monster (described by "dsc_monster1") bumping into "monster2". */
17 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
18 struct Monster * monster2);
20 /* Decrement player HPs due to attack of monster described by "dsc_monster",
21 * kill player if his HP hit zero; log the whole action.
23 static void monster_hits_player(struct World * world, char * dsc_monster);
25 /* Decrement HP of "monster" hit by player, kill it if its HP hit zero, create a
26 * corpse and increment player's score by the amount of hitpoints the monster
27 * started with; log the whole action.
29 static void player_hits_monster(struct World * world, struct Monster * monster);
31 /* Try moving the player in direction "d" towards coordinate "target"; log
32 * success or failure of the whole action.
34 static void try_player_move(struct World * world,
35 enum dir d, struct yx_uint16 target);
39 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
40 struct Monster * monster2)
42 char * bump_dsc = " bumps into ";
43 struct MapObjDef * mod = get_map_obj_def(world, monster2->map_obj.type);
44 char msg[strlen(dsc_monster1) + strlen(bump_dsc) + strlen(mod->desc) + 3];
45 sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->desc);
46 update_log(world, msg);
51 static void monster_hits_player(struct World * world, char * dsc_monster)
53 char * hit_dsc = " hits you";
54 char msg[strlen(dsc_monster) + strlen(hit_dsc) + 3];
55 sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc);
56 update_log(world, msg);
57 world->player->hitpoints--;
58 if (0 == world->player->hitpoints)
60 update_log(world, "\nYou are dead.");
66 static void player_hits_monster(struct World * world, struct Monster * monster)
68 char * f_name = "player_hits_monster()";
69 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
70 char * hit_dsc = "You hit the ";
71 char * monster_dsc = mod->desc;
72 char hitmsg[strlen(hit_dsc) + strlen(monster_dsc) + 3];
73 sprintf(hitmsg, "\n%s%s.", hit_dsc, monster_dsc);
74 update_log(world, hitmsg);
76 if (0 == monster->hitpoints)
78 hit_dsc = "You kill the ";
79 char kill_msg[strlen(hit_dsc) + strlen(monster_dsc) + 3];
80 sprintf(kill_msg, "\n%s%s.", hit_dsc, monster_dsc);
81 update_log(world, kill_msg);
82 struct MonsterDef * md = (struct MonsterDef * ) mod;
83 struct Item * corpse = try_malloc(sizeof(struct Item), world, f_name);
84 corpse->map_obj.type = md->corpse_id;
85 corpse->map_obj.pos = monster->map_obj.pos;
86 corpse->map_obj.next = world->item;
88 if (world->monster == monster)
90 world->monster = world->monster->map_obj.next;
94 struct Monster * m_prev;
95 for (m_prev = world->monster;
96 m_prev->map_obj.next != monster;
97 m_prev = m_prev->map_obj.next);
99 m_prev->map_obj.next = monster->map_obj.next;
102 uint8_t score = md->hitpoints_start;
103 world->score = world->score + score;
110 static void try_player_move(struct World * world,
111 enum dir d, struct yx_uint16 target)
130 char * dsc_move = "You fail to move ";
131 if (is_passable(world->map, target))
133 dsc_move = "You move ";
134 world->player->pos = target;
136 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
137 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
138 update_log(world, msg);
143 extern void move_monster(struct World * world, struct Monster * monster)
145 char d = rrand() % 5;
146 struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
147 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
148 char * dsc = mod->desc;
149 if (yx_uint16_cmp(&t, &world->player->pos))
151 monster_hits_player(world, dsc);
154 struct Monster * other_monster;
155 for (other_monster = world->monster;
157 other_monster = other_monster->map_obj.next)
159 if (other_monster == monster)
163 if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
165 monster_bumps_monster(world, dsc, other_monster);
169 if (is_passable(world->map, t))
171 monster->map_obj.pos = t;
177 extern void move_player(struct World * world, enum dir d)
179 char * action_dsc_prototype = "player_";
180 uint8_t len = strlen(action_dsc_prototype);
181 char action_dsc[len + 2];
182 memcpy(action_dsc, action_dsc_prototype, len);
185 action_dsc[len] = 'u';
189 action_dsc[len] = 'd';
193 action_dsc[len] = 'l';
197 action_dsc[len] = 'r';
199 action_dsc[len + 1] = '\0';
200 uint8_t action_id = get_command_id(world, action_dsc);
201 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
202 struct Monster * monster;
203 for (monster = world->monster;
205 monster = monster->map_obj.next)
207 if (yx_uint16_cmp(&t, &monster->map_obj.pos))
209 player_hits_monster(world, monster);
210 turn_over(world, action_id);
214 try_player_move(world, d, t);
215 turn_over(world, action_id);
220 extern void player_wait (struct World * world)
222 update_log(world, "\nYou wait.");
223 turn_over(world, get_command_id(world, "wait"));
228 extern char is_passable (struct Map * map, struct yx_uint16 pos)
231 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
233 if ('.' == map->cells[pos.y * map->size.x + pos.x])