home · contact · privacy
Moved initialization of map object definitions from defs file into map_objects library.
[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 readwrite_map_objects_dummy (void * dummy, FILE * file) {
53 // Dummy function for calls of (write|read)_map_objects on map objects without specific attributes.
54   ; }
55
56 extern void write_map_objects_monsterdata (void * start, FILE * file) {
57 // Write to file data specific to map objects of type monster.
58   struct Monster * m = (struct Monster *) start;
59   fputc(m->hitpoints, file); }
60
61 extern void write_map_objects (void * start, FILE * file, void (* w_typedata) (void *, FILE *) ) {
62 // Write into file the map object chain starting at start, use write_type() for object-type specific data.
63   struct MapObj * map_obj;
64   for (map_obj = start; map_obj != 0; map_obj = map_obj->next) {
65     write_uint16_bigendian(map_obj->pos.y + 1, file);
66     write_uint16_bigendian(map_obj->pos.x + 1, file);
67     fputc(map_obj->type, file);
68     w_typedata (map_obj, file); }
69   write_uint16_bigendian(0, file); }
70
71 extern void read_map_objects_monsterdata (void * start, FILE * file) {
72 // Read from file data speciifc to map objects of type monster.
73   struct Monster * m = (struct Monster *) start;
74   m->hitpoints = fgetc(file); }
75
76 static struct MapObj * get_next_map_obj (void * start, char * first, size_t size, struct MapObj * map_obj) {
77 // Return pointer to map object of "size". If first in chain ("first" pointing to !0), point "start" to it.
78   if (* first) {
79     struct MapObj * * z = start;
80     map_obj = malloc(size);
81     * z = map_obj;
82     * first = 0; }
83   else {
84     map_obj->next = malloc(size);
85     map_obj = map_obj->next; }
86   return map_obj; }
87
88 extern void read_map_objects (void * start, FILE * file, size_t size, void (* r_typedata) (void *, FILE *) ) {
89 // Read from file chain of map objects starting at start, use r_typedata() for object-type specific data.
90   struct MapObj * map_obj;
91   uint16_t test;
92   char first = 1;
93   while (1) {
94     test = read_uint16_bigendian(file);
95     if (0 == test)
96       break;
97     map_obj = get_next_map_obj(start, &first, size, map_obj);
98     map_obj->pos.y = test - 1;
99     map_obj->pos.x = read_uint16_bigendian(file) - 1;
100     map_obj->type = fgetc(file);
101     r_typedata (map_obj, file); }
102   if (!first)
103     map_obj->next = 0; }
104
105 extern void build_map_objects_monsterdata (struct MapObjDef * map_obj_def, void * start) {
106 // Build data specific to map objects of type monster.
107   struct Monster * m = (struct Monster *) start;
108   m->map_obj.type = map_obj_def->id;
109   struct MonsterDef * md = (struct MonsterDef *) map_obj_def;
110   m->hitpoints = md->hitpoints_start; }
111
112 extern void build_map_objects_itemdata (struct MapObjDef * map_obj_def, void * start) {
113 // Build data speciifc to map objects of type data.
114   struct Item * i = (struct Item *) start;
115   i->map_obj.type = map_obj_def->id; }
116
117 extern void * build_map_objects (struct World * world, void * start, char def_id, unsigned char n,
118                                  size_t size, void (* b_typedata) (struct MapObjDef *, void *)) {
119 // Build chain of n map objects starting at start, use f() for object-specific data.
120   unsigned char i;
121   struct MapObj * mo;
122   char first = 1;
123   struct MapObjDef * mod = get_map_obj_def (world, def_id);
124   for (i = 0; i < n; i++) {
125     mo = get_next_map_obj(start, &first, size, mo);
126     mo->pos = find_passable_pos(world->map);
127     b_typedata (mod, mo); }
128   if (!first)
129     mo->next = 0;
130   return &mo->next; }
131
132 extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id) {
133 // Get pointer to the map object definition with id "def_id".
134   struct MapObjDef * d = NULL;
135   for (d = (struct MapObjDef *) world->monster_def; d->id != def_id && 0 != d->next; d = d->next);
136   if (d->id != def_id)
137     for (d = (struct MapObjDef *) world->item_def; d->id != def_id && 0 != d->next; d = d->next);
138   return d; }