git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / client / water / WaterWaveDistance.cpp
blob69851170ec2e0793efdb64aa618caafd8ff71ecc
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <water/WaterWaveDistance.h>
22 #include <client/ScorchedClient.h>
23 #include <landscapemap/LandscapeMaps.h>
24 #include <lang/LangResource.h>
25 #include <common/ProgressCounter.h>
27 WaterWaveDistance::WaterWaveDistance() : waveDistance_(0)
31 WaterWaveDistance::~WaterWaveDistance()
35 void WaterWaveDistance::generate(
36 int mapWidth, int mapHeight, float waterHeight,
37 ProgressCounter *counter)
39 if (counter) counter->setNewOp(LANG_RESOURCE("WAVES_DISTANCE", "Waves Distance"));
41 // Wave distance
42 distanceWidthMult_ = 4;
43 distanceHeightMult_ = 4;
44 distanceWidth_ = mapWidth / distanceWidthMult_;
45 distanceHeight_ = mapHeight / distanceHeightMult_;
47 delete [] waveDistance_;
48 waveDistance_ = new float[distanceWidth_ * distanceHeight_];
49 for (int a=0; a<distanceWidth_; a++)
51 for (int b=0; b<distanceHeight_; b++)
53 waveDistance_[a + b * distanceWidth_] = 255.0f;
57 const int skip = distanceWidthMult_;
58 for (int y=0; y<mapHeight; y+=skip)
60 if (counter) counter->setNewPercentage(float(y)/
61 float(mapHeight)*100.0f);
63 for (int x=0; x<mapWidth; x+=skip)
65 float height =
66 ScorchedClient::instance()->getLandscapeMaps().
67 getGroundMaps().getHeight(x, y).asFloat();
68 if (height > waterHeight - 4.0f && height < waterHeight)
70 float height2=
71 ScorchedClient::instance()->getLandscapeMaps().
72 getGroundMaps().getHeight(x+skip, y).asFloat();
73 float height3=
74 ScorchedClient::instance()->getLandscapeMaps().
75 getGroundMaps().getHeight(x-skip, y).asFloat();
76 float height4=
77 ScorchedClient::instance()->getLandscapeMaps().
78 getGroundMaps().getHeight(x, y+skip).asFloat();
79 float height5=
80 ScorchedClient::instance()->getLandscapeMaps().
81 getGroundMaps().getHeight(x, y-skip).asFloat();
83 if (height2 > waterHeight || height3 > waterHeight ||
84 height4 > waterHeight || height5 > waterHeight)
86 Vector posA((float) x, (float) y, 0.0f);
87 for (int b=0; b<distanceHeight_; b++)
89 for (int a=0; a<distanceWidth_; a++)
91 Vector posB(
92 float(float(a) / float(distanceWidth_) * float(mapWidth)),
93 float(float(b) / float(distanceHeight_) * float(mapHeight)), 0.0f);
94 float distance = (posB - posA).Magnitude();
95 waveDistance_[a + b * distanceWidth_] = MIN(waveDistance_[a + b * distanceWidth_], distance);
105 float WaterWaveDistance::getWaveDistance(int posx, int posy)
107 int a = int(posx) / distanceWidthMult_;
108 int b = int(posy) / distanceHeightMult_;
109 int x = MAX(0, MIN(a, (distanceWidth_ - 1)));
110 int y = MAX(0, MIN(b, (distanceHeight_ - 1)));
112 float distance = waveDistance_[x + y * distanceWidth_];
113 distance += fabsf(float(a - x)) + fabsf(float(b - y));
114 return distance;