home · contact · privacy
Simplified adding new objects to map.
[plomrogue] / src / map_objects.c
1 /* map_objects.c */
2
3 #include "map_objects.h"
4 #include <stdlib.h> /* for free(), atoi() */
5 #include <stdint.h> /* for uint8_t */
6 #include <stdio.h> /* for FILE typedef */
7 #include <string.h> /* for strchr(), strlen(), memcpy(), strtok() */
8 #include "readwrite.h" /* for get_linemax(), try_fopen(), try_fclose()
9                         * [read/write]_uint[8/16/23][_bigendian]()
10                         */
11 #include "misc.h" /* for try_malloc(), try_calloc(), find_passable_pos() */
12 #include "main.h" /* for World struct */
13 #include "rexit.h" /* for err_exit() */
14
15
16
17 extern void init_map_object_defs(struct World * world, char * filename)
18 {
19     char * f_name = "init_map_object_defs()";
20     FILE * file = try_fopen(filename, "r", world, f_name);
21     uint16_t linemax = get_linemax(file, world, f_name);
22     struct MapObjDef ** last_mod_ptr_ptr = &world->map_obj_defs;
23     char * delim = " ";
24     char line[linemax + 1];
25     while (try_fgets(line, linemax + 1, file, world, f_name))
26     {
27         struct MapObjDef * mod;
28         mod = try_malloc(sizeof(struct MapObjDef), world, f_name);
29         mod->next = NULL;
30         mod->id = atoi(strtok(line, delim));
31         mod->corpse_id = atoi(strtok(NULL, delim));
32         mod->char_on_map = * strtok(NULL, delim);
33         mod->lifepoints = atoi(strtok(NULL, delim));
34         char * name = strtok(NULL, "\n");
35         mod->name = try_malloc(strlen(name) + 1, world, f_name);
36         memcpy(mod->name, name, strlen(name) + 1);
37         * last_mod_ptr_ptr = mod;
38         last_mod_ptr_ptr = &mod->next;
39     }
40     try_fclose(file, world, f_name);
41 }
42
43
44
45 extern void free_map_object_defs(struct MapObjDef * mod_start)
46 {
47     if (NULL == mod_start)
48     {
49         return;
50     }
51     free_map_object_defs(mod_start->next);
52     free(mod_start->name);
53     free(mod_start);
54 }
55
56
57
58 extern void write_map_objects(struct World * world, FILE * file)
59 {
60     char * f_name = "write_map_objects()";
61     struct MapObj * mo = world->map_objs;
62     uint8_t size = 3 + 1 + 3 + 1 + 3 + 1 + 5 + 1 + 5 + 1;
63     char line[size];
64     while (NULL != mo)
65     {
66         sprintf(line, "%d %d %d %d %d\n",
67                 mo->id, mo->type, mo->lifepoints, mo->pos.y, mo->pos.x);
68         try_fwrite(line, strlen(line), 1, file, world, f_name);
69         mo = mo->next;
70     }
71 }
72
73
74
75 extern void read_map_objects(struct World * world, FILE * file, char * line,
76                               int linemax)
77 {
78     char * f_name = "read_map_objects()";
79     struct MapObj ** mo_ptr_ptr = &world->map_objs;
80     char * delim = " ";
81     while (try_fgets(line, linemax + 1, file, world, f_name))
82     {
83         struct MapObj * mo = malloc(sizeof(struct MapObj));
84         mo->next = NULL;
85         mo->id = atoi(strtok(line, delim));
86         if (mo->id > world->map_obj_count)
87         {
88             world->map_obj_count = mo->id;
89         }
90         mo->type = atoi(strtok(NULL, delim));
91         mo->lifepoints = atoi(strtok(NULL, delim));
92         mo->pos.y = atoi(strtok(NULL, delim));
93         mo->pos.x = atoi(strtok(NULL, delim));
94         * mo_ptr_ptr = mo;
95         mo_ptr_ptr = &mo->next;
96     }
97 }
98
99
100
101 extern void add_map_object(struct World * world, uint8_t type)
102 {
103     char * f_name = "add_map_object";
104     struct MapObjDef * mod = get_map_object_def(world, type);
105     struct MapObj * mo = try_malloc(sizeof(struct MapObj), world, f_name);
106     mo->id = world->map_obj_count;
107     world->map_obj_count++;
108     mo->type = mod->id;
109     mo->lifepoints = mod->lifepoints;
110     mo->pos = find_passable_pos(world->map);
111     mo->next = world->map_objs;
112     world->map_objs = mo;
113 }
114
115
116
117 extern void add_map_objects(struct World * world, uint8_t type, uint8_t n)
118 {
119     uint8_t i;
120     for (i = 0; i < n; i++)
121     {
122         add_map_object(world, type);
123     }
124 }
125
126
127
128 extern void free_map_objects(struct MapObj * mo_start)
129 {
130     if (NULL == mo_start)
131     {
132         return;
133     }
134     free_map_objects(mo_start->next);
135     free(mo_start);
136 }
137
138
139
140 extern struct MapObjDef * get_map_object_def(struct World * w, uint8_t id)
141 {
142     struct MapObjDef * mod = w->map_obj_defs;
143     while (id != mod->id)
144     {
145         mod = mod->next;
146     }
147     return mod;
148 }