1. Shamus Young
  2. Frontier

Source

Frontier / Terrain / CPage.cpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*-----------------------------------------------------------------------------

  CPage.cpp


-------------------------------------------------------------------------------

  The Page class is used to generate and cache pages of world texture data.  

  The pages are generated by combining the topographical data (elevations)
  with the region data (modifying the evevation to make the different land
  formations) and then is used to generate the table of surface data, which
  describes how to paint the textures for the given area.
 
-----------------------------------------------------------------------------*/

#include "stdafx.h"
#include "cpage.h"
#include "ctree.h"
#include "entropy.h"
#include "file.h"
#include "game.h"
#include "sdl.h"
#include "world.h"

#define SAVE_INTERVAL     1000

static unsigned     save_cooldown;

/*-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------*/

static char* page_file_name (GLcoord p)
{

  static char     name[256];

  sprintf (name, "%s//cache%d-%d.pag", GameDirectory (), p.x, p.y);
  return name;

}


/*-----------------------------------------------------------------------------
 
-----------------------------------------------------------------------------*/

SurfaceType CPage::Surface (int x, int y)
{

  _last_touched = SdlTick ();
  return (SurfaceType)_cell[x][y].surface;

}

bool CPage::Ready ()
{

  _last_touched = SdlTick ();
  return _stage == PAGE_STAGE_DONE;

}

GLvector CPage::Position (int x, int y)
{

  _last_touched = SdlTick ();
  return glVector ((float)(x + _origin.x * PAGE_SIZE), (float)(y + _origin.y * PAGE_SIZE), _cell[(x % PAGE_SIZE)][(y % PAGE_SIZE)].elevation);

}

GLvector CPage::Normal (int x, int y)
{

  _last_touched = SdlTick ();
  return _cell[(x % PAGE_SIZE)][(y % PAGE_SIZE)].normal;

}

GLrgba CPage::Color (int x, int y)
{

  return _cell[(x % PAGE_SIZE)][(y % PAGE_SIZE)].color;

}

void CPage::Render ()
{

  int     elapsed;
  float   n;

  glDisable (GL_TEXTURE_2D);
  glDisable (GL_LIGHTING);
  elapsed = SdlTick () - _last_touched;
  n = (float)elapsed / PAGE_EXPIRE;
  n = clamp (n, 0.0f, 1.0f);
  glColor3f (n, 1.0f - n, 0.0f);
  _bbox.Render ();

}


unsigned CPage::Tree (int x, int y)
{

  _last_touched = SdlTick ();
  return _cell[x][y].tree_id;

}

float CPage::Elevation (int x, int y)
{

  _last_touched = SdlTick ();
  return _cell[x][y].elevation;

}

float CPage::Detail (int x, int y)
{

  _last_touched = SdlTick ();
  return _cell[x][y].detail;

}

bool CPage::Expired ()
{

  return (_last_touched + PAGE_EXPIRE) < SdlTick ();

}

void CPage::DoPosition ()
{

  int     world_x, world_y;
  Cell    c;

  world_x = (_origin.x * PAGE_SIZE + _walk.x);
  world_y = (_origin.y * PAGE_SIZE + _walk.y);
  c = WorldCell (world_x, world_y);
  _cell[_walk.x][_walk.y].elevation = c.elevation;
  _cell[_walk.x][_walk.y].detail = c.detail;
  _cell[_walk.x][_walk.y].water_level = c.water_level;
  _cell[_walk.x][_walk.y].tree_id = 0;
  _bbox.ContainPoint (Position (world_x, world_y));
  if (_walk.Walk (PAGE_SIZE))
    _stage++;

}

void CPage::DoColor ()
{

  int     world_x, world_y;

  world_x = (_origin.x * PAGE_SIZE + _walk.x);
  world_y = (_origin.y * PAGE_SIZE + _walk.y);
  if (_cell[_walk.x][_walk.y].surface == SURFACE_GRASS || _cell[_walk.x][_walk.y].surface == SURFACE_GRASS_EDGE)
    _cell[_walk.x][_walk.y].color = WorldColorGet (world_x, world_y, SURFACE_COLOR_GRASS);
  else if (_cell[_walk.x][_walk.y].surface == SURFACE_DIRT || _cell[_walk.x][_walk.y].surface == SURFACE_DIRT_DARK || _cell[_walk.x][_walk.y].surface == SURFACE_FOREST)
    _cell[_walk.x][_walk.y].color = WorldColorGet (world_x, world_y, SURFACE_COLOR_DIRT);
  else if (_cell[_walk.x][_walk.y].surface == SURFACE_SAND || _cell[_walk.x][_walk.y].surface == SURFACE_SAND_DARK)
    _cell[_walk.x][_walk.y].color = WorldColorGet (world_x, world_y, SURFACE_COLOR_SAND);
  else if (_cell[_walk.x][_walk.y].surface == SURFACE_SNOW)
    _cell[_walk.x][_walk.y].color = glRgba (1.0f, 1.0f, 1.0f);
  else 
    _cell[_walk.x][_walk.y].color = WorldColorGet (world_x, world_y, SURFACE_COLOR_ROCK);
  if (_walk.Walk (PAGE_SIZE))
    _stage++;

}


