CWater* lua_towater ( lua_State* luaVM, int iArgument ) { CElement* pElement = lua_toelement ( luaVM, iArgument ); if ( pElement && IS_WATER ( pElement ) ) return static_cast < CWater* > ( pElement ); else return NULL; }
static void overfill_lake(int x, int y, Shoreline * shore, int lake_id) { // Starting point is a local minimum // Lake growth is done iteratively by flooding the lowest shore point and rising water level // shore point = neighbour without water // (at this point we have no water in the map, except other lakes and rivers) // // We have a list of shore points sorted by altitude int i, level; if (is_border(x, y)) return; set_river_tile(x, y); level = ALT(x, y); // find neighbours for (i = 0; i < 8; i++) { if (in_map(x + di[i], y + dj[i]) && !IS_WATER(x + di[i], y + dj[i])) try_shore_point(x + di[i], y + dj[i], shore); } if (shore->next != NULL) { shore = shore->next; x = shore->x; y = shore->y; if ((ALT(x, y) < level)) { set_river_tile(x, y); // create river and continue to build shoreline // we will continue to overfill (from a lower point) until we reach border of the map //fprintf(stdout, "We found a pass x %i, y %i, alt %i \n", x, y, ALT(x,y)); setup_one_river(x, y, lake_id, shore); } overfill_lake(x, y, shore, lake_id); } else { // Q: ? Should this happen ? // A: yes if we are in a lake that was previously filled by a higher one which overfilled here // else ? it should not happen ? //fprintf(stderr,"the shoreline list is empty, x = %i, y = %i\n", x, y); } }
static int setup_one_river(int xx, int yy, int lake_id, Shoreline * shore) { int alt_max, x, y, alt, x0, y0; // start a river from point (xx, yy) set_river_tile(xx, yy); alt_max = ALT(xx, yy); x0 = xx; y0 = yy; /* follow most important slope and go downward */ while (((xx != x) || (yy != y)) && (xx != 0) && (xx != (WORLD_SIDE_LEN - 1)) && (yy != 0) && (yy != WORLD_SIDE_LEN - 1)) { int m = 0; x = xx; y = yy; alt = ALT(x, y); for (int n = 0; n < 8; n++) { if (in_map(x + di[n], y + dj[n])) { if (ALT(x + di[n], y + dj[n]) < alt) { xx = x + di[n]; yy = y + dj[n]; alt = ALT(xx, yy); m = n; } // find neighbours and update shore line if needed // may mark as shoreline a point which will be set as river later. We don't care if (!IS_WATER(x + di[n], y + dj[n])) try_shore_point(x + di[n], y + dj[n], shore); } } set_river_tile(xx, yy); if (m > 3) { // we did diagonal move, so we need to connect river if (ALT(x + di[m], y) > ALT(x, y + dj[m])) set_river_tile(x, y + dj[m]); else set_river_tile(x + di[m], y); } }; // We are in a local minimum or at the borders of the map (strictly the lowest points) }