ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/quadland.C
Revision: 1.10
Committed: Wed May 4 12:04:33 2011 UTC (13 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.9: +25 -19 lines
Log Message:
ugh

File Contents

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