1 /* map_object_actions.c */
3 #include "map_object_actions.h"
4 #include <string.h> /* for strlen() */
5 #include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
6 #include "misc.h" /* for update_log(), turn_over() */
7 #include "map.h" /* for Map struct */
8 #include "main.h" /* for World struct */
9 #include "map_objects.h" /* for structs MapObj, MapObjDef,
10 * get_map_object_def()
12 #include "rrand.h" /* for rrand() */
13 #include "command_db.h" /* for get_command_id() */
17 /* Log monster (described by "dsc_monster1") bumping into "monster2". */
18 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
19 struct MapObj * monster2);
21 /* Decrement player HPs due to attack of monster described by "dsc_monster",
22 * kill player if his HP hit zero; log the whole action.
24 static void monster_hits_player(struct World * world, char * dsc_monster);
26 /* Decrement HP of "monster" hit by player, kill it if its HP hit zero, create a
27 * corpse and increment player's score by the amount of hitpoints the monster
28 * started with; log the whole action.
30 static void player_hits_monster(struct World * world, struct MapObj * monster);
32 /* Try moving the player in direction "d" towards coordinate "target"; log
33 * success or failure of the whole action.
35 static void try_player_move(struct World * world,
36 enum dir d, struct yx_uint16 target);
40 static void monster_bumps_monster(struct World * world, char * dsc_monster1,
41 struct MapObj * monster2)
43 char * bump_dsc = " bumps into ";
44 struct MapObjDef * mod = get_map_object_def(world, monster2->type);
45 char msg[strlen(dsc_monster1) + strlen(bump_dsc) + strlen(mod->name) + 3];
46 sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->name);
47 update_log(world, msg);
52 static void monster_hits_player(struct World * world, char * dsc_monster)
54 char * hit_dsc = " hits you";
55 char msg[strlen(dsc_monster) + strlen(hit_dsc) + 3];
56 sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc);
57 update_log(world, msg);
58 world->player->hitpoints--;
60 if (0 == world->player->hitpoints)
62 update_log(world, "\nYou are dead.");
68 static void player_hits_monster(struct World * world, struct MapObj * monster)
70 struct MapObjDef * mod = get_map_object_def(world, monster->type);
71 char * hit_dsc = "You hit the ";
72 char * monster_dsc = mod->name;
73 char hitmsg[strlen(hit_dsc) + strlen(monster_dsc) + 3];
74 sprintf(hitmsg, "\n%s%s.", hit_dsc, monster_dsc);
75 update_log(world, hitmsg);
76 monster->lifepoints--;
77 if (0 == monster->lifepoints)
79 hit_dsc = "You kill the ";
80 char kill_msg[strlen(hit_dsc) + strlen(monster_dsc) + 3];
81 sprintf(kill_msg, "\n%s%s.", hit_dsc, monster_dsc);
82 update_log(world, kill_msg);
83 struct MapObjDef * md = mod;
84 monster->type = md->corpse_id;
85 uint8_t score = md->lifepoints;
86 world->score = world->score + score;
92 static void try_player_move(struct World * world,
93 enum dir d, struct yx_uint16 target)
112 char * dsc_move = "You fail to move ";
113 if (is_passable(world->map, target))
115 dsc_move = "You move ";
116 world->player->pos = target;
118 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
119 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
120 update_log(world, msg);
125 extern void move_monster(struct World * world, struct MapObj * monster)
127 char d = rrand() % 5;
128 struct yx_uint16 t = mv_yx_in_dir(d, monster->pos);
129 struct MapObjDef * mod = get_map_object_def(world, monster->type);
130 char * dsc = mod->name;
131 if (yx_uint16_cmp(&t, &world->player->pos))
133 monster_hits_player(world, dsc);
136 struct MapObj * other_monster;
137 for (other_monster = world->map_objs;
139 other_monster = other_monster->next)
141 if (0 == other_monster->lifepoints || other_monster == monster)
145 if (yx_uint16_cmp(&t, &other_monster->pos))
147 monster_bumps_monster(world, dsc, other_monster);
151 if (is_passable(world->map, t))
159 extern void move_player(struct World * world, enum dir d)
161 char * action_dsc_prototype = "player_";
162 uint8_t len = strlen(action_dsc_prototype);
163 char action_dsc[len + 2];
164 memcpy(action_dsc, action_dsc_prototype, len);
167 action_dsc[len] = 'u';
171 action_dsc[len] = 'd';
175 action_dsc[len] = 'l';
179 action_dsc[len] = 'r';
181 action_dsc[len + 1] = '\0';
182 uint8_t action_id = get_command_id(world, action_dsc);
183 struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
184 struct MapObj * monster;
185 for (monster = world->map_objs;
187 monster = monster->next)
189 if (0 < monster->lifepoints && yx_uint16_cmp(&t, &monster->pos))
191 player_hits_monster(world, monster);
192 turn_over(world, action_id);
196 try_player_move(world, d, t);
197 turn_over(world, action_id);
202 extern void player_wait (struct World * world)
204 update_log(world, "\nYou wait.");
205 turn_over(world, get_command_id(world, "wait"));
210 extern char is_passable (struct Map * map, struct yx_uint16 pos)
213 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
215 if ('.' == map->cells[pos.y * map->size.x + pos.x])