WeaponAimedUnder.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <weapons/WeaponAimedUnder.h>
00022 #include <weapons/AccessoryStore.h>
00023 #include <engine/ActionController.h>
00024 #include <landscapemap/LandscapeMaps.h>
00025 #include <tank/TankLib.h>
00026 #include <tank/Tank.h>
00027 #include <tank/TankPosition.h>
00028 #include <common/Defines.h>
00029 #include <list>
00030 #include <math.h>
00031 
00032 REGISTER_ACCESSORY_SOURCE(WeaponAimedUnder);
00033 
00034 WeaponAimedUnder::WeaponAimedUnder() : warHeads_(0), moveUnderground_(true)
00035 {
00036 
00037 }
00038 
00039 WeaponAimedUnder::~WeaponAimedUnder()
00040 {
00041 
00042 }
00043 
00044 bool WeaponAimedUnder::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
00045 {
00046         if (!Weapon::parseXML(context, accessoryNode)) return false;
00047 
00048         // Get the accessory size
00049         if (!accessoryNode->getNamedChild("nowarheads", warHeads_)) return false;
00050 
00051         // Get the next weapon
00052         XMLNode *subNode = 0;
00053         if (!accessoryNode->getNamedChild("aimedweapon", subNode)) return false;
00054 
00055         // Check next weapon is correct type
00056         AccessoryPart *accessory = context.getAccessoryStore().
00057                 createAccessoryPart(context, parent_, subNode);
00058         if (!accessory || accessory->getType() != AccessoryPart::AccessoryWeapon)
00059         {
00060                 return subNode->returnError("Failed to find sub weapon, not a weapon");
00061         }
00062         aimedWeapon_ = (Weapon*) accessory;
00063         
00064         // Get the accessory aimed distance
00065         if (!accessoryNode->getNamedChild("maxaimdistance", maxAimedDistance_)) return false;
00066         
00067         // Get the accessory percentage miss chance
00068         if (!accessoryNode->getNamedChild("percentagemiss", percentageMissChance_)) return false;
00069 
00070         // Get the accessory percentage miss chance
00071         if (!accessoryNode->getNamedChild("inaccuracy", maxInacuracy_)) return false;
00072 
00073         // Get optional moveunderground attribute
00074         accessoryNode->getNamedChild("moveunderground", moveUnderground_, false);
00075 
00076         return true;
00077 }
00078 
00079 void WeaponAimedUnder::fireWeapon(ScorchedContext &context,
00080         WeaponFireContext &weaponContext, FixedVector &position, FixedVector &oldvelocity)
00081 {
00082         // NOTE: This code is very similar to the funky bomb code
00083         // except it works under ground
00084         if (moveUnderground_)
00085         {
00086                 fixed height = context.getLandscapeMaps().getGroundMaps().
00087                         getInterpHeight(position[0], position[1]);
00088                 if (position[2] < height + 1)
00089                 {
00090                         position[2] = context.getLandscapeMaps().getGroundMaps().
00091                                 getInterpHeight(position[0], position[1]) / 2;
00092                 }
00093         }
00094 
00095         // Get all of the distances of the tanks less than 50 away
00096         std::list<std::pair<fixed, Tank *> > sortedTanks;
00097         TankLib::getTanksSortedByDistance(
00098                 context,
00099                 position, 
00100                 sortedTanks,
00101                 0,
00102                 maxAimedDistance_.getValue(context));
00103 
00104         // Add all of these distances together
00105         fixed totalDist = 0;
00106         std::list<std::pair<fixed, Tank *> >::iterator itor;
00107         for (itor = sortedTanks.begin();
00108                 itor != sortedTanks.end();
00109                 itor++)
00110         {
00111                 totalDist += (*itor).first;
00112         }
00113 
00114         // Turn distance into a probablity that we will fire a the tank
00115         fixed maxDist = 0;
00116         if (sortedTanks.size() == 1)
00117         {
00118                 maxDist = totalDist;
00119         }
00120         else
00121         {
00122                 for (itor = sortedTanks.begin();
00123                         itor != sortedTanks.end();
00124                         itor++)
00125                 {
00126                         (*itor).first = totalDist - (*itor).first;
00127                         maxDist += (*itor).first;
00128                 }
00129         }
00130         
00131         // Add a percetage that we will not fire at any tank
00132         maxDist *= (percentageMissChance_.getValue(context)/ 100) + 1;
00133 
00134         RandomGenerator &random = context.getActionController().getRandom();
00135 
00136         // For each war head
00137         for (int i=0; i<warHeads_; i++)
00138         {
00139                 // Random probablity
00140                 fixed dist = maxDist * random.getRandFixed();
00141 
00142                 // Find which tank fits this probability
00143                 Tank *shootAt = 0;
00144                 fixed distC = 0;
00145                 for (itor = sortedTanks.begin();
00146                         itor != sortedTanks.end();
00147                         itor++)
00148                 {
00149                         distC += (*itor).first;
00150                         if (dist < distC)
00151                         {
00152                                 shootAt = (*itor).second;
00153                                 break;
00154                         }
00155                 }                       
00156 
00157                 // Calcuate the angle for the shot
00158                 fixed angleXYDegs = random.getRandFixed() * 360;
00159                 fixed angleYZDegs = random.getRandFixed() * 30 + 50;
00160                 fixed power = 1000;
00161                 if (shootAt)
00162                 {
00163                         // We have a tank to aim at
00164                         // Aim a shot towards it
00165                         TankLib::getSniperShotTowardsPosition(
00166                                 context,
00167                                 position, 
00168                                 shootAt->getPosition().getTankPosition(), -1, 
00169                                 angleXYDegs, angleYZDegs, power);
00170 
00171                         angleXYDegs += (random.getRandFixed() * maxInacuracy_.getValue(context)) - 
00172                                 (maxInacuracy_.getValue(context) / 2);
00173                         angleYZDegs += (random.getRandFixed() * maxInacuracy_.getValue(context)) - 
00174                                 (maxInacuracy_.getValue(context) / 2);
00175                 }
00176 
00177                 // Create the shot
00178                 FixedVector &velocity = TankLib::getVelocityVector(
00179                         angleXYDegs, angleYZDegs);
00180                 velocity *= power;
00181 
00182                 aimedWeapon_->fireWeapon(context, weaponContext, position, velocity);
00183         }
00184 }

Generated on Mon Feb 16 15:14:52 2009 for Scorched3D by  doxygen 1.5.3