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(),
8 #include "map_objects.h" /* for MapObj, MapObjDef structs, get_player(),
9 * set_object_position(), own_map_object()
11 #include "misc.h" /* for update_log(), turn_over() */
12 #include "map.h" /* for Map struct */
13 #include "main.h" /* for World struct */
14 #include "command_db.h" /* for get_command_id() */
18 /* One actor "wounds" another actor, decrementing his lifepoints and, if they
19 * reach zero in the process, killing it. Generates appropriate log message.
21 static void actor_hits_actor(struct World * world, struct MapObj * hitter,
22 struct MapObj * hitted);
26 static void actor_hits_actor(struct World * world, struct MapObj * hitter,
27 struct MapObj * hitted)
29 struct MapObjDef * mod_hitter = get_map_object_def(world, hitter->type);
30 struct MapObjDef * mod_hitted = get_map_object_def(world, hitted->type);
31 struct MapObj * player = get_player(world);
33 char * msg2 = "wound";
37 msg1 = mod_hitter->name;
42 msg3 = mod_hitted->name;
44 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
46 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
47 update_log(world, msg);
49 if (0 == hitted->lifepoints)
51 hitted->type = mod_hitted->corpse_id;
54 update_log(world, " You die.");
58 update_log(world, " It dies.");
61 world->score = world->score + mod_hitted->lifepoints;
69 extern uint8_t move_actor(struct World * world, struct MapObj * actor,
72 struct yx_uint16 target = mv_yx_in_dir(d, actor->pos);
73 struct MapObj * other_actor;
74 for (other_actor = world->map_objs;
76 other_actor = other_actor->next)
78 if (0 == other_actor->lifepoints || other_actor == actor)
82 if (yx_uint16_cmp(&target, &other_actor->pos))
84 actor_hits_actor(world, actor, other_actor);
88 if (is_passable(world->map, target))
90 set_object_position(actor, target);
98 extern void move_player(struct World * world, enum dir d)
101 char * action_dsc_prototype = "player_";
102 uint8_t len_action_dsc_prototype = strlen(action_dsc_prototype);
103 char action_dsc[len_action_dsc_prototype + 2];
104 memcpy(action_dsc, action_dsc_prototype, len_action_dsc_prototype);
108 action_dsc[len_action_dsc_prototype] = 'u';
113 action_dsc[len_action_dsc_prototype] = 'r';
118 action_dsc[len_action_dsc_prototype] = 'd';
123 action_dsc[len_action_dsc_prototype] = 'l';
125 action_dsc[len_action_dsc_prototype + 1] = '\0';
126 uint8_t res = move_actor(world, get_player(world), d);
129 char * dsc_move = "You fail to move ";
132 dsc_move = "You move ";
134 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
135 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
136 update_log(world, msg);
138 turn_over(world, get_command_id(world, action_dsc));
143 extern void player_wait(struct World * world)
145 update_log(world, "\nYou wait.");
146 turn_over(world, get_command_id(world, "wait"));
151 extern char is_passable(struct Map * map, struct yx_uint16 pos)
154 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
156 if ('.' == map->cells[pos.y * map->size.x + pos.x])
166 extern void player_drop(struct World * world)
168 struct MapObj * player = get_player(world);
169 if (NULL == player->owns)
171 update_log(world, "\nYou try to drop an object, but you own none.");
172 world->old_inventory_select = 0;
176 world->old_inventory_select = world->inventory_select;
177 struct MapObj * owned = player->owns;
179 for (; i != world->inventory_select; i++, owned = owned->next);
180 if (0 < world->inventory_select)
182 world->inventory_select--;
184 own_map_object(&world->map_objs, &player->owns, owned->id);
185 update_log(world, "\nYou drop an object.");
187 turn_over(world, get_command_id(world, "drop"));
192 extern void player_pick(struct World * world)
194 struct MapObj * player = get_player(world);
195 struct MapObj * picked;
196 for (picked = world->map_objs; NULL != picked; picked = picked->next)
198 if (picked != player && yx_uint16_cmp(&picked->pos, &player->pos))
205 update_log(world, "\nYou try to pick up an object, but there is none.");
209 own_map_object(&player->owns, &world->map_objs, picked->id);
210 set_object_position(picked, player->pos);
211 update_log(world, "\nYou pick up an object.");
213 turn_over(world, get_command_id(world, "pick"));