1 #include <getline/getline.h>
4 #include "server/schematic.h"
5 #include "server/server_node.h"
8 void schematic_load(List *schematic, const char *path, SchematicMapping *mappings, size_t num_mappings)
12 FILE *file = fopen(path, "r");
14 fprintf(stderr, "[warning] failed to open schematic %s\n", path);
23 // getline is POSIX 2008, so we can use it
24 while ((length = getline(&line, &siz, file)) > 0) {
30 SchematicNode *node = malloc(sizeof *node);
33 if (sscanf(line, "%d %d %d %2x%2x%2x",
34 &node->pos.x, &node->pos.z, &node->pos.y,
35 &color.x, &color.y, &color.z) != 6) {
36 fprintf(stderr, "[warning] syntax error in schematic %s in line %d: %s\n",
42 SchematicMapping *mapping = NULL;
43 for (size_t i = 0; i < num_mappings; i++)
44 if (v3s32_equals(color, mappings[i].color)) {
45 mapping = &mappings[i];
50 fprintf(stderr, "[warning] color not mapped to node in schematic %s in line %d: %02x%02x%02x\n",
51 path, count, color.x, color.y, color.z);
56 node->node = mapping->use_color
57 ? server_node_create_color(mapping->type, (v3f32) {
61 }) : server_node_create(mapping->type);
63 list_apd(schematic, node);
72 void schematic_place(List *schematic, v3s32 pos, TerrainGenStage tgs, List *changed_chunks)
74 LIST_ITERATE(schematic, list_node) {
75 SchematicNode *node = list_node->dat;
77 server_terrain_gen_node(
78 v3s32_add(pos, node->pos),
79 server_node_copy(node->node),
84 static void delete_schematic_node(SchematicNode *node)
86 server_node_delete(&node->node);
90 void schematic_delete(List *schematic)
92 list_clr(schematic, &delete_schematic_node, NULL, NULL);