git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / placement / PlacementTypeMask.cpp
blob8ebfa83e9c6d17df327c087c5d337f76b1dc38c9
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 <placement/PlacementTypeMask.h>
22 #include <landscapemap/LandscapeMaps.h>
23 #include <engine/ScorchedContext.h>
24 #include <common/ProgressCounter.h>
25 #include <common/RandomGenerator.h>
26 #include <common/Defines.h>
27 #include <image/ImageFactory.h>
28 #include <XML/XMLParser.h>
30 PlacementTypeMask::PlacementTypeMask()
34 PlacementTypeMask::~PlacementTypeMask()
38 bool PlacementTypeMask::readXML(XMLNode *node)
40 if (!node->getNamedChild("numobjects", numobjects)) return false;
41 if (!node->getNamedChild("mask", mask)) return false;
42 if (!node->getNamedChild("minheight", minheight)) return false;
43 if (!node->getNamedChild("maxheight", maxheight)) return false;
44 if (!node->getNamedChild("mincloseness", mincloseness)) return false;
45 if (!node->getNamedChild("minslope", minslope)) return false;
46 if (!node->getNamedChild("xsnap", xsnap)) return false;
47 if (!node->getNamedChild("ysnap", ysnap)) return false;
48 return PlacementType::readXML(node);
51 void PlacementTypeMask::getPositions(ScorchedContext &context,
52 RandomGenerator &generator,
53 std::list<Position> &returnPositions,
54 ProgressCounter *counter)
56 int groundMapWidth = context.getLandscapeMaps().getGroundMaps().getLandscapeWidth();
57 int groundMapHeight = context.getLandscapeMaps().getGroundMaps().getLandscapeHeight();
59 ImageHandle map = ImageFactory::loadImageHandle(S3D::getDataFile(mask.c_str()));
60 if (!map.getBits())
62 S3D::dialogExit("PlacementTypeMask",
63 S3D::formatStringBuffer("Error: failed to find mask \"%s\"",
64 mask.c_str()));
67 const int NoIterations = numobjects;
68 int objectCount = 0;
69 for (int i=0; i<NoIterations; i++)
71 if (i % 1000 == 0) if (counter)
72 counter->setNewPercentage(float(i)/float(NoIterations)*100.0f);
74 fixed lx = generator.getRandFixed() * fixed(groundMapWidth);
75 fixed ly = generator.getRandFixed() * fixed(groundMapHeight);
77 if (xsnap > 0)
79 lx = fixed((lx / xsnap).asInt()) * xsnap;
81 if (ysnap > 0)
83 ly = fixed((ly / ysnap).asInt()) * ysnap;
85 lx = MIN(MAX(fixed(0), lx), fixed(groundMapWidth));
86 ly = MIN(MAX(fixed(0), ly), fixed(groundMapHeight));
88 fixed height =
89 context.getLandscapeMaps().getGroundMaps().getInterpHeight(lx, ly);
90 FixedVector normal;
91 context.getLandscapeMaps().
92 getGroundMaps().getInterpNormal(lx, ly, normal);
93 if (height > minheight &&
94 height < maxheight &&
95 normal[2] > minslope)
97 int mx = (map.getWidth() * lx.asInt()) / groundMapWidth;
98 int my = (map.getHeight() * ly.asInt()) / groundMapHeight;
99 unsigned char *bits = map.getBits() +
100 mx * 3 + my * map.getWidth() * 3;
101 if (bits[0] > 127)
103 Position position;
104 position.position[0] = lx;
105 position.position[1] = ly;
106 position.position[2] = height;
108 if (checkCloseness(position.position, context,
109 returnPositions, mincloseness))
111 returnPositions.push_back(position);