Merge from development branch heightmap to main.
[scorched3d.git] / src / common / weapons / WeaponMirv.cpp
blobb8a2e99ab30638a831a907a7a8e3665fb7cebb8e
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2003
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/WeaponMirv.h>
22 #include <weapons/AccessoryStore.h>
23 #include <engine/ActionController.h>
24 #include <common/Defines.h>
26 REGISTER_ACCESSORY_SOURCE(WeaponMirv);
28 WeaponMirv::WeaponMirv() :
29 noWarheads_(0), hspreadDist_(0), vspreadDist_(0)
34 WeaponMirv::~WeaponMirv()
39 bool WeaponMirv::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
41 if (!Weapon::parseXML(context, accessoryNode)) return false;
43 // Get the accessory spread
44 if (!accessoryNode->getNamedChild("hspreaddist", hspreadDist_))
45 return false;
46 if (!accessoryNode->getNamedChild("vspreaddist", vspreadDist_))
47 return false;
49 // Get the next weapon
50 XMLNode *subNode = 0;
51 if (!accessoryNode->getNamedChild("aimedweapon", subNode)) return false;
53 // Check next weapon is correct type
54 AccessoryPart *accessory = context.getAccessoryStore().
55 createAccessoryPart(context, parent_, subNode);
56 if (!accessory || accessory->getType() != AccessoryPart::AccessoryWeapon)
58 return subNode->returnError("Failed to find sub weapon, not a weapon");
60 aimedWeapon_ = (Weapon*) accessory;
62 // Get the accessory warheads
63 if (!accessoryNode->getNamedChild("nowarheads", noWarheads_)) return false;
65 return true;
68 void WeaponMirv::fireWeapon(ScorchedContext &context,
69 WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity)
71 // Add a shot that will fall where the original was aimed
72 aimedWeapon_->fireWeapon(context, weaponContext, position, velocity);
74 RandomGenerator &random = context.getActionController().getRandom();
76 // Add all of the sub warheads that have a random spread
77 fixed hspreadDist;
78 for (int i=0; i<noWarheads_ - 1; i++)
80 FixedVector newDiff = velocity;
81 newDiff[2] = 0;
82 // Ensure the same value is used in all parts of the calc
83 hspreadDist = hspreadDist_.getValue(context);
84 if (hspreadDist != 0)
86 FixedVector diff = newDiff;
87 diff[2] -= 1;
88 FixedVector perp = newDiff * diff;
90 newDiff += (perp * ((random.getRandFixed() * hspreadDist) - (hspreadDist * fixed(true, 5000))));
92 newDiff[2] += (fixed(i - (noWarheads_ / 2)) /
93 fixed(noWarheads_ / 2)) * vspreadDist_.getValue(context);
95 aimedWeapon_->fireWeapon(context, weaponContext, position, newDiff);