1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
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.
10 //
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.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <weapons/WeaponReference.h>
22 #include <weapons/AccessoryStore.h>
23 #include <common/DefinesString.h>
24
25 REGISTER_ACCESSORY_SOURCE(WeaponReference);
26
WeaponReference()27 WeaponReference::WeaponReference() :
28 refWeapon_(0)
29 {
30
31 }
32
~WeaponReference()33 WeaponReference::~WeaponReference()
34 {
35 delete refWeapon_;
36 refWeapon_ = 0;
37 }
38
parseXML(AccessoryCreateContext & context,XMLNode * accessoryNode)39 bool WeaponReference::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
40 {
41 if (!Weapon::parseXML(context, accessoryNode)) return false;
42
43 // Get the next weapon
44 std::string subNode;
45 if (!accessoryNode->getNamedChild("weapon", subNode)) return false;
46
47 // Find the accessory name
48 std::map<std::string, XMLNode *> &nodes = context.getAccessoryStore().getParsingNodes();
49 std::map<std::string, XMLNode *>::iterator finditor =
50 nodes.find(subNode.c_str());
51 if (finditor == nodes.end())
52 {
53 S3D::dialogMessage("WeaponReference", S3D::formatStringBuffer(
54 "Failed to find weapon \"%s\"",
55 subNode.c_str()));
56 return false;
57 }
58 XMLNode *weaponNode = (*finditor).second;
59 weaponNode->resurrectRemovedChildren();
60
61 // Action
62 XMLNode *actionNode = 0;
63 if (!weaponNode->getNamedChild("accessoryaction", actionNode)) return false;
64
65 // Create the new weapon
66 AccessoryPart *accessory = context.getAccessoryStore().
67 createAccessoryPart(context, parent_, actionNode);
68 if (!accessory || accessory->getType() != AccessoryPart::AccessoryWeapon)
69 {
70 S3D::dialogMessage("Accessory", S3D::formatStringBuffer(
71 "Failed to find create weapon/wrong type \"%s\"",
72 subNode.c_str()));
73 return false;
74 }
75 refWeapon_ = (Weapon*) accessory;
76
77 return true;
78 }
79
fireWeapon(ScorchedContext & context,WeaponFireContext & weaponContext,FixedVector & position,FixedVector & velocity)80 void WeaponReference::fireWeapon(ScorchedContext &context,
81 WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity)
82 {
83 refWeapon_->fire(context, weaponContext, position, velocity);
84 }
85
86