home · contact · privacy
Replaced dummy function by just passing NULL and checking for it.
[plomrogue] / src / map_objects.c
1 #include "map_objects.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "readwrite.h"
6 #include "misc.h"
7 #include "main.h"
8
9 static struct MapObj * get_next_map_obj (void *, char *, size_t, struct MapObj *);
10
11 extern void init_map_object_defs (struct World * world, char * filename) {
12 // Initialize map object definitions from file at path "filename".
13   world->item_def = 0;
14   world->monster_def = 0;
15   FILE *                file    = fopen(filename, "r");
16   uint16_t              linemax;
17   textfile_sizes (file, &linemax, NULL);
18   struct MapObjDef      mod;
19   struct ItemDef        id;
20   struct MonsterDef     md;
21   struct ItemDef    * * p_p_id  = &world->item_def;
22   struct MonsterDef * * p_p_md  = &world->monster_def;
23   char *                defline = malloc(linemax);
24   char *                line_p;
25   char                  m_or_i;
26   while (fgets (defline, linemax, file)) {
27     mod.next    = 0;
28     mod.id      = atoi(defline);
29     line_p      = strchr(defline, ' ') + 1;
30     m_or_i      = * line_p;
31     mod.mapchar = * (line_p + 2);
32     if ('i' == m_or_i)
33       line_p = line_p + 5;
34     else {
35       md.hitpoints_start = atoi   (line_p + 4);
36       line_p             = strchr (line_p + 4, ' ') + 1; }
37     mod.desc = calloc (strlen (line_p), sizeof(char));
38     memcpy (mod.desc, line_p, strlen(line_p) - 1);
39     if ('i' == m_or_i) {
40       id.map_obj_def = mod;
41       * p_p_id       = malloc (sizeof (struct ItemDef));
42       * * p_p_id     = id;
43       p_p_id         = (struct ItemDef    * *) * p_p_id; }
44     else {
45       md.map_obj_def = mod;
46       * p_p_md       = malloc (sizeof (struct MonsterDef));
47       * * p_p_md     = md;
48       p_p_md         = (struct MonsterDef * *) * p_p_md; } }
49   free(defline);
50   fclose(file); };
51
52 extern void write_map_objects_monsterdata (void * start, FILE * file) {
53 // Write to file data specific to map objects of type monster.
54   struct Monster * m = (struct Monster *) start;
55   fputc(m->hitpoints, file); }
56
57 extern void write_map_objects (void * start, FILE * file, void (* w_typedata) (void *, FILE *) ) {
58 // Write into file the map object chain starting at start, use write_type() for object-type specific data.
59   struct MapObj * map_obj;
60   for (map_obj = start; map_obj != 0; map_obj = map_obj->next) {
61     write_uint16_bigendian(map_obj->pos.y + 1, file);
62     write_uint16_bigendian(map_obj->pos.x + 1, file);
63     fputc(map_obj->type, file);
64     if (w_typedata)
65       w_typedata (map_obj, file); }
66   write_uint16_bigendian(0, file); }
67
68 extern void read_map_objects_monsterdata (void * start, FILE * file) {
69 // Read from file data speciifc to map objects of type monster.
70   struct Monster * m = (struct Monster *) start;
71   m->hitpoints = fgetc(file); }
72
73 static struct MapObj * get_next_map_obj (void * start, char * first, size_t size, struct MapObj * map_obj) {
74 // Return pointer to map object of "size". If first in chain ("first" pointing to !0), point "start" to it.
75   if (* first) {
76     struct MapObj * * z = start;
77     map_obj = malloc(size);
78     * z = map_obj;
79     * first = 0; }
80   else {
81     map_obj->next = malloc(size);
82     map_obj = map_obj->next; }
83   return map_obj; }
84
85 extern void read_map_objects (void * start, FILE * file, size_t size, void (* r_typedata) (void *, FILE *) ) {
86 // Read from file chain of map objects starting at start, use r_typedata() for object-type specific data.
87   struct MapObj * map_obj;
88   uint16_t test;
89   char first = 1;
90   while (1) {
91     test = read_uint16_bigendian(file);
92     if (0 == test)
93       break;
94     map_obj = get_next_map_obj(start, &first, size, map_obj);
95     map_obj->pos.y = test - 1;
96     map_obj->pos.x = read_uint16_bigendian(file) - 1;
97     map_obj->type = fgetc(file);
98     if (r_typedata)
99       r_typedata (map_obj, file); }
100   if (!first)
101     map_obj->next = 0; }
102
103 extern void build_map_objects_monsterdata (struct MapObjDef * map_obj_def, void * start) {
104 // Build data specific to map objects of type monster.
105   struct Monster * m = (struct Monster *) start;
106   m->map_obj.type = map_obj_def->id;
107   struct MonsterDef * md = (struct MonsterDef *) map_obj_def;
108   m->hitpoints = md->hitpoints_start; }
109
110 extern void build_map_objects_itemdata (struct MapObjDef * map_obj_def, void * start) {
111 // Build data speciifc to map objects of type data.
112   struct Item * i = (struct Item *) start;
113   i->map_obj.type = map_obj_def->id; }
114
115 extern void * build_map_objects (struct World * world, void * start, char def_id, unsigned char n,
116                                  size_t size, void (* b_typedata) (struct MapObjDef *, void *)) {
117 // Build chain of n map objects starting at start, use f() for object-specific data.
118   unsigned char i;
119   struct MapObj * mo;
120   char first = 1;
121   struct MapObjDef * mod = get_map_obj_def (world, def_id);
122   for (i = 0; i < n; i++) {
123     mo = get_next_map_obj(start, &first, size, mo);
124     mo->pos = find_passable_pos(world->map);
125     b_typedata (mod, mo); }
126   if (!first)
127     mo->next = 0;
128   return &mo->next; }
129
130 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id) {
131 // Get pointer to the map object definition with id "def_id".
132   struct MapObjDef * d = NULL;
133   for (d = (struct MapObjDef *) world->monster_def; d->id != def_id && 0 != d->next; d = d->next);
134   if (d->id != def_id)
135     for (d = (struct MapObjDef *) world->item_def; d->id != def_id && 0 != d->next; d = d->next);
136   return d; }