/* map_object_actions.c */
#include "map_object_actions.h"
-#include <stdlib.h> /* for malloc(), calloc(), free() */
+#include <stdlib.h> /* for free() */
#include <string.h> /* 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() */
{
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);
+ 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 * dsc_monster)
{
char * hit_dsc = " hits you";
- char * msg = malloc(strlen(dsc_monster) + strlen(hit_dsc) + 3);
+ 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)
{
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_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);
+ 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_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);
+ 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 = malloc(sizeof(struct Item));
+ 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;
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);
}
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;
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);
}
extern void player_wait (struct World * world)
{
update_log(world, "\nYou wait.");
- turn_over(world, 0);
+ turn_over(world, get_command_id(world, "wait"));
}