void CPage::DoNormal ()
{

  GLvector        normal_y, normal_x;
  float           world_x, world_y;

  world_x = (float)(_origin.x + _walk.x);
  world_y = (float)(_origin.y + _walk.y);
  if (_walk.x < 1 || _walk.x >= PAGE_SIZE - 1) 
    normal_x = glVector (-1, 0, 0);
  else
    normal_x = glVector (world_x - 1, world_y, _cell[_walk.x - 1][_walk.y].elevation) -
      glVector (world_x + 1, world_y, _cell[_walk.x + 1][_walk.y].elevation);
  if (_walk.y < 1 || _walk.y >= PAGE_SIZE - 1) 
    normal_y = glVector (0, -1, 0);
  else
    normal_y = glVector (world_x, world_y - 1, _cell[_walk.x][_walk.y - 1].elevation) -
      glVector (world_x, world_y, _cell[_walk.x][_walk.y + 1].elevation);
  _cell[_walk.x][_walk.y].normal = glVectorCrossProduct (normal_x, normal_y);
  _cell[_walk.x][_walk.y].normal.z *= NORMAL_SCALING;
  _cell[_walk.x][_walk.y].normal.Normalize ();
  if (_walk.Walk (PAGE_SIZE))
    _stage++;

}


void CPage::DoTrees ()
{

  GLcoord   worldpos;
  Region    region;
  pcell*    c;
  int       x, y;
  GLcoord   plant;
  bool      valid;
  float     best;
  CTree*    tree;

  worldpos.x = _origin.x * PAGE_SIZE + _walk.x;
  worldpos.y = _origin.y * PAGE_SIZE + _walk.y;
  region = WorldRegionFromPosition (worldpos.x, worldpos.y);
  valid = false;

  tree = WorldTree (region.tree_type);
  if (tree->GrowsHigh ())
    best = -99999.9f;
  else
    best = 99999.9f;
  for (x = 0; x < TREE_SPACING - 2; x++) {
    for (y = 0; y < TREE_SPACING - 2; y++) {
      c = &_cell[_walk.x * TREE_SPACING + x][_walk.y * TREE_SPACING + y];
      if (c->surface != SURFACE_GRASS && c->surface != SURFACE_SNOW && c->surface != SURFACE_FOREST)
        continue;
      //Don't spawn trees that might touch water.  Looks odd.
      if (c->elevation < c->water_level + 1.2f)
        continue;
      if (tree->GrowsHigh() && (c->detail + region.tree_threshold) > 1.0f && c->elevation > best) {
        plant.x = _walk.x * TREE_SPACING + x;
        plant.y = _walk.y * TREE_SPACING + y;
        best = c->elevation;
        valid = true;
      }
      if (!tree->GrowsHigh() && (c->detail - region.tree_threshold) < 0.0f && c->elevation < best) {
        plant.x = _walk.x * TREE_SPACING + x;
        plant.y = _walk.y * TREE_SPACING + y;
        best = c->elevation;
        valid = true;
      }
    }
  }
  if (valid) {
    c = &_cell[plant.x][plant.y];
    c->tree_id = region.tree_type;
  }
  if (_walk.Walk (TREE_MAP))
    _stage++;

}


