X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fmap_object_actions.c;h=1b1433949599d5792cf7f1540bc56347d3b87663;hb=ec5c4edd169be8fe8c778cabdfc7ada665629ffd;hp=490874f249f018c9315e753e8e6633c8940343b6;hpb=10c1227861382eb2bc754dc5a91eaf662e1f9231;p=plomrogue diff --git a/src/map_object_actions.c b/src/map_object_actions.c index 490874f..1b14339 100644 --- a/src/map_object_actions.c +++ b/src/map_object_actions.c @@ -1,60 +1,59 @@ /* map_object_actions.c */ #include "map_object_actions.h" -#include /* for malloc(), calloc(), free() */ +#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()*/ +#include "misc.h" /* for update_log(), turn_over(), try_malloc() */ #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 "desc_monster1") bumping into "monster2". */ -static void monster_bumps_monster(struct World * world, char * desc_monster1, +/* 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 "desc_monster", +/* 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 * desc_monster); +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. +/* Decrement HP of "monster" hit by player, kill it if its HP hit zero, create a + * 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 Monster * monster); /* Try moving the player in direction "d" towards coordinate "target"; log * success or failure of the whole action. */ static void try_player_move(struct World * world, - enum dir d, struct yx_uint16 target) + enum dir d, struct yx_uint16 target); -static void monster_bumps_monster(struct World * world, char * desc_monster1, +static void monster_bumps_monster(struct World * world, char * dsc_monster1, struct Monster * monster2) { - char * bumpdesc = " bumps into "; + char * bump_dsc = " bumps into "; struct MapObjDef * mod = get_map_obj_def(world, monster2->map_obj.type); - char * msg = malloc(strlen(desc_monster1) + strlen(bumpdesc) - + strlen(mod->desc) + 3); - sprintf(msg, "\n%s%s%s.", desc_monster1, bumpdesc, mod->desc); + char msg[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 * desc_monster) +static void monster_hits_player(struct World * world, char * dsc_monster) { - char * hitdesc = " hits you"; - char * msg = malloc(strlen(desc_monster) + strlen(hitdesc) + 3); - sprintf(msg, "\n%s%s.", desc_monster, hitdesc); + char * hit_dsc = " hits you"; + char msg[strlen(dsc_monster) + strlen(hit_dsc) + 3]; + sprintf(msg, "\n%s%s.", dsc_monster, hit_dsc); update_log(world, msg); - free(msg); world->player->hitpoints--; if (0 == world->player->hitpoints) { @@ -66,22 +65,26 @@ static void monster_hits_player(struct World * world, char * desc_monster) static void player_hits_monster(struct World * world, struct Monster * monster) { + char * f_name = "player_hits_monster()"; struct MapObjDef * mod = get_map_obj_def(world, monster->map_obj.type); - char * hit_desc = "You hit the "; - char * monster_desc = mod->desc; - char * msg = malloc(strlen(hit_desc) + strlen(monster_desc) + 3); - sprintf(msg, "\n%s%s.", hit_desc, monster_desc); - update_log(world, msg); - free(msg); + char * hit_dsc = "You hit the "; + char * monster_dsc = mod->desc; + 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) { - hit_desc = "You kill the "; - msg = malloc(strlen(hit_desc) + strlen(monster_desc) + 3); - sprintf(msg, "\n%s%s.", hit_desc, monster_desc); - update_log(world, msg); - free(msg); + 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; @@ -96,6 +99,8 @@ static void player_hits_monster(struct World * world, struct Monster * monster) m_prev->map_obj.next = monster->map_obj.next; } } + uint8_t score = md->hitpoints_start; + world->score = world->score + score; free(monster); } } @@ -128,10 +133,9 @@ static void try_player_move(struct World * world, dsc_move = "You move "; world->player->pos = target; } - char * msg = malloc(strlen(dsc_move) + strlen (dsc_dir) + 3); + char msg[strlen(dsc_move) + strlen (dsc_dir) + 3]; sprintf(msg, "\n%s%s.", dsc_move, dsc_dir); update_log(world, msg); - free(msg); } @@ -141,10 +145,10 @@ extern void move_monster(struct World * world, struct Monster * 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 * desc = mod->desc; + char * dsc = mod->desc; if (yx_uint16_cmp(&t, &world->player->pos)) { - monster_hits_player(world, desc); + monster_hits_player(world, dsc); return; } struct Monster * other_monster; @@ -158,7 +162,7 @@ extern void move_monster(struct World * world, struct Monster * monster) } if (yx_uint16_cmp(&t, &other_monster->map_obj.pos)) { - monster_bumps_monster(world, desc, other_monster); + monster_bumps_monster(world, dsc, other_monster); return; } } @@ -170,8 +174,30 @@ extern void move_monster(struct World * world, struct Monster * monster) -extern void move_player (struct World * world, enum dir d) +extern void move_player(struct World * world, enum dir d) { + char * action_dsc_prototype = "player_"; + uint8_t len = strlen(action_dsc_prototype); + char action_dsc[len + 2]; + memcpy(action_dsc, action_dsc_prototype, len); + if (NORTH == d) + { + action_dsc[len] = 'u'; + } + else if (SOUTH == d) + { + action_dsc[len] = 'd'; + } + else if (WEST == d) + { + action_dsc[len] = 'l'; + } + else if (EAST == d) + { + action_dsc[len] = 'r'; + } + 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; @@ -181,12 +207,12 @@ extern void move_player (struct World * world, enum dir d) if (yx_uint16_cmp(&t, &monster->map_obj.pos)) { player_hits_monster(world, monster); - turn_over(world, d); + turn_over(world, action_id); return; } } try_player_move(world, d, t); - turn_over(world, d); + turn_over(world, action_id); } @@ -194,7 +220,7 @@ extern void move_player (struct World * world, enum dir d) extern void player_wait (struct World * world) { update_log(world, "\nYou wait."); - turn_over(world, 0); + turn_over(world, get_command_id(world, "wait")); } @@ -211,4 +237,3 @@ extern char is_passable (struct Map * map, struct yx_uint16 pos) } return passable; } -