1 /* map_object_actions.c */
3 #include "map_object_actions.h"
4 #include <stdlib.h> /* for malloc(), calloc(), 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()*/
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 = malloc(strlen(dsc_monster1) + strlen(bump_dsc)
45 + strlen(mod->desc) + 3);
46 sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->desc);
47 update_log(world, msg);
53 static void monster_hits_player(struct World * world, char * dsc_monster)
55 char * hit_dsc = " hits you";
56 char * msg = malloc(strlen(dsc_monster) + strlen(hit_dsc) + 3);
57 sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc);
58 update_log(world, msg);
60 world->player->hitpoints--;
61 if (0 == world->player->hitpoints)
63 update_log(world, "\nYou are dead.");
69 static void player_hits_monster(struct World * world, struct Monster * monster)
71 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
72 char * hit_dsc = "You hit the ";
73 char * monster_dsc = mod->desc;
74 char * msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
75 sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
76 update_log(world, msg);
79 if (0 == monster->hitpoints)
81 hit_dsc = "You kill the ";
82 msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
83 sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
84 update_log(world, msg);
86 struct MonsterDef * md = (struct MonsterDef * ) mod;
87 struct Item * corpse = malloc(sizeof(struct Item));
88 corpse->map_obj.type = md->corpse_id;
89 corpse->map_obj.pos = monster->map_obj.pos;
90 corpse->map_obj.next = world->item;
92 if (world->monster == monster)
94 world->monster = world->monster->map_obj.next;
98 struct Monster * m_prev;
99 for (m_prev = world->monster;
100 m_prev->map_obj.next != monster;
101 m_prev = m_prev->map_obj.next);
103 m_prev->map_obj.next = monster->map_obj.next;
106 uint8_t score = md->hitpoints_start;
107 world->score = world->score + score;
114 static void try_player_move(struct World * world,
115 enum dir d, struct yx_uint16 target)
134 char * dsc_move = "You fail to move ";
135 if (is_passable(world->map, target))
137 dsc_move = "You move ";
138 world->player->pos = target;
140 char * msg = malloc(strlen(dsc_move) + strlen (dsc_dir) + 3);
141 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
142 update_log(world, msg);
148 extern void move_monster(struct World * world, struct Monster * monster)
150 char d = rrand() % 5;
151 struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
152 struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
153 char * dsc = mod->desc;
154 if (yx_uint16_cmp(&t, &world->player->pos))
156 monster_hits_player(world, dsc);
159 struct Monster * other_monster;
160 for (other_monster = world->monster;
162 other_monster = other_monster->map_obj.next)
164 if (other_monster == monster)
168 if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
170 monster_bumps_monster(world, dsc, other_monster);
174 if (is_passable(world->map, t))
176 monster->map_obj.pos = t;
182 extern void move_player(struct World * world, enum dir d)
184 char * action_dsc_prototype = "player_";
185 uint8_t len = strlen(action_dsc_prototype);
186 char * action_dsc = malloc(len + 2);
187 memcpy(action_dsc, action_dsc_prototype, len);
190 action_dsc[len] = 'u';
194 action_dsc[len] = 'd';
198 action_dsc[len] = 'l';
202 action_dsc[len] = 'r';
204 action_dsc[len + 1] = '\0';
205 uint8_t action_id = get_command_id(world, action_dsc);
206 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
207 struct Monster * monster;
208 for (monster = world->monster;
210 monster = monster->map_obj.next)
212 if (yx_uint16_cmp(&t, &monster->map_obj.pos))
214 player_hits_monster(world, monster);
215 turn_over(world, action_id);
219 try_player_move(world, d, t);
220 turn_over(world, action_id);
225 extern void player_wait (struct World * world)
227 update_log(world, "\nYou wait.");
228 turn_over(world, get_command_id(world, "wait"));
233 extern char is_passable (struct Map * map, struct yx_uint16 pos)
236 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
238 if ('.' == map->cells[pos.y * map->size.x + pos.x])