-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
executable file
·306 lines (269 loc) · 8.23 KB
/
map.c
File metadata and controls
executable file
·306 lines (269 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
Copyright 2017-2020 Anthony Cohn-Richardby
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//define DEBUG
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "debug.h"
#include "map.h"
#include "vector.h"
#include "window.h"
#include "entity.h"
#include "rouge.h"
#include "ground.h"
#include "script.h"
#include "creature.h"
#include "memory.h"
#define SOUND_REALLOC_STEP 10
struct map *
init_map(int width, int height)
{
struct map *map = malloc(sizeof(struct map));
map->width = width;
map->height = height;
map->size = width * height;
map->cells = malloc(map->size * sizeof(struct cell));
DEBUG_PRINT(("Allocated map memory\n"));
struct vector2i c_point = {0, 0};
struct cell *c_cell;
for (int i = 0; i < map->height; ++i) {
for (int j = 0; j < map->width; ++j) {
c_point.x = j; c_point.y = i;
c_cell = get_cell(map, &c_point);
c_point.x = j; c_point.y = i + 1;
c_cell->north = get_cell(map, &c_point);
c_point.x = j + 1; c_point.y = i;
c_cell->east = get_cell(map, &c_point);
c_point.x = j; c_point.y = i - 1;
c_cell->south = get_cell(map, &c_point);
c_point.x = j - 1; c_point.y = i;
c_cell->west = get_cell(map, &c_point);
c_cell->ground = 0;
c_cell->inventory_size = 0;
c_cell->inventory = NULL;
c_cell->creature = NULL;
c_cell->sound = NULL;
}
}
return map;
}
struct cell *
get_cell(struct map *map, struct vector2i *point)
{
if (point->x >= 0 && point->x < map->width && point->y >= 0 && point->y < map->height) {
return map->cells + (point->y * map->height + point->x);
}
else {
return NULL;
}
}
void
render_map(struct map *map, struct wnw *window, struct vector2i *from, struct vector2i *to)
{
char *data = window->data;
int index = 0;
struct vector2i c_pnt;
for (int i = from->y; i < to->y; ++i) {
for (int j = from->x; j < to->x; ++j) {
c_pnt.x = j; c_pnt.y = i;
data[index] = cell_to_char(get_cell(map, &c_pnt));
++index;
}
data[index] = '\n';
++index;
}
data[index] = '\0';
}
struct cell *
dir_to_cell(struct cell *cel, enum dir_t direction)
{
struct cell *rtn_cell = NULL;
switch (direction) {
case NORTH:
rtn_cell = cel->north;
break;
case EAST:
rtn_cell = cel->east;
break;
case SOUTH:
rtn_cell = cel->south;
break;
case WEST:
rtn_cell = cel->west;
break;
default:
break;
}
return rtn_cell;
}
void
insert_sound(struct map *map, struct sound *sound)
{
DEBUG_PRINT(("Inserting sound into map\n"));
if (map->sound_count % SOUND_REALLOC_STEP == 0) {
DEBUG_PRINT(("At sound count %d, need to realloc another block\n", map->sound_count));
int new_chunks = (map->sound_count / SOUND_REALLOC_STEP) + 1;
map->sounds = realloc(map->sounds, new_chunks * 100 * sizeof(struct sound*));
DEBUG_PRINT(("Successfully realloc'd space\n"));
}
map->sounds[map->sound_count++] = sound;
DEBUG_PRINT(("Successfully inserted sound\n"));
}
void
clear_sounds(struct map *map)
{
for (int i = 0; i < map->sound_count; i++) {
DEBUG_PRINT(("Clearing sound, number: %d\n", i));
struct sound *sound = map->sounds[i];
sound->cell->sound = NULL;
// If we reference the type from the 'all_sounds' array, don't free it - else free internals
if (!(sound->type >= all_sound_types && sound->type < (all_sound_types + all_sound_types_count))) {
DEBUG_PRINT(("Sound was not referenced from all_sound_types, deinit\n"));
deinit_sound(sound);
}
free(sound);
}
DEBUG_PRINT(("Freeing sound array\n"));
free(map->sounds);
map->sound_count = 0;
// Must be null for future realloc
map->sounds = NULL;
}
void
insert_entity(struct map *map, struct ent entity, struct scrpt *script, struct vector2i position)
{
struct cell *cel = get_cell(map, &position);
struct ent *entity_copy = init_entity(malloc(sizeof (struct ent)), entity.name, entity.desc, entity.icon, entity.weight, entity.size);
entity_copy->position = position;
entity_copy->map = map;
entity_copy->prev = NULL;
entity_copy->next = cel->inventory;
if (cel->inventory) {
cel->inventory->prev = entity_copy;
}
entity_copy->script = script;
entity_copy->memory = new_memory();
cel->inventory = entity_copy;
cel->inventory_size += 1;
}
void
map_remove_entity(struct ent *entity)
{
struct cell *cell = get_cell(entity->map, &entity->position);
if (entity->prev == NULL) {
cell->inventory = entity->next;
} else {
entity->prev->next = entity->next;
}
entity->position = (struct vector2i){ 0, 0 };
entity->map = NULL;
entity->prev = NULL;
entity->next = NULL;
cell->inventory_size--;
}
struct ctr*
insert_creature(struct map *map, struct ctr *creature, struct scrpt *script, struct vector2i position)
{
struct cell *cell = get_cell(map, &position);
struct ctr *new_creature = &map->creatures[map->creature_count];
new_creature->idx = map->creature_count;
new_creature->position = position;
new_creature->health = creature->health;
new_creature->tp = creature->tp;
new_creature->inventory_size = creature->inventory_size;
new_creature->hearing = creature->hearing;
new_creature->name = malloc(strlen(creature->name) + 1);
strcpy(new_creature->name, creature->name);
new_creature->desc = malloc(strlen(creature->desc) + 1);
strcpy(new_creature->desc, creature->desc);
new_creature->inventory_max = creature->inventory_max;
new_creature->inventory = malloc(sizeof (struct ent*) * creature->inventory_max);
new_creature->map = map;
new_creature->script = script;
new_creature->speech = new_memory();
new_creature->memory = new_memory();
cell->creature = new_creature;
map->creature_count++;
return new_creature;
}
void
map_consume_speech(struct ctr *creature)
{
char *text = NULL;
struct mem *speech = creature->speech;
char *strings[MAX_SPEECH_SIZE];
int lengths[MAX_SPEECH_SIZE];
int len = 0;
int i = 0;
do {
if (speech->type != MEM_TYPE_STRING)
continue;
strings[i] = speech->data.string.chars;
lengths[i] = strlen(strings[i]);
len += lengths[i];
DEBUG_PRINT(("Found string %s, length: %d\n", strings[i], lengths[i]));
i++;
} while (i < MAX_SPEECH_SIZE && (speech = speech->next));
if (i == 0)
return;
DEBUG_PRINT(("Found strings, count: %d, length: %d\n", i, len));
text = malloc(len + 1);
text[len] = 0;
for (i--, len = 0; i >= 0; i--) {
memcpy(&text[len], strings[i], lengths[i]);
len += lengths[i];
}
while ((speech = memory_pop(&creature->speech))) {
deinit_memory(speech);
free(speech);
}
struct sound_t *sound_type = malloc(sizeof(struct sound_t));
sound_type->id = "";
sound_type->text = text;
struct sound *sound = sound_creature(sound_type, creature);
if (sound) {
insert_sound(creature->map, sound);
DEBUG_PRINT(("Generated speech: %s\n", sound->type->text));
}
}
char
cell_to_char(struct cell *cel)
{
if (cel == NULL || cel->ground < 0)
return CELL_SPACE_ICON;
struct grnd ground = all_grounds[(int) cel->ground];
char rtn = ground.icon;
struct ent *entity = cel->inventory;
size_t invsz = cel->inventory_size;
if (invsz > 0) {
rtn = entity->icon;
}
struct ctr *creature = cel->creature;
if (creature) {
rtn = creature->name[0];
}
return rtn;
}
void
destroy_map(struct map* map)
{
free(map->creatures);
free(map->cells);
free(map);
}
bool
cell_blocks_sight(struct cell *c)
{
return false;
}