git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / weapons / WeaponGiveAccessory.cpp
blob84203728b239caab086b8570c41ffb4010b076ae
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/WeaponGiveAccessory.h>
22 #include <weapons/AccessoryStore.h>
23 #include <engine/ActionController.h>
24 #include <tank/TankContainer.h>
25 #include <tank/TankAccessories.h>
26 #include <tank/TankScore.h>
27 #include <common/Defines.h>
28 #include <common/ChannelManager.h>
29 #include <lang/LangResource.h>
31 REGISTER_ACCESSORY_SOURCE(WeaponGiveAccessory);
33 WeaponGiveAccessory::WeaponGiveAccessory()
38 WeaponGiveAccessory::~WeaponGiveAccessory()
43 bool WeaponGiveAccessory::parseXML(AccessoryCreateContext &context, XMLNode *accessoryNode)
45 if (!Weapon::parseXML(context, accessoryNode)) return false;
47 std::string giveaccessory;
48 while (accessoryNode->getNamedChild("giveaccessory", giveaccessory, false))
50 Accessory *accessory = context.getAccessoryStore().
51 findByPrimaryAccessoryName(giveaccessory.c_str());
52 if (!accessory)
54 return accessoryNode->returnError(
55 S3D::formatStringBuffer("Failed to find accessory named %s",
56 giveaccessory.c_str()));
58 giveAccessories_.push_back(accessory);
61 if (!accessoryNode->getNamedChild("number", number_)) return false;
63 return true;
66 void WeaponGiveAccessory::fireWeapon(ScorchedContext &context,
67 WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity)
69 context.getActionController().addAction(
70 new CallbackWeapon("WeaponGiveAccessory", this, 0, 0,
71 weaponContext, position, velocity));
74 void WeaponGiveAccessory::weaponCallback(
75 ScorchedContext &context,
76 WeaponFireContext &weaponContext, FixedVector &position, FixedVector &velocity,
77 unsigned int userData)
79 Tank *tank = context.getTankContainer().getTankById(weaponContext.getPlayerId());
80 if (!tank) return;
82 std::vector<Accessory *>::iterator itor;
83 for (itor = giveAccessories_.begin();
84 itor != giveAccessories_.end();
85 itor++)
87 Accessory *accessory = (*itor);
89 if (number_ > 0)
91 if (tank->getAccessories().accessoryAllowed(accessory, number_))
93 tank->getAccessories().add(accessory, number_);
95 ChannelText text("combat",
96 LANG_RESOURCE_3("TANK_GET_ACCESSORY",
97 "[p:{0}] received {1} * [w:{2}]",
98 tank->getTargetName(),
99 S3D::formatStringBuffer("%i", number_),
100 accessory->getName()));
101 ChannelManager::showText(context, text);
104 else
106 int money = accessory->getSellPrice() * number_;
107 tank->getScore().setMoney(tank->getScore().getMoney() + money);
109 ChannelText text("combat",
110 LANG_RESOURCE_2("TANK_GET_MONEY",
111 "[p:{0}] received ${1}",
112 tank->getTargetName(),
113 S3D::formatStringBuffer("%i", money)));
114 ChannelManager::showText(context, text);
118 else
120 int count = tank->getAccessories().getAccessoryCount(accessory);
121 if (count > 0)
123 int loose = MIN(count, -number_);
125 tank->getAccessories().rm(accessory, loose);
127 ChannelText text("combat",
128 LANG_RESOURCE_3("TANK_LOST_ACCESSORY",
129 "[p:{0}] lost {1} * [w:{2}]",
130 tank->getTargetName(),
131 S3D::formatStringBuffer("%i", loose),
132 accessory->getName()));
133 ChannelManager::showText(context, text);