WeaponMirv.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/WeaponMirv.h>
00022 #include <weapons/AccessoryStore.h>
00023 #include <engine/ActionController.h>
00024 #include <common/Defines.h>
00025 
00026 REGISTER_ACCESSORY_SOURCE(WeaponMirv);
00027 
00028 WeaponMirv::WeaponMirv() :
00029         noWarheads_(0), hspreadDist_(0), vspreadDist_(0)
00030 {
00031 
00032 }
00033 
00034 WeaponMirv::~WeaponMirv()
00035 {
00036 
00037 }
00038 
00039 bool WeaponMirv::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
00040 {
00041         if (!Weapon::parseXML(context, accessoryNode)) return false;
00042 
00043         // Get the accessory spread
00044         if (!accessoryNode->getNamedChild("hspreaddist", hspreadDist_)) 
00045                 return false;
00046         if (!accessoryNode->getNamedChild("vspreaddist", vspreadDist_)) 
00047                 return false;
00048 
00049         // Get the next weapon
00050         XMLNode *subNode = 0;
00051         if (!accessoryNode->getNamedChild("aimedweapon", subNode)) return false;
00052 
00053         // Check next weapon is correct type
00054         AccessoryPart *accessory = context.getAccessoryStore().
00055                 createAccessoryPart(context, parent_, subNode);
00056         if (!accessory || accessory->getType() != AccessoryPart::AccessoryWeapon)
00057         {
00058                 return subNode->returnError("Failed to find sub weapon, not a weapon");
00059         }
00060         aimedWeapon_ = (Weapon*) accessory;
00061 
00062         // Get the accessory warheads
00063         if (!accessoryNode->getNamedChild("nowarheads", noWarheads_)) return false;
00064 
00065         return true;
00066 }
00067 
00068 void WeaponMirv::fireWeapon(ScorchedContext &context,
00069         WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity)
00070 {
00071         // Add a shot that will fall where the original was aimed
00072         aimedWeapon_->fireWeapon(context, weaponContext, position, velocity);
00073 
00074         RandomGenerator &random = context.getActionController().getRandom();
00075 
00076         // Add all of the sub warheads that have a random spread
00077         fixed hspreadDist;
00078         for (int i=0; i<noWarheads_ - 1; i++)
00079         {
00080                 FixedVector newDiff = velocity;
00081                 newDiff[2] = 0;
00082                 // Ensure the same value is used in all parts of the calc
00083                 hspreadDist = hspreadDist_.getValue(context);
00084                 if (hspreadDist != 0)
00085                 {
00086                         FixedVector diff = newDiff;
00087                         diff[2] -= 1;
00088                         FixedVector perp = newDiff * diff;
00089 
00090                         newDiff += (perp * ((random.getRandFixed() * hspreadDist) - (hspreadDist * fixed(true, 5000))));
00091                 }
00092                 newDiff[2] += (fixed(i - (noWarheads_ / 2)) / 
00093                         fixed(noWarheads_ / 2)) * vspreadDist_.getValue(context);
00094 
00095                 aimedWeapon_->fireWeapon(context, weaponContext, position, newDiff);
00096         }
00097 }
00098 

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