From: Christian Heller Date: Wed, 5 Feb 2014 15:11:20 +0000 (+0100) Subject: Some code-internal restructuring following the assumption that game map height / X-Git-Tag: tce~844 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Byoutube_prefix%7D%7D%7B%7Bvideo_data.id_%7D%7D?a=commitdiff_plain;h=f9c94db47aa883149aa762fa128ac1ff1b3f92e1;p=plomrogue Some code-internal restructuring following the assumption that game map height / width can't be higher than uint8_t values. --- diff --git a/src/client/draw_wins.c b/src/client/draw_wins.c index 7cdb0b5..f10e9cb 100644 --- a/src/client/draw_wins.c +++ b/src/client/draw_wins.c @@ -323,7 +323,7 @@ extern void draw_win_map(struct Win * win) { try_resize_winmap(win, world.map.size.y, world.map.size.x); uint16_t z = 0; - uint16_t x, y; + uint8_t x, y; for (y = 0; y < world.map.size.y; y++) { for (x = 0; x < world.map.size.x; x++) diff --git a/src/client/io.c b/src/client/io.c index c86ff84..2b1c165 100644 --- a/src/client/io.c +++ b/src/client/io.c @@ -110,7 +110,7 @@ static void read_map_cells(FILE * file) char * f_name = "read_map_cells()"; free(world.map.cells); world.map.cells = try_malloc(world.map.size.y * world.map.size.x, f_name); - uint16_t y, x; + uint8_t y, x; for (y = 0; y < world.map.size.y; y++) { for (x = 0; x < world.map.size.x; x++) diff --git a/src/client/map.c b/src/client/map.c index a419039..5e4e9c6 100644 --- a/src/client/map.c +++ b/src/client/map.c @@ -1,7 +1,7 @@ /* src/client/map.c */ #include "map.h" -#include /* uint16_t */ +#include /* uint8_t */ #include "misc.h" /* center_offset() */ #include "windows.h" /* struct Win, get_win_by_id() */ #include "world.h" /* for global world */ @@ -11,7 +11,7 @@ extern void map_scroll(char d) { struct Win * win = get_win_by_id('m'); - uint16_t offset; + uint8_t offset; if (('8' == d || '2' == d) && world.map.size.y > win->frame_size.y) { offset = center_offset(win->center.y, @@ -43,5 +43,6 @@ extern void map_scroll(char d) extern void map_center() { struct Win * win_map = get_win_by_id('m'); - win_map->center = world.player_pos; + win_map->center.y = world.player_pos.y; + win_map->center.x = world.player_pos.x; } diff --git a/src/client/windows.h b/src/client/windows.h index 4ac01d0..92a35a1 100644 --- a/src/client/windows.h +++ b/src/client/windows.h @@ -26,8 +26,8 @@ #include /* WINDOW, chtype */ #include /* uint8_t, int16_t, uint16_t */ -#include "keybindings.h" /* struct KeyBindingDB */ #include "../common/yx_uint16.h" /* yx_uint16 struct */ +#include "keybindings.h" /* struct KeyBindingDB */ diff --git a/src/client/world.h b/src/client/world.h index cbb640a..7d1ccbe 100644 --- a/src/client/world.h +++ b/src/client/world.h @@ -9,7 +9,7 @@ #include /* uint8_t, uint16_t */ #include /* time_t */ #include "map.h" /* struct Map */ -#include "../common/yx_uint16.h" /* struct yx_uint16 */ +#include "../common/yx_uint8.h" /* struct yx_uint8 */ #include "keybindings.h" /* stuct KeyBindingDB */ #include "command_db.h" /* struct CommandDB */ #include "windows.h" /* WinDB */ @@ -25,13 +25,13 @@ struct World struct KeyBindingDB kb_winkeys; /* Win keybindings config view keybindings*/ struct Map map; /* game map geometry and content */ time_t last_update; /* used for comparison with server outfile' mtime */ - struct yx_uint16 player_pos; /* coordinates of player on map */ char * log; /* log of player's activities */ char * path_server_in; /* path of server's input fifo */ char * path_interface; /* path of interface configuration file */ char * path_commands; /* path of commands config file */ char * player_inventory; /* one-item-per-line string list of owned items */ char * delim; /* delimiter for multi-line blocks in config files */ + struct yx_uint8 player_pos; /* coordinates of player on map */ uint16_t turn; /* world/game turn */ uint16_t player_score; /* player's score*/ uint8_t halfdelay; /* how long to wait for getch() input in io_loop() */ diff --git a/src/common/map.h b/src/common/map.h deleted file mode 100644 index cac29dc..0000000 --- a/src/common/map.h +++ /dev/null @@ -1,21 +0,0 @@ -/* src/common/map.h - * - * Struct for the game map. - */ - -#ifndef MAP_H -#define MAP_H - -#include "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 */ -}; - - - -#endif diff --git a/src/common/yx_uint16.h b/src/common/yx_uint16.h index 6dd359a..05cf29e 100644 --- a/src/common/yx_uint16.h +++ b/src/common/yx_uint16.h @@ -1,7 +1,6 @@ /* yx_uint16.h * - * Struct coordinates in 2-dimensional space (such as the ncurses screen and - * game maps). + * Used for window / screen maps and game map size. */ #ifndef YX_UINT16_H diff --git a/src/server/io.c b/src/server/io.c index 817d542..a516d81 100644 --- a/src/server/io.c +++ b/src/server/io.c @@ -183,7 +183,7 @@ static void write_inventory(struct MapObj * player, FILE * file) static void write_map(FILE * file) { char * f_name = "write_map()"; - uint32_t map_size = world.map.size.y * world.map.size.x; + uint16_t map_size = world.map.size.y * world.map.size.x; char visible_map[map_size]; memcpy(visible_map, world.map.cells, map_size); struct MapObj * o; @@ -203,7 +203,7 @@ static void write_map(FILE * file) } } } - uint16_t x, y; + uint8_t x, y; for (y = 0; y < world.map.size.y; y++) { for (x = 0; x < world.map.size.x; x++) diff --git a/src/server/map.c b/src/server/map.c index 2969b6d..29beb1a 100644 --- a/src/server/map.c +++ b/src/server/map.c @@ -1,9 +1,9 @@ /* src/server/map.c */ #include "map.h" -#include /* uint8_t, uint16_t, uint32_t */ +#include /* uint8_t, uint16_t */ #include "../common/try_malloc.h" /* try_malloc() */ -#include "../common/yx_uint16.h" /* struct yx_uint16 */ +#include "../common/yx_uint8.h" /* struct yx_uint8 */ #include "rrand.h" /* rrand() */ #include "world.h" /* global world */ @@ -12,9 +12,9 @@ extern void init_map() { char * f_name = "init_map()"; - uint32_t size = world.map.size.x * world.map.size.y; + uint16_t size = world.map.size.x * world.map.size.y; world.map.cells = try_malloc(size, f_name); - uint16_t y, x; + uint8_t y, x; for (y = 0; y < world.map.size.y; y++) { for (x = 0; @@ -22,7 +22,7 @@ extern void init_map() world.map.cells[(y * world.map.size.x) + x] = '~', x++); } world.map.cells[size / 2 + (world.map.size.x / 2)] = '.'; - uint32_t curpos; + uint16_t curpos; while (1) { y = rrand() % world.map.size.y; @@ -51,7 +51,7 @@ extern void init_map() -extern uint8_t is_passable(struct yx_uint16 pos) +extern uint8_t is_passable(struct yx_uint8 pos) { uint8_t passable = 0; if ( 0 <= pos.x && pos.x < world.map.size.x diff --git a/src/server/map.h b/src/server/map.h index 9846a58..b2b1ba2 100644 --- a/src/server/map.h +++ b/src/server/map.h @@ -7,13 +7,14 @@ #define MAP_H #include /* uint8_t */ +#include "../common/yx_uint8.h" /* yx_uint8 struct */ #include "../common/yx_uint16.h" /* yx_uint16 struct */ struct Map { - struct yx_uint16 size; /* Map's height/width in number of cells. */ + struct yx_uint16 size; /* Map's height/width (use max. 256x256)! */ 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). */ @@ -33,7 +34,7 @@ extern void init_map(); /* Check if coordinate "pos" on (or beyond) world.map is accessible to map * object movement. */ -extern uint8_t is_passable(struct yx_uint16 pos); +extern uint8_t is_passable(struct yx_uint8 pos); diff --git a/src/server/map_object_actions.c b/src/server/map_object_actions.c index 174ec64..6f94ce1 100644 --- a/src/server/map_object_actions.c +++ b/src/server/map_object_actions.c @@ -14,14 +14,14 @@ */ #include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ -#include "../common/yx_uint16.h" /* yx_uint16 struct */ +#include "../common/yx_uint8.h" /* struct yx_uint8 */ #include "cleanup.h" /* set_cleanup_flag() */ #include "map_objects.h" /* structs MapObj, MapObjDef, get_player(), * set_object_position(), own_map_object(), * get_map_object_def() */ #include "map.h" /* is_passable() */ -#include "yx_uint16.h" /* mv_yx_in_dir(), yx_uint16_cmp() */ +#include "yx_uint8.h" /* mv_yx_in_dir(), yx_uint8_cmp() */ #include "world.h" /* global world */ @@ -232,7 +232,7 @@ extern void init_map_object_actions() { char * f_name = "init_map_object_actions()"; FILE * file = try_fopen(world.path_map_obj_acts, "r", f_name); - uint16_t linemax = textfile_sizes(file, NULL); + uint32_t linemax = textfile_sizes(file, NULL); char line[linemax + 1]; struct MapObjAct ** moa_ptr_ptr = &world.map_obj_acts; char * context = "Failed reading map object actions config file. "; @@ -325,7 +325,7 @@ extern void actor_wait(struct MapObj * mo) extern void actor_move(struct MapObj * mo) { char d = mo->arg; - struct yx_uint16 target = mv_yx_in_dir(d, mo->pos); + struct yx_uint8 target = mv_yx_in_dir(d, mo->pos); struct MapObj * other_mo; for (other_mo = world.map_objs; other_mo != 0; other_mo = other_mo->next) { @@ -333,7 +333,7 @@ extern void actor_move(struct MapObj * mo) { continue; } - if (yx_uint16_cmp(&target, &other_mo->pos)) + if (yx_uint8_cmp(&target, &other_mo->pos)) { actor_hits_actor(mo, other_mo); return; @@ -377,7 +377,7 @@ extern void actor_pick(struct MapObj * mo) struct MapObj * mo_i; for (mo_i = world.map_objs; NULL != mo_i; mo_i = mo_i->next) { - if (mo_i != mo && yx_uint16_cmp(&mo_i->pos, &mo->pos)) + if (mo_i != mo && yx_uint8_cmp(&mo_i->pos, &mo->pos)) { picked = mo_i; } diff --git a/src/server/map_objects.c b/src/server/map_objects.c index 0401c76..696db04 100644 --- a/src/server/map_objects.c +++ b/src/server/map_objects.c @@ -14,12 +14,12 @@ */ #include "../common/rexit.h" /* exit_err(), exit_trouble() */ #include "../common/try_malloc.h" /* try_malloc() */ -#include "../common/yx_uint16.h" /* yx_uint16 struct */ +#include "../common/yx_uint8.h" /* yx_uint8 struct */ #include "cleanup.h" /* set_cleanup_flag() */ #include "map.h" /* is_passable() */ #include "rrand.h" /* rrand() */ #include "world.h" /* global world */ -#include "yx_uint16.h" /* yx_uint16_cmp() */ +#include "yx_uint8.h" /* yx_uint8_cmp() */ @@ -27,7 +27,7 @@ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id); /* Return random passable (as by is_passable()) position on world.map. */ -static struct yx_uint16 find_passable_pos(); +static struct yx_uint8 find_passable_pos(); /* Add object of "type" to map on random position. Don't place actor on actor.*/ static void add_map_object(uint8_t type); @@ -53,9 +53,9 @@ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id) -static struct yx_uint16 find_passable_pos() +static struct yx_uint8 find_passable_pos() { - struct yx_uint16 pos; + struct yx_uint8 pos; for (pos.y = pos.x = 0; 0 == is_passable(pos);) { pos.y = rrand() % world.map.size.y; @@ -77,12 +77,12 @@ static void add_map_object(uint8_t type) mo->lifepoints = mod->lifepoints; while (1) { - struct yx_uint16 pos = find_passable_pos(world.map); + struct yx_uint8 pos = find_passable_pos(world.map); struct MapObj * mo_ptr; uint8_t clear = 1; for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next) { - if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints) + if (yx_uint8_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints) { clear = 0; break; @@ -108,7 +108,7 @@ extern void init_map_object_defs() char * err_toolarge = "Value is too large."; char * err_uniq = "Declaration of ID already used."; FILE * file = try_fopen(world.path_map_obj_defs, "r", f_name); - uint16_t linemax = textfile_sizes(file, NULL); + uint32_t linemax = textfile_sizes(file, NULL); struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs; char line[linemax + 1]; reset_err_try_fgets_counter(); @@ -244,7 +244,7 @@ extern struct MapObjDef * get_map_object_def(uint8_t id) -extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos) +extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos) { mo->pos = pos; struct MapObj * owned = mo->owns; diff --git a/src/server/map_objects.h b/src/server/map_objects.h index 7b6c6b2..a73aa80 100644 --- a/src/server/map_objects.h +++ b/src/server/map_objects.h @@ -8,7 +8,7 @@ #define MAP_OBJECTS_H #include /* uint8_t */ -#include "../common/yx_uint16.h" /* yx_uint16 structs */ +#include "../common/yx_uint8.h" /* yx_uint8 structs */ @@ -16,7 +16,7 @@ struct MapObj { struct MapObj * next; /* pointer to next one in map object chain */ struct MapObj * owns; /* chain of map objects owned / in inventory */ - struct yx_uint16 pos; /* coordinate on map */ + struct yx_uint8 pos; /* coordinate on map */ uint8_t id; /* individual map object's unique identifier */ uint8_t type; /* ID of appropriate map object definition */ uint8_t lifepoints; /* 0: object is inanimate; >0: hitpoints */ @@ -62,7 +62,7 @@ extern struct MapObj * get_player(); extern struct MapObjDef * get_map_object_def(uint8_t id); /* Move not only "mo" to "pos", but also all map objects owned by it. */ -extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos); +extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos); diff --git a/src/server/yx_uint16.c b/src/server/yx_uint16.c deleted file mode 100644 index e64e98f..0000000 --- a/src/server/yx_uint16.c +++ /dev/null @@ -1,59 +0,0 @@ -/* src/server/yx_uint16.c */ - -#include "yx_uint16.h" -#include /* uint8_t, UINT16_MAX */ -#include "../common/yx_uint16.h" /* yx_uint16 struct */ - - - -extern uint8_t yx_uint16_cmp(struct yx_uint16 * a, struct yx_uint16 * b) -{ - if (a->y == b->y && a->x == b->x) - { - return 1; - } - return 0; -} - - - -extern struct yx_uint16 mv_yx_in_dir(char d, struct yx_uint16 yx) -{ - if (d == '8' && yx.y > 0) - { - yx.y--; - } - 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 == '2' && yx.y < UINT16_MAX) - { - yx.y++; - } - 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 deleted file mode 100644 index 516050e..0000000 --- a/src/server/yx_uint16.h +++ /dev/null @@ -1,25 +0,0 @@ -/* src/server/yx_uint16.h - * - * Routines for comparison and movement with yx_uin16 structs. - */ - -#ifndef YX_UINT16_H_SERVER -#define YX_UINT16_H_SERVER - -#include /* uint8_t */ -#include "../common/yx_uint16.h" /* yx_uint16 struct */ - - - -/* 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" (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); - - - -#endif diff --git a/src/server/yx_uint8.c b/src/server/yx_uint8.c new file mode 100644 index 0000000..06fa81d --- /dev/null +++ b/src/server/yx_uint8.c @@ -0,0 +1,59 @@ +/* src/server/yx_uint8.c */ + +#include "yx_uint8.h" +#include /* uint8_t, UINT8_MAX */ +#include "../common/yx_uint8.h" /* yx_uint8 struct */ + + + +extern uint8_t yx_uint8_cmp(struct yx_uint8 * a, struct yx_uint8 * b) +{ + if (a->y == b->y && a->x == b->x) + { + return 1; + } + return 0; +} + + + +extern struct yx_uint8 mv_yx_in_dir(char d, struct yx_uint8 yx) +{ + if (d == '8' && yx.y > 0) + { + yx.y--; + } + else if (d == '9' && yx.y > 0 && yx.x < UINT8_MAX) + { + yx.y--; + yx.x++; + } + else if (d == '6' && yx.x < UINT8_MAX) + { + yx.x++; + } + else if (d == '3' && yx.x < UINT8_MAX && yx.y < UINT8_MAX) + { + yx.y++; + yx.x++; + } + else if (d == '2' && yx.y < UINT8_MAX) + { + yx.y++; + } + else if (d == '1' && yx.y < UINT8_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_uint8.h b/src/server/yx_uint8.h new file mode 100644 index 0000000..1463c5f --- /dev/null +++ b/src/server/yx_uint8.h @@ -0,0 +1,25 @@ +/* src/server/yx_uint8.h + * + * Routines for comparison and movement with yx_uint8 structs. + */ + +#ifndef YX_UINT8_H_SERVER +#define YX_UINT8_H_SERVER + +#include /* uint8_t */ +#include "../common/yx_uint8.h" /* yx_uint8 struct */ + + + +/* Return 1 if two yx_uint8 coordinates at "a" and "b" are equal, else 0. */ +extern uint8_t yx_uint8_cmp(struct yx_uint8 * a, struct yx_uint8 * b); + +/* Return yx_uint8 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_uint8 mv_yx_in_dir(char dir, struct yx_uint8 yx); + + + +#endif