From d0007033936ffe8d8ae952aef91b216d4f9a8e95 Mon Sep 17 00:00:00 2001 From: Sveinung Kvilhaugsvik Date: Mon, 15 Feb 2021 16:43:36 +0100 Subject: [PATCH] ruleset: factor out bv_actions read/write. Factor out actions.move_is_blocked_by's loading and storing of a list of actions in the ruleset corresponding to a bv_actions variable. See osdn #41584 --- server/ruleset.c | 59 ++++++++++++++++++++++++--------------- tools/ruleutil/rulesave.c | 41 +++++++++++++++++++-------- 2 files changed, 66 insertions(+), 34 deletions(-) diff --git a/server/ruleset.c b/server/ruleset.c index 1f64b275e5..fd907cf6fe 100644 --- a/server/ruleset.c +++ b/server/ruleset.c @@ -6045,6 +6045,40 @@ static bool load_action_post_success_force(struct section_file *file, return TRUE; } +/**********************************************************************//** + Load a list of actions from the ruleset to a bv_actions bit vector. +**************************************************************************/ +static bool lookup_bv_actions(struct section_file *file, + const char *filename, + bv_actions *target, + const char *path) +{ + if (secfile_entry_by_path(file, path)) { + enum gen_action *listed_actions; + size_t asize; + int j; + + listed_actions = secfile_lookup_enum_vec(file, &asize, gen_action, + "%s", path); + + if (!listed_actions) { + /* Entity exists but couldn't read it. */ + ruleset_error(LOG_ERROR, "\"%s\": %s: bad action list", + filename, path); + + return FALSE; + } + + for (j = 0; j < asize; j++) { + BV_SET(*target, listed_actions[j]); + } + + free(listed_actions); + } + + return TRUE; +} + /**********************************************************************//** Load ruleset file. **************************************************************************/ @@ -6671,29 +6705,10 @@ static bool load_ruleset_game(struct section_file *file, bool act, } } action_iterate_end; - if (secfile_entry_by_path(file, "actions.move_is_blocked_by")) { - enum gen_action *blocking_actions; - size_t asize; - int j; - blocking_actions = - secfile_lookup_enum_vec(file, &asize, gen_action, - "actions.move_is_blocked_by"); - - if (!blocking_actions) { - /* Entity exists but couldn't read it. */ - ruleset_error(LOG_ERROR, - "\"%s\": actions.move_is_blocked_by: bad action list", - filename); - - ok = FALSE; - } - - for (j = 0; j < asize; j++) { - BV_SET(game.info.move_is_blocked_by, blocking_actions[j]); - } - - free(blocking_actions); + if (!lookup_bv_actions(file, filename, &game.info.move_is_blocked_by, + "actions.move_is_blocked_by")) { + ok = FALSE; } /* If the "Poison City" action or the "Poison City Escape" action diff --git a/tools/ruleutil/rulesave.c b/tools/ruleutil/rulesave.c index b6f2ffec00..42a8702202 100644 --- a/tools/ruleutil/rulesave.c +++ b/tools/ruleutil/rulesave.c @@ -983,6 +983,33 @@ static bool save_action_post_success_force(struct section_file *sfile, return TRUE; } +/**********************************************************************//** + Save a bv_actions to the ruleset as a list of actions. +**************************************************************************/ +static bool save_bv_actions(struct section_file *sfile, + bv_actions content, + const char *path) +{ + enum gen_action action_vec[MAX_NUM_ACTIONS]; + int i = 0; + + action_iterate(act_id) { + if (BV_ISSET(content, act_id)) { + action_vec[i] = act_id; + i++; + } + } action_iterate_end; + + if (secfile_insert_enum_vec(sfile, &action_vec, i, gen_action, + "%s", path) != i) { + log_error("Didn't save all of %s.", path); + + return FALSE; + } + + return TRUE; +} + /**********************************************************************//** Save game.ruleset **************************************************************************/ @@ -1214,18 +1241,8 @@ static bool save_game_ruleset(const char *filename, const char *name) "auto_attack", "if_attacker"); } - i = 0; - action_iterate(act_id) { - if (BV_ISSET(game.info.move_is_blocked_by, act_id)) { - action_vec[i] = act_id; - i++; - } - } action_iterate_end; - - if (secfile_insert_enum_vec(sfile, &action_vec, i, gen_action, - "actions.move_is_blocked_by") != i) { - log_error("Didn't save all move blocking actions."); - + if (!save_bv_actions(sfile, game.info.move_is_blocked_by, + "actions.move_is_blocked_by")) { return FALSE; } -- 2.20.1