git-svn-id: https://scorched3d.svn.sourceforge.net/svnroot/scorched3d/trunk/scorched...
[scorched3d/parasti.git] / src / common / landscapedef / LandscapeEvents.cpp
blob390e280fb27f3686ec01411af99f3c9f8699e7a1
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 <landscapedef/LandscapeEvents.h>
22 #include <landscapemap/LandscapeMaps.h>
23 #include <engine/ScorchedContext.h>
24 #include <engine/ActionController.h>
25 #include <weapons/AccessoryStore.h>
26 #include <common/Logger.h>
27 #include <XML/XMLNode.h>
29 // LandscapeEvent
30 LandscapeEvent::~LandscapeEvent()
32 delete condition;
33 delete action;
36 bool LandscapeEvent::readXML(XMLNode *node)
39 XMLNode *conditionNode;
40 std::string conditiontype;
41 if (!node->getNamedChild("condition", conditionNode)) return false;
42 if (!conditionNode->getNamedParameter("type", conditiontype)) return false;
43 if (!(condition = LandscapeCondition::create(conditiontype.c_str()))) return false;
44 if (!condition->readXML(conditionNode)) return false;
47 XMLNode *actionNode;
48 std::string actiontype;
49 if (!node->getNamedChild("action", actionNode)) return false;
50 if (!actionNode->getNamedParameter("type", actiontype)) return false;
51 if (!(action = LandscapeAction::create(actiontype.c_str()))) return false;
52 if (!action->readXML(actionNode)) return false;
55 return node->failChildren();
58 LandscapeCondition *LandscapeCondition::create(const char *type)
60 if (0 == strcmp(type, "time")) return new LandscapeConditionTime;
61 if (0 == strcmp(type, "random")) return new LandscapeConditionRandom;
62 if (0 == strcmp(type, "groupsize")) return new LandscapeConditionGroupSize;
63 S3D::dialogMessage("LandscapeCondition", S3D::formatStringBuffer("Unknown condition type %s", type));
64 return 0;
67 // LandscapeConditionGroupSize
68 fixed LandscapeConditionGroupSize::getNextEventTime(ScorchedContext &context, int eventNumber)
70 return fixed::MAX_FIXED;
73 bool LandscapeConditionGroupSize::fireEvent(ScorchedContext &context,
74 fixed timeLeft, int eventNumber)
76 if (eventNumber == 1) // i.e. the first event
78 TargetGroupsGroupEntry *groupEntry =
79 context.getLandscapeMaps().getGroundMaps().getGroups().getGroup(
80 groupname.c_str());
81 if (groupEntry)
83 int groupCount = groupEntry->getObjectCount();
84 if (groupCount <= groupsize) return true;
88 return false;
91 bool LandscapeConditionGroupSize::readXML(XMLNode *node)
93 if (!node->getNamedChild("groupname", groupname)) return false;
94 if (!node->getNamedChild("groupsize", groupsize)) return false;
95 return node->failChildren();
98 // LandscapeConditionTime
99 fixed LandscapeConditionTime::getNextEventTime(ScorchedContext &context, int eventNumber)
101 if (eventNumber > 1 &&
102 singletimeonly)
104 return fixed::MAX_FIXED;
107 return context.getActionController().getRandom().getRandFixed() *
108 (maxtime - mintime) + mintime;
111 bool LandscapeConditionTime::fireEvent(ScorchedContext &context,
112 fixed timeLeft, int eventNumber)
114 return (timeLeft < fixed(0));
117 bool LandscapeConditionTime::readXML(XMLNode *node)
119 if (!node->getNamedChild("mintime", mintime)) return false;
120 if (!node->getNamedChild("maxtime", maxtime)) return false;
121 if (!node->getNamedChild("singletimeonly", singletimeonly)) return false;
122 return node->failChildren();
125 // LandscapeConditionRandom
126 fixed LandscapeConditionRandom::getNextEventTime(ScorchedContext &context, int eventNumber)
128 if (eventNumber > 1)
130 return fixed::MAX_FIXED;
133 if (context.getActionController().getRandom().getRandFixed() < randomchance)
135 return randomdelay;
137 return fixed::MAX_FIXED;
140 bool LandscapeConditionRandom::fireEvent(ScorchedContext &context,
141 fixed timeLeft, int eventNumber)
143 return (timeLeft < fixed(0));
146 bool LandscapeConditionRandom::readXML(XMLNode *node)
148 if (!node->getNamedChild("randomchance", randomchance)) return false;
149 if (!node->getNamedChild("randomdelay", randomdelay)) return false;
150 return node->failChildren();
153 LandscapeAction *LandscapeAction::create(const char *type)
155 if (0 == strcmp(type, "fireweapon")) return new LandscapeActionFireWeapon;
156 S3D::dialogMessage("LandscapeAction", S3D::formatStringBuffer("Unknown action type %s", type));
157 return 0;
160 // LandscapeActionFireWeapon
161 void LandscapeActionFireWeapon::fireAction(ScorchedContext &context)
163 Accessory *accessory =
164 context.getAccessoryStore().findByPrimaryAccessoryName(
165 weapon.c_str());
166 if (!accessory) S3D::dialogExit("LandscapeActionFireWeapon",
167 S3D::formatStringBuffer("Failed to find weapon named \"%s\"", weapon.c_str()));
168 if (accessory->getType() != AccessoryPart::AccessoryWeapon)
169 S3D::dialogExit("LandscapeActionFireWeapon",
170 S3D::formatStringBuffer("Accessory named \"%s\" is not a weapon", weapon.c_str()));
171 Weapon *weapon = (Weapon *) accessory->getAction();
173 WeaponFireContext weaponContext(0, Weapon::eDataDeathAnimation);
174 FixedVector pos, vel;
175 weapon->fireWeapon(context, weaponContext, pos, vel);
178 bool LandscapeActionFireWeapon::readXML(XMLNode *node)
180 if (!node->getNamedChild("weapon", weapon)) return false;
181 return node->failChildren();