/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
*
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* and the GNU General Public License along with this program. If not, see
* .
*
* The authors can be reached via e-mail to
*/
#include
#include "noise.h"
/////////////////////////////////////////////////////////////////////////////
static bool
has_floor (maptile *m, sint16 x, sint16 y)
{
mapxy pos (m, x, y);
if (!pos.normalise ())
return true;
mapspace &ms = pos.ms ();
for (object *ob = ms.bot; ob; ob = ob->above)
if (ob->arch->archname == shstr_quad_open_space)
return false;
else if (ob->flag [FLAG_IS_FLOOR])
return true;
return false;
}
void
move_into_wall (object *ob, object *wall)
{
bool visible = !wall->invisible && !ob->flag [FLAG_BLIND];
if (wall->flag [FLAG_IS_QUAD] && visible)
{
maptile *m = wall->map;
if (ob->map->tile_path [TILE_UP] && wall->map->tile_path [TILE_UP])
{
maptile *wall_up = wall->map->tile_available (TILE_UP);
maptile *ob_up = ob ->map->tile_available (TILE_UP);
if (wall_up && ob_up)
{
if (ob->blocked (ob_up, ob->x, ob->y) || has_floor (ob_up, ob->x, ob->y))
ob->failmsg (format ("Ouch, you hit your head while climbing the %s! H", query_name (wall)));
//TODO: reduce health
else if (ob->blocked (wall_up, wall->x, wall->y))
ob->failmsg (format ("You try to climb up, but the %s is too high for you! H", query_name (wall)));
//TODO: reduce health
else
{
ob->statusmsg (format ("You successfully climb up the %s.", query_name (wall)));
// here we assume that ob is a player...
ob->enter_map (wall_up, wall->x, wall->y);
}
}
else
ob->failmsg (format ("You try to climb the %s, but you fall down! H", query_name (wall)));
}
else
ob->failmsg (format ("You fail to climb up the %s! H", query_name (wall)));
return;
}
if (ob->contr->ns->bumpmsg)
{
ob->play_sound (sound_find ("bump_wall"));
ob->statusmsg (visible
? format ("You bump into the %s.", query_name (wall))
: "You bump into something."
);
}
}
/////////////////////////////////////////////////////////////////////////////
physics_queue::physics_queue ()
{
i = 0;
}
physics_queue::~physics_queue ()
{
while (object *ob = pop ())
ob->refcnt_dec ();
}
object *
physics_queue::pop ()
{
if (expect_true (i < size ()))
return (*this)[i++];
i = 0;
clear (); //TODO: this frees, but we do not want to free, unless really a lot of objects were queued
return 0;
}
inline unsigned int
queue_of (tick_t tick)
{
return tick & (PHYSICS_QUEUES - 1);
}
void update_physics (maptile *m, int x, int y)
{
}
// handle physics updates
void move_physics (object *ob)
{
}
// preliminary slots docs
// level - water level 1..7
// value - pressure
//
static object *
flow_to (maptile *m, sint16 x, sint16 y)
{
if (!xy_normalise (m, x, y))
return 0;
mapspace &ms = m->at (x, y);
ms.update ();
if (ms.move_block & MOVE_WALK)
return 0;
for (object *w = ms.top; w; w = w->below)
if (w->type == PHYSICS)
if (w->subtype == ST_WATER_SOURCE)
return 0;
else if (w->subtype == ST_WATER_FLOW)
return w;
printf ("creating flow at %d,%d\n", x, y);//D
object *w = archetype::get (shstr_quad_water_flow);
w->level = 0;
w->value = 0;
m->insert (w, x, y);
m->queue_physics (w, 1);
return w;
}
#if 0
static int
find_ortho_flow (object *src, object **result)
{
object **res = result;
for (int dir = 1; dir < 8; dir += 2)
{
mapxy pos (src->map, src->x, src->y);
pos.move (dir);
if (pos.normalise ())
if (object *w = flow_to (pos.m, pos.x, pos.y))
*res++ = w;
}
return res - result;
}
#endif
static bool
water_set (object *w, int level, int pressure = -1)
{
if (!level)
{
w->destroy ();
return true;
}
bool activity = false;
min_it (level, 7);
if (level != w->level)
{
w->level = level;
// update face
activity = true;
}
if (pressure != w->value && pressure >= 0)
{
w->value = pressure;
activity = true;
}
if (activity) // this is overkill, but...
w->map->queue_physics_at (w->x, w->y);
return activity;
}
static void
run_physics (object *ob)
{
//TODO: behaviour objects with virtual methods?
switch (ob->subtype)
{
case ST_WATER_SOURCE:
{
bool activity = false;
// water sources generate flowing water "everywhere", but are currently
// very simple otherwise.
if (!has_floor (ob->map, ob->x, ob->y) && ob->map->tile_path [TILE_DOWN])
if (maptile *m = ob->map->tile_available (TILE_DOWN))
{
if (object *w = flow_to (m, ob->x, ob->y))
activity = activity || water_set (w, 7);
}
else
activity = true;
#if 0
object *w [4];
int w_cnt = find_ortho_flow (ob, w);
while (w_cnt--)
activity = activity || water_set (w [w_cnt], 7);
if (activity)
ob->map->queue_physics (ob, 1);
#endif
}
break;
case ST_WATER_FLOW:
#if 0
// flow down as much as possible
if (!has_floor (ob->map, ob->x, ob->y) && ob->map->tile_path [TILE_DOWN])
if (maptile *m = ob->map->tile_available (TILE_DOWN))
{
if (object *w = flow_to (m, ob->x, ob->y))
{
if (int flow = min (ob->level, 7 - w->level))
{
water_set (w, w->level + flow);
water_set (ob, w->level - flow);
if (!ob->level)
return; // we escaped downwards
}
else if (ob->level >= 5) // exert pressure -> does not count as activity
water_set (w, w->level, ob->value - 1);
}
}
if (ob->level >= 2)
{
object *ws [4];
int w_cnt = find_ortho_flow (ob, ws);
bool activity = false;
// distribute 1 to every neighbour that has less - should use some random ordering of course
while (w_cnt--)
{
object *w = ws [w_cnt];
if (w->level < ob->level - 1)
{
water_set (w, w->level + 1);
--ob->level;
activity = true;
}
}
if (activity)
ob->map->queue_physics (ob, 1);
}
else if (ob->level)
;
else
ob->destroy ();
#endif
// TODO: flow up with pressure
break;
default:
LOG (llevError, "object with unsupported physics subtype %d: %s\n", ob->subtype, ob->debug_desc ());
break;
}
}
int
maptile::run_physics (tick_t tick, int max_objects)
{
if (state != MAP_ACTIVE)
return 0;
int orig_max_object = max_objects;
physics_queue &q = pq [queue_of (server_tick)];
//if (q.size()) printf ("q %d < %d\n", q.i, (int)q.size());//D
while (object *ob = q.pop ())
{
ob->flag [FLAG_PHYSICS_QUEUE] = false;
ob->refcnt_dec ();
if (ob->map != this)
{
if (!ob->flag [FLAG_FREED])
{
LOG (llevError, "%s: physical object on wrong map\n", ob->debug_desc ());
//ob->map->queue_physics (ob);
}
}
else
{
//printf ("handling ob %s\n", ob->debug_desc());//D
::run_physics (ob);
}
if (--max_objects <= 0)
break;
}
return orig_max_object - max_objects;
}
void
maptile::queue_physics (object *ob, int after)
{
if (!after) after = 14; //D
if (!ob->flag [FLAG_PHYSICS_QUEUE])
{
ob->flag [FLAG_PHYSICS_QUEUE] = true;
ob->refcnt_inc ();
pq [queue_of (server_tick + min (PHYSICS_QUEUES - 2, after))].push_back (ob);
}
}
static void
queue_physics_at (maptile *m, int x, int y)
{
mapspace &ms = m->at (x, y);
for (object *ob = ms.bot; ob; ob = ob->above)
if (ob->type == PHYSICS)
m->queue_physics (ob, 1);
}
// this mapspace has changed - potentially activate dormant physics objects
// in the vicinity. vicinity is the 4 spaces around in the same z layer, and above and below.
// TODO: maybe include diagonals in the same z-layer?
// TODO: do not go through floors?
void
maptile::queue_physics_at (int x, int y)
{
if (maptile *m = tile_available (TILE_DOWN))
::queue_physics_at (m, x, y);
if (maptile *m = tile_available (TILE_UP))
::queue_physics_at (m, x, y);
::queue_physics_at (this, x, y);
for (int dir = 1; dir < 8; dir += 2)
{
mapxy pos (this, x, y);
pos.move (dir);
if (pos.normalise ())
::queue_physics_at (pos.m, pos.x, pos.y);
}
}
void
maptile::activate_physics ()
{
// most of this is total overkill, but better be safe than sorry, eh?
coroapi::cede_to_tick ();
if (maptile *m = tile_map [TILE_UP])
for (mapspace *ms = spaces + size (); ms-- > spaces; )
ms->invalidate (); // invalidate faces, in case...
coroapi::cede_to_tick ();
for (mapspace *ms = spaces + size (); ms-- > spaces; )
for (object *op = ms->bot; op; op = op->above)
if (op->type == PHYSICS)
queue_physics (op, 0);
}
/////////////////////////////////////////////////////////////////////////////
#define FANCY_GRAPHICS 0
static void
gen_quadspace (maptile *m, int mx, int my, int x, int y, int z)
{
vec2d P = vec2d (x, y);
const int deep_sea_z = -200;
static frac2d gen(13);
static frac2d vec_gen1 (6, 2, 0.5, 1);
static frac2d vec_gen2 (6, 2, 0.5, 2);
const float continent_scale = 0.00008;
vec2d perturb_pos = pow (P, vec2d (1.4)) * 1e-5;
vec2d perturb (
vec_gen1.fBm (perturb_pos),
vec_gen2.fBm (perturb_pos)
);
float perturb_perturb = 1 - (P[1] - P[0]) * (1. / 25000 / 2);
perturb_perturb = perturb_perturb * perturb_perturb * 0.4;
perturb *= perturb_perturb;
vec2d P_continent = P * continent_scale + perturb;
static frac2d continent_gen (13, 2.13, 0.5);
float continent = continent_gen.fBm (P_continent) + 0.05f;
float x_gradient = P[0] * (1. / 25000);
float y_gradient = P[1] * (1. / 25000);
float xy_gradient = (P[0] + P[1]) * (0.5 / 25000);
const float N = (25000 - 1) * continent_scale;
// we clip a large border on the perturbed shape, to get irregular coastline
// and then clip a smaller border around the real shape
//continent = border_blend (-1.f, continent, P_continent , N, 400 * continent_scale);
continent = border_blend (-1.f, continent, P * continent_scale + perturb * 0.1, N, 100 * continent_scale);
enum {
T_NONE,
T_OCEAN,
T_RIVER,
T_VALLEY,
T_MOUNTAIN,
T_UNDERGROUND,
T_AIR, // unused
T_ACQUIFER,
} t = T_NONE;
vec3d c;
int h0 = 0; // "water level"
int h = 1000000; // height form heightmap
// the continent increases in height from 0 to ~700 levels in the absence of anything else
// thats about one step every 7 maps.
int base_height = blend (0, 300, xy_gradient, 0.2f, 0.9f);
int river_height = base_height; // * 9 / 10;
// add this to rivers to "dry them out"
float dry_out = max (0.f, lerp (xy_gradient, 0.7f, 1.f, 0.f, 0.3f));
static frac2d river_gen (2);
float river1 = abs (river_gen.fBm (P * 0.001 + perturb * 4)) + dry_out;
float river2 = river_gen.ridgedmultifractal (P * 0.04, 0.8, 10) - y_gradient * 0.2 - 0.16 - dry_out;
float valley = river1 - 0.2f;
static frac2d mountain_gen (6, 2.14, 0.5);
float mountain = mountain_gen.ridgedmultifractal (P * 0.004);
//TODO: mountains should not lower the height, should they?
t = valley < 0 ? T_VALLEY : T_MOUNTAIN;
c = blend0 (vec3d (0, 0.8, 0), vec3d (0.8, 0, 0), valley, 0.1f);
h = blend0 (base_height + continent * 0, base_height + mountain * xy_gradient * 100, valley, 0.1f);
if (river1 < 0.01f)
{
// main rivers - they cut deeply into the mountains (base_height * 0.9f)
// well, silly, they don't
t = T_RIVER;
c = vec3d (0.2, 0.2, 1);
h0 = river_height;
min_it (h, river_height + lerp (river1, 0.f, 0.01f, -10, -1));
}
if (river2 > 0)
{
t = T_RIVER;
c = vec3d (0.2, 0.2, 1);
h0 = river_height;
min_it (h, river_height + max (-5, lerp (river2, 0.01f, 0, -4, -1)));
}
if (continent < 0)
{
t = T_OCEAN;
h0 = 0;
min_it (h, min (continent * 200, -1));
c = vec3d (0, 0, 1);
}
// now we have the base height, and base terrain
#if FANCY_GRAPHICS
z = h; // show the surface, not the given z layer
#endif
max_it (h0, h);
// everything below the surface is underground, or a variant
if (z < h)
t = T_UNDERGROUND;
// put acquifers a bit below the surface, to reduce them leaking out (will still happen)
if (z < h - 3)
{
static frac3d acquifer_gen (4);
float acquifer = acquifer_gen.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.01), 1.003, 2);
if (acquifer > 0.48)
{
t = T_ACQUIFER;
c = vec3d (1,1,1);
}
}
//printf ("+%d+%d %d z %d h %d,%d P%g,%g\n", mx, my, t, z, h,h0, P[0],P[1]);//D
// TODO: caves
// TODO: chees areas
// TODO: minerals
// TODO: monsters
#if FANCY_GRAPHICS
float v = clamp (lerp (h, deep_sea_z, 800, 0.f, 1.f), 0.f, 1.f);
c *= v;
putc (clamp (255 * c[0], 0, 255), stdout);
putc (clamp (255 * c[1], 0, 255), stdout);
putc (clamp (255 * c[2], 0, 255), stdout);
#else
shstr arch_floor = shstr ("quad_open_space");
shstr arch_wall;
// TODO: this is shit - we should never generatea water surface, but only
// water above the surface
switch (t)
{
case T_OCEAN:
if (z < h0)
arch_wall = shstr_quad_water_source;
else if (z == h0)
arch_floor = shstr ("quad_ocean_floor");
break;
case T_RIVER:
if (z < h0)
arch_wall = shstr_quad_water_source;
else if (z == h0)
arch_floor = shstr ("quad_water_floor");
break;
case T_VALLEY:
if (z == h)
arch_floor = shstr ("quad_dirt_floor");
break;
case T_MOUNTAIN:
if (z == h)
arch_floor = shstr ("quad_stone_floor");
break;
case T_UNDERGROUND:
// todo, use a fractal
if (z < h - 10)
{
arch_floor = shstr ("quad_stone_floor");
arch_wall = shstr ("quad_stone_wall");
}
else
{
arch_floor = shstr ("quad_dirt_floor");
arch_wall = shstr ("quad_dirt_wall");
}
break;
case T_ACQUIFER:
arch_wall = shstr_quad_water_source;
break;
default:
abort ();
}
if (arch_floor)
m->insert (archetype::get (arch_floor), mx, my);
if (arch_wall)
m->insert (archetype::get (arch_wall ), mx, my);
#endif
}
void
gen_quadmap (maptile *m, int x, int y, int z)
{
assert (m->width == 50);
assert (m->height == 50);
for (int mx = 0; mx < 50; ++mx)
for (int my = 0; my < 50; ++my)
gen_quadspace (m, mx, my, x + mx, y + my, z);
}
/////////////////////////////////////////////////////////////////////////////
void noise_test ();
void noise_test ()
{
#if 1
int Nw = 700;
printf ("P6 %d %d 255\n", Nw * 3, Nw * 2);
// pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
for (int y = 0; y < Nw; ++y)
{
if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw);//D
for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x * 25000 / Nw, y * 25000 / Nw, 0);
for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 400, y, 0);
for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 22000, y + 2000, 0);
}
for (int y = 0; y < Nw; ++y)
{
if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw+50);//D
for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 1000, y + 22000, 0);
for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 12500, y + 12500, 0);
for (int x = 0; x < Nw; ++x) gen_quadspace (0, 0, 0, x + 22000, y + 22000, 0);
}
//putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout);
//putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout);
//putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout);
//putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout);
//putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout);
//putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout);
//putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout);
//putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout);
#elif 1
int N = 25000;
printf ("P6 %d %d 255\n", N, N);
for (int y = 0; y < N; ++y)
{
if (!(y&63))fprintf (stderr, "y %d\n", y);//D
for (int x = 0; x < N; ++x) gen_quadspace (0, 0, 0, x, y, 0);
}
#else
int N = 200;
//printf ("P6 %d %d 255\n", N, N);
// pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm
for (int z = 0; z < N; ++z)
{
if (!(z&7))fprintf (stderr, "z %d\n", z);//D
for (int y = 0; y < N; ++y)
for (int x = 0; x < N; ++x)
{
#if 0
float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2;
if (z < 64)
v = v * (z * z) / (64 * 64);
if (v <= 0.9)
continue;
#endif
static frac3d gen3 (10);
//float v = gen3.turbulence (vec3d (x * 0.01, y * 0.01, z * 0.01));
float v = gen3.ridgedmultifractal (vec3d (x * 0.001, y * 0.001, z * 0.001), 1.003, 2);
if (v <= 0.48) continue;
float r[4];
int i[4];
r[0] = x;
r[1] = y;
r[2] = z;
r[3] = v;
memcpy (i, r, 16);
i[0] = htonl (i[0]);
i[1] = htonl (i[1]);
i[2] = htonl (i[2]);
i[3] = htonl (i[3]);
fwrite (i, 4*4, 1, stdout);
}
}
#endif
exit (0);
}