From: Christian Heller Date: Wed, 5 Feb 2014 14:18:46 +0000 (+0100) Subject: Added diagonal movement, with a 1.4 penalty. X-Git-Tag: tce~845 X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/bar%20baz.html?a=commitdiff_plain;h=e03020342a74aef143b1ec38c18966dac64181b5;p=plomrogue Added diagonal movement, with a 1.4 penalty. --- diff --git a/README b/README index 722ce9d..7106769 100644 --- a/README +++ b/README @@ -16,6 +16,9 @@ Enemies' AI is very dumb so far: Each turn, they try to move towards their path-wise nearest enemy. If no enemy is found in their surroundings, they just wait. +Note that diagonal movement is possible, but takes (40%) longer than orthogonal +movement. + Once you start a new world, every move of yours is recorded in a file called "record". Once you re-start the game, all of your previous moves are replayed automatically up to the point wherere you left the game. To start over in a new diff --git a/confclient/commands b/confclient/commands index 50c2069..cd945de 100644 --- a/confclient/commands +++ b/confclient/commands @@ -3,25 +3,45 @@ wait / next turn wait 0 % -player_u -player up +move_8 +move north move -N +8 % -player_d -player down +move_9 +move north-east move -S +9 % -player_l -player left +move_6 +move east move -W +6 % -player_r -player right +move_3 +move south-east move -E +3 +% +move_2 +move south +move +2 +% +move_1 +move south-west +move +1 +% +move_4 +move west +move +4 +% +move_7 +move north-west +move +7 % pick pick up object from ground diff --git a/confclient/interface_conf b/confclient/interface_conf index fdc2609..da4442d 100644 --- a/confclient/interface_conf +++ b/confclient/interface_conf @@ -12,7 +12,7 @@ 60 cyc_win_b 262 scrl_l 360 scrl_r -114 reload_conf +82 reload_conf 67 save_conf % 258 shift_f @@ -62,7 +62,7 @@ Inventory 100 drop 259 inv_u 258 inv_d -117 use +99 use % i Info @@ -85,10 +85,14 @@ Map -64 112 pick 58 wait -107 player_u -106 player_d -104 player_l -108 player_r +107 move_8 +117 move_9 +108 move_6 +110 move_3 +106 move_2 +98 move_1 +104 move_4 +121 move_7 259 map_u 258 map_d 260 map_l diff --git a/confclient/single_wins/info b/confclient/single_wins/info index a450a8e..aef1276 100644 --- a/confclient/single_wins/info +++ b/confclient/single_wins/info @@ -1,11 +1,15 @@ 81 quit 58 wait -107 player_u -106 player_d -104 player_l -108 player_r +107 move_8 +117 move_9 +108 move_6 +110 move_3 +106 move_2 +98 move_1 +104 move_4 +121 move_7 112 pick -117 use +99 use 100 drop 46 map_c 265 to_a_keywin diff --git a/confclient/single_wins/inventory b/confclient/single_wins/inventory index 6e30f7e..d15ff2c 100644 --- a/confclient/single_wins/inventory +++ b/confclient/single_wins/inventory @@ -1,11 +1,15 @@ 81 quit 58 wait -107 player_u -106 player_d -104 player_l -108 player_r +107 move_8 +117 move_9 +108 move_6 +110 move_3 +106 move_2 +98 move_1 +104 move_4 +121 move_7 112 pick -117 use +99 use 100 drop 46 map_c 265 to_a_keywin diff --git a/confclient/single_wins/log b/confclient/single_wins/log index 13ea5b0..c578e38 100644 --- a/confclient/single_wins/log +++ b/confclient/single_wins/log @@ -1,11 +1,15 @@ 81 quit 58 wait -107 player_u -106 player_d -104 player_l -108 player_r +107 move_8 +117 move_9 +108 move_6 +110 move_3 +106 move_2 +98 move_1 +104 move_4 +121 move_7 112 pick -117 use +99 use 100 drop 46 map_c 265 to_a_keywin diff --git a/confclient/single_wins/map b/confclient/single_wins/map index 99ed310..91bf2f9 100644 --- a/confclient/single_wins/map +++ b/confclient/single_wins/map @@ -1,11 +1,15 @@ 81 quit 58 wait -107 player_u -106 player_d -104 player_l -108 player_r +107 move_8 +117 move_9 +108 move_6 +110 move_3 +106 move_2 +98 move_1 +104 move_4 +121 move_7 112 pick -117 use +99 use 100 drop 46 map_c 265 to_a_keywin diff --git a/confserver/map_object_actions b/confserver/map_object_actions index 54dc543..dc0de83 100644 --- a/confserver/map_object_actions +++ b/confserver/map_object_actions @@ -3,18 +3,18 @@ wait %% 2 -3 +5 move %% 3 -10 +15 pick_up %% 4 -3 +5 drop %% 5 -30 +45 use %% diff --git a/src/client/control.c b/src/client/control.c index b73d801..dff79ab 100644 --- a/src/client/control.c +++ b/src/client/control.c @@ -8,7 +8,7 @@ #include "keybindings.h" /* get_command_to_keycode(), get_keycode_to_command(), * mod_selected_keyb(), move_keyb_selection() */ -#include "map_window.h" /* for map_scroll(), map_center() */ +#include "map.h" /* for map_scroll(), map_center() */ #include "misc.h" /* reload_interface_conf(), save_interface_conf(), * nav_inventory() */ @@ -81,10 +81,10 @@ static uint8_t try_2args(struct Command * command, char * match, static uint8_t try_client_commands(struct Command * command) { return ( try_0args(command, "map_c", map_center) - || try_1args(command, "map_u", map_scroll, 'N') - || try_1args(command, "map_d", map_scroll, 'S') - || try_1args(command, "map_r", map_scroll, 'E') - || try_1args(command, "map_l", map_scroll, 'W') + || try_1args(command, "map_u", map_scroll, '8') + || try_1args(command, "map_d", map_scroll, '2') + || try_1args(command, "map_r", map_scroll, '6') + || try_1args(command, "map_l", map_scroll, '4') || try_1args(command, "inv_u", nav_inventory, 'u') || try_1args(command, "inv_d", nav_inventory, 'd') || try_1args(command, "cyc_win_f", cycle_active_win, 'f') diff --git a/src/client/io.c b/src/client/io.c index 481495d..c86ff84 100644 --- a/src/client/io.c +++ b/src/client/io.c @@ -18,7 +18,7 @@ * try_fgetc() */ #include "control.h" /* try_key() */ -#include "map_window.h" /* map_center() */ +#include "map.h" /* map_center() */ #include "misc.h" /* reset_windows() */ #include "windows.h" /* reset_windows_on_winch(), draw_all_wins() */ #include "world.h" /* world global */ diff --git a/src/client/map.c b/src/client/map.c new file mode 100644 index 0000000..a419039 --- /dev/null +++ b/src/client/map.c @@ -0,0 +1,47 @@ +/* src/client/map.c */ + +#include "map.h" +#include /* uint16_t */ +#include "misc.h" /* center_offset() */ +#include "windows.h" /* struct Win, get_win_by_id() */ +#include "world.h" /* for global world */ + + + +extern void map_scroll(char d) +{ + struct Win * win = get_win_by_id('m'); + uint16_t offset; + if (('8' == d || '2' == d) && world.map.size.y > win->frame_size.y) + { + offset = center_offset(win->center.y, + world.map.size.y, win->frame_size.y); + win->center.y = offset + (win->frame_size.y / 2); + if ('2' == d && win->center.y < world.map.size.y - 1) + { + win->center.y++; + return; + } + win->center.y = win->center.y - ('8' == d && win->center.y > 0); + } + else if (('4' == d || '6' == d) && world.map.size.x > win->frame_size.x) + { + offset = center_offset(win->center.x, + world.map.size.x, win->frame_size.x); + win->center.x = offset + (win->frame_size.x / 2); + if ('6' == d && win->center.x < world.map.size.x - 1) + { + win->center.x++; + return; + } + win->center.x = win->center.x - ('4' == d && win->center.x > 0); + } +} + + + +extern void map_center() +{ + struct Win * win_map = get_win_by_id('m'); + win_map->center = world.player_pos; +} diff --git a/src/client/map.h b/src/client/map.h new file mode 100644 index 0000000..d104c28 --- /dev/null +++ b/src/client/map.h @@ -0,0 +1,28 @@ +/* src/client/map.h + * + * Routines for the game map window. + */ + +#ifndef MAP_H +#define MAP_H + +#include "../common/yx_uint16.h" /* yx_uint16 struct */ + + +struct Map +{ + struct yx_uint16 size; /* map's height/width in number of cells */ + char * cells; /* sequence of bytes encoding map cells */ +}; + + + +/* Try changing map window's focus into direction "d" (north = "N" etc.). */ +extern void map_scroll(char d); + +/* Center map window on player (even if it is non-visible). */ +extern void map_center(); + + + +#endif diff --git a/src/client/map_window.c b/src/client/map_window.c deleted file mode 100644 index e9f7ced..0000000 --- a/src/client/map_window.c +++ /dev/null @@ -1,47 +0,0 @@ -/* src/client/map_window.c */ - -#include "map_window.h" -#include /* uint16_t */ -#include "misc.h" /* center_offset() */ -#include "windows.h" /* struct Win, get_win_by_id() */ -#include "world.h" /* for global world */ - - - -extern void map_scroll(char d) -{ - struct Win * win = get_win_by_id('m'); - uint16_t offset; - if (('N' == d || 'S' == d) && world.map.size.y > win->frame_size.y) - { - offset = center_offset(win->center.y, - world.map.size.y, win->frame_size.y); - win->center.y = offset + (win->frame_size.y / 2); - if ('S' == d && win->center.y < world.map.size.y - 1) - { - win->center.y++; - return; - } - win->center.y = win->center.y - ('N' == d && win->center.y > 0); - } - else if (('W' == d || 'E' == d) && world.map.size.x > win->frame_size.x) - { - offset = center_offset(win->center.x, - world.map.size.x, win->frame_size.x); - win->center.x = offset + (win->frame_size.x / 2); - if ('E' == d && win->center.x < world.map.size.x - 1) - { - win->center.x++; - return; - } - win->center.x = win->center.x - ('W' == d && win->center.x > 0); - } -} - - - -extern void map_center() -{ - struct Win * win_map = get_win_by_id('m'); - win_map->center = world.player_pos; -} diff --git a/src/client/map_window.h b/src/client/map_window.h deleted file mode 100644 index 39b0e6d..0000000 --- a/src/client/map_window.h +++ /dev/null @@ -1,19 +0,0 @@ -/* src/client/map_window.h - * - * Routines to re-focus the game map window. - */ - -#ifndef MAP_WINDOW_H -#define MAP_WINDOW_H - - - -/* Try changing map window's focus into direction "d" (north = "N" etc.). */ -extern void map_scroll(char d); - -/* Center map window on player (even if it is non-visible). */ -extern void map_center(); - - - -#endif diff --git a/src/client/misc.c b/src/client/misc.c index 4af2beb..09268b5 100644 --- a/src/client/misc.c +++ b/src/client/misc.c @@ -18,7 +18,7 @@ #include "keybindings.h" /* free_keybindings(), read_keybindings_from_file(), * write_keybindings_to_file() */ -#include "map_window.h" /* map_center() */ +#include "map.h" /* map_center() */ #include "windows.h" /* free_winDB(), make_v_screen_and_init_win_sizes(), * read_winconf_from_file(), write_winconf_of_id_to_file(), * toggle_window() diff --git a/src/client/world.h b/src/client/world.h index 307f53f..cbb640a 100644 --- a/src/client/world.h +++ b/src/client/world.h @@ -8,7 +8,7 @@ #include /* uint8_t, uint16_t */ #include /* time_t */ -#include "../common/map.h" /* struct Map */ +#include "map.h" /* struct Map */ #include "../common/yx_uint16.h" /* struct yx_uint16 */ #include "keybindings.h" /* stuct KeyBindingDB */ #include "command_db.h" /* struct CommandDB */ diff --git a/src/server/ai.c b/src/server/ai.c index 6c45571..50c5031 100644 --- a/src/server/ai.c +++ b/src/server/ai.c @@ -1,110 +1,130 @@ /* src/server/ai.c */ #include "ai.h" -#include /* uint8_t, uint32_t, UINT8_MAX */ -#include /* strlen(), memset() */ -#include "../common/yx_uint16.h" /* struct yx_uint16 */ +#include /* NULL */ +#include /* uint8_t, uint16_t, uint32_t, UINT16_MAX */ #include "map_object_actions.h" /* get_moa_id_by_name() */ #include "map_objects.h" /* struct MapObj */ #include "world.h" /* global world */ -/* Write into "neighbor_scores" scores for immediate neighbors to cell at - * "pos_yx" (YX coordinates) and "pos_i" (arry_index) in "score_map". Directions - * determining neighborhood are defined by the letters of "dir"; their order - * also determines in what order scores are written into "neighbor_score". - * "len_dirs" is to store the result of a previous strlen(dir) (so it does not - * have to be called repeatedly and costly in dijkstra_map(); same reason for - * "pos_i"'s redundancy.). "max_score" is written into "neighbor_scores" for - * illegal directions (that from "pos_yx" would lead beyond the map's border). +#define N_DIRS 8 + + + +/* Write into "neighbors" scores of the eight immediate 2D neighbors of the + * "score_map" cell at "pos_i" (array index), as found in the directions north, + * north-east, east etc. (clockwise order). "max_score" is used for illegal + * neighborhoods (i.e. if the direction would lead beyond the map's border). */ -static void get_neighbor_scores(char * dirs, uint8_t len_dirs, - uint8_t * score_map, struct yx_uint16 pos_yx, - uint32_t pos_i, uint8_t max_score, - uint8_t * neighbor_scores); +static void get_neighbor_scores(uint32_t * score_map, uint16_t pos_i, + uint32_t max_score, uint32_t * neighbors); /* Iterate over scored cells in "score_map" of world.map's 2D geometry. Compare - * each cell's score against the scores of its immediate neighbors in "dirs" - * directions; if at least one of these is lower, re-set the current cell's - * score to one higher than its lowest neighbor score. Repeat this whole process - * until all cells have settled on their final score. Ignore cells whose - * position in "score_map" fits a non-island cell in world.map.cells. Expect - * "max_score" to be the maximum score for cells, marking them as unreachable. + * each cell's score against the score of its immediate neighbors in N_DIRS + * directions. If it's neighbors are low enough that the result would be lower + * than the current value, re-set it to world.map.dist_orthogonal points higher + * than its lowest-scored orthogonal neighbor or world.map.dist_diagonal points + * higher than its lowest-scored diagonal neighbor (whatever would result in a + * lower value). Repeat this whole process until all cells have settled on their + * final score. Ignore cells whose position in "score_map" fits cells of + * unreachable terrain in world.map.cells. Expect "max_score" to be the maximum + * score for cells, marking them as unreachable. */ -static void dijkstra_map(char * dirs, uint8_t * score_map, uint8_t max_score); +static void dijkstra_map(uint32_t * score_map, uint32_t max_score); -/* Return char of direction ("N", "E", "S" or "W") of enemy with the shortest - * path to "mo_target". If no enemy is around, return 0. +/* Return numpad char of direction ("8", "6", "2", "4" etc.) of enemy with the + * shortest path to "mo_origin". If no enemy is around, return 0. */ -static char get_dir_to_nearest_enemy(struct MapObj * mo_target); +static char get_dir_to_nearest_enemy(struct MapObj * mo_origin); -static void get_neighbor_scores(char * dirs, uint8_t len_dirs, - uint8_t * score_map, struct yx_uint16 pos_yx, - uint32_t pos_i, uint8_t max_score, - uint8_t * neighbor_scores) +static void get_neighbor_scores(uint32_t * score_map, uint16_t pos_i, + uint32_t max_score, uint32_t * neighbors) { - memset(neighbor_scores, max_score, len_dirs); - uint8_t i_dirs; - for (i_dirs = 0; i_dirs < len_dirs; i_dirs++) + uint16_t map_size = world.map.size.y * world.map.size.x; + uint8_t i_dir; + for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++); + uint8_t open_north = pos_i >= world.map.size.x; + uint8_t open_east = pos_i + 1 % world.map.size.x; + uint8_t open_south = pos_i + world.map.size.x < map_size; + uint8_t open_west = pos_i % world.map.size.x; + if (open_north) { - if ('N' == dirs[i_dirs] && pos_yx.y > 0) - { - neighbor_scores[i_dirs] = score_map[pos_i - world.map.size.x]; - } - else if ('E' == dirs[i_dirs] && pos_yx.x < world.map.size.x - 1) - { - neighbor_scores[i_dirs] = score_map[pos_i + 1]; - } - else if ('S' == dirs[i_dirs] && pos_yx.y < world.map.size.y - 1) - { - neighbor_scores[i_dirs] = score_map[pos_i + world.map.size.x]; - } - else if ('W' == dirs[i_dirs] && pos_yx.x > 0) - { - neighbor_scores[i_dirs] = score_map[pos_i - 1]; - } + neighbors[0] = score_map[pos_i - world.map.size.x]; + } + if (open_north && open_east) + { + neighbors[1] = score_map[pos_i - world.map.size.x + 1]; + } + if (open_east) + { + neighbors[2] = score_map[pos_i + 1]; + } + if (open_east && open_south) + { + neighbors[3] = score_map[pos_i + 1 + world.map.size.x]; + } + if (open_south) + { + neighbors[4] = score_map[pos_i + world.map.size.x]; + } + if (open_south && open_west) + { + neighbors[5] = score_map[pos_i + world.map.size.x - 1]; + } + if (open_west) + { + neighbors[6] = score_map[pos_i - 1]; + } + if (open_west && open_north) + { + neighbors[7] = score_map[pos_i - 1 - world.map.size.x]; } } -static void dijkstra_map(char * dirs, uint8_t * score_map, uint8_t max_score) +static void dijkstra_map(uint32_t * score_map, uint32_t max_score) { - uint8_t len_dirs = strlen(dirs); - uint8_t neighbor_scores[len_dirs]; - struct yx_uint16 pos_yx; - uint32_t pos_i; - uint8_t i_scans, i_dirs, local_score, min_neighbor_score; + uint32_t i_scans, neighbors[N_DIRS], min_neighbor_o, min_neighbor_d; + uint16_t map_size = world.map.size.y * world.map.size.x; + uint16_t pos; uint8_t scores_still_changing = 1; + uint8_t i_dirs; for (i_scans = 0; scores_still_changing; i_scans++) { scores_still_changing = 0; - for (pos_yx.y = 0, pos_i = 0; pos_yx.y < world.map.size.y; pos_yx.y++) + for (pos = 0; pos < map_size; pos++) { - for (pos_yx.x = 0; pos_yx.x < world.map.size.x; pos_yx.x++, pos_i++) + if ('.' == world.map.cells[pos]) { - if ('.' == world.map.cells[pos_i]) + get_neighbor_scores(score_map, pos, max_score, neighbors); + min_neighbor_d = max_score; + min_neighbor_o = max_score; + for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++) { - local_score = score_map[pos_i]; - get_neighbor_scores(dirs, len_dirs, score_map, pos_yx, - pos_i, max_score, neighbor_scores); - min_neighbor_score = max_score; - for (i_dirs = 0; i_dirs < len_dirs; i_dirs++) + if (!(i_dirs % 2) && min_neighbor_o > neighbors[i_dirs]) { - if (min_neighbor_score > neighbor_scores[i_dirs]) - { - min_neighbor_score = neighbor_scores[i_dirs]; - } + min_neighbor_o = neighbors[i_dirs]; } - if (local_score > min_neighbor_score + 1) + else if (i_dirs % 2 && min_neighbor_d > neighbors[i_dirs]) { - score_map[pos_i] = min_neighbor_score + 1; - scores_still_changing = 1; + min_neighbor_d = neighbors[i_dirs]; } } + if (score_map[pos] > min_neighbor_o + world.map.dist_orthogonal) + { + score_map[pos] = min_neighbor_o + world.map.dist_orthogonal; + scores_still_changing = 1; + } + if (score_map[pos] > min_neighbor_d + world.map.dist_diagonal) + { + score_map[pos] = min_neighbor_d + world.map.dist_diagonal; + scores_still_changing = 1; + } } } } @@ -112,42 +132,44 @@ static void dijkstra_map(char * dirs, uint8_t * score_map, uint8_t max_score) -static char get_dir_to_nearest_enemy(struct MapObj * mo_target) +static char get_dir_to_nearest_enemy(struct MapObj * mo_origin) { /* Calculate for each cell the distance to the nearest map actor that is - * not "mo_target", with movement only possible in the directions of "dir". + * not "mo_origin", with movement only possible in the directions of "dir". * (Actors' own cells start with a distance of 0 towards themselves.) */ - uint8_t max_score = UINT8_MAX; /* Score for cells treated as unreachable. */ - char * dirs = "NESW"; - uint8_t score_map[world.map.size.y * world.map.size.x]; - memset(score_map, max_score, world.map.size.y * world.map.size.x); + uint16_t map_size = world.map.size.y * world.map.size.x; + uint32_t max_score = UINT32_MAX - (world.map.dist_diagonal + 1); + uint32_t score_map[map_size]; + uint32_t i; + for (i = 0; i < map_size; i++) + { + score_map[i] = max_score; + } struct MapObj * mo = world.map_objs; for (; mo != NULL; mo = mo->next) { - if (!mo->lifepoints || mo == mo_target) + if (!mo->lifepoints || mo == mo_origin) { continue; } score_map[(mo->pos.y * world.map.size.x) + mo->pos.x] = 0; } - dijkstra_map(dirs, score_map, max_score); - - /* Return direction of "mo_target"'s lowest-scored neighbor cell. */ - uint8_t len_dirs = strlen(dirs); - uint32_t pos_i = (mo_target->pos.y * world.map.size.x) + mo_target->pos.x; - uint8_t neighbor_scores[len_dirs]; - get_neighbor_scores(dirs, len_dirs, score_map, mo_target->pos, pos_i, - max_score, neighbor_scores); + dijkstra_map(score_map, max_score); + + /* Return direction of "mo_origin"'s lowest-scored neighbor cell. */ + uint32_t neighbors[N_DIRS]; + uint16_t pos_i = (mo_origin->pos.y * world.map.size.x) + mo_origin->pos.x; + get_neighbor_scores(score_map, pos_i, max_score, neighbors); char dir_to_nearest_enemy = 0; - uint8_t min_neighbor_score = max_score; - uint8_t i_dirs; - for (i_dirs = 0; i_dirs < len_dirs; i_dirs++) + uint32_t min_neighbor = max_score; + char * dirs = "89632147"; /* get_neighbor_scores()'s clockwise dir order.*/ + for (i = 0; i < N_DIRS; i++) { - if (min_neighbor_score > neighbor_scores[i_dirs]) + if (min_neighbor > neighbors[i]) { - min_neighbor_score = neighbor_scores[i_dirs]; - dir_to_nearest_enemy = dirs[i_dirs]; + min_neighbor = neighbors[i]; + dir_to_nearest_enemy = dirs[i]; } } return dir_to_nearest_enemy; diff --git a/src/server/main.c b/src/server/main.c index 0384ada..64d4434 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -49,6 +49,12 @@ int main(int argc, char ** argv) world.tmp_suffix = "_tmp"; set_err_try_fgets_delim("%%\n"); + /* Set map geometry. */ + world.map.size.x = 64; + world.map.size.y = 64; + world.map.dist_orthogonal = 5; + world.map.dist_diagonal = 7; + /* Check existence of config files. */ char * err_mod = "No map object definitions file."; char * err_moa = "No map object actions file."; diff --git a/src/server/map.c b/src/server/map.c index f46b03e..2969b6d 100644 --- a/src/server/map.c +++ b/src/server/map.c @@ -2,7 +2,6 @@ #include "map.h" #include /* uint8_t, uint16_t, uint32_t */ -#include "../common/map.h" /* struct Map */ #include "../common/try_malloc.h" /* try_malloc() */ #include "../common/yx_uint16.h" /* struct yx_uint16 */ #include "rrand.h" /* rrand() */ @@ -13,8 +12,6 @@ extern void init_map() { char * f_name = "init_map()"; - world.map.size.x = 64; - world.map.size.y = 64; uint32_t size = world.map.size.x * world.map.size.y; world.map.cells = try_malloc(size, f_name); uint16_t y, x; diff --git a/src/server/map.h b/src/server/map.h index 464f896..9846a58 100644 --- a/src/server/map.h +++ b/src/server/map.h @@ -3,21 +3,30 @@ * Struct for the game map and routines to create and scroll on it. */ -#ifndef MAP_H_SERVER -#define MAP_H_SERVER +#ifndef MAP_H +#define MAP_H #include /* uint8_t */ -#include "../common/map.h" /* struct Map */ #include "../common/yx_uint16.h" /* yx_uint16 struct */ -/* Initialize island map as 64 x 64 "~" cells representing water and "." cells - * representing land. The shape of the island is generated randomly by starting - * with a sea containing one land cell in the middle and then going into a cycle - * of repeatedly selecting a random cell on the map and transforming it into a - * land cell if it is horizontally or vertically neighbor to one; the cycle ends - * when a land cell is due to be created right at the border of the map. +struct Map +{ + struct yx_uint16 size; /* Map's height/width in number of cells. */ + char * cells; /* Sequence of bytes encoding map cells. */ + uint8_t dist_orthogonal; /* Ratio of the diagonal movement penalty as */ + uint8_t dist_diagonal; /* encoded by (.dist_diagonal/.dist_orthonal). */ +}; + + + +/* Initialize island map "~" cells representing water and "." cells representing + * land. The shape of the island is generated randomly by starting with a sea + * containing one land cell in the middle and then going into a cycle of + * repeatedly selecting a random cell on the map and transforming it into a land + * cell if it is horizontally or vertically neighbor to one; the cycle ends when + * a land cell is due to be created right at the border of the map. */ extern void init_map(); diff --git a/src/server/map_object_actions.c b/src/server/map_object_actions.c index 0cbc1bf..174ec64 100644 --- a/src/server/map_object_actions.c +++ b/src/server/map_object_actions.c @@ -40,8 +40,10 @@ static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted); /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0. + * (match_dir() is just a little helper to playerbonus_move().) */ static void playerbonus_wait(); +static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match); static void playerbonus_move(char d, uint8_t passable); static void playerbonus_drop(uint8_t owns_none); static void playerbonus_pick(uint8_t picked); @@ -151,21 +153,28 @@ static void playerbonus_wait() -static void playerbonus_move(char d, uint8_t passable) +static uint8_t match_dir(char d, char ** dsc_d, char match, char * dsc_match) { - char * dsc_dir = "north"; - if ('E' == d) - { - dsc_dir = "east" ; - } - else if ('S' == d) + if (d == match) { - dsc_dir = "south"; - } - else if ('W' == d) - { - dsc_dir = "west" ; + * dsc_d = dsc_match; + return 1; } + return 0; +} + + + +static void playerbonus_move(char d, uint8_t passable) +{ + char * dsc_dir = "north"; + if ( match_dir(d, &dsc_dir, '6', "east") + || match_dir(d, &dsc_dir, '2', "south") + || match_dir(d, &dsc_dir, '4', "west") + || match_dir(d, &dsc_dir, '7', "north-west") + || match_dir(d, &dsc_dir, '9', "north-east") + || match_dir(d, &dsc_dir, '1', "south-west") + || match_dir(d, &dsc_dir, '3', "south-east")); char * dsc_move = "You move "; if (0 == passable) { diff --git a/src/server/map_object_actions.h b/src/server/map_object_actions.h index 1eca591..eec0087 100644 --- a/src/server/map_object_actions.h +++ b/src/server/map_object_actions.h @@ -37,7 +37,7 @@ extern uint8_t get_moa_id_by_name(char * name); extern void actor_wait(struct MapObj * mo); /* Actor "mo" tries to move one step in direction described by char mo->arg - * (where east is 'E', north 'N') etc. Move either succeeds, or another actor is + * (where east is '6', north '8') etc. Move either succeeds, or another actor is * encountered and hit (which leads ot its lifepoint decreasing by one and * eventually death), or the move fails due to an impassable target square. */ diff --git a/src/server/run.c b/src/server/run.c index 1df4b63..83bb724 100644 --- a/src/server/run.c +++ b/src/server/run.c @@ -26,6 +26,17 @@ */ static void turn_over(); +/* Helper to turn_over() to determine whether a map object's action effort has + * reached its end. The simplicity of just comparing map_object->progress to + * moa->effort is suspended for actor movement, for in this case the effort + * depends on the diagonal movement penalty expressed in the ratio of + * world.map.dist_diagonal / world.map.dist_orthogonal. (Movement being diagonal + * or orthogonal is determined by the ->arg char encoding an even or un-even + * number digit). + */ +static uint8_t is_effort_finished(struct MapObjAct * moa, + struct MapObj * map_object); + /* If "msg"'s first part matches "command_name", set player's MapObj's .command * to the command's id and its .arg to a numerical value following in the latter * part of "msg" (if no digits are found, use 0); then finish player's turn and @@ -64,7 +75,7 @@ static void turn_over() { moa = moa->next; } - if (map_object->progress == moa->effort) + if (is_effort_finished(moa, map_object)) { moa->func(map_object); map_object->command = 0; @@ -77,6 +88,37 @@ static void turn_over() +static uint8_t is_effort_finished(struct MapObjAct * moa, + struct MapObj * map_object) +{ + if (moa->func != actor_move) + { + if (map_object->progress == moa->effort) + { + return 1; + } + } + else if (strchr("8624", map_object->arg)) + { + if (map_object->progress == moa->effort) + { + return 1; + } + } + else if (strchr("1379", map_object->arg)) + { + uint16_t diagonal_effort = (moa->effort * world.map.dist_diagonal) + / world.map.dist_orthogonal; + if (map_object->progress == diagonal_effort) + { + return 1; + } + } + return 0; +} + + + static uint8_t apply_player_command(char * msg, char * command_name) { if (!strncmp(msg, command_name, strlen(command_name))) diff --git a/src/server/world.h b/src/server/world.h index 3bea1f1..c17fd60 100644 --- a/src/server/world.h +++ b/src/server/world.h @@ -7,7 +7,7 @@ #define MAIN_H #include /* uint8_t, uint16_t, uint32_t */ -#include "../common/map.h" /* struct Map */ +#include "map.h" /* struct Map */ struct MapObjDef; struct MapObjAct; struct MapObj; diff --git a/src/server/yx_uint16.c b/src/server/yx_uint16.c index d793000..e64e98f 100644 --- a/src/server/yx_uint16.c +++ b/src/server/yx_uint16.c @@ -19,20 +19,40 @@ extern uint8_t yx_uint16_cmp(struct yx_uint16 * a, struct yx_uint16 * b) extern struct yx_uint16 mv_yx_in_dir(char d, struct yx_uint16 yx) { - if (d == 'N' && yx.y > 0) + if (d == '8' && yx.y > 0) { yx.y--; } - else if (d == 'E' && yx.x < UINT16_MAX) + else if (d == '9' && yx.y > 0 && yx.x < UINT16_MAX) { + yx.y--; + yx.x++; + } + else if (d == '6' && yx.x < UINT16_MAX) + { + yx.x++; + } + else if (d == '3' && yx.x < UINT16_MAX && yx.y < UINT16_MAX) + { + yx.y++; yx.x++; } - else if (d == 'S' && yx.y < UINT16_MAX) + else if (d == '2' && yx.y < UINT16_MAX) { yx.y++; } - else if (d == 'W' && yx.x > 0) + else if (d == '1' && yx.y < UINT16_MAX && yx.x > 0) { + yx.y++; + yx.x--; + } + else if (d == '4' && yx.x > 0) + { + yx.x--; + } + else if (d == '7' && yx.x > 0 && yx.y > 0) + { + yx.y--; yx.x--; } return yx; diff --git a/src/server/yx_uint16.h b/src/server/yx_uint16.h index 04d2573..516050e 100644 --- a/src/server/yx_uint16.h +++ b/src/server/yx_uint16.h @@ -14,8 +14,8 @@ /* Return 1 if two yx_uint16 coordinates at "a" and "b" are equal, else 0. */ extern uint8_t yx_uint16_cmp(struct yx_uint16 * a, struct yx_uint16 * b); -/* Return yx_uint16 coordinate one step from "yx" in direction "dir" (east: 'E', - * west: 'W', north: 'N', south: 'S'). If "dir" is invalid or would wrap the +/* Return yx_uint16 coordinate one step from "yx" in direction "dir" (numpad + * digits: north '8', east: '6', etc.) If "dir" is invalid or would wrap the * move around the edge of a 2^16x2^16 cells field, "yx" remains unchanged. */ extern struct yx_uint16 mv_yx_in_dir(char dir, struct yx_uint16 yx);