LandscapeEvents.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <landscapedef/LandscapeEvents.h>
00022 #include <landscapemap/LandscapeMaps.h>
00023 #include <engine/ScorchedContext.h>
00024 #include <engine/ActionController.h>
00025 #include <weapons/AccessoryStore.h>
00026 #include <common/Logger.h>
00027 #include <XML/XMLNode.h>
00028 
00029 // LandscapeEvent
00030 LandscapeEvent::~LandscapeEvent()
00031 {
00032         delete condition;
00033         delete action;
00034 }
00035 
00036 bool LandscapeEvent::readXML(XMLNode *node)
00037 {
00038         {
00039                 XMLNode *conditionNode;
00040                 std::string conditiontype;
00041                 if (!node->getNamedChild("condition", conditionNode)) return false;
00042                 if (!conditionNode->getNamedParameter("type", conditiontype)) return false;
00043                 if (!(condition = LandscapeCondition::create(conditiontype.c_str()))) return false;
00044                 if (!condition->readXML(conditionNode)) return false;
00045         }
00046         {
00047                 XMLNode *actionNode;
00048                 std::string actiontype;
00049                 if (!node->getNamedChild("action", actionNode)) return false;
00050                 if (!actionNode->getNamedParameter("type", actiontype)) return false;
00051                 if (!(action = LandscapeAction::create(actiontype.c_str()))) return false;
00052                 if (!action->readXML(actionNode)) return false;
00053         }       
00054         
00055         return node->failChildren();
00056 }
00057 
00058 LandscapeCondition *LandscapeCondition::create(const char *type)
00059 {
00060         if (0 == strcmp(type, "time")) return new LandscapeConditionTime;
00061         if (0 == strcmp(type, "random")) return new LandscapeConditionRandom;
00062         if (0 == strcmp(type, "groupsize")) return new LandscapeConditionGroupSize;
00063         S3D::dialogMessage("LandscapeCondition", S3D::formatStringBuffer("Unknown condition type %s", type));
00064         return 0;
00065 }
00066 
00067 // LandscapeConditionGroupSize
00068 fixed LandscapeConditionGroupSize::getNextEventTime(ScorchedContext &context, int eventNumber)
00069 {
00070         return fixed::MAX_FIXED;
00071 }
00072 
00073 bool LandscapeConditionGroupSize::fireEvent(ScorchedContext &context, 
00074         fixed timeLeft, int eventNumber)
00075 {
00076         if (eventNumber == 1) // i.e. the first event
00077         {
00078                 TargetGroupsGroupEntry *groupEntry =
00079                         context.getLandscapeMaps().getGroundMaps().getGroups().getGroup(
00080                                 groupname.c_str());
00081                 if (groupEntry)
00082                 {
00083                         int groupCount = groupEntry->getObjectCount();
00084                         if (groupCount <= groupsize) return true;
00085                 }
00086         }
00087 
00088         return false;
00089 }
00090 
00091 bool LandscapeConditionGroupSize::readXML(XMLNode *node)
00092 {
00093         if (!node->getNamedChild("groupname", groupname)) return false;
00094         if (!node->getNamedChild("groupsize", groupsize)) return false;
00095         return node->failChildren();
00096 }
00097 
00098 // LandscapeConditionTime
00099 fixed LandscapeConditionTime::getNextEventTime(ScorchedContext &context, int eventNumber)
00100 {
00101         if (eventNumber > 1 &&
00102                 singletimeonly)
00103         {
00104                 return fixed::MAX_FIXED;
00105         }
00106 
00107         return context.getActionController().getRandom().getRandFixed() * 
00108                 (maxtime - mintime) + mintime;
00109 }
00110 
00111 bool LandscapeConditionTime::fireEvent(ScorchedContext &context, 
00112         fixed timeLeft, int eventNumber)
00113 {
00114         return (timeLeft < fixed(0));
00115 }
00116 
00117 bool LandscapeConditionTime::readXML(XMLNode *node)
00118 {
00119         if (!node->getNamedChild("mintime", mintime)) return false;
00120         if (!node->getNamedChild("maxtime", maxtime)) return false;
00121         if (!node->getNamedChild("singletimeonly", singletimeonly)) return false;
00122         return node->failChildren();
00123 }
00124 
00125 // LandscapeConditionRandom
00126 fixed LandscapeConditionRandom::getNextEventTime(ScorchedContext &context, int eventNumber)
00127 {
00128         if (eventNumber > 1)
00129         {
00130                 return fixed::MAX_FIXED;
00131         }
00132 
00133         if (context.getActionController().getRandom().getRandFixed() < randomchance)
00134         {
00135                 return randomdelay;
00136         }
00137         return fixed::MAX_FIXED;
00138 }
00139 
00140 bool LandscapeConditionRandom::fireEvent(ScorchedContext &context, 
00141         fixed timeLeft, int eventNumber)
00142 {
00143         return (timeLeft < fixed(0));
00144 }
00145 
00146 bool LandscapeConditionRandom::readXML(XMLNode *node)
00147 {
00148         if (!node->getNamedChild("randomchance", randomchance)) return false;
00149         if (!node->getNamedChild("randomdelay", randomdelay)) return false;
00150         return node->failChildren();
00151 }
00152 
00153 LandscapeAction *LandscapeAction::create(const char *type)
00154 {
00155         if (0 == strcmp(type, "fireweapon")) return new LandscapeActionFireWeapon;
00156         S3D::dialogMessage("LandscapeAction", S3D::formatStringBuffer("Unknown action type %s", type));
00157         return 0;
00158 }
00159 
00160 // LandscapeActionFireWeapon
00161 void LandscapeActionFireWeapon::fireAction(ScorchedContext &context)
00162 {
00163         Accessory *accessory = 
00164                 context.getAccessoryStore().findByPrimaryAccessoryName(
00165                         weapon.c_str());
00166         if (!accessory) S3D::dialogExit("LandscapeActionFireWeapon",
00167                 S3D::formatStringBuffer("Failed to find weapon named \"%s\"", weapon.c_str()));
00168         if (accessory->getType() != AccessoryPart::AccessoryWeapon) 
00169                 S3D::dialogExit("LandscapeActionFireWeapon",
00170                         S3D::formatStringBuffer("Accessory named \"%s\" is not a weapon", weapon.c_str()));
00171         Weapon *weapon = (Weapon *) accessory->getAction();
00172 
00173         WeaponFireContext weaponContext(0, Weapon::eDataDeathAnimation);
00174         FixedVector pos, vel;
00175         weapon->fireWeapon(context, weaponContext, pos, vel);
00176 }
00177 
00178 bool LandscapeActionFireWeapon::readXML(XMLNode *node)
00179 {
00180         if (!node->getNamedChild("weapon", weapon)) return false;
00181         return node->failChildren();
00182 }

Generated on Mon Feb 16 15:14:50 2009 for Scorched3D by  doxygen 1.5.3