some meshgen and map rendering updates
[voxelands-alt.git] / src / map / mapgen_terrain.c
blobcfb4cea6bd56a76ab7e766de05bb4cbc8353cc3a
1 /************************************************************************
2 * mapgen_terrain.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
22 #define _MAPGEN_LOCAL
23 #include "map.h"
24 #include "content_block.h"
26 typedef struct mg_offsets_s {
27 int x;
28 int y;
29 int z;
30 int cx;
31 int cy;
32 int cz;
33 } mg_offsets_t;
35 static block_t terrain_blocks[32][288][32];
36 static int terrain_heights[32][32];
37 static int terrain_genheights[32][32];
38 static mg_offsets_t terrain_offsets[72] = {
39 {16,0,16, 0,-80,0},
40 {0,0,16, -16,-80,0},
41 {16,0,0, 0,-80,-16},
42 {0,0,0, -16,-80,-16},
43 {16,16,16, 0,-64,0},
44 {0,16,16, -16,-64,0},
45 {16,16,0, 0,-64,-16},
46 {0,16,0, -16,-64,-16},
47 {16,32,16, 0,-48,0},
48 {0,32,16, -16,-48,0},
49 {16,32,0, 0,-48,-16},
50 {0,32,0, -16,-48,-16},
51 {16,48,16, 0,-32,0},
52 {0,48,16, -16,-32,0},
53 {16,48,0, 0,-32,-16},
54 {0,48,0, -16,-32,-16},
55 {16,64,16, 0,-16,0},
56 {0,64,16, -16,-16,0},
57 {16,64,0, 0,-16,-16},
58 {0,64,0, -16,-16,-16},
59 {16,80,16, 0,0,0},
60 {0,80,16, -16,0,0},
61 {16,80,0, 0,0,-16},
62 {0,80,0, -16,0,-16},
63 {16,96,16, 0,16,0},
64 {0,96,16, -16,16,0},
65 {16,96,0, 0,16,-16},
66 {0,96,0, -16,16,-16},
67 {16,112,16, 0,32,0},
68 {0,112,16, -16,32,0},
69 {16,112,0, 0,32,-16},
70 {0,112,0, -16,32,-16},
71 {16,128,16, 0,48,0},
72 {0,128,16, -16,48,0},
73 {16,128,0, 0,48,-16},
74 {0,128,0, -16,48,-16},
75 {16,144,16, 0,64,0},
76 {0,144,16, -16,64,0},
77 {16,144,0, 0,64,-16},
78 {0,144,0, -16,64,-16},
79 {16,160,16, 0,80,0},
80 {0,160,16, -16,80,0},
81 {16,160,0, 0,80,-16},
82 {0,160,0, -16,80,-16},
83 {16,176,16, 0,96,0},
84 {0,176,16, -16,96,0},
85 {16,176,0, 0,96,-16},
86 {0,176,0, -16,96,-16},
87 {16,192,16, 0,112,0},
88 {0,192,16, -16,112,0},
89 {16,192,0, 0,112,-16},
90 {0,192,0, -16,112,-16},
91 {16,208,16, 0,128,0},
92 {0,208,16, -16,128,0},
93 {16,208,0, 0,128,-16},
94 {0,208,0, -16,128,-16},
95 {16,224,16, 0,144,0},
96 {0,224,16, -16,144,0},
97 {16,224,0, 0,144,-16},
98 {0,224,0, -16,144,-16},
99 {16,240,16, 0,160,0},
100 {0,240,16, -16,160,0},
101 {16,240,0, 0,160,-16},
102 {0,240,0, -16,160,-16},
103 {16,256,16, 0,176,0},
104 {0,256,16, -16,176,0},
105 {16,256,0, 0,176,-16},
106 {0,256,0, -16,176,-16},
107 {16,272,16, 0,192,0},
108 {0,272,16, -16,192,0},
109 {16,272,0, 0,192,-16},
110 {0,272,0, -16,192,-16}
113 /* generate a ground-level region (-80 to 208) */
114 void mapgen_terrain(pos_t *p)
116 int x;
117 int y;
118 int z;
119 int i;
120 int u;
121 block_t *b;
122 block_t *cb;
123 pos_t cp;
124 chunk_t *ch;
126 int h = 1+80;
128 for (x=0; x<32; x++) {
129 for (z=0; z<32; z++) {
130 h = noise_height(p->x+x,p->z+z);
131 terrain_heights[x][z] = h;
132 terrain_genheights[x][z] = h;
133 h += 80;
134 for (y=0; y<288; y++) {
135 b = &terrain_blocks[x][y][z];
136 b->param1 = 0;
137 b->param2 = 0;
138 b->param3 = 0;
139 if (y > h) {
140 b->content = CONTENT_AIR;
141 }else if (y < (h-5)) {
142 b->content = CONTENT_STONE;
143 }else if (y == h && y>79) {
144 b->content = CONTENT_GRASS;
145 }else{
146 b->content = CONTENT_MUD;
152 for (i=0; i<72; i++) {
153 cp.x = p->x+terrain_offsets[i].cx;
154 cp.y = p->y+terrain_offsets[i].cy;
155 cp.z = p->z+terrain_offsets[i].cz;
156 ch = mapgen_create_chunk(&cp);
157 u = 0;
158 for (x=0; x<16; x++) {
159 for (y=0; y<16; y++) {
160 for (z=0; z<16; z++) {
161 b = &terrain_blocks[x+terrain_offsets[i].x][y+terrain_offsets[i].y][z+terrain_offsets[i].z];
162 cb = &ch->blocks[x][y][z];
163 cb->content = b->content;
164 cb->param1 = b->param1;
165 cb->param2 = b->param2;
166 cb->param3 = b->param3;
167 if (cb->content != CONTENT_AIR)
168 u++;
173 ch->state &= ~CHUNK_STATE_UNGENERATED;
174 if (!u)
175 ch->state |= CHUNK_STATE_EMPTY;
176 if (u == 4096)
177 ch->state |= CHUNK_STATE_UNDERGROUND;
179 map_add_chunk(ch);