/* -*- Objc -*- */ /* * $Id: MapSandHeap.m,v 1.1 2003/08/06 16:23:27 gabby Exp $ * * Copyright (C) 2003 Free Software Foundation, Inc. * * This file is part of GNU Hégémonie. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * 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 GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #include "MapSandHeap.h" @implementation MapSandHeap + (void) createMapSandHeap: (MapTerrain *)heightField withNbElements: (int)nbElements andMaxHeight: (int)maxHeight slope: (int)slope { NSParameterAssert (heightField); NSParameterAssert (nbElements > 0); NSParameterAssert (maxHeight > 1); NSParameterAssert (maxHeight <= 255); NSParameterAssert (slope > 0); int xLength = [heightField xLength]; int zLength = [heightField zLength]; srand (time (0)); int i; for (i = 0; i < nbElements; i++) { //int x = (int) ((double) (xLength - 1) * rand() / (RAND_MAX + 1.0)); //int z = (int) ((double) (zLength - 1) * rand() / (RAND_MAX + 1.0)); int x = (int) ((double) xLength * rand() / (RAND_MAX + 1.0)); int z = (int) ((double) zLength * rand() / (RAND_MAX + 1.0)); [self addSand: heightField atPosition: MakeMapCoord (x, z) slope: slope]; } } + (void) moutainSandHeap: (MapTerrain *)heightField atCenter: (map_coord_t)center withNbElements: (int)nbElements andMaxHeight: (int)maxHeight slope: (int)slope { NSParameterAssert (heightField); NSParameterAssert (nbElements > 0); NSParameterAssert (maxHeight > 1); NSParameterAssert (maxHeight <= 255); NSParameterAssert (slope > 0); int i; for (i = 0; i < nbElements; i++) [self addSand: heightField atPosition: center slope: slope]; } /** * returns YES if pos1 height is higher than pos2 height + slope. */ + (BOOL) _testSlopeBetween: (map_coord_t)pos1 and: (map_coord_t)pos2 slope: (int)slope withHF: (MapTerrain *)heightField { int height1 = [heightField heightAtPosition: pos1]; int height2 = [heightField heightAtPosition: pos2]; return (height1 - height2 > slope); } + (void) addSand: (MapTerrain *)heightField atPosition: (map_coord_t)position slope: (int)slope { BOOL tab[8]; map_coord_t tabPos[8]; int nbElts = 0; int height = [heightField heightAtPosition: position]; if (height < 255) { int pos = 0; int x2, z2; for (z2 = -1; z2 <= 1; z2++) { for (x2 = -1; x2 <= 1; x2++) { if (!(x2 == 0 && z2 == 0)) { map_coord_t position2 = MakeMapCoord (position.x + x2, position.z + z2); tab[pos] = [self _testSlopeBetween: position and: position2 slope: slope withHF: heightField]; tabPos[pos] = position2; if (tab[pos]) nbElts++; pos++; } } } if (nbElts == 0) [heightField setHeightAtPosition: position toHeight: (u_int8_t) (height + 1)]; else { int r = (int) (8.0 * rand() / (RAND_MAX + 1.0)); while (!tab[r]) r = (int) (8.0 * rand() / (RAND_MAX + 1.0)); [self addSand: heightField atPosition: tabPos[r] slope: slope]; } } } + (void) subSand: (MapTerrain *)heightField atPosition: (map_coord_t)position slope: (int)slope { BOOL tab[8]; map_coord_t tabPos[8]; int nbElts = 0; int height = [heightField heightAtPosition: position]; if (height > 0) { int pos = 0; int x2, z2; for (z2 = -1; z2 <= 1; z2++) { for (x2 = -1; x2 <= 1; x2++) { if (!(x2 == 0 && z2 == 0)) { map_coord_t position2 = MakeMapCoord (position.x + x2, position.z + z2); tab[pos] = [self _testSlopeBetween: position2 and: position slope: slope withHF: heightField]; tabPos[pos] = position2; if (tab[pos]) nbElts++; pos++; } } } if (nbElts == 0) [heightField setHeightAtPosition: position toHeight: (u_int8_t) (height - 1)]; else { int r = (int) (8.0 * rand() / (RAND_MAX + 1.0)); while (!tab[r]) r = (int) (8.0 * rand() / (RAND_MAX + 1.0)); [self subSand: heightField atPosition: tabPos[r] slope: slope]; } } } @end