1 #include "objects_on_map.h"
8 static struct ChainMapObject * get_next_cmo (void *, char *, size_t, struct ChainMapObject *);
10 extern void readwrite_map_objects_dummy (void * dummy, FILE * file) {
11 // Dummy function for calls of (write|read)_map_objects on map objects without specific attributes.
14 extern void write_map_objects_monsterdata (void * start, FILE * file) {
15 // Write to file data specific tto map objects of type monster.
16 struct Monster * monster = (struct Monster *) start;
17 fputc(monster->hitpoints, file); }
19 extern void write_map_objects (void * start, FILE * file, void (* f) (void *, FILE *) ) {
20 // Write into file the map object chain starting at start, use f() for object-type specific data.
21 struct ChainMapObject * cmo;
22 for (cmo = start; cmo != 0; cmo = cmo->next) {
23 write_uint16_bigendian(cmo->pos.y + 1, file);
24 write_uint16_bigendian(cmo->pos.x + 1, file);
25 fputc(cmo->name, file);
27 write_uint16_bigendian(0, file); }
29 extern void read_map_objects_monsterdata (void * start, FILE * file) {
30 // Read from file data speciifc to map objects of type monster.
31 struct Monster * monster = (struct Monster *) start;
32 monster->hitpoints = fgetc(file); }
34 static struct ChainMapObject * get_next_cmo (void * start, char * still_at_start, size_t size, struct ChainMapObject * cmo) {
35 // Return pointer to map object of size "size". If first in chain ("still_at_start"), make "start" point to it.
36 if (*still_at_start) {
37 struct ChainMapObject * * z = start;
40 *still_at_start = 0; }
42 cmo->next = malloc(size);
46 extern void read_map_objects (void * start, FILE * file, size_t size, void (* f) (void *, FILE *) ) {
47 // Read from file chain of map objects starting at start, use f() for object-type specific data.
48 struct ChainMapObject * cmo;
50 char still_at_start = 1;
52 test = read_uint16_bigendian(file);
55 cmo = get_next_cmo(start, &still_at_start, size, cmo);
56 cmo->pos.y = test - 1;
57 cmo->pos.x = read_uint16_bigendian(file) - 1;
58 cmo->name = fgetc(file);
63 extern void build_map_objects_monsterdata (void * start) {
64 // Build data specific to map objects of type monster.
65 struct Monster * monster = (struct Monster *) start;
66 monster->cmo.name = 'A' + (rrand(0, 0) % 8);
67 monster->hitpoints = 5; }
69 extern void build_map_objects_itemdata (void * start) {
70 // Build data speciifc to map objects of type data.
71 struct Item * item = (struct Item *) start;
72 item->cmo.name = '#' + (rrand(0, 0) % 4); }
74 extern void build_map_objects (void * start, unsigned char n, size_t size, void (* f) (void *), struct Map * map) {
75 // Build chain of n map objects starting at start, use f() for object-specific data.
77 struct ChainMapObject * cmo;
78 char still_at_start = 1;
79 for (i = 0; i < n; i++) {
80 cmo = get_next_cmo(start, &still_at_start, size, cmo);
81 cmo->pos = find_passable_pos(map);
86 extern struct yx_uint16 find_passable_pos (struct Map * map) {
87 // Return a random passable position on map.
89 for (pos.y = pos.x = 0; 0 == is_passable(map, pos);) {
90 pos.y = rrand(0, 0) % map->size.y;
91 pos.x = rrand(0, 0) % map->size.x; }
94 extern char is_passable (struct Map * map, struct yx_uint16 pos) {
95 // Check if coordinate on (or beyond) map is accessible to actor movement.
97 if (0 <= pos.x && pos.x < map->size.x && 0 <= pos.y && pos.y < map->size.y)
98 if ('.' == map->cells[pos.y * map->size.x + pos.x])
102 extern void move_monster (struct World * world, struct Monster * monster) {
103 // Move monster in random direction, trigger fighting when hindered by player/monster.
104 char d = rrand(0, 0) % 5;
105 struct yx_uint16 t = mv_yx_in_dir (d, monster->cmo.pos);
106 if (yx_uint16_cmp (t, world->player->pos)) {
107 update_log (world, "\nThe monster hits you.");
108 world->player->hitpoints--;
109 if (0 == world->player->hitpoints)
110 update_log (world, "\nYou are dead.");
112 struct Monster * other_monster;
113 for (other_monster = world->monster; other_monster != 0; other_monster = other_monster->cmo.next) {
114 if (other_monster == monster)
116 if (yx_uint16_cmp (t, other_monster->cmo.pos)) {
117 update_log (world, "\nMonster bumps into monster.");
119 if (is_passable(world->map, t))
120 monster->cmo.pos = t; }
122 extern void move_player (struct World * world, char d) {
123 // Move player in direction d, update log and turn over to the enemy.
124 struct yx_uint16 t = mv_yx_in_dir (d, world->player->pos);
125 struct Monster * monster;
126 for (monster = world->monster; monster != 0; monster = monster->cmo.next)
127 if (yx_uint16_cmp (t, monster->cmo.pos)) {
128 update_log (world, "\nYou hit the monster.");
129 monster->hitpoints--;
130 if (0 == monster->hitpoints) {
131 update_log (world, "\nYou kill the monster.");
132 if (world->monster == monster)
133 world->monster = world->monster->cmo.next;
135 struct Monster * m_prev;
136 for (m_prev = world->monster; m_prev->cmo.next != monster; m_prev = m_prev->cmo.next);
137 m_prev->cmo.next = monster->cmo.next; }
139 turn_over (world, d);
141 char * msg = calloc(25, sizeof(char));
142 char * msg_content = "You fail to move";
144 if (NORTH == d) dir = "north";
145 else if (EAST == d) dir = "east" ;
146 else if (SOUTH == d) dir = "south";
147 else if (WEST == d) dir = "west" ;
148 if (is_passable(world->map, t)) {
149 msg_content = "You move";
150 world->player->pos = t; }
151 sprintf(msg, "\n%s %s.", msg_content, dir);
152 update_log (world, msg);
154 turn_over (world, d); }
156 extern void player_wait (struct World * world) {
157 // Make player wait one turn.
158 update_log (world, "\nYou wait.");
159 turn_over (world, 0); }