X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fmap_object_actions.c;h=23152e229de5d78476178c20feb3d6e179b68900;hb=fb8ddca6abc66eb7e52a007850689309b4cda938;hp=1b1433949599d5792cf7f1540bc56347d3b87663;hpb=ec5c4edd169be8fe8c778cabdfc7ada665629ffd;p=plomrogue diff --git a/src/map_object_actions.c b/src/map_object_actions.c index 1b14339..23152e2 100644 --- a/src/map_object_actions.c +++ b/src/map_object_actions.c @@ -1,13 +1,14 @@ /* map_object_actions.c */ #include "map_object_actions.h" -#include /* for 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(), try_malloc() */ +#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 "map_objects.h" /* for structs MapObj, MapObjDef, + * get_map_object_def() + */ #include "rrand.h" /* for rrand() */ #include "command_db.h" /* for get_command_id() */ @@ -15,7 +16,7 @@ /* Log monster (described by "dsc_monster1") bumping into "monster2". */ static void monster_bumps_monster(struct World * world, char * dsc_monster1, - struct Monster * monster2); + struct MapObj * monster2); /* Decrement player HPs due to attack of monster described by "dsc_monster", * kill player if his HP hit zero; log the whole action. @@ -26,7 +27,7 @@ static void monster_hits_player(struct World * world, char * dsc_monster); * corpse and increment player's score by the amount of hitpoints the monster * started with; log the whole action. */ -static void player_hits_monster(struct World * world, struct Monster * monster); +static void player_hits_monster(struct World * world, struct MapObj * monster); /* Try moving the player in direction "d" towards coordinate "target"; log * success or failure of the whole action. @@ -37,12 +38,12 @@ static void try_player_move(struct World * world, static void monster_bumps_monster(struct World * world, char * dsc_monster1, - struct Monster * monster2) + struct MapObj * monster2) { char * bump_dsc = " bumps into "; - struct MapObjDef * mod = get_map_obj_def(world, monster2->map_obj.type); - char msg[strlen(dsc_monster1) + strlen(bump_dsc) + strlen(mod->desc) + 3]; - sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->desc); + struct MapObjDef * mod = get_map_object_def(world, monster2->type); + char msg[strlen(dsc_monster1) + strlen(bump_dsc) + strlen(mod->name) + 3]; + sprintf(msg, "\n%s%s%s.", dsc_monster1, bump_dsc, mod->name); update_log(world, msg); } @@ -55,6 +56,7 @@ static void monster_hits_player(struct World * world, char * dsc_monster) sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc); update_log(world, msg); world->player->hitpoints--; + if (0 == world->player->hitpoints) { update_log(world, "\nYou are dead."); @@ -63,45 +65,25 @@ static void monster_hits_player(struct World * world, char * dsc_monster) -static void player_hits_monster(struct World * world, struct Monster * monster) +static void player_hits_monster(struct World * world, struct MapObj * monster) { - char * f_name = "player_hits_monster()"; - struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type); + struct MapObjDef * mod = get_map_object_def(world, monster->type); char * hit_dsc = "You hit the "; - char * monster_dsc = mod->desc; + char * monster_dsc = mod->name; char hitmsg[strlen(hit_dsc) + strlen(monster_dsc) + 3]; sprintf(hitmsg, "\n%s%s.", hit_dsc, monster_dsc); update_log(world, hitmsg); - monster->hitpoints--; - if (0 == monster->hitpoints) + monster->lifepoints--; + if (0 == monster->lifepoints) { hit_dsc = "You kill the "; char kill_msg[strlen(hit_dsc) + strlen(monster_dsc) + 3]; sprintf(kill_msg, "\n%s%s.", hit_dsc, monster_dsc); update_log(world, kill_msg); - struct MonsterDef * md = (struct MonsterDef * ) mod; - struct Item * corpse = try_malloc(sizeof(struct Item), world, f_name); - corpse->map_obj.type = md->corpse_id; - corpse->map_obj.pos = monster->map_obj.pos; - corpse->map_obj.next = world->item; - world->item = corpse; - if (world->monster == monster) - { - world->monster = world->monster->map_obj.next; - } - else - { - 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; - } - } - uint8_t score = md->hitpoints_start; + struct MapObjDef * md = mod; + monster->type = md->corpse_id; + uint8_t score = md->lifepoints; world->score = world->score + score; - free(monster); } } @@ -140,27 +122,27 @@ static void try_player_move(struct World * world, -extern void move_monster(struct World * world, struct Monster * monster) +extern void move_monster(struct World * world, struct MapObj * monster) { 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; + struct yx_uint16 t = mv_yx_in_dir(d, monster->pos); + struct MapObjDef * mod = get_map_object_def(world, monster->type); + char * dsc = mod->name; if (yx_uint16_cmp(&t, &world->player->pos)) { monster_hits_player(world, dsc); return; } - struct Monster * other_monster; - for (other_monster = world->monster; + struct MapObj * other_monster; + for (other_monster = world->map_objs; other_monster != 0; - other_monster = other_monster->map_obj.next) + other_monster = other_monster->next) { - if (other_monster == monster) + if (0 == other_monster->lifepoints || other_monster == monster) { continue; } - if (yx_uint16_cmp(&t, &other_monster->map_obj.pos)) + if (yx_uint16_cmp(&t, &other_monster->pos)) { monster_bumps_monster(world, dsc, other_monster); return; @@ -168,7 +150,7 @@ extern void move_monster(struct World * world, struct Monster * monster) } if (is_passable(world->map, t)) { - monster->map_obj.pos = t; + monster->pos = t; } } @@ -199,12 +181,12 @@ extern void move_player(struct World * world, enum dir d) action_dsc[len + 1] = '\0'; uint8_t action_id = get_command_id(world, action_dsc); struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos); - struct Monster * monster; - for (monster = world->monster; + struct MapObj * monster; + for (monster = world->map_objs; monster != 0; - monster = monster->map_obj.next) + monster = monster->next) { - if (yx_uint16_cmp(&t, &monster->map_obj.pos)) + if (0 < monster->lifepoints && yx_uint16_cmp(&t, &monster->pos)) { player_hits_monster(world, monster); turn_over(world, action_id);