-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.c
More file actions
executable file
·176 lines (157 loc) · 4.5 KB
/
memory.c
File metadata and controls
executable file
·176 lines (157 loc) · 4.5 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
/*
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 "memory.h"
#include "creature.h"
#include "utility.h"
#include "debug.h"
#include "map.h"
#include "entity.h"
#include "memory.h"
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
struct mem*
new_memory()
{
struct mem *ret = calloc(1, sizeof(struct mem));
return ret;
}
struct mem *
memory_copy(struct mem *other)
{
struct mem *memory = new_memory();
memory->type = other->type;
if (other->type == MEM_TYPE_STRING) {
memory->data.string.chars = malloc(strlen(other->data.string.chars));
strcpy(memory->data.string.chars, other->data.string.chars);
} else if (other->type == MEM_TYPE_SOUND) {
memory->data.sound.text = malloc(strlen(other->data.sound.text));
strcpy(memory->data.sound.text, other->data.sound.text);
} else if (other->type == MEM_TYPE_CREATURE) {
memory->data.creature.name = malloc(strlen(other->data.creature.name));
strcpy(memory->data.creature.name, other->data.creature.name);
}
return memory;
}
void deinit_memory(struct mem *memory)
{
if (memory->type == MEM_TYPE_STRING)
free(memory->data.string.chars);
else if (memory->type == MEM_TYPE_SOUND)
free(memory->data.sound.text);
else if (memory->type == MEM_TYPE_CREATURE)
free(memory->data.creature.name);
}
void
memory_set_string(struct mem *memory, char *string)
{
DEBUG_PRINT(("mem set string\n"));
deinit_memory(memory);
memory->type = MEM_TYPE_STRING;
int len = strlen(string) + 1;
memory->data.string.chars = malloc(len);
strncpy(memory->data.string.chars, string, len);
}
void
memory_set_creature(struct mem *memory, struct ctr *creature)
{
DEBUG_PRINT(("mem set creature\n"));
deinit_memory(memory);
int len = strlen(creature->name) + 1;
memory->type = MEM_TYPE_CREATURE;
memory->data.creature.idx = creature->idx;
memory->data.creature.name = malloc(len);
strncpy(memory->data.creature.name, creature->name, len);
}
void
memory_set_sound(struct mem *memory, struct sound *sound)
{
DEBUG_PRINT(("mem set speech\n"));
deinit_memory(memory);
int len = strlen(sound->type->text) + 1;
memory->type = MEM_TYPE_SOUND;
if (sound->source_t == SOUND_SOURCE_CREATURE) {
memory->data.sound.creature_source_idx = sound->source.creature->idx;
} else if (sound->source_t == SOUND_SOURCE_ENTITY) {
memory->data.sound.entity_source_idx = sound->source.entity->idx;
}
memory->data.sound.text = malloc(len);
strncpy(memory->data.sound.text, sound->type->text, len);
memory->data.sound.source_t = sound->source_t;
}
void
memory_push(struct mem **top, struct mem *addition)
{
addition->next = *top;
*top = addition;
}
void
memory_push_copy(struct mem **top, struct mem *addition)
{
struct mem *copy = memory_copy(addition);
memory_push(top, copy);
}
struct mem*
memory_pop(struct mem **top)
{
if ((*top)->next == NULL) {
return NULL;
}
struct mem* ret = *top;
*top = ret->next;
return ret;
}
bool
memory_eq_string(struct mem *memory, char *string)
{
switch (memory->type) {
case MEM_TYPE_CREATURE:
return strcmp(memory->data.creature.name, string) == 0;
break;
case MEM_TYPE_STRING:
return strcmp(memory->data.string.chars, string) == 0;
break;
case MEM_TYPE_SOUND:
return strcmp(memory->data.sound.text, string) == 0;
break;
case MEM_TYPE_NONE:
return false;
break;
}
return false;
}
bool
memory_eq_ctr(struct mem *memory, struct ctr *creature)
{
if (memory->type != MEM_TYPE_CREATURE)
return false;
return memory->data.creature.idx == creature->idx;
}
bool
memory_is_none(struct mem *memory)
{
return memory->type == MEM_TYPE_NONE;
}
struct ctr *
memory_active_creature(struct mem *memory, struct map *map)
{
if (memory->type != MEM_TYPE_CREATURE) {
DEBUG_PRINT(("Invalid memory type for creature\n"));
return NULL;
}
int idx = memory->data.creature.idx;
DEBUG_PRINT(("Getting active creature at index: %d\n", idx));
return &map->creatures[idx];
}