/
world.c
184 lines (166 loc) · 3.4 KB
/
world.c
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
#include "game.h"
int find_empty_slot(Object * obj, char *mode)
{
char (*slots)[SHORT_STRLEN];
if (!strcmp(mode, "debug"))
slots = obj->debug_slots;
else if (!strcmp(mode, "skill"))
slots = obj->skill_slots;
else if (!strcmp(mode, "item"))
slots = obj->item_slots;
else
return -1;
int i;
for (i = 0; i < 10; i++) {
if (!strcmp(slots[i], ""))
return i;
}
return -1;
}
int num_of_empty_slot(Object * obj, char (*slots)[SHORT_STRLEN])
{
int empty = 0;
int i;
for (i = 0; i < 10; i++) {
if (!strcmp(slots[i], ""))
empty++;
}
return empty;
}
Object *obj_at(World * w, int x, int y, Object * exclude)
{
int i;
for (i = 0; i < 500; i++) {
if (w->objs[i].exist == 0)
continue;
if (w->objs[i].x != x)
continue;
if (w->objs[i].y != y)
continue;
if (&(w->objs[i]) == exclude)
continue;
return &(w->objs[i]);
}
return 0;
}
//can be improved by making scratch buffer.
//loop for all object and put obj to scratch.
//access scratch by index
//in this way, only one full loop is required.
Cell *get_cell_layer(World * w, int x, int y, int layer)
{
int i;
if (x < 0 || y < 0 || x >= 100 || y >= 100)
return 0;
for (i = 0; i < 500; i++) {
Object *o = &(w->objs[i]);
if (o->exist == 0)
continue;
if (o->x == x && o->y == y && o->shape.layer == layer)
return &(o->shape);
}
Cell *mc = &(w->map[y][x]);
if (layer == mc->layer)
return &(w->map[y][x]);
return 0;;
}
int add_slot(Object * obj, char *mode, char *new_slot)
{
char (*slots)[SHORT_STRLEN];
if (!strcmp(mode, "debug"))
slots = obj->debug_slots;
else if (!strcmp(mode, "skill"))
slots = obj->skill_slots;
else if (!strcmp(mode, "item"))
slots = obj->item_slots;
else
return 0;
int empty_i = find_empty_slot(obj, mode);
if (empty_i == -1)
return 0;
strcpy(slots[empty_i], new_slot);
return 1;
}
int save_world(World * w, char *file_name)
{
FILE *fp = fopen(file_name, "wb");
fwrite(w, sizeof(World), 1, fp);
fclose(fp);
return 1;
}
int load_world(World * w, char *file_name)
{
FILE *fp = fopen(file_name, "rb");
fread(w, sizeof(World), 1, fp);
fclose(fp);
int i;
for (i = 0; i < 500; i++) {
if (w->objs[i].exist == 1)
w->objs[i].world = w;
//need debugging
if (w->objs[i].shape.str[0] == '@')
w->hero = &(w->objs[i]);
}
return 1;
}
int put_cell(World * w, int x, int y, Cell c)
{
if (y < 0 || x < 0 || y >= 100 || x >= 100)
return 0;
w->map[y][x] = c;
return 1;
}
Cell interactive_new_cell()
{
}
Cell new_cell(char ch, int bg, int fg, int layer, int block)
{
Cell c;
c.bg_color = bg;
c.fg_color = fg;
c.layer = layer;
c.str[0] = ch;
c.str[1] = 0;
c.block = block;
return c;
}
int delete_cell(World * w, int x, int y)
{
w->map[y][x].str[0] = '.';
w->map[y][x].str[1] = 0;
w->map[y][x].layer = 0;
w->map[y][x].block = 0;
w->map[y][x].bg_color = 0;
w->map[y][x].fg_color = 6;
}
int get_empty_obj_index(World * w)
{
int i;
for (i = 0; i < 500; i++) {
if (w->objs[i].exist == 0)
return i;
}
return -1;
}
//layer -1 can go through anywhere
int check_block(World * w, int x, int y, int layer)
{
int i;
if (x >= 100 || y >= 100 || x < 0 || y < 0)
return 1;
if (w->map[y][x].layer == layer && w->map[y][x].block != 0)
return 1;
if (layer == -1)
return 0;
for (i = 0; i < 500; i++) {
Object *o = &(w->objs[i]);
if (o->exist == 0)
continue;
if (o->x == x &&
o->y == y && o->shape.layer == layer
&& o->shape.block != 0) {
return 1;
}
}
return 0;
}