void CPage::DoSurface ()
{

  float     high, low, delta;
  float     fade;
  int       xx, yy;
  int       neighbor_x, neighbor_y;
  GLcoord   worldpos;
  Region    region;
  pcell*    c;

  worldpos.x = _origin.x * PAGE_SIZE + _walk.x;
  worldpos.y = _origin.y * PAGE_SIZE + _walk.y;
  region = WorldRegionFromPosition (worldpos.x, worldpos.y);
  c = &_cell[_walk.x][_walk.y];
  if (_stage == PAGE_STAGE_SURFACE1) {
    //Get the elevation of our neighbors
    high = low = c->elevation;
    for (xx = -2; xx <= 2; xx++) {
      neighbor_x = _walk.x + xx;
      if (neighbor_x < 0 || neighbor_x >= PAGE_SIZE) 
        continue;
      for (yy = -2; yy <= 2; yy++) {
        neighbor_y = _walk.y + yy;
        if (neighbor_y < 0 || neighbor_y >= PAGE_SIZE) 
          continue;
        high = max (high, _cell[neighbor_x][neighbor_y].elevation);
        low = min (low, _cell[neighbor_x][neighbor_y].elevation);
      }
    }
    delta = high - low;
    //Default surface. If the climate can support life, default to grass.
    if (region.temperature > 0.1f && region.moisture > 0.1f)
      c->surface = SURFACE_GRASS;
    else //Too cold or dry
      c->surface = SURFACE_ROCK;
    if (region.climate == CLIMATE_DESERT)
      c->surface = SURFACE_SAND;
    //Sand is only for coastal regions
    if (low <= 2.0f && (region.climate == CLIMATE_COAST))
      c->surface = SURFACE_SAND;
    if (low <= 2.0f && (region.climate == CLIMATE_OCEAN))
      c->surface = SURFACE_SAND;
    //Forests are for... forests?
    if (c->detail < 0.75f && c->detail > 0.25f && (region.climate == CLIMATE_FOREST))
      c->surface = SURFACE_FOREST;
    if (delta >= region.moisture * 6)
      c->surface = SURFACE_DIRT;
    if (low <= region.geo_water && region.climate != CLIMATE_SWAMP)
      c->surface = SURFACE_DIRT;
    if (low <= region.geo_water && region.climate != CLIMATE_SWAMP)
      c->surface = SURFACE_DIRT_DARK;
    //The colder it is, the more surface becomes snow, beginning at the lowest points.
    if (region.temperature < FREEZING) {
      fade = region.temperature / FREEZING;
      if ((1.0f - c->detail) > fade)
        c->surface = SURFACE_SNOW;
    }
    if (low <= 2.5f && (region.climate == CLIMATE_OCEAN))
      c->surface = SURFACE_SAND;
    if (low <= 2.5f && (region.climate == CLIMATE_COAST))
      c->surface = SURFACE_SAND;
    //dirt touched by water is dark
    if (region.climate != CLIMATE_SWAMP) {
      if (c->surface == SURFACE_SAND && low <= 0)
        c->surface = SURFACE_SAND_DARK;
      if (low <= c->water_level)
        c->surface = SURFACE_DIRT_DARK;
    }
    if (delta > 4.0f && region.temperature > 0.0f)
      c->surface = SURFACE_ROCK;
    if ((region.climate == CLIMATE_DESERT) && c->surface != SURFACE_ROCK)
      c->surface = SURFACE_SAND;
  } else {
    if (c->surface == SURFACE_GRASS && _walk.x > 0 && _walk.x < PAGE_SIZE - 1 && _walk.y > 0 && _walk.y < PAGE_SIZE - 1) {  
      bool all_grass = true;
      for (xx = -1; xx <= 1; xx++) {
        if (!all_grass)
          break;
        for (yy = -1; yy <= 1; yy++) {
          if (_cell[_walk.x + xx][_walk.y + yy].surface != SURFACE_GRASS && _cell[_walk.x + xx][_walk.y + yy].surface != SURFACE_GRASS_EDGE) {
            all_grass = false;
            break;
          }
        }
      }
      if (!all_grass)
        c->surface = SURFACE_GRASS_EDGE;
    }
  }
  if (_walk.Walk (PAGE_SIZE))
    _stage++;
  
}

void CPage::Build (int stop)
{

  while (_stage != PAGE_STAGE_DONE && SdlTick () < stop) {
    switch (_stage) {
    case PAGE_STAGE_BEGIN:
      _stage++;
      break;
    case PAGE_STAGE_POSITION:
      DoPosition ();
      break;
    case PAGE_STAGE_NORMAL:
      DoNormal ();
      break;
    case PAGE_STAGE_SURFACE1:
    case PAGE_STAGE_SURFACE2:
      DoSurface ();
      break;
    case PAGE_STAGE_COLOR:
      DoColor ();
      break;
    case PAGE_STAGE_TREES:
      DoTrees ();
      break;
    case PAGE_STAGE_SAVE:
      Save ();
      return;
    }
  }

}

void CPage::Cache (int origin_x, int origin_y)
{

  _origin.x = origin_x;
  _origin.y = origin_y;
  _stage = PAGE_STAGE_BEGIN;
  _bbox.Clear ();
  if (FileExists (page_file_name (_origin))) {
    char*   buf;
    long    size;
    long    my_size;

    my_size = sizeof (CPage);
    buf = NULL;
    buf = FileBinaryLoad (page_file_name (_origin), &size);
    if (buf && size == my_size) 
      memcpy (this, buf, size);
    if (buf)  
      free (buf);
  }  
  _walk.Clear ();
  _last_touched = SdlTick ();
 
}

void CPage::Save ()
{

  unsigned    now;

  if (!CVarUtils::GetCVar<bool> ("cache.active")) {
    _stage++;
    return;
  }
  now = SdlTick ();
  if (now < save_cooldown)
    return;
  if (_stage < PAGE_STAGE_SAVE)
    return;
  if (_stage == PAGE_STAGE_SAVE)
    _stage++;
  FileSave (page_file_name (_origin), (char*)this, sizeof (CPage));
  save_cooldown = now + SAVE_INTERVAL;

}