X-Git-Url: https://plomlompom.com/repos/test.html?a=blobdiff_plain;f=src%2Fmap_object_actions.c;h=4217145843fc2d1bf80b22df7dd0cbe65f5be159;hb=657d5dbc6d362d7b20693c63b38d8d99f3d2dbbd;hp=a4e186d28b17f0acbd0cb4333fcdb0715efbacb6;hpb=e43611d44a32c92274d08dcc42e2541cf1ecdc72;p=plomrogue
diff --git a/src/map_object_actions.c b/src/map_object_actions.c
index a4e186d..4217145 100644
--- a/src/map_object_actions.c
+++ b/src/map_object_actions.c
@@ -1,214 +1,212 @@
/* map_object_actions.c */
#include "map_object_actions.h"
-#include /* for malloc(), calloc(), free() */
#include /* for strlen() */
-#include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */
-#include "misc.h" /* for update_log(), turn_over()*/
+#include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(),
+ * yx_uint16_cmp()
+ */
+#include "map_objects.h" /* for MapObj, MapObjDef structs, get_player(),
+ * set_object_position(), own_map_object()
+ */
+#include "misc.h" /* for update_log(), turn_over() */
#include "map.h" /* for Map struct */
#include "main.h" /* for World struct */
-#include "map_objects.h" /* for map object (definition) structs */
-#include "rrand.h" /* for rrand() */
+#include "command_db.h" /* for get_command_id() */
-/* Log monster (described by "dsc_monster1") bumping into "monster2". */
-static void monster_bumps_monster(struct World * world, char * dsc_monster1,
- struct Monster * monster2);
-
-/* Decrement player HPs due to attack of monster described by "dsc_monster",
- * kill player if his HP hit zero; log the whole action.
- */
-static void monster_hits_player(struct World * world, char * dsc_monster);
-
-/* Decrement HP of "monster" hit by player, kill it if its HP hit zero; log the
- * whole action.
- */
-static void player_hits_monster(struct World * world, struct Monster * monster);
-
-/* Try moving the player in direction "d" towards coordinate "target"; log
- * success or failure of the whole action.
+/* One actor "wounds" another actor, decrementing his lifepoints and, if they
+ * reach zero in the process, killing it. Generates appropriate log message.
*/
-static void try_player_move(struct World * world,
- enum dir d, struct yx_uint16 target);
+static void actor_hits_actor(struct World * world, struct MapObj * hitter,
+ struct MapObj * hitted);
-static void monster_bumps_monster(struct World * world, char * dsc_monster1,
- struct Monster * monster2)
+static void actor_hits_actor(struct World * world, struct MapObj * hitter,
+ struct MapObj * hitted)
{
- char * bump_dsc = " bumps into ";
- struct MapObjDef * mod = get_map_obj_def(world, monster2->map_obj.type);
- char * msg = malloc(strlen(dsc_monster1) + strlen(bump_dsc)
- + strlen(mod->desc) + 3);
- sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->desc);
- update_log(world, msg);
- free(msg);
-}
-
-
-
-static void monster_hits_player(struct World * world, char * dsc_monster)
-{
- char * hit_dsc = " hits you";
- char * msg = malloc(strlen(dsc_monster) + strlen(hit_dsc) + 3);
- sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc);
+ struct MapObjDef * mod_hitter = get_map_object_def(world, hitter->type);
+ struct MapObjDef * mod_hitted = get_map_object_def(world, hitted->type);
+ struct MapObj * player = get_player(world);
+ char * msg1 = "You";
+ char * msg2 = "wound";
+ char * msg3 = "you";
+ if (player != hitter)
+ {
+ msg1 = mod_hitter->name;
+ msg2 = "wounds";
+ }
+ if (player != hitted)
+ {
+ msg3 = mod_hitted->name;
+ }
+ uint8_t len = 1 + strlen(msg1) + 1 + strlen(msg2) + 1 + strlen(msg3) + 2;
+ char msg[len];
+ sprintf(msg, "\n%s %s %s.", msg1, msg2, msg3);
update_log(world, msg);
- free(msg);
- world->player->hitpoints--;
- if (0 == world->player->hitpoints)
+ hitted->lifepoints--;
+ if (0 == hitted->lifepoints)
{
- update_log(world, "\nYou are dead.");
+ hitted->type = mod_hitted->corpse_id;
+ if (player == hitted)
+ {
+ update_log(world, " You die.");
+ }
+ else
+ {
+ update_log(world, " It dies.");
+ if (player == hitter)
+ {
+ world->score = world->score + mod_hitted->lifepoints;
+ }
+ }
}
}
-static void player_hits_monster(struct World * world, struct Monster * monster)
+extern uint8_t move_actor(struct World * world, struct MapObj * actor,
+ enum dir d)
{
- struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
- char * hit_dsc = "You hit the ";
- char * monster_dsc = mod->desc;
- char * msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
- sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
- update_log(world, msg);
- free(msg);
- monster->hitpoints--;
-
- if (0 == monster->hitpoints)
+ struct yx_uint16 target = mv_yx_in_dir(d, actor->pos);
+ struct MapObj * other_actor;
+ for (other_actor = world->map_objs;
+ other_actor != 0;
+ other_actor = other_actor->next)
{
- hit_dsc = "You kill the ";
- msg = malloc(strlen(hit_dsc) + strlen(monster_dsc) + 3);
- sprintf(msg, "\n%s%s.", hit_dsc, monster_dsc);
- update_log(world, msg);
- free(msg);
- if (world->monster == monster)
+ if (0 == other_actor->lifepoints || other_actor == actor)
{
- world->monster = world->monster->map_obj.next;
+ continue;
}
- else
+ if (yx_uint16_cmp(&target, &other_actor->pos))
{
- struct Monster * m_prev;
- for (m_prev = world->monster;
- m_prev->map_obj.next != monster;
- m_prev = m_prev->map_obj.next);
- {
- m_prev->map_obj.next = monster->map_obj.next;
- }
+ actor_hits_actor(world, actor, other_actor);
+ return 2;
}
- free(monster);
}
+ if (is_passable(world->map, target))
+ {
+ set_object_position(actor, target);
+ return 0;
+ }
+ return 1;
}
-static void try_player_move(struct World * world,
- enum dir d, struct yx_uint16 target)
+extern void move_player(struct World * world, enum dir d)
{
char * dsc_dir;
+ char * action_dsc_prototype = "player_";
+ uint8_t len_action_dsc_prototype = strlen(action_dsc_prototype);
+ char action_dsc[len_action_dsc_prototype + 2];
+ memcpy(action_dsc, action_dsc_prototype, len_action_dsc_prototype);
if (NORTH == d)
{
dsc_dir = "north";
+ action_dsc[len_action_dsc_prototype] = 'u';
}
else if (EAST == d)
{
dsc_dir = "east" ;
+ action_dsc[len_action_dsc_prototype] = 'r';
}
else if (SOUTH == d)
{
dsc_dir = "south";
+ action_dsc[len_action_dsc_prototype] = 'd';
}
else if (WEST == d)
{
dsc_dir = "west" ;
+ action_dsc[len_action_dsc_prototype] = 'l';
}
- char * dsc_move = "You fail to move ";
- if (is_passable(world->map, target))
+ action_dsc[len_action_dsc_prototype + 1] = '\0';
+ uint8_t res = move_actor(world, get_player(world), d);
+ if (1 >= res)
{
- dsc_move = "You move ";
- world->player->pos = target;
+ char * dsc_move = "You fail to move ";
+ if (0 == res)
+ {
+ dsc_move = "You move ";
+ }
+ char msg[strlen(dsc_move) + strlen (dsc_dir) + 3];
+ sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
+ update_log(world, msg);
}
- char * msg = malloc(strlen(dsc_move) + strlen (dsc_dir) + 3);
- sprintf(msg, "\n%s%s.", dsc_move, dsc_dir);
- update_log(world, msg);
- free(msg);
+ turn_over(world, get_command_id(world, action_dsc));
}
-extern void move_monster(struct World * world, struct Monster * monster)
+extern void player_wait(struct World * world)
{
- char d = rrand() % 5;
- struct yx_uint16 t = mv_yx_in_dir(d, monster->map_obj.pos);
- struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type);
- char * dsc = mod->desc;
- if (yx_uint16_cmp(&t, &world->player->pos))
- {
- monster_hits_player(world, dsc);
- return;
- }
- struct Monster * other_monster;
- for (other_monster = world->monster;
- other_monster != 0;
- other_monster = other_monster->map_obj.next)
- {
- if (other_monster == monster)
- {
- continue;
- }
- if (yx_uint16_cmp(&t, &other_monster->map_obj.pos))
- {
- monster_bumps_monster(world, dsc, other_monster);
- return;
- }
- }
- if (is_passable(world->map, t))
- {
- monster->map_obj.pos = t;
- }
+ update_log(world, "\nYou wait.");
+ turn_over(world, get_command_id(world, "wait"));
}
-extern void move_player(struct World * world, enum dir d)
+extern char is_passable(struct Map * map, struct yx_uint16 pos)
{
- struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos);
- struct Monster * monster;
- for (monster = world->monster;
- monster != 0;
- monster = monster->map_obj.next)
+ char passable = 0;
+ if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
{
- if (yx_uint16_cmp(&t, &monster->map_obj.pos))
+ if ('.' == map->cells[pos.y * map->size.x + pos.x])
{
- player_hits_monster(world, monster);
- turn_over(world, d);
- return;
- }
+ passable = 1;
+ }
}
- try_player_move(world, d, t);
- turn_over(world, d);
+ return passable;
}
-extern void player_wait (struct World * world)
+extern void player_drop(struct World * world)
{
- update_log(world, "\nYou wait.");
- turn_over(world, 0);
+ struct MapObj * player = get_player(world);
+ if (NULL == player->owns)
+ {
+ update_log(world, "\nYou try to drop an object, but you own none.");
+ }
+ else
+ {
+ struct MapObj * owned = player->owns;
+ uint8_t i = 0;
+ for (; i != world->inventory_select; i++, owned = owned->next);
+ if (0 < world->inventory_select)
+ {
+ world->inventory_select--;
+ }
+ own_map_object(&world->map_objs, &player->owns, owned->id);
+ update_log(world, "\nYou drop an object.");
+ }
+ turn_over(world, get_command_id(world, "drop"));
}
-extern char is_passable (struct Map * map, struct yx_uint16 pos)
+extern void player_pick(struct World * world)
{
- char passable = 0;
- if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
+ struct MapObj * player = get_player(world);
+ struct MapObj * picked;
+ for (picked = world->map_objs; NULL != picked; picked = picked->next)
{
- if ('.' == map->cells[pos.y * map->size.x + pos.x])
+ if (picked != player && yx_uint16_cmp(&picked->pos, &player->pos))
{
- passable = 1;
+ break;
}
}
- return passable;
+ if (NULL == picked)
+ {
+ update_log(world, "\nYou try to pick up an object, but there is none.");
+ }
+ else
+ {
+ own_map_object(&player->owns, &world->map_objs, picked->id);
+ set_object_position(picked, player->pos);
+ update_log(world, "\nYou pick up an object.");
+ }
+ turn_over(world, get_command_id(world, "pick"));
}
-