ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/quadland.C
Revision: 1.24
Committed: Wed Dec 5 19:03:27 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.23: +1 -1 lines
Log Message:
some bugfixes

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 *
7 * Deliantra is free software: you can redistribute it and/or modify it under
8 * the terms of the Affero GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the Affero GNU General Public License
18 * and the GNU General Public License along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 #include <global.h>
25
26 #include "noise.h"
27
28 /////////////////////////////////////////////////////////////////////////////
29
30 static bool
31 has_floor (maptile *m, sint16 x, sint16 y)
32 {
33 mapxy pos (m, x, y);
34
35 if (!pos.normalise ())
36 return true;
37
38 mapspace &ms = pos.ms ();
39
40 for (object *ob = ms.bot; ob; ob = ob->above)
41 if (ob->arch->archname == shstr_quad_open_space)
42 return false;
43 else if (ob->flag [FLAG_IS_FLOOR])
44 return true;
45
46 return false;
47 }
48
49 void
50 move_into_wall (object *ob, object *wall)
51 {
52 bool visible = !wall->invisible && !ob->flag [FLAG_BLIND];
53
54 if (wall->flag [FLAG_IS_QUAD] && visible)
55 {
56 maptile *m = wall->map;
57
58 if (ob->map->tile_path [TILE_UP] && wall->map->tile_path [TILE_UP])
59 {
60 maptile *wall_up = wall->map->tile_available (TILE_UP);
61 maptile *ob_up = ob ->map->tile_available (TILE_UP);
62
63 if (wall_up && ob_up)
64 {
65 if (ob->blocked (ob_up, ob->x, ob->y) || has_floor (ob_up, ob->x, ob->y))
66 ob->failmsg (format ("Ouch, you hit your head while climbing the %s! H<Didn't you see the ceiling? :)>", query_name (wall)));
67 //TODO: reduce health
68 else if (ob->blocked (wall_up, wall->x, wall->y))
69 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)));
70 //TODO: reduce health
71 else
72 {
73 ob->statusmsg (format ("You successfully climb up the %s.", query_name (wall)));
74 // here we assume that ob is a player...
75 ob->enter_map (wall_up, wall->x, wall->y);
76 }
77 }
78 else
79 ob->failmsg (format ("You try to climb the %s, but you fall down! H<Try again.>", query_name (wall)));
80 }
81 else
82 ob->failmsg (format ("You fail to climb up the %s! H<You cannot climb up here.>", query_name (wall)));
83
84
85 return;
86 }
87
88 if (ob->contr->ns->bumpmsg)
89 {
90 ob->play_sound (sound_find ("bump_wall"));
91
92 ob->statusmsg (visible
93 ? format ("You bump into the %s.", query_name (wall))
94 : "You bump into something."
95 );
96 }
97 }
98
99 /////////////////////////////////////////////////////////////////////////////
100
101 physics_queue::physics_queue ()
102 {
103 i = 0;
104 }
105
106 physics_queue::~physics_queue ()
107 {
108 while (object *ob = pop ())
109 ob->refcnt_dec ();
110 }
111
112 object *
113 physics_queue::pop ()
114 {
115 if (ecb_expect_true (i < size ()))
116 return (*this)[i++];
117
118 i = 0;
119 clear (); //TODO: this frees, but we do not want to free, unless really a lot of objects were queued
120 return 0;
121 }
122
123 inline unsigned int
124 queue_of (tick_t tick)
125 {
126 return tick & (PHYSICS_QUEUES - 1);
127 }
128
129 void update_physics (maptile *m, int x, int y)
130 {
131 }
132
133 // handle physics updates
134 void move_physics (object *ob)
135 {
136 }
137
138 // preliminary slots docs
139 // level - water level 1..7
140 // value - pressure
141 //
142
143 static object *
144 flow_to (maptile *m, sint16 x, sint16 y)
145 {
146 if (!xy_normalise (m, x, y))
147 return 0;
148
149 mapspace &ms = m->at (x, y);
150
151 ms.update ();
152 if (ms.move_block & MOVE_WALK)
153 return 0;
154
155 for (object *w = ms.top; w; w = w->below)
156 if (w->type == PHYSICS)
157 if (w->subtype == ST_WATER_SOURCE)
158 return 0;
159 else if (w->subtype == ST_WATER_FLOW)
160 return w;
161
162 printf ("creating flow at %d,%d\n", x, y);//D
163 object *w = archetype::get (shstr_quad_water_flow);
164 w->level = 0;
165 w->value = 0;
166
167 m->insert (w, x, y);
168 m->queue_physics (w, 1);
169
170 return w;
171 }
172
173 #if 0
174 static int
175 find_ortho_flow (object *src, object **result)
176 {
177 object **res = result;
178
179 for (int dir = 1; dir < 8; dir += 2)
180 {
181 mapxy pos (src->map, src->x, src->y);
182 pos.move (dir);
183 if (pos.normalise ())
184 if (object *w = flow_to (pos.m, pos.x, pos.y))
185 *res++ = w;
186 }
187
188 return res - result;
189 }
190 #endif
191
192 static bool
193 water_set (object *w, int level, int pressure = -1)
194 {
195 if (!level)
196 {
197 w->destroy ();
198 return true;
199 }
200
201 bool activity = false;
202
203 min_it (level, 7);
204
205 if (level != w->level)
206 {
207 w->level = level;
208 // update face
209 activity = true;
210 }
211
212 if (pressure != w->value && pressure >= 0)
213 {
214 w->value = pressure;
215 activity = true;
216 }
217
218 if (activity) // this is overkill, but...
219 w->map->queue_physics_at (w->x, w->y);
220
221 return activity;
222 }
223
224 static void
225 run_physics (object *ob)
226 {
227 //TODO: behaviour objects with virtual methods?
228 switch (ob->subtype)
229 {
230 case ST_WATER_SOURCE:
231 {
232 bool activity = false;
233
234 // water sources generate flowing water "everywhere", but are currently
235 // very simple otherwise.
236 if (!has_floor (ob->map, ob->x, ob->y) && ob->map->tile_path [TILE_DOWN])
237 if (maptile *m = ob->map->tile_available (TILE_DOWN))
238 {
239 if (object *w = flow_to (m, ob->x, ob->y))
240 activity = activity || water_set (w, 7);
241 }
242 else
243 activity = true;
244
245 #if 0
246 object *w [4];
247 int w_cnt = find_ortho_flow (ob, w);
248
249 while (w_cnt--)
250 activity = activity || water_set (w [w_cnt], 7);
251
252 if (activity)
253 ob->map->queue_physics (ob, 1);
254 #endif
255 }
256 break;
257
258 case ST_WATER_FLOW:
259 #if 0
260 // flow down as much as possible
261 if (!has_floor (ob->map, ob->x, ob->y) && ob->map->tile_path [TILE_DOWN])
262 if (maptile *m = ob->map->tile_available (TILE_DOWN))
263 {
264 if (object *w = flow_to (m, ob->x, ob->y))
265 {
266 if (int flow = min (ob->level, 7 - w->level))
267 {
268 water_set (w, w->level + flow);
269 water_set (ob, w->level - flow);
270 if (!ob->level)
271 return; // we escaped downwards
272 }
273 else if (ob->level >= 5) // exert pressure -> does not count as activity
274 water_set (w, w->level, ob->value - 1);
275 }
276 }
277
278 if (ob->level >= 2)
279 {
280 object *ws [4];
281 int w_cnt = find_ortho_flow (ob, ws);
282
283 bool activity = false;
284
285 // distribute 1 to every neighbour that has less - should use some random ordering of course
286
287 while (w_cnt--)
288 {
289 object *w = ws [w_cnt];
290
291 if (w->level < ob->level - 1)
292 {
293 water_set (w, w->level + 1);
294 --ob->level;
295 activity = true;
296 }
297 }
298
299 if (activity)
300 ob->map->queue_physics (ob, 1);
301 }
302 else if (ob->level)
303 ;
304 else
305 ob->destroy ();
306 #endif
307
308 // TODO: flow up with pressure
309 break;
310
311 default:
312 LOG (llevError, "object with unsupported physics subtype %d: %s\n", ob->subtype, ob->debug_desc ());
313 break;
314 }
315 }
316
317 int
318 maptile::run_physics (tick_t tick, int max_objects)
319 {
320 if (state != MAP_ACTIVE)
321 return 0;
322
323 int orig_max_object = max_objects;
324 physics_queue &q = pq [queue_of (server_tick)];
325
326 //if (q.size()) printf ("q %d < %d\n", q.i, (int)q.size());//D
327
328 while (object *ob = q.pop ())
329 {
330 ob->flag [FLAG_PHYSICS_QUEUE] = false;
331 ob->refcnt_dec ();
332
333 if (ob->map != this)
334 {
335 if (!ob->flag [FLAG_FREED])
336 {
337 LOG (llevError, "%s: physical object on wrong map\n", ob->debug_desc ());
338 //ob->map->queue_physics (ob);
339 }
340 }
341 else
342 {
343 //printf ("handling ob %s\n", ob->debug_desc());//D
344 ::run_physics (ob);
345 }
346
347 if (--max_objects <= 0)
348 break;
349 }
350
351 return orig_max_object - max_objects;
352 }
353
354 void
355 maptile::queue_physics (object *ob, int after)
356 {
357 if (!after) after = 14; //D
358 if (!ob->flag [FLAG_PHYSICS_QUEUE])
359 {
360 ob->flag [FLAG_PHYSICS_QUEUE] = true;
361 ob->refcnt_inc ();
362
363 pq [queue_of (server_tick + min (PHYSICS_QUEUES - 2, after))].push_back (ob);
364 }
365 }
366
367 static void
368 queue_physics_at (maptile *m, int x, int y)
369 {
370 mapspace &ms = m->at (x, y);
371
372 for (object *ob = ms.bot; ob; ob = ob->above)
373 if (ob->type == PHYSICS)
374 m->queue_physics (ob, 1);
375 }
376
377 // this mapspace has changed - potentially activate dormant physics objects
378 // in the vicinity. vicinity is the 4 spaces around in the same z layer, and above and below.
379 // TODO: maybe include diagonals in the same z-layer?
380 // TODO: do not go through floors?
381 void
382 maptile::queue_physics_at (int x, int y)
383 {
384 if (maptile *m = tile_available (TILE_DOWN))
385 ::queue_physics_at (m, x, y);
386
387 if (maptile *m = tile_available (TILE_UP))
388 ::queue_physics_at (m, x, y);
389
390 ::queue_physics_at (this, x, y);
391
392 for (int dir = 1; dir < 8; dir += 2)
393 {
394 mapxy pos (this, x, y);
395 pos.move (dir);
396 if (pos.normalise ())
397 ::queue_physics_at (pos.m, pos.x, pos.y);
398 }
399 }
400
401 void
402 maptile::activate_physics ()
403 {
404 // most of this is total overkill, but better be safe than sorry, eh?
405
406 coroapi::cede_to_tick ();
407
408 if (maptile *m = tile_map [TILE_UP])
409 for (mapspace *ms = spaces + size (); ms-- > spaces; )
410 ms->invalidate (); // invalidate faces, in case...
411
412 coroapi::cede_to_tick ();
413
414 for (mapspace *ms = spaces + size (); ms-- > spaces; )
415 for (object *op = ms->bot; op; op = op->above)
416 if (op->type == PHYSICS)
417 queue_physics (op, 0);
418 }
419
420 /////////////////////////////////////////////////////////////////////////////
421
422 #define FANCY_GRAPHICS 0
423
424 static void
425 gen_quadspace (maptile *m, int mx, int my, int x, int y, int z)
426 {
427 vec2d P = vec2d (x, y);
428
429 const int deep_sea_z = -200;
430
431 static frac2d gen(13);
432
433 static frac2d vec_gen1 (6, 2, 0.5, 1);
434 static frac2d vec_gen2 (6, 2, 0.5, 2);
435
436 const float continent_scale = 0.00008;
437
438 vec2d perturb_pos = pow (P, vec2d (1.4)) * 1e-5;
439
440 vec2d perturb (
441 vec_gen1.fBm (perturb_pos),
442 vec_gen2.fBm (perturb_pos)
443 );
444
445 float perturb_perturb = 1 - (P[1] - P[0]) * (1. / 25000 / 2);
446 perturb_perturb = perturb_perturb * perturb_perturb * 0.4;
447 perturb *= perturb_perturb;
448
449 vec2d P_continent = P * continent_scale + perturb;
450
451 static frac2d continent_gen (13, 2.13, 0.5);
452 float continent = continent_gen.fBm (P_continent) + 0.05f;
453
454 float x_gradient = P[0] * (1. / 25000);
455 float y_gradient = P[1] * (1. / 25000);
456 float xy_gradient = (P[0] + P[1]) * (0.5 / 25000);
457
458 const float N = (25000 - 1) * continent_scale;
459
460 // we clip a large border on the perturbed shape, to get irregular coastline
461 // and then clip a smaller border around the real shape
462 //continent = border_blend (-1.f, continent, P_continent , N, 400 * continent_scale);
463 continent = border_blend (-1.f, continent, P * continent_scale + perturb * 0.1, N, 100 * continent_scale);
464
465 enum {
466 T_NONE,
467 T_OCEAN,
468 T_RIVER,
469 T_VALLEY,
470 T_MOUNTAIN,
471 T_UNDERGROUND,
472 T_AIR, // unused
473 T_ACQUIFER,
474 } t = T_NONE;
475
476 vec3d c;
477 int h0 = 0; // "water level"
478 int h = 1000000; // height form heightmap
479
480 // the continent increases in height from 0 to ~700 levels in the absence of anything else
481 // thats about one step every 7 maps.
482 int base_height = blend (0, 300, xy_gradient, 0.2f, 0.9f);
483 int river_height = base_height; // * 9 / 10;
484
485 // add this to rivers to "dry them out"
486 float dry_out = max (0.f, lerp (xy_gradient, 0.7f, 1.f, 0.f, 0.3f));
487
488 static frac2d river_gen (2);
489 float river1 = abs (river_gen.fBm (P * 0.001 + perturb * 4)) + dry_out;
490 float river2 = river_gen.ridgedmultifractal (P * 0.04, 0.8, 10) - y_gradient * 0.2 - 0.16 - dry_out;
491
492 float valley = river1 - 0.2f;
493
494 static frac2d mountain_gen (6, 2.14, 0.5);
495 float mountain = mountain_gen.ridgedmultifractal (P * 0.004);
496
497 //TODO: mountains should not lower the height, should they?
498 t = valley < 0 ? T_VALLEY : T_MOUNTAIN;
499 c = blend0 (vec3d (0, 0.8, 0), vec3d (0.8, 0, 0), valley, 0.1f);
500 h = blend0 (base_height + continent * 0, base_height + mountain * xy_gradient * 100, valley, 0.1f);
501
502 if (river1 < 0.01f)
503 {
504 // main rivers - they cut deeply into the mountains (base_height * 0.9f)
505 // well, silly, they don't
506 t = T_RIVER;
507 c = vec3d (0.2, 0.2, 1);
508 h0 = river_height;
509 min_it (h, river_height + lerp<float> (river1, 0.f, 0.01f, -10, -1));
510 }
511
512 if (river2 > 0)
513 {
514 t = T_RIVER;
515 c = vec3d (0.2, 0.2, 1);
516 h0 = river_height;
517 min_it (h, river_height + max (-5, lerp<float> (river2, 0.01f, 0, -4, -1)));
518 }
519
520 if (continent < 0)
521 {
522 t = T_OCEAN;
523 h0 = 0;
524 min_it (h, min (continent * 200, -1));
525 c = vec3d (0, 0, 1);
526 }
527
528 // now we have the base height, and base terrain
529
530 #if FANCY_GRAPHICS
531 z = h; // show the surface, not the given z layer
532 #endif
533
534 max_it (h0, h);
535
536 // everything below the surface is underground, or a variant
537 if (z < h)
538 t = T_UNDERGROUND;
539
540 // put acquifers a bit below the surface, to reduce them leaking out (will still happen)
541 if (z < h - 3)
542 {
543 static frac3d acquifer_gen (4);
544 float acquifer = acquifer_gen.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.01), 1.003, 2);
545
546 if (acquifer > 0.48)
547 {
548 t = T_ACQUIFER;
549 c = vec3d (1,1,1);
550 }
551 }
552
553 //printf ("+%d+%d %d z %d h %d,%d P%g,%g\n", mx, my, t, z, h,h0, P[0],P[1]);//D
554
555 // TODO: caves
556 // TODO: chees areas
557 // TODO: minerals
558 // TODO: monsters
559
560 #if FANCY_GRAPHICS
561 float v = clamp (lerp<float> (h, deep_sea_z, 800, 0.f, 1.f), 0.f, 1.f);
562 c *= v;
563
564 putc (clamp<int> (255 * c[0], 0, 255), stdout);
565 putc (clamp<int> (255 * c[1], 0, 255), stdout);
566 putc (clamp<int> (255 * c[2], 0, 255), stdout);
567 #else
568 shstr arch_floor = shstr ("quad_open_space");
569 shstr arch_wall;
570
571 // TODO: this is shit - we should never generatea water surface, but only
572 // water above the surface
573 switch (t)
574 {
575 case T_OCEAN:
576 if (z < h0)
577 arch_wall = shstr_quad_water_source;
578 else if (z == h0)
579 arch_floor = shstr ("quad_ocean_floor");
580 break;
581
582 case T_RIVER:
583 if (z < h0)
584 arch_wall = shstr_quad_water_source;
585 else if (z == h0)
586 arch_floor = shstr ("quad_water_floor");
587 break;
588
589 case T_VALLEY:
590 if (z == h)
591 arch_floor = shstr ("quad_dirt_floor");
592 break;
593
594 case T_MOUNTAIN:
595 if (z == h)
596 arch_floor = shstr ("quad_stone_floor");
597 break;
598
599 case T_UNDERGROUND:
600 // todo, use a fractal
601 if (z < h - 10)
602 {
603 arch_floor = shstr ("quad_stone_floor");
604 arch_wall = shstr ("quad_stone_wall");
605 }
606 else
607 {
608 arch_floor = shstr ("quad_dirt_floor");
609 arch_wall = shstr ("quad_dirt_wall");
610 }
611 break;
612
613 case T_ACQUIFER:
614 arch_wall = shstr_quad_water_source;
615 break;
616
617 default:
618 abort ();
619 }
620
621 if (arch_floor)
622 m->insert (archetype::get (arch_floor), mx, my);
623
624 if (arch_wall)
625 m->insert (archetype::get (arch_wall ), mx, my);
626 #endif
627 }
628
629 void
630 gen_quadmap (maptile *m, int x, int y, int z)
631 {
632 assert (m->width == 50);
633 assert (m->height == 50);
634
635 for (int mx = 0; mx < 50; ++mx)
636 for (int my = 0; my < 50; ++my)
637 gen_quadspace (m, mx, my, x + mx, y + my, z);
638 }
639
640 /////////////////////////////////////////////////////////////////////////////
641
642 void noise_test ();
643 void noise_test ()
644 {
645 #if 1
646 int Nw = 700;
647
648 printf ("P6 %d %d 255\n", Nw * 3, Nw * 2);
649 // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
650 for (int y = 0; y < Nw; ++y)
651 {
652 if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw);//D
653
654 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x * 25000 / Nw, y * 25000 / Nw, 0);
655
656 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 400, y, 0);
657 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 22000, y + 2000, 0);
658 }
659 for (int y = 0; y < Nw; ++y)
660 {
661 if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw+50);//D
662
663 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 1000, y + 22000, 0);
664 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 12500, y + 12500, 0);
665 for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 22000, y + 22000, 0);
666 }
667
668 //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout);
669 //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout);
670 //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout);
671 //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout);
672 //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout);
673 //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout);
674 //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout);
675 //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout);
676 #elif 1
677 int N = 25000;
678
679 printf ("P6 %d %d 255\n", N, N);
680 for (int y = 0; y < N; ++y)
681 {
682 if (!(y&63))fprintf (stderr, "y %d\n", y);//D
683
684 for (int x = 0; x < N; ++x) gen_quadspace (0, 0, 0, x, y, 0);
685 }
686 #else
687 int N = 200;
688
689 //printf ("P6 %d %d 255\n", N, N);
690 // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
691 for (int z = 0; z < N; ++z)
692 {
693 if (!(z&7))fprintf (stderr, "z %d\n", z);//D
694 for (int y = 0; y < N; ++y)
695 for (int x = 0; x < N; ++x)
696 {
697 #if 0
698 float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2;
699
700 if (z < 64)
701 v = v * (z * z) / (64 * 64);
702
703 if (v <= 0.9)
704 continue;
705 #endif
706 static frac3d gen3 (10);
707 //float v = gen3.turbulence (vec3d (x * 0.01, y * 0.01, z * 0.01));
708 float v = gen3.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.001), 1.003, 2);
709
710 if (v <= 0.48) continue;
711
712 float r[4];
713 int i[4];
714
715 r[0] = x;
716 r[1] = y;
717 r[2] = z;
718 r[3] = v;
719
720 memcpy (i, r, 16);
721
722 i[0] = htonl (i[0]);
723 i[1] = htonl (i[1]);
724 i[2] = htonl (i[2]);
725 i[3] = htonl (i[3]);
726
727 fwrite (i, 4*4, 1, stdout);
728 }
729 }
730 #endif
731
732 exit (0);
733 }
734