From 951248dddace9f7cadcf30700a3c3e6ad7ae2888 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Thu, 29 Aug 2013 04:20:25 +0200 Subject: [PATCH] Use command IDs from command DB as what is recorded in record file. --- config/keybindings | 2 +- src/command_db.c | 35 +++++++++++++++++++++++++++++++++++ src/command_db.h | 11 ++++++++--- src/control.c | 11 ++++++----- src/map_object_actions.c | 29 ++++++++++++++++++++++++++--- 5 files changed, 76 insertions(+), 12 deletions(-) diff --git a/config/keybindings b/config/keybindings index 2718818..704fed9 100644 --- a/config/keybindings +++ b/config/keybindings @@ -1,4 +1,4 @@ -90 quit +81 quit 75 save_keys 260 scrl_l 261 scrl_r diff --git a/src/command_db.c b/src/command_db.c index cbf6959..2a37b40 100644 --- a/src/command_db.c +++ b/src/command_db.c @@ -25,6 +25,41 @@ static void copy_tokenized_string(struct World * world, +extern uint8_t is_command_id_shortdsc(struct World * world, + uint8_t id, char * shortdsc) +{ + struct Command * cmd_ptr = world->cmd_db->cmds; + while (1) + { + if (id == cmd_ptr->id) + { + if (strcmp(shortdsc, cmd_ptr->dsc_short)) + { + return 0; + } + return 1; + } + cmd_ptr = &cmd_ptr[1]; + } +} + + + +extern uint8_t get_command_id(struct World * world, char * dsc_short) +{ + struct Command * cmd_ptr = world->cmd_db->cmds; + while (1) + { + if (0 == strcmp(dsc_short, cmd_ptr->dsc_short)) + { + return cmd_ptr->id; + } + cmd_ptr = &cmd_ptr[1]; + } +} + + + extern char * get_command_longdsc(struct World * world, char * dsc_short) { struct Command * cmd_ptr = world->cmd_db->cmds; diff --git a/src/command_db.h b/src/command_db.h index efa342a..6b08700 100644 --- a/src/command_db.h +++ b/src/command_db.h @@ -30,7 +30,14 @@ struct CommandDB -/* Give short description of command ("dsc_short"), get long descrption. */ +/* Is "id" the ID of command whose dsc_short is "shortdsc"? Answer in binary. */ +extern uint8_t is_command_id_shortdsc(struct World * world, + uint8_t id, char * shortdsc); + +/* Give short description of command ("dsc_short"), get its ID. */ +extern uint8_t get_command_id(struct World * world, char * dsc_short); + +/* Give short description of command ("dsc_short"), get long description. */ extern char * get_command_longdsc(struct World * world, char * dsc_short); @@ -38,8 +45,6 @@ extern char * get_command_longdsc(struct World * world, char * dsc_short); /* Read in CommandDB from file "config/commands" to world.cmd_db. */ extern void init_command_db(struct World * world); - - /* Free all memory allocated with init_command_db. */ extern void free_command_db(struct World * world); diff --git a/src/control.c b/src/control.c index 9915891..a2a44b2 100644 --- a/src/control.c +++ b/src/control.c @@ -15,28 +15,29 @@ * growshrink_active_window() */ #include "map_object_actions.h" /* for player_wait(), move_player() */ +#include "command_db.h" /* for is_command_id_shortdsc() */ extern void record_control(int action, struct World * world) { - if (0 == action) + if (is_command_id_shortdsc(world, action, "wait")) { player_wait(world); } - else if (NORTH == action) + else if (is_command_id_shortdsc(world, action, "player_u")) { move_player(world, NORTH); } - else if (EAST == action) + else if (is_command_id_shortdsc(world, action, "player_r")) { move_player(world, EAST); } - else if (SOUTH == action) + else if (is_command_id_shortdsc(world, action, "player_d")) { move_player(world, SOUTH); } - else if (WEST == action) + else if (is_command_id_shortdsc(world, action, "player_l")) { move_player(world, WEST); } diff --git a/src/map_object_actions.c b/src/map_object_actions.c index c0d43e7..97f33d6 100644 --- a/src/map_object_actions.c +++ b/src/map_object_actions.c @@ -9,6 +9,7 @@ #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() */ @@ -180,6 +181,28 @@ extern void move_monster(struct World * world, struct Monster * monster) 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 = malloc(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; @@ -189,12 +212,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); } @@ -202,7 +225,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")); } -- 2.30.2