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 global */
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 MapObj * hitter, struct MapObj * hitted);
25 static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted)
27 struct MapObjDef * mod_hitter = get_map_object_def(hitter->type);
28 struct MapObjDef * mod_hitted = get_map_object_def(hitted->type);
29 struct MapObj * player = get_player();
31 char * msg2 = "wound";
35 msg1 = mod_hitter->name;
40 msg3 = mod_hitted->name;
42 uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
44 sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
47 if (0 == hitted->lifepoints)
49 hitted->type = mod_hitted->corpse_id;
52 update_log(" You die.");
56 update_log(" It dies.");
59 world.score = world.score + mod_hitted->lifepoints;
67 extern uint8_t move_actor(struct MapObj * actor, enum dir d)
69 struct yx_uint16 target = mv_yx_in_dir(d, actor->pos);
70 struct MapObj * other_actor;
71 for (other_actor = world.map_objs;
73 other_actor = other_actor->next)
75 if (0 == other_actor->lifepoints || other_actor == actor)
79 if (yx_uint16_cmp(&target, &other_actor->pos))
81 actor_hits_actor(actor, other_actor);
85 if (is_passable(world.map, target))
87 set_object_position(actor, target);
95 extern void move_player(enum dir d)
98 char * action_dsc_prototype = "player_";
99 uint8_t len_action_dsc_prototype = strlen(action_dsc_prototype);
100 char action_dsc[len_action_dsc_prototype + 2];
101 memcpy(action_dsc, action_dsc_prototype, len_action_dsc_prototype);
105 action_dsc[len_action_dsc_prototype] = 'u';
110 action_dsc[len_action_dsc_prototype] = 'r';
115 action_dsc[len_action_dsc_prototype] = 'd';
120 action_dsc[len_action_dsc_prototype] = 'l';
122 action_dsc[len_action_dsc_prototype + 1] = '\0';
123 uint8_t res = move_actor(get_player(), d);
126 char * dsc_move = "You fail to move ";
129 dsc_move = "You move ";
131 char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
132 sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
135 turn_over(get_command_id(action_dsc));
140 extern void player_wait()
142 update_log("\nYou wait.");
143 turn_over(get_command_id("wait"));
148 extern char is_passable(struct Map * map, struct yx_uint16 pos)
150 uint8_t passable = 0;
151 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
153 passable = (('.' == map->cells[pos.y * map->size.x + pos.x]));
160 extern void player_drop()
162 struct MapObj * player = get_player();
163 if (NULL == player->owns)
165 update_log("\nYou try to drop an object, but you own none.");
166 world.old_inventory_select = 0;
170 world.old_inventory_select = world.inventory_select;
171 struct MapObj * owned = player->owns;
173 for (; i != world.inventory_select; i++, owned = owned->next);
174 if (0 < world.inventory_select)
176 world.inventory_select--;
178 own_map_object(&world.map_objs, &player->owns, owned->id);
179 update_log("\nYou drop an object.");
181 turn_over(get_command_id("drop"));
186 extern void player_pick()
188 struct MapObj * player = get_player();
189 struct MapObj * picked;
190 for (picked = world.map_objs; NULL != picked; picked = picked->next)
192 if (picked != player && yx_uint16_cmp(&picked->pos, &player->pos))
199 update_log("\nYou try to pick up an object, but there is none.");
203 own_map_object(&player->owns, &world.map_objs, picked->id);
204 set_object_position(picked, player->pos);
205 update_log("\nYou pick up an object.");
207 turn_over(get_command_id("pick"));