git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / weapons / WeaponRandomChoice.cpp
blob4b3868e45963f6f4cd83efe62425b26f150743a7
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/WeaponRandomChoice.h>
22 #include <weapons/AccessoryStore.h>
23 #include <engine/ActionController.h>
24 #include <common/Defines.h>
25 #include <stdlib.h>
27 REGISTER_ACCESSORY_SOURCE(WeaponRandomChoice);
29 WeaponRandomChoice::WeaponRandomChoice() :
30 totalWeight_(0)
34 WeaponRandomChoice::~WeaponRandomChoice()
38 bool WeaponRandomChoice::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
40 if (!Weapon::parseXML(context, accessoryNode)) return false;
42 for (int i=1;;i++)
44 // Get the next weaponchoice
45 char buffer[128];
46 snprintf(buffer, 128, "weaponchoice%i", i);
47 XMLNode *subNode = 0;
48 accessoryNode->getNamedChild(buffer, subNode, false);
49 if (!subNode) break;
51 // Get the weight
52 int weight = 0;
53 if (!subNode->getNamedChild("weight", weight)) return false;
55 // Get the weapon
56 XMLNode *weaponNode = 0;
57 if (!subNode->getNamedChild("weapon", weaponNode)) return false;
58 AccessoryPart *accessory = context.getAccessoryStore().
59 createAccessoryPart(context, parent_, weaponNode);
60 if (!accessory || accessory->getType() != AccessoryPart::AccessoryWeapon)
62 return weaponNode->returnError("Failed to find sub weapon, not a weapon");
64 Weapon *weapon = (Weapon*) accessory;
66 // Add this entry
67 WeaponWeight weaponWeight = { weight, weapon };
68 weaponsChoice_.push_back(weaponWeight);
69 totalWeight_ += weight;
71 // Check the node is empty
72 if (!subNode->failChildren()) return false;
75 if (weaponsChoice_.empty())
77 return accessoryNode->returnError(
78 "Must provide at least one random choice");
81 return accessoryNode->failChildren();
84 void WeaponRandomChoice::fireWeapon(ScorchedContext &context,
85 WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity)
87 RandomGenerator &random = context.getActionController().getRandom();
88 unsigned int randWeight = random.getRandUInt() % totalWeight_;
89 unsigned int currentWeight = 0;
91 std::list<WeaponWeight>::iterator itor;
92 for (itor = weaponsChoice_.begin();
93 itor != weaponsChoice_.end();
94 itor++)
96 WeaponWeight &weightEntry = *itor;
97 currentWeight += weightEntry.weight;
99 if (currentWeight >= randWeight)
101 Weapon *weapon = weightEntry.weapon;
102 weapon->fireWeapon(context, weaponContext, position, velocity);
103 break;