ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/quadland.C
Revision: 1.9
Committed: Wed May 4 07:36:41 2011 UTC (13 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.8: +2 -2 lines
Log Message:
clean up in_memory handling

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 *
6 * Deliantra is free software: you can redistribute it and/or modify it under
7 * the terms of the Affero GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the Affero GNU General Public License
17 * and the GNU General Public License along with this program. If not, see
18 * <http://www.gnu.org/licenses/>.
19 *
20 * The authors can be reached via e-mail to <support@deliantra.net>
21 */
22
23 #include <global.h>
24
25 #include "noise.h"
26
27 /////////////////////////////////////////////////////////////////////////////
28
29 static bool
30 has_floor (maptile *m, sint16 x, sint16 y)
31 {
32 mapxy pos (m, x, y);
33
34 if (!pos.normalise ())
35 return true;
36
37 mapspace &ms = pos.ms ();
38
39 for (object *ob = ms.bot; ob; ob = ob->above)
40 if (ob->arch->archname == shstr_quad_open_space)
41 return false;
42 else if (ob->flag [FLAG_IS_FLOOR])
43 return true;
44
45 return false;
46 }
47
48 void
49 move_into_wall (object *ob, object *wall)
50 {
51 bool visible = !wall->invisible && !ob->flag [FLAG_BLIND];
52
53 if (wall->flag [FLAG_IS_QUAD] && visible)
54 {
55 maptile *m = wall->map;
56
57 if (m->tile_path [TILE_UP])
58 if (maptile *up = m->tile_available (TILE_UP))
59 {
60 if (ob->blocked (up, ob->x, ob->y) || has_floor (up, ob->x, ob->y))
61 ob->failmsg (format ("Ouch, you hit your head while climbing the %s! H<Didn't you see the ceiling? :)>", query_name (wall)));
62 //TODO: reduce health
63 else if (ob->blocked (up, wall->x, wall->y))
64 ob->failmsg (format ("You try to climb up, but the %s is too high for you! H<No free space on top of this block.>", query_name (wall)));
65 //TODO: reduce health
66 else
67 {
68 ob->statusmsg (format ("You successfully climb up the %s.", query_name (wall)));
69 // here we assume that ob is a player...
70 ob->enter_map (up, wall->x, wall->y);
71 }
72 }
73 else
74 ob->failmsg (format ("You try to climb the %s, but you fall down! H<Try again.>", query_name (wall)));
75 else
76 ob->failmsg (format ("You fail to climb up the %s! H<You cannot climb up here.>", query_name (wall)));
77
78
79 return;
80 }
81
82 if (ob->contr->ns->bumpmsg)
83 {
84 ob->play_sound (sound_find ("bump_wall"));
85
86 ob->statusmsg (visible
87 ? format ("You bump into the %s.", query_name (wall))
88 : "You bump into something."
89 );
90 }
91 }
92
93 /////////////////////////////////////////////////////////////////////////////
94
95 // this mapspace has changed - potentialyl activate dormant physics objects
96 // in the vicinity
97
98 void update_physics (maptile *m, int x, int y)
99 {
100 }
101
102 // handle physics updates
103 void move_physics (object *ob)
104 {
105 }
106
107 /////////////////////////////////////////////////////////////////////////////
108
109 #define FANCY_GRAPHICS 0
110
111 static void
112 gen_quadspace (maptile *m, int mx, int my, int x, int y, int z)
113 {
114 vec2d P = vec2d (x, y);
115
116 const int deep_sea_z = -200;
117
118 static frac2d gen(13);
119
120 static frac2d vec_gen1 (6, 2, 0.5, 1);
121 static frac2d vec_gen2 (6, 2, 0.5, 2);
122
123 const float continent_scale = 0.00008;
124
125 vec2d perturb_pos = pow (P, 1.4) * 1e-5;
126
127 vec2d perturb (
128 vec_gen1.fBm (perturb_pos),
129 vec_gen2.fBm (perturb_pos)
130 );
131
132 float perturb_perturb = 1 - (P[1] - P[0]) * (1. / 25000 / 2);
133 perturb_perturb = perturb_perturb * perturb_perturb * 0.4;
134 perturb *= perturb_perturb;
135
136 vec2d P_continent = P * continent_scale + perturb;
137
138 static frac2d continent_gen (13, 2.13, 0.5);
139 float continent = continent_gen.fBm (P_continent) + 0.05f;
140
141 float x_gradient = P[0] * (1. / 25000);
142 float y_gradient = P[1] * (1. / 25000);
143 float xy_gradient = (P[0] + P[1]) * (0.5 / 25000);
144
145 const float N = (25000 - 1) * continent_scale;
146
147 // we clip a large border on the perturbed shape, to get irregular coastline
148 // and then clip a smaller border around the real shape
149 continent = border_blend (-1.f, continent, P_continent , N, 400 * continent_scale);
150 continent = border_blend (-1.f, continent, P * continent_scale + perturb * 0.1, N, 100 * continent_scale);
151
152 enum {
153 T_NONE,
154 T_OCEAN,
155 T_RIVER,
156 T_VALLEY,
157 T_MOUNTAIN,
158 T_UNDERGROUND,
159 T_AIR, // unused
160 T_ACQUIFER,
161 } t = T_NONE;
162
163 vec3d c;
164 int h0 = 0; // "water level"
165 int h = 1000000; // height form heightmap
166
167 // the continent increases in height from 0 to ~700 levels in the absence of anything else
168 // thats about one step every 7 maps.
169 int base_height = blend (0, 300, xy_gradient, 0.2f, 0.9f);
170 int river_height = base_height * 9 / 10;
171
172 // add this to rivers to "dry them out"
173 float dry_out = max (0.f, lerp (xy_gradient, 0.7f, 1.f, 0.f, 0.3f));
174
175 static frac2d river_gen (2);
176 float river1 = abs (river_gen.fBm (P * 0.001 + perturb * 4)) + dry_out;
177 float river2 = river_gen.ridgedmultifractal (P * 0.04, 0.8, 10) - y_gradient * 0.2 - 0.16 - dry_out;
178
179 float valley = river1 - 0.2f;
180
181 static frac2d mountain_gen (8, 2.14, 0.5);
182 float mountain = mountain_gen.ridgedmultifractal (P * 0.004);
183
184 //TODO: mountains should not lower the height, should they?
185 t = valley < 0 ? T_VALLEY : T_MOUNTAIN;
186 c = blend0 (vec3d (0, 0.8, 0), vec3d (0.8, 0, 0), valley, 0.1f);
187 h = blend0 (base_height + continent * 300, base_height + mountain * xy_gradient * 400, valley, 0.1f);
188
189 if (river1 < 0.01f)
190 {
191 // main rivers - they cut deeply into the mountains (base_height * 0.9f)
192 t = T_RIVER;
193 c = vec3d (0.2, 0.2, 1);
194 h0 = river_height;
195 min_it (h, river_height + lerp<float> (river1, 0.f, 0.01f, -20, -1));
196 }
197
198 if (river2 > 0)
199 {
200 t = T_RIVER;
201 c = vec3d (0.2, 0.2, 1);
202 h0 = river_height;
203 min_it (h, river_height + lerp<float> (river1, 0.f, 0.01f, -5, -1));
204 }
205
206 if (continent < 0)
207 {
208 t = T_OCEAN;
209 min_it (h, min (continent * 200, -1));
210 c = vec3d (0, 0, 1);
211 }
212
213 // now we have the base height, and base terrain
214
215 #if FANCY_GRAPHICS
216 z = h; // show the surface, not the given z layer
217 #endif
218
219 max_it (h0, h);
220
221 // everything below the surface is underground, or a variant
222 if (z < h)
223 t = T_UNDERGROUND;
224
225 // put acquifers a bit below the surface, to reduce them leaking out (will still happen)
226 if (z < h - 3)
227 {
228 static frac3d acquifer_gen (4);
229 float acquifer = acquifer_gen.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.01), 1.003, 2);
230
231 if (acquifer > 0.48)
232 {
233 t = T_ACQUIFER;
234 c = vec3d (1,1,1);
235 }
236 }
237
238 // TODO: caves
239 // TODO: chees areas
240 // TODO: minerals
241 // TODO: monsters
242
243 #if FANCY_GRAPHICS
244 float v = clamp (lerp<float> (h, deep_sea_z, 800, 0.f, 1.f), 0.f, 1.f);
245 c *= v;
246
247 putc (clamp<int> (255 * c[0], 0, 255), stdout);
248 putc (clamp<int> (255 * c[1], 0, 255), stdout);
249 putc (clamp<int> (255 * c[2], 0, 255), stdout);
250 #else
251 shstr arch_floor = shstr ("quad_open_space");
252 shstr arch_wall;
253
254 // TODO: this is shit - we should never generatea water surface, but only
255 // water above the surface
256 switch (t)
257 {
258 case T_OCEAN:
259 if (z < h0)
260 arch_wall = shstr ("quad_water_wall");
261 else if (z == h0)
262 arch_floor = shstr ("quad_ocean_floor");
263 break;
264
265 case T_RIVER:
266 if (z < h0)
267 arch_wall = shstr ("quad_water_wall");
268 else if (z == h0)
269 arch_floor = shstr ("quad_water_floor");
270 break;
271
272 case T_VALLEY:
273 if (z == h)
274 arch_floor = shstr ("quad_dirt_floor");
275 break;
276
277 case T_MOUNTAIN:
278 if (z == h)
279 arch_floor = shstr ("quad_stone_floor");
280 break;
281
282 case T_UNDERGROUND:
283 // todo, use a fractal
284 if (z < h - 10)
285 {
286 arch_floor = shstr ("quad_dirt_floor");
287 arch_wall = shstr ("quad_dirt_wall");
288 }
289 else
290 {
291 arch_floor = shstr ("quad_stone_floor");
292 arch_wall = shstr ("quad_stone_wall");
293 }
294 break;
295
296 case T_ACQUIFER:
297 arch_wall = shstr ("quad_water_wall");
298 break;
299
300 default:
301 abort ();
302 }
303
304 if (arch_floor)
305 m->insert (archetype::get (arch_floor), mx, my);
306
307 if (arch_wall)
308 m->insert (archetype::get (arch_wall ), mx, my);
309 #endif
310 }
311
312 void
313 gen_quadmap (maptile *m, int x, int y, int z)
314 {
315 assert (m->width == 50);
316 assert (m->height == 50);
317
318 for (int mx = 0; mx < 50; ++mx)
319 for (int my = 0; my < 50; ++my)
320 gen_quadspace (m, mx, my, x + mx, y + my, z);
321 }
322
323 /////////////////////////////////////////////////////////////////////////////
324
325 void noise_test ();
326 void noise_test ()
327 {
328 #if 1
329 int Nw = 700;
330
331 printf ("P6 %d %d 255\n", Nw * 3, Nw * 2);
332 // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
333 for (int y = 0; y < Nw; ++y)
334 {
335 if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw);//D
336
337 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x * 25000 / Nw, y * 25000 / Nw, 0);
338
339 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 400, y, 0);
340 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 22000, y + 2000, 0);
341 }
342 for (int y = 0; y < Nw; ++y)
343 {
344 if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw+50);//D
345
346 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 1000, y + 22000, 0);
347 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 12500, y + 12500, 0);
348 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 22000, y + 22500, 0);
349 }
350
351 //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout);
352 //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout);
353 //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout);
354 //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout);
355 //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout);
356 //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout);
357 //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout);
358 //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout);
359 #elif 1
360 int N = 25000;
361
362 printf ("P6 %d %d 255\n", N, N);
363 for (int y = 0; y < N; ++y)
364 {
365 if (!(y&63))fprintf (stderr, "y %d\n", y);//D
366
367 for (int x = 0; x < N; ++x) gen_quadspace (0, 0, 0, x, y, 0);
368 }
369 #else
370 int N = 200;
371
372 //printf ("P6 %d %d 255\n", N, N);
373 // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
374 for (int z = 0; z < N; ++z)
375 {
376 if (!(z&7))fprintf (stderr, "z %d\n", z);//D
377 for (int y = 0; y < N; ++y)
378 for (int x = 0; x < N; ++x)
379 {
380 #if 0
381 float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2;
382
383 if (z < 64)
384 v = v * (z * z) / (64 * 64);
385
386 if (v <= 0.9)
387 continue;
388 #endif
389 static frac3d gen3 (10);
390 //float v = gen3.turbulence (vec3d (x * 0.01, y * 0.01, z * 0.01));
391 float v = gen3.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.001), 1.003, 2);
392
393 if (v <= 0.48) continue;
394
395 float r[4];
396 int i[4];
397
398 r[0] = x;
399 r[1] = y;
400 r[2] = z;
401 r[3] = v;
402
403 memcpy (i, r, 16);
404
405 i[0] = htonl (i[0]);
406 i[1] = htonl (i[1]);
407 i[2] = htonl (i[2]);
408 i[3] = htonl (i[3]);
409
410 fwrite (i, 4*4, 1, stdout);
411 }
412 }
413 #endif
414
415 exit (0);
416 }