From f2a7bc28be634fde4dfe4bde4c83395cab9efa91 Mon Sep 17 00:00:00 2001 From: Christian Heller <c.heller@plomlompom.de> Date: Tue, 23 Jul 2013 05:37:21 +0200 Subject: [PATCH] More documentation and re-styling of code. --- src/map_object_actions.c | 229 +++++++++++++++++++++++++-------------- src/map_object_actions.h | 44 +++++++- src/map_objects.h | 3 +- 3 files changed, 186 insertions(+), 90 deletions(-) diff --git a/src/map_object_actions.c b/src/map_object_actions.c index c9bdea3..f766715 100644 --- a/src/map_object_actions.c +++ b/src/map_object_actions.c @@ -1,89 +1,150 @@ +/* map_object_actions.c */ + #include "map_object_actions.h" -#include <stdlib.h> -#include "yx_uint16.h" -#include "misc.h" -#include "map.h" -#include "main.h" -#include "map_objects.h" +#include <stdlib.h> /* for malloc(), calloc(), free() */ +#include "yx_uint16.h" /* for yx_uint16 struct, mv_yx_in_dir(), yx_uint16_cmp */ +#include "misc.h" /* for rrand(), update_log(), turn_over()*/ +#include "map.h" /* for Map struct */ +#include "main.h" /* for World struct */ +#include "map_objects.h" /* for map object (definition) structs */ + + + +extern void move_monster(struct World * world, struct Monster * monster) +{ + char d = rrand(0, 0) % 5; + 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)) + { + 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->map_obj.next) + { + if (other_monster == monster) + { + continue; + } + 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->map_obj.pos = t; + } +} + + + +extern void move_player (struct World * world, enum dir d) +{ + struct yx_uint16 t = mv_yx_in_dir(d, world->player->pos); + struct Monster * 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) + { + sprintf(msg, "\nYou kill the %s.", desc); + update_log(world, msg); + if (world->monster == monster) + { + world->monster = world->monster->map_obj.next; + } + else + { + struct Monster * m_prev; + 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_content = "You fail to move"; + char * dir; + if (NORTH == d) + { + dir = "north"; + } + else if (EAST == d) + { + dir = "east" ; + } + else if (SOUTH == d) + { + dir = "south"; + } + else if (WEST == d) + { + dir = "west" ; + } + if (is_passable(world->map, t)) + { + msg_content = "You move"; + world->player->pos = t; + } + sprintf(msg, "\n%s %s.", msg_content, dir); + update_log(world, msg); + free(msg); + turn_over(world, d); +} + + + +extern void player_wait (struct World * world) +{ + update_log(world, "\nYou wait."); + turn_over(world, 0); +} -extern char is_passable (struct Map * map, struct yx_uint16 pos) { -// Check if coordinate on (or beyond) map is accessible to actor movement. - char passable = 0; - if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y) - if ('.' == map->cells[pos.y * map->size.x + pos.x]) - passable = 1; - return passable; } -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->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)) { - 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->map_obj.next) { - if (other_monster == monster) - continue; - 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->map_obj.pos = t; } -extern void move_player (struct World * world, enum dir 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; - 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) { - sprintf (msg, "\nYou kill the %s.", desc); - update_log (world, msg); - if (world->monster == monster) - world->monster = world->monster->map_obj.next; - else { - struct Monster * m_prev; - 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_content = "You fail to move"; - char * dir; - if (NORTH == d) dir = "north"; - else if (EAST == d) dir = "east" ; - else if (SOUTH == d) dir = "south"; - else if (WEST == d) dir = "west" ; - if (is_passable(world->map, t)) { - msg_content = "You move"; - world->player->pos = t; } - sprintf(msg, "\n%s %s.", msg_content, dir); - update_log (world, msg); - free(msg); - turn_over (world, d); } +extern char is_passable (struct Map * map, struct yx_uint16 pos) +{ + char passable = 0; + if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y) + { + if ('.' == map->cells[pos.y * map->size.x + pos.x]) + { + passable = 1; + } + } + return passable; +} -extern void player_wait (struct World * world) { -// Make player wait one turn. - update_log (world, "\nYou wait."); - turn_over (world, 0); } diff --git a/src/map_object_actions.h b/src/map_object_actions.h index 2202b27..2edc172 100644 --- a/src/map_object_actions.h +++ b/src/map_object_actions.h @@ -1,15 +1,49 @@ +/* map_object_actions.h + * + * Routines for the actions available to map objects. + */ + #ifndef MAP_OBJECT_ACTIONS_H #define MAP_OBJECT_ACTIONS_H -#include "yx_uint16.h" + +#include "yx_uint16.h" /* for yx_uint16 coordinates */ struct World; struct Map; struct Monster; -extern char is_passable (struct Map *, struct yx_uint16); -extern void move_monster (struct World *, struct Monster *); -extern void move_player (struct World *, enum dir); -extern void player_wait(struct World *); + + +/* Try to move "monster" in random direction. On contact with other monster, + * only bump. On contact with player, fight / reduce player's hitpoints, + * and thereby potentially trigger the player's death. Update the log for any + * contact action. + */ +extern void move_monster(struct World * world, struct Monster * monster); + + + +/* Try to move player in direction "d". On contact with monster, fight / reduce + * monster's hitpoints, and thereby potentially trigger the monster's death. + * Update the log on whatever the player did and turn control over to the enemy. + */ +extern void move_player (struct World * world, enum dir d); + + + +/* Make player wait one turn, i.e. only update_log with a "you wait" message + * and turn control over to the enemy. + */ +extern void player_wait(struct World * world); + + + +/* Check if coordinate pos on (or beyond) map is accessible to map object + * movement. + */ +extern char is_passable (struct Map * map, struct yx_uint16 pos); + + #endif diff --git a/src/map_objects.h b/src/map_objects.h index ebda314..d598514 100644 --- a/src/map_objects.h +++ b/src/map_objects.h @@ -16,7 +16,6 @@ struct World; /* Player is non-standard: single and of a hard-coded type. */ - struct Player { struct yx_uint16 pos; @@ -119,4 +118,6 @@ extern void read_map_objects_monsterdata( void * start, FILE * file); /* Get pointer to the map object definition of identifier "def_id". */ extern struct MapObjDef * get_map_obj_def(struct World * world, char def_id); + + #endif -- 2.30.2