summaryrefslogtreecommitdiffstats
path: root/world.c
blob: 59c32e03d3b59cc86a319a57fc17278f81459620 (plain)
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
307
308
309
310
311
312
313
314
315
316
//
//  world.c
//  mcc
//
//  Created by Finn Behrens on 28.02.21.
//
#include <string.h>

#include "world.h"
#include "main.h"


#define MC_DIM_CODEC_CNAME "element"
#define MC_DIM_CODEC_NAME_PIGLIN_SAFE "piglin_safe"
#define MC_DIM_CODEC_NAME_NATURAL "natural"
#define MC_DIM_CODEC_NAME_AMBIENT_LIGHT "ambient_light"
#define MC_DIM_CODEC_NAME_FIXED_TIME "fixed_time"
#define MC_DIM_CODEC_NAME_INFINIBURN "infiniburn"
#define MC_DIM_CODEC_NAME_RESPAWN_ANCHOR_WORKS "respawn_anchor_works"
#define MC_DIM_CODEC_NAME_HAS_SKYLIGHT "has_skylight"
#define MC_DIM_CODEC_NAME_BED_WORKS "bed_works"
#define MC_DIM_CODEC_NAME_EFFECTS "effects"
#define MC_DIM_CODEC_NAME_HAS_RAIDS "has_raids"
#define MC_DIM_CODEC_NAME_LOGICAL_HEIGHT "logical_height"
#define MC_DIM_CODEC_NAME_COORDINATE_SCALE "coordinate_scale"
#define MC_DIM_CODEC_NAME_ULTRAWARM "ultrawarm"
#define MC_DIM_CODEC_NAME_HAS_CEILING "has_ceiling"


int mc_nbt_tail_list_byte(struct nbt_list *list, int8_t data, char* name)
{
    struct nbt_list *new;
    
    new = malloc(sizeof(struct nbt_list));
    if (new == NULL) {
        return -ENOMEM;
    }
    
    new->data = malloc(sizeof(nbt_node));
    new->data->type = TAG_BYTE;
    new->data->payload.tag_byte = data;
    new->data->name = malloc(strlen(name) + 1);
    strcpy(new->data->name, name);
    list_add_tail(&new->entry, &list->entry);
    
    return 0;
}

int mc_nbt_tail_list_int(struct nbt_list *list, int32_t data, char* name)
{
    struct nbt_list *new;
    
    new = malloc(sizeof(struct nbt_list));
    if (new == NULL) {
        return -ENOMEM;
    }
    
    new->data = malloc(sizeof(nbt_node));
    new->data->type = TAG_INT;
    new->data->payload.tag_int = data;
    new->data->name = malloc(strlen(name) + 1);
    strcpy(new->data->name, name);
    list_add_tail(&new->entry, &list->entry);
    
    return 0;
}

int mc_nbt_tail_list_long(struct nbt_list *list, int64_t data, char* name)
{
    struct nbt_list *new;
    
    new = malloc(sizeof(struct nbt_list));
    if (new == NULL) {
        return -ENOMEM;
    }
    
    new->data = malloc(sizeof(nbt_node));
    new->data->type = TAG_LONG;
    new->data->payload.tag_long = data;
    new->data->name = malloc(strlen(name) + 1);
    strcpy(new->data->name, name);
    list_add_tail(&new->entry, &list->entry);
    
    return 0;
}

int mc_nbt_tail_list_float(struct nbt_list *list, float data, char* name)
{
    struct nbt_list *new;
    
    new = malloc(sizeof(struct nbt_list));
    if (new == NULL) {
        return -ENOMEM;
    }
    
    new->data = malloc(sizeof(nbt_node));
    new->data->type = TAG_FLOAT;
    new->data->payload.tag_float = data;
    new->data->name = malloc(strlen(name) + 1);
    strcpy(new->data->name, name);
    list_add_tail(&new->entry, &list->entry);
    
    return 0;
}

int mc_nbt_tail_list_strcpy(struct nbt_list *list, char* data, char* name)
{
    struct nbt_list *new;
    
    if (data == NULL || name == NULL)
        return -EFAULT;
    
    new = malloc(sizeof(struct nbt_list));
    if (new == NULL) {
        return -ENOMEM;
    }
    
    new->data = malloc(sizeof(nbt_node));
    new->data->type = TAG_STRING;
    new->data->payload.tag_string = malloc(strlen(data) + 1);
    strcpy(new->data->payload.tag_string, data);
    new->data->name = malloc(strlen(name) + 1);
    
    strcpy(new->data->name, name);
    list_add_tail(&new->entry, &list->entry);
    
    return 0;
}


struct nbt_list *mc_dimension_codec_to_list(mc_dimension_element_t *codec)
{
    struct nbt_list *ret, *new;
    struct list_head *pos;
    
    if (codec == NULL) {
        errno = -EFAULT;
        return NULL;
    }
    
    ret = malloc(sizeof(*ret));
    if (ret == NULL) {
        errno = -ENOMEM;
        return NULL;
    }
    
    INIT_LIST_HEAD(&ret->entry);
    
    if ((errno = mc_nbt_tail_list_byte(ret, codec->piglin_safe, MC_DIM_CODEC_NAME_PIGLIN_SAFE)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_byte(ret, codec->natural, MC_DIM_CODEC_NAME_NATURAL)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_float(ret, codec->ambient_light, MC_DIM_CODEC_NAME_AMBIENT_LIGHT)) != 0)
        goto out_err;
    
    if (codec->fixed_time >= 0 && codec->fixed_time <= 24000)
        if ((errno = mc_nbt_tail_list_long(ret, codec->fixed_time, MC_DIM_CODEC_NAME_FIXED_TIME)) != 0)
            goto out_err;
    
    if ((errno = mc_nbt_tail_list_strcpy(ret, codec->infiniburn, MC_DIM_CODEC_NAME_INFINIBURN)) != 0)
        goto out_err;
    
    if ((errno = mc_nbt_tail_list_byte(ret, codec->respawn_anchor_works, MC_DIM_CODEC_NAME_RESPAWN_ANCHOR_WORKS)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_byte(ret, codec->has_skylight, MC_DIM_CODEC_NAME_HAS_SKYLIGHT)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_byte(ret, codec->bed_works, MC_DIM_CODEC_NAME_BED_WORKS)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_strcpy(ret, codec->effects, MC_DIM_CODEC_NAME_EFFECTS)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_byte(ret, codec->has_raids, MC_DIM_CODEC_NAME_HAS_RAIDS)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_int(ret, codec->logical_height, MC_DIM_CODEC_NAME_LOGICAL_HEIGHT)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_float(ret, codec->coordinate_scale, MC_DIM_CODEC_NAME_COORDINATE_SCALE)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_byte(ret, codec->ultrawarm, MC_DIM_CODEC_NAME_ULTRAWARM)) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_byte(ret, codec->has_ceiling, MC_DIM_CODEC_NAME_HAS_CEILING)) != 0)
        goto out_err;
    
    return ret;
out_err:
    nbt_free_list(ret);
    return NULL;
}

nbt_node *mc_dimension_element_to_nbt(mc_dimension_element_t *codec)
{
    // TODO: memleak at error
    struct nbt_list *list;
    nbt_node *root = malloc(sizeof(nbt_node));
    
    list = mc_dimension_codec_to_list(codec);
    if (list == NULL)
        return NULL;
    
    root->type = TAG_COMPOUND;
    root->payload.tag_compound = list;
    root->name = malloc(strlen(MC_DIM_CODEC_CNAME) + 1);
    strcpy(root->name, MC_DIM_CODEC_CNAME);
    
    
    return root;
}

nbt_node *mc_dimension_registry_element_to_nbt(mc_dimension_registry_element_t *el)
{
    struct nbt_list *new;
    nbt_node *root = malloc(sizeof(nbt_node));
    if (root == NULL) {
        errno = -ENOMEM;
        return NULL;
    }
    
    root->type = TAG_COMPOUND;
    root->name = malloc(0);
    root->payload.tag_compound = malloc(sizeof(struct nbt_list));
    if (root->payload.tag_compound == NULL) {
        errno = -ENOMEM;
        goto out_err;
    }
    
    INIT_LIST_HEAD(&root->payload.tag_compound->entry);
    
    if ((errno = mc_nbt_tail_list_strcpy(root->payload.tag_compound, el->name, "name")) != 0)
        goto out_err;
    if ((errno = mc_nbt_tail_list_int(root->payload.tag_compound, el->id, "id")) != 0)
        goto out_err;
    
    new = malloc(sizeof(*new));
    if (new == NULL) {
        errno = -ENOMEM;
        goto out_err;
    }
    
    new->data = mc_dimension_element_to_nbt(el->element);
    if (new->data == NULL)
        goto out_err_new;
    
    list_add_tail(&new->entry, &root->payload.tag_compound->entry);
    
    return root;
out_err_new:
    nbt_free_list(new);
out_err:
    nbt_free(root);
    return NULL;
}

mc_dimension_element_t *mc_dim_element_default_overworld()
{
    mc_dimension_element_t *dimc = malloc(sizeof(mc_dimension_element_t));
    if (dimc == NULL) {
        errno = -ENOMEM;
        return NULL;
    }
    dimc->piglin_safe = false;
    dimc->natural = true;
    dimc->ambient_light = 0.0;
    dimc->fixed_time = -1; // no fixed time
    dimc->infiniburn = malloc(strlen("minecraft:infiburn-overworld") + 1);
    strcpy(dimc->infiniburn, "minecraft:infiburn-overworld");
    dimc->respawn_anchor_works = false;
    dimc->has_skylight = true;
    dimc->bed_works = true;
    dimc->effects = malloc(strlen("minecraft:overworld") + 1);
    strcpy(dimc->effects, "minecraft:overworld");
    dimc->has_raids = true;
    dimc->logical_height = 256;
    dimc->coordinate_scale = 1.0;
    dimc->ultrawarm = false;
    dimc->has_ceiling = false;
    
    return dimc;
}

mc_dimension_registry_element_t *mc_dim_registry_element_default_overworld()
{
    mc_dimension_registry_element_t *reg = malloc(sizeof(mc_dimension_registry_element_t));
    if (reg == NULL) {
        errno = -ENOMEM;
        return NULL;
    }
    
    reg->element = mc_dim_element_default_overworld();
    if (reg->element == NULL) {
        // errno set by mc_dim_element_default_overworld();
        free(reg);
        return NULL;
    }
    
    reg->name = malloc(strlen("minecraft:overworld") + 1);
    if (reg->name == NULL) {
        free(reg->element);
        free(reg);
        return NULL;
    }
    strcpy(reg->name, "minecraft:overworld");
    reg->id = 0;
    
    return reg;
}

mc_world_t *world;

int init_world()
{
    world = calloc(1, sizeof(mc_world_t));
    if (world == NULL)
        return -ENOMEM;
    
    world->max_players = config->max_user; // TODO: read from config
    world->curr_players = 5; // TODO: get real data
}