From: Christian Heller Date: Thu, 18 Jul 2013 01:44:23 +0000 (+0200) Subject: Support different kinds of monsters and items defined in map object definition struct... X-Git-Tag: tce~1153 X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bitem_name%7D%7D?a=commitdiff_plain;h=d165f8ae619c78fd1ef98a9ba2abdaeeceff853e;p=plomrogue Support different kinds of monsters and items defined in map object definition structs. To support this, large parts of the map object system were rewritten. --- diff --git a/src/draw_wins.c b/src/draw_wins.c index c32d8b4..9efc8c9 100644 --- a/src/draw_wins.c +++ b/src/draw_wins.c @@ -10,7 +10,7 @@ #include "map.h" #include "main.h" -static void draw_map_objects (void *, struct Map *, struct Win *); +static void draw_map_objects (struct World *, struct MapObj *, struct Map *, struct Win *); extern void draw_with_linebreaks (struct Win * win, char * text, uint16_t start_y) { // Write text into window content space. Start on row start_y. Fill unused rows with whitespace. @@ -74,13 +74,17 @@ extern void draw_log_win (struct Win * win) { struct World * world = (struct World *) win->data; draw_text_from_bottom(win, world->log); } -static void draw_map_objects (void * start, struct Map * map, struct Win * win) { +static void draw_map_objects (struct World * world, struct MapObj * start, struct Map * map, struct Win * win) { // Draw onto map in win the objects in the chain at start. - struct ChainMapObject * cmo; - for (cmo = start; cmo != 0; cmo = cmo->next) - if ( cmo->pos.y >= map->offset.y && cmo->pos.y < map->offset.y + win->frame.size.y - && cmo->pos.x >= map->offset.x && cmo->pos.x < map->offset.x + win->frame.size.x) - mvwaddch(win->frame.curses_win, cmo->pos.y - map->offset.y, cmo->pos.x - map->offset.x, cmo->name); } + struct MapObj * o; + struct MapObjDef * d; + char c; + for (o = start; o != 0; o = o->next) + if ( o->pos.y >= map->offset.y && o->pos.y < map->offset.y + win->frame.size.y + && o->pos.x >= map->offset.x && o->pos.x < map->offset.x + win->frame.size.x) { + d = get_map_obj_def (world, o->type); + c = d->mapchar; + mvwaddch(win->frame.curses_win, o->pos.y - map->offset.y, o->pos.x - map->offset.x, c); } } extern void draw_map_win (struct Win * win) { // Draw map determined by map (from win->data) and various actors/objects into window. Respect scroll offset. @@ -97,8 +101,8 @@ extern void draw_map_win (struct Win * win) { if (y < height_map_av && x < width_map_av) { mvwaddch(win->frame.curses_win, y, x, cells[z]); z++; } } } - draw_map_objects (world->item, map, win); - draw_map_objects (world->monster, map, win); + draw_map_objects (world, world->item, map, win); + draw_map_objects (world, world->monster, map, win); if ( player->pos.y >= map->offset.y && player->pos.y < map->offset.y + win->frame.size.y && player->pos.x >= map->offset.x && player->pos.x < map->offset.x + win->frame.size.x) mvwaddch(win->frame.curses_win, player->pos.y - map->offset.y, player->pos.x - map->offset.x, '@'); } diff --git a/src/main.c b/src/main.c index c22e0c2..53784b8 100644 --- a/src/main.c +++ b/src/main.c @@ -37,6 +37,34 @@ int main (int argc, char *argv[]) { world.player = &player; world.monster = 0; world.item = 0; + struct MonsterDef monster_def_A; + monster_def_A.map_obj_def.id = 1; + monster_def_A.map_obj_def.mapchar = 'a'; + monster_def_A.map_obj_def.desc = "ANT"; + monster_def_A.hitpoints_start = 1; + struct MonsterDef monster_def_B; + monster_def_B.map_obj_def.id = 2; + monster_def_B.map_obj_def.mapchar = 'z'; + monster_def_B.map_obj_def.desc = "ZOMBIE"; + monster_def_B.hitpoints_start = 3; + struct MonsterDef monster_def_C; + monster_def_C.map_obj_def.id = 3; + monster_def_C.map_obj_def.mapchar = 'S'; + monster_def_C.map_obj_def.desc = "SHOGGOTH"; + monster_def_C.hitpoints_start = 9; + world.monster_def = &monster_def_A; + monster_def_A.map_obj_def.next = (struct MapObjDef *) &monster_def_B; + monster_def_B.map_obj_def.next = (struct MapObjDef *) &monster_def_C; + monster_def_C.map_obj_def.next = NULL; + struct ItemDef item_def_A; + item_def_A.map_obj_def.id = 4; + item_def_A.map_obj_def.mapchar = '#'; + struct ItemDef item_def_B; + item_def_B.map_obj_def.id = 5; + item_def_B.map_obj_def.mapchar = '%'; + world.item_def = &item_def_A; + item_def_A.map_obj_def.next = (struct MapObjDef *) &item_def_B; + item_def_B.map_obj_def.next = NULL; // For interactive mode, try to load world state from savefile. FILE * file; @@ -71,10 +99,15 @@ int main (int argc, char *argv[]) { world.map = ↦ if (1 == world.turn) { player.pos = find_passable_pos(&map); - unsigned char n_monsters = rrand(0, 0) % 16; - unsigned char n_items = rrand(0, 0) % 48; - build_map_objects (&world.monster, n_monsters, sizeof(struct Monster), build_map_objects_monsterdata, &map); - build_map_objects (&world.item, n_items, sizeof(struct Item), build_map_objects_itemdata, &map); } + void * foo = build_map_objects (&world, &world.monster, 1, 1 + rrand(0,0) % 27, sizeof(struct Monster), + build_map_objects_monsterdata); + foo = build_map_objects (&world, foo, 2, 1 + rrand(0,0) % 9, sizeof(struct Monster), + build_map_objects_monsterdata); + build_map_objects (&world, foo, 3, 1 + rrand(0,0) % 3, sizeof(struct Monster), + build_map_objects_monsterdata); + foo = build_map_objects (&world, &world.item, 4, 1 + rrand(0,0) % 3, sizeof(struct Item), + build_map_objects_itemdata); + build_map_objects (&world, foo, 5, 1 + rrand(0,0) % 3, sizeof(struct Item), build_map_objects_itemdata); } // Initialize window system and windows. WINDOW * screen = initscr(); diff --git a/src/main.h b/src/main.h index 9c04273..14287d0 100644 --- a/src/main.h +++ b/src/main.h @@ -8,6 +8,8 @@ struct Win; struct KeyBinding; struct KeysWinData; struct Map; +struct ItemDef; +struct MonsterDef; struct World { char interactive; @@ -17,7 +19,9 @@ struct World { uint32_t turn; char * log; struct Map * map; + struct ItemDef * item_def; struct Item * item; + struct MonsterDef * monster_def; struct Monster * monster; struct Player * player; }; diff --git a/src/map.c b/src/map.c index 2db0b9b..394f818 100644 --- a/src/map.c +++ b/src/map.c @@ -48,4 +48,3 @@ void map_center_player (struct Map * map, struct Player * player, struct yx_uint // Center map on player. map->offset.y = center_offset (player->pos.y, map->size.y, frame_size.y); map->offset.x = center_offset (player->pos.x, map->size.x, frame_size.x); } - diff --git a/src/map_objects.c b/src/map_objects.c index 1927e14..7c13ed2 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -7,83 +7,95 @@ #include "map.h" #include "main.h" -static struct ChainMapObject * get_next_cmo (void *, char *, size_t, struct ChainMapObject *); +static struct MapObj * get_next_map_obj (void *, char *, size_t, struct MapObj *); extern void readwrite_map_objects_dummy (void * dummy, FILE * file) { // Dummy function for calls of (write|read)_map_objects on map objects without specific attributes. ; } extern void write_map_objects_monsterdata (void * start, FILE * file) { -// Write to file data specific tto map objects of type monster. - struct Monster * monster = (struct Monster *) start; - fputc(monster->hitpoints, file); } - -extern void write_map_objects (void * start, FILE * file, void (* f) (void *, FILE *) ) { -// Write into file the map object chain starting at start, use f() for object-type specific data. - struct ChainMapObject * cmo; - for (cmo = start; cmo != 0; cmo = cmo->next) { - write_uint16_bigendian(cmo->pos.y + 1, file); - write_uint16_bigendian(cmo->pos.x + 1, file); - fputc(cmo->name, file); - f (cmo, file); } +// Write to file data specific to map objects of type monster. + struct Monster * m = (struct Monster *) start; + fputc(m->hitpoints, file); } + +extern void write_map_objects (void * start, FILE * file, void (* w_typedata) (void *, FILE *) ) { +// Write into file the map object chain starting at start, use write_type() for object-type specific data. + struct MapObj * map_obj; + for (map_obj = start; map_obj != 0; map_obj = map_obj->next) { + write_uint16_bigendian(map_obj->pos.y + 1, file); + write_uint16_bigendian(map_obj->pos.x + 1, file); + fputc(map_obj->type, file); + w_typedata (map_obj, file); } write_uint16_bigendian(0, file); } extern void read_map_objects_monsterdata (void * start, FILE * file) { // Read from file data speciifc to map objects of type monster. - struct Monster * monster = (struct Monster *) start; - monster->hitpoints = fgetc(file); } - -static struct ChainMapObject * get_next_cmo (void * start, char * still_at_start, size_t size, struct ChainMapObject * cmo) { -// Return pointer to map object of size "size". If first in chain ("still_at_start"), make "start" point to it. - if (*still_at_start) { - struct ChainMapObject * * z = start; - cmo = malloc(size); - * z = cmo; - *still_at_start = 0; } + struct Monster * m = (struct Monster *) start; + m->hitpoints = fgetc(file); } + +static struct MapObj * get_next_map_obj (void * start, char * first, size_t size, struct MapObj * map_obj) { +// Return pointer to map object of "size". If first in chain ("first" pointing to !0), point "start" to it. + if (* first) { + struct MapObj * * z = start; + map_obj = malloc(size); + * z = map_obj; + * first = 0; } else { - cmo->next = malloc(size); - cmo = cmo->next; } - return cmo; } + map_obj->next = malloc(size); + map_obj = map_obj->next; } + return map_obj; } -extern void read_map_objects (void * start, FILE * file, size_t size, void (* f) (void *, FILE *) ) { -// Read from file chain of map objects starting at start, use f() for object-type specific data. - struct ChainMapObject * cmo; +extern void read_map_objects (void * start, FILE * file, size_t size, void (* r_typedata) (void *, FILE *) ) { +// Read from file chain of map objects starting at start, use r_typedata() for object-type specific data. + struct MapObj * map_obj; uint16_t test; - char still_at_start = 1; + char first = 1; while (1) { test = read_uint16_bigendian(file); if (0 == test) break; - cmo = get_next_cmo(start, &still_at_start, size, cmo); - cmo->pos.y = test - 1; - cmo->pos.x = read_uint16_bigendian(file) - 1; - cmo->name = fgetc(file); - f (cmo, file); } - if (!still_at_start) - cmo->next = 0; } - -extern void build_map_objects_monsterdata (void * start) { + map_obj = get_next_map_obj(start, &first, size, map_obj); + map_obj->pos.y = test - 1; + map_obj->pos.x = read_uint16_bigendian(file) - 1; + map_obj->type = fgetc(file); + r_typedata (map_obj, file); } + if (!first) + map_obj->next = 0; } + +extern void build_map_objects_monsterdata (struct MapObjDef * map_obj_def, void * start) { // Build data specific to map objects of type monster. - struct Monster * monster = (struct Monster *) start; - monster->cmo.name = 'A' + (rrand(0, 0) % 8); - monster->hitpoints = 5; } + struct Monster * m = (struct Monster *) start; + m->map_obj.type = map_obj_def->id; + struct MonsterDef * md = (struct MonsterDef *) map_obj_def; + m->hitpoints = md->hitpoints_start; } -extern void build_map_objects_itemdata (void * start) { +extern void build_map_objects_itemdata (struct MapObjDef * map_obj_def, void * start) { // Build data speciifc to map objects of type data. - struct Item * item = (struct Item *) start; - item->cmo.name = '#' + (rrand(0, 0) % 4); } + struct Item * i = (struct Item *) start; + i->map_obj.type = map_obj_def->id; } -extern void build_map_objects (void * start, unsigned char n, size_t size, void (* f) (void *), struct Map * map) { +extern void * build_map_objects (struct World * world, void * start, char def_id, unsigned char n, + size_t size, void (* b_typedata) (struct MapObjDef *, void *)) { // Build chain of n map objects starting at start, use f() for object-specific data. unsigned char i; - struct ChainMapObject * cmo; - char still_at_start = 1; + struct MapObj * mo; + char first = 1; + struct MapObjDef * mod = get_map_obj_def (world, def_id); for (i = 0; i < n; i++) { - cmo = get_next_cmo(start, &still_at_start, size, cmo); - cmo->pos = find_passable_pos(map); - f (cmo); } - if (!still_at_start) - cmo->next = 0; } + mo = get_next_map_obj(start, &first, size, mo); + mo->pos = find_passable_pos(world->map); + b_typedata (mod, mo); } + if (!first) + mo->next = 0; + return &mo->next; } + +extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id) { +// Get pointer to the map object definition with id "def_id". + struct MapObjDef * d = NULL; + for (d = (struct MapObjDef *) world->monster_def; d->id != def_id && 0 != d->next; d = d->next); + if (d->id != def_id) + for (d = (struct MapObjDef *) world->item_def; d->id != def_id && 0 != d->next; d = d->next); + return d; } extern struct yx_uint16 find_passable_pos (struct Map * map) { // Return a random passable position on map. @@ -104,43 +116,58 @@ extern char is_passable (struct Map * map, struct yx_uint16 pos) { extern void move_monster (struct World * world, struct Monster * monster) { // Move monster in random direction, trigger fighting when hindered by player/monster. char d = rrand(0, 0) % 5; - struct yx_uint16 t = mv_yx_in_dir (d, monster->cmo.pos); + struct yx_uint16 t = mv_yx_in_dir (d, monster->map_obj.pos); + char * msg = malloc(100); + struct MapObjDef * mod = get_map_obj_def (world, monster->map_obj.type); + char * desc = mod->desc; + char * desc_other; if (yx_uint16_cmp (t, world->player->pos)) { - update_log (world, "\nThe monster hits you."); + sprintf(msg, "\nThe %s hits you.", desc); + update_log (world, msg); world->player->hitpoints--; if (0 == world->player->hitpoints) update_log (world, "\nYou are dead."); return; } struct Monster * other_monster; - for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->cmo.next) { + for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->map_obj.next) { if (other_monster == monster) continue; - if (yx_uint16_cmp (t, other_monster->cmo.pos)) { - update_log (world, "\nMonster bumps into monster."); + if (yx_uint16_cmp (t, other_monster->map_obj.pos)) { + mod = get_map_obj_def (world, monster->map_obj.type); + desc_other = mod->desc; + sprintf(msg, "\n%s bumps into %s.", desc, desc_other); + update_log (world, msg); return; } } + free (msg); if (is_passable(world->map, t)) - monster->cmo.pos = t; } + monster->map_obj.pos = t; } extern void move_player (struct World * world, char d) { // Move player in direction d, update log and turn over to the enemy. struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos); struct Monster * monster; - for (monster = world->monster; monster != 0; monster = monster->cmo.next) - if (yx_uint16_cmp (t, monster->cmo.pos)) { - update_log (world, "\nYou hit the monster."); + struct MapObjDef * mod; + char * msg = calloc(100, sizeof(char)); + char * desc; + for (monster = world->monster; monster != 0; monster = monster->map_obj.next) + if (yx_uint16_cmp (t, monster->map_obj.pos)) { + mod = get_map_obj_def (world, monster->map_obj.type); + desc = mod->desc; + sprintf (msg, "\nYou hit the %s.", desc); + update_log (world, msg); monster->hitpoints--; if (0 == monster->hitpoints) { - update_log (world, "\nYou kill the monster."); + sprintf (msg, "\nYou kill the %s.", desc); + update_log (world, msg); if (world->monster == monster) - world->monster = world->monster->cmo.next; + world->monster = world->monster->map_obj.next; else { struct Monster * m_prev; - for (m_prev = world->monster; m_prev->cmo.next != monster; m_prev = m_prev->cmo.next); - m_prev->cmo.next = monster->cmo.next; } + for (m_prev = world->monster; m_prev->map_obj.next != monster; m_prev = m_prev->map_obj.next); + m_prev->map_obj.next = monster->map_obj.next; } free(monster); } turn_over (world, d); return; } - char * msg = calloc(25, sizeof(char)); char * msg_content = "You fail to move"; char * dir; if (NORTH == d) dir = "north"; diff --git a/src/map_objects.h b/src/map_objects.h index 8b58c10..80e7c0f 100644 --- a/src/map_objects.h +++ b/src/map_objects.h @@ -11,26 +11,41 @@ struct Player { struct yx_uint16 pos; unsigned char hitpoints; }; -struct ChainMapObject { +struct MapObj { void * next; - unsigned char name; + char type; struct yx_uint16 pos; }; +struct MapObjDef { + struct MapObjDef * next; + char id; + char mapchar; + char * desc; }; + struct Item { - struct ChainMapObject cmo; }; + struct MapObj map_obj; }; struct Monster { - struct ChainMapObject cmo; + struct MapObj map_obj; unsigned char hitpoints; }; +struct ItemDef { + struct MapObjDef map_obj_def; }; + +struct MonsterDef { + struct MapObjDef map_obj_def; + unsigned char hitpoints_start; }; + extern void readwrite_map_objects_dummy (void *, FILE *); extern void write_map_objects_monsterdata (void *, FILE *); extern void write_map_objects (void * start, FILE *, void (*) (void *, FILE *) ); extern void read_map_objects_monsterdata (void *, FILE *); extern void read_map_objects (void *, FILE *, size_t, void (*) (void *, FILE *) ); -extern void build_map_objects_monsterdata (void *); -extern void build_map_objects_itemdata (void *); -extern void build_map_objects (void *, unsigned char, size_t, void (*) (void *), struct Map *); +extern void build_map_objects_monsterdata (struct MapObjDef *, void *); +extern void build_map_objects_itemdata (struct MapObjDef *, void *); +extern void * build_map_objects (struct World *, void *, char, unsigned char, size_t, + void (*) (struct MapObjDef *, void *)); +extern struct MapObjDef * get_map_obj_def (struct World *, char); extern struct yx_uint16 find_passable_pos (struct Map *); extern char is_passable (struct Map *, struct yx_uint16); diff --git a/src/misc.c b/src/misc.c index 6832b1a..83ed263 100644 --- a/src/misc.c +++ b/src/misc.c @@ -59,7 +59,7 @@ extern void turn_over (struct World * world, char action) { world->turn++; rrand(1, world->seed * world->turn); struct Monster * monster; - for (monster = world->monster; monster != 0; monster = monster->cmo.next) + for (monster = world->monster; monster != 0; monster = monster->map_obj.next) move_monster(world, monster); } extern void save_game(struct World * world) {