git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / weapons / WeaponAimedUnder.cpp
blob39bd6b5244c48a490dfddfbf713f8a480319309c
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 <weapons/WeaponAimedUnder.h>
22 #include <weapons/AccessoryStore.h>
23 #include <engine/ActionController.h>
24 #include <landscapemap/LandscapeMaps.h>
25 #include <tank/TankLib.h>
26 #include <tank/Tank.h>
27 #include <tank/TankPosition.h>
28 #include <common/Defines.h>
29 #include <list>
30 #include <math.h>
32 REGISTER_ACCESSORY_SOURCE(WeaponAimedUnder);
34 WeaponAimedUnder::WeaponAimedUnder() : warHeads_(0), moveUnderground_(true)
39 WeaponAimedUnder::~WeaponAimedUnder()
44 bool WeaponAimedUnder::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
46 if (!Weapon::parseXML(context, accessoryNode)) return false;
48 // Get the accessory size
49 if (!accessoryNode->getNamedChild("nowarheads", warHeads_)) return false;
51 // Get the next weapon
52 XMLNode *subNode = 0;
53 if (!accessoryNode->getNamedChild("aimedweapon", subNode)) return false;
55 // Check next weapon is correct type
56 AccessoryPart *accessory = context.getAccessoryStore().
57 createAccessoryPart(context, parent_, subNode);
58 if (!accessory || accessory->getType() != AccessoryPart::AccessoryWeapon)
60 return subNode->returnError("Failed to find sub weapon, not a weapon");
62 aimedWeapon_ = (Weapon*) accessory;
64 // Get the accessory aimed distance
65 if (!accessoryNode->getNamedChild("maxaimdistance", maxAimedDistance_)) return false;
67 // Get the accessory percentage miss chance
68 if (!accessoryNode->getNamedChild("percentagemiss", percentageMissChance_)) return false;
70 // Get the accessory percentage miss chance
71 if (!accessoryNode->getNamedChild("inaccuracy", maxInacuracy_)) return false;
73 // Get optional moveunderground attribute
74 accessoryNode->getNamedChild("moveunderground", moveUnderground_, false);
76 return true;
79 void WeaponAimedUnder::fireWeapon(ScorchedContext &context,
80 WeaponFireContext &weaponContext, FixedVector &position, FixedVector &oldvelocity)
82 // NOTE: This code is very similar to the funky bomb code
83 // except it works under ground
84 if (moveUnderground_)
86 fixed height = context.getLandscapeMaps().getGroundMaps().
87 getInterpHeight(position[0], position[1]);
88 if (position[2] < height + 1)
90 position[2] = context.getLandscapeMaps().getGroundMaps().
91 getInterpHeight(position[0], position[1]) / 2;
95 // Get all of the distances of the tanks less than 50 away
96 std::list<std::pair<fixed, Tank *> > sortedTanks;
97 TankLib::getTanksSortedByDistance(
98 context,
99 position,
100 sortedTanks,
102 maxAimedDistance_.getValue(context));
104 // Add all of these distances together
105 fixed totalDist = 0;
106 std::list<std::pair<fixed, Tank *> >::iterator itor;
107 for (itor = sortedTanks.begin();
108 itor != sortedTanks.end();
109 itor++)
111 totalDist += (*itor).first;
114 // Turn distance into a probablity that we will fire a the tank
115 fixed maxDist = 0;
116 if (sortedTanks.size() == 1)
118 maxDist = totalDist;
120 else
122 for (itor = sortedTanks.begin();
123 itor != sortedTanks.end();
124 itor++)
126 (*itor).first = totalDist - (*itor).first;
127 maxDist += (*itor).first;
131 // Add a percetage that we will not fire at any tank
132 maxDist *= (percentageMissChance_.getValue(context)/ 100) + 1;
134 RandomGenerator &random = context.getActionController().getRandom();
136 // For each war head
137 for (int i=0; i<warHeads_; i++)
139 // Random probablity
140 fixed dist = maxDist * random.getRandFixed();
142 // Find which tank fits this probability
143 Tank *shootAt = 0;
144 fixed distC = 0;
145 for (itor = sortedTanks.begin();
146 itor != sortedTanks.end();
147 itor++)
149 distC += (*itor).first;
150 if (dist < distC)
152 shootAt = (*itor).second;
153 break;
157 // Calcuate the angle for the shot
158 fixed angleXYDegs = random.getRandFixed() * 360;
159 fixed angleYZDegs = random.getRandFixed() * 30 + 50;
160 fixed power = 1000;
161 if (shootAt)
163 // We have a tank to aim at
164 // Aim a shot towards it
165 TankLib::getSniperShotTowardsPosition(
166 context,
167 position,
168 shootAt->getPosition().getTankPosition(), -1,
169 angleXYDegs, angleYZDegs, power);
171 angleXYDegs += (random.getRandFixed() * maxInacuracy_.getValue(context)) -
172 (maxInacuracy_.getValue(context) / 2);
173 angleYZDegs += (random.getRandFixed() * maxInacuracy_.getValue(context)) -
174 (maxInacuracy_.getValue(context) / 2);
177 // Create the shot
178 FixedVector &velocity = TankLib::getVelocityVector(
179 angleXYDegs, angleYZDegs);
180 velocity *= power;
182 aimedWeapon_->fireWeapon(context, weaponContext, position, velocity);