From: Christian Heller Date: Sun, 1 Dec 2013 02:29:27 +0000 (+0100) Subject: Changed way world.map_obj_count is transmitted; also re-factoring of map_objects... X-Git-Tag: tce~902 X-Git-Url: https://plomlompom.com/repos/?a=commitdiff_plain;h=e9ee94eeb27e1121f8383cb6fc3775f18261df75;p=plomrogue Changed way world.map_obj_count is transmitted; also re-factoring of map_objects library. --- diff --git a/src/map_objects.c b/src/map_objects.c index 0407352..fd4bd16 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -2,15 +2,15 @@ #include "map_objects.h" #include /* for free(), atoi() */ -#include /* for uint8_t */ +#include /* for uint8_t, uint16_t */ #include /* for FILE typedef */ -#include /* for strchr(), strlen(), memcpy(), strtok() */ +#include /* for strlen(), memcpy(), strtok() */ #include "readwrite.h" /* for textfile_sizes(), try_fopen(), try_fclose(), * try_fgets() */ #include "misc.h" /* for try_malloc(), find_passable_pos() */ #include "main.h" /* for world global */ -#include "rexit.h" /* for err_exit() */ +#include "rexit.h" /* for exit_err() */ #include "yx_uint16.h" /* for yx_uint16 struct, yx_uint16_cmp() */ @@ -78,8 +78,7 @@ extern void init_map_object_defs(char * filename) char line[linemax + 1]; while (try_fgets(line, linemax + 1, file, f_name)) { - struct MapObjDef * mod; - mod = try_malloc(sizeof(struct MapObjDef), f_name); + struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name); mod->next = NULL; mod->id = atoi(strtok(line, delim)); mod->corpse_id = atoi(strtok(NULL, delim)); @@ -142,10 +141,6 @@ extern void read_map_objects(FILE * file, char * line, int linemax) mo->arg = atoi(strtok(NULL, delim));; mo->progress = atoi(strtok(NULL, delim));; mo->owns = NULL; - if (mo->id > world.map_obj_count) - { - world.map_obj_count = mo->id; - } * mo_ptr_ptr = mo; mo_ptr_ptr = &mo->next; } @@ -154,10 +149,7 @@ extern void read_map_objects(FILE * file, char * line, int linemax) { uint8_t id = atoi(strtok(line, delim)); uint8_t i; - for (i = 0; i < 7; i++) - { - strtok(NULL, delim); - } + for (i = 0; i < 7; i++, strtok(NULL, delim)); char * owned = strtok(NULL, "\n"); if (NULL != owned) { @@ -166,8 +158,7 @@ extern void read_map_objects(FILE * file, char * line, int linemax) owned_id = strtok(owned, delim); while (NULL != owned_id) { - own_map_object(&mo->owns, &world.map_objs, - (uint8_t) atoi(owned_id)); + own_map_object(&mo->owns, &world.map_objs, atoi(owned_id)); owned_id = strtok(NULL, delim); } } @@ -180,10 +171,9 @@ extern void add_map_object(uint8_t type) { char * f_name = "add_map_object()"; struct MapObjDef * mod = get_map_object_def(type); - struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name); - mo->id = world.map_obj_count; - world.map_obj_count++; - mo->type = mod->id; + struct MapObj * mo = try_malloc(sizeof(struct MapObj), f_name); + mo->id = world.map_obj_count++; + mo->type = mod->id; mo->lifepoints = mod->lifepoints; while (1) { @@ -205,18 +195,13 @@ extern void add_map_object(uint8_t type) } } mo->progress = 0; - mo->command = 0; - mo->arg = 0; - mo->owns = NULL; - mo->next = NULL; - struct MapObj ** last_ptr_ptr = &world.map_objs; - struct MapObj * mo_ptr; - while (NULL != * last_ptr_ptr) - { - mo_ptr = * last_ptr_ptr; - last_ptr_ptr = & mo_ptr->next; - } - * last_ptr_ptr = mo; + mo->command = 0; + mo->arg = 0; + mo->owns = NULL; + mo->next = NULL; + struct MapObj ** mo_ptr_ptr = &world.map_objs; + for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next); + * mo_ptr_ptr = mo; } @@ -268,14 +253,9 @@ extern void own_map_object(struct MapObj ** target, struct MapObj ** source, mo = penult->next; penult->next = mo->next; } - struct MapObj ** last_ptr_ptr = target; - struct MapObj * mo_ptr; - while (NULL != * last_ptr_ptr) - { - mo_ptr = * last_ptr_ptr; - last_ptr_ptr = & mo_ptr->next; - } - * last_ptr_ptr = mo; + struct MapObj ** mo_ptr_ptr = target; + for (; NULL != * mo_ptr_ptr; mo_ptr_ptr = &(*mo_ptr_ptr)->next); + * mo_ptr_ptr = mo; mo->next = NULL; } @@ -291,10 +271,7 @@ extern struct MapObj * get_player() extern struct MapObjDef * get_map_object_def(uint8_t id) { struct MapObjDef * mod = world.map_obj_defs; - while (id != mod->id) - { - mod = mod->next; - } + for (; id != mod->id; mod = mod->next); return mod; } @@ -304,8 +281,6 @@ extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos) { mo->pos = pos; struct MapObj * owned = mo->owns; - for (; owned != NULL; owned = owned->next) - { - set_object_position(owned, pos); - } + for (; owned != NULL; set_object_position(owned, pos), owned = owned->next); } + diff --git a/src/map_objects.h b/src/map_objects.h index 2bd1bec..fd7a819 100644 --- a/src/map_objects.h +++ b/src/map_objects.h @@ -21,44 +21,43 @@ struct MapObj uint8_t type; /* ID of appropriate map object definition */ uint8_t lifepoints; /* 0: object is inanimate; >0: hitpoints */ struct yx_uint16 pos; /* coordinate on map */ - uint8_t command; /* command map object tries to realize now*/ - uint8_t arg; /* optional field for command argument */ + uint8_t command; /* map object's current action */ + uint8_t arg; /* optional field for .command argument */ uint8_t progress; /* turns already passed to realize .command */ }; struct MapObjDef { struct MapObjDef * next; - uint8_t id; /* unique identifier of map object type */ - uint8_t corpse_id; /* id of type to change into upon destruction */ + uint8_t id; /* map object definition identifier / sets .type */ + uint8_t corpse_id; /* type to change map object into upon destruction */ char char_on_map; /* map object symbol to appear on map */ - char * name; /* string to describe object in game log*/ - uint8_t lifepoints; /* default value for map object lifepoints member */ + char * name; /* string to describe object in game log */ + uint8_t lifepoints; /* default start value for map object's .lifepoints */ }; -/* Initialize map object defnitions chain from file at path "filename". */ +/* Initialize map object definitions chain from file at path "filename". */ extern void init_map_object_defs(char * filename); /* Free map object definitions chain starting at "mod_start". */ extern void free_map_object_defs(struct MapObjDef * mod_start); -/* Add new object(s) ("n": how many?) of "type" to map on random position(s). - * New animate objects are never placed in the same square with other animated - * ones. - */ -extern void add_map_object(uint8_t type); -extern void add_map_objects(uint8_t type, uint8_t n); - /* Write map objects chain to "file". */ extern void write_map_objects(FILE * file); -/* Read from "file" map objects chain; use "line" as char array for fgets() and - * expect strings of max. "linemax" length. +/* Read map objects chain from "file"; use "line" as char array for fgets() and + * expect line strings of max. "linemax" length to be read by it. */ extern void read_map_objects(FILE * file, char * line, int linemax); +/* Add object(s) ("n": how many?) of "type" to map on random position(s). New + * animate objects are never placed in the same square with other animate ones. + */ +extern void add_map_object(uint8_t type); +extern void add_map_objects(uint8_t type, uint8_t n); + /* Free map objects in map object chain starting at "mo_start. */ extern void free_map_objects(struct MapObj * mo_start); diff --git a/src/misc.c b/src/misc.c index 122f404..e6f0df1 100644 --- a/src/misc.c +++ b/src/misc.c @@ -267,6 +267,8 @@ extern void save_game() try_fwrite(line, strlen(line), 1, file, f_name); sprintf(line, "%u\n", world.seed); try_fwrite(line, strlen(line), 1, file, f_name); + sprintf(line, "%u\n", world.map_obj_count); + try_fwrite(line, strlen(line), 1, file, f_name); sprintf(line, "%u\n", world.turn); try_fwrite(line, strlen(line), 1, file, f_name); sprintf(line, "%u\n", world.score); @@ -289,6 +291,8 @@ extern void load_game() try_fgets(line, linemax + 1, file, f_name); world.seed = atoi(line); try_fgets(line, linemax + 1, file, f_name); + world.map_obj_count = atoi(line); + try_fgets(line, linemax + 1, file, f_name); world.turn = atoi(line); try_fgets(line, linemax + 1, file, f_name); world.score = atoi(line);