TargetMovementEntryBoids.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 <movement/TargetMovementEntryBoids.h>
00022 #include <movement/Boid2.h>
00023 #include <common/Defines.h>
00024 #include <common/RandomGenerator.h>
00025 #include <engine/ScorchedContext.h>
00026 #include <target/Target.h>
00027 #include <target/TargetLife.h>
00028 #include <target/TargetState.h>
00029 #include <landscapemap/LandscapeMaps.h>
00030 #include <landscapedef/LandscapeTex.h>
00031 #include <landscapedef/LandscapeMovement.h>
00032 
00033 TargetMovementEntryBoids::TargetMovementEntryBoids() :
00034         movementNumber_(0)
00035 {
00036 }
00037 
00038 TargetMovementEntryBoids::~TargetMovementEntryBoids()
00039 {
00040 }
00041 
00042 void TargetMovementEntryBoids::generate(ScorchedContext &context, 
00043         RandomGenerator &random, LandscapeMovementType *movementType)
00044 {
00045         LandscapeMovementTypeBoids *boids = 
00046                 (LandscapeMovementTypeBoids *) movementType;
00047 
00048         // Set the boid parameters
00049         cruiseDistance_ = boids->cruisedistance * fixed(10);
00050         maxVelocity_ = boids->maxvelocity * fixed(true, 750);
00051         cruiseVelocity_ = boids->maxvelocity * fixed(true, 400);
00052         maxAcceleration_ = boids->maxacceleration * fixed(true, 20);
00053         minBounds_ = boids->minbounds;
00054         maxBounds_ = boids->maxbounds;
00055 
00056         // Find the group to move the objects in
00057         groupEntry_ = context.getLandscapeMaps().getGroundMaps().getGroups().
00058                 getGroup(boids->groupname.c_str());
00059         if (!groupEntry_)
00060         {
00061                 S3D::dialogExit("TargetMovementEntryBoids", 
00062                         S3D::formatStringBuffer("Group entry %s has no objects defined for it", 
00063                         boids->groupname.c_str()));
00064         }
00065 
00066         // Create boids
00067         makeBoids(context, random, boids->maxbounds, boids->minbounds);
00068 }
00069 
00070 void TargetMovementEntryBoids::makeBoids(ScorchedContext &context, 
00071         RandomGenerator &random, FixedVector &maxBounds, FixedVector &minBounds)
00072 {
00073         // Generate the list of offsets for all of the targets in the group
00074         std::map<unsigned int, TargetGroup *> &objects = groupEntry_->getObjects();
00075         std::map<unsigned int, TargetGroup *>::iterator itor;
00076         for (itor = objects.begin();
00077                 itor != objects.end();
00078                 itor++)
00079         {
00080                 unsigned int playerId = (*itor).first;
00081                 TargetGroup *groupEntry = (*itor).second;
00082                 makeBoid(context, groupEntry);
00083         }
00084 }
00085 
00086 Boid2 *TargetMovementEntryBoids::makeBoid(ScorchedContext &context, TargetGroup *groupEntry)
00087 {
00088         if (!groupEntry->getTarget()->isTarget() ||
00089                 groupEntry->getTarget()->getPlayerId() >= TargetID::MIN_TARGET_TRANSIENT_ID)
00090         {
00091                 S3D::dialogExit("TargetMovementEntryBoids",
00092                         "Movement can be assigned to level targets only (no tanks)");
00093         }
00094         if (groupEntry->getTarget()->getTargetState().getMovement())
00095         {
00096                 S3D::dialogExit("TargetMovementEntryBoids",
00097                         "Only one movement can be assigned to each target");
00098         }
00099 
00100         // Set this target as moving
00101         Boid2 *boid = new Boid2(context, groupEntry->getTarget(), this);
00102         groupEntry->getTarget()->getTargetState().setMovement(boid);
00103         return boid;
00104 }
00105 
00106 void TargetMovementEntryBoids::simulate(ScorchedContext &context, fixed frameTime)
00107 {
00108         movementNumber_++;
00109         std::vector<Boid2*> boidSet;
00110 
00111         // For each target set position and rotation based on its offset
00112         std::map<unsigned int, TargetGroup *> &objects = groupEntry_->getObjects();
00113         std::map<unsigned int, TargetGroup *>::iterator itor;
00114         for (itor = objects.begin();
00115                 itor != objects.end();
00116                 itor++)
00117         {
00118                 unsigned int playerId = (*itor).first;
00119                 TargetGroup *groupEntry = (*itor).second;
00120 
00121                 Boid2 *boid = (Boid2 *) groupEntry->getTarget()->getTargetState().getMovement();
00122                 if (!boid) boid = makeBoid(context, groupEntry);
00123 
00124                 boidSet.push_back(boid);
00125                 if (boidSet.size() == 5)
00126                 {
00127                         processSet(frameTime, boidSet);
00128                         boidSet.clear();
00129                 }
00130         }
00131 
00132         processSet(frameTime, boidSet);
00133 }
00134 
00135 void TargetMovementEntryBoids::processSet(fixed frameTime, std::vector<Boid2*> &boidSet)
00136 {
00137         if (boidSet.empty()) return;
00138 
00139         std::vector<Boid2*>::iterator itor;
00140         for (itor = boidSet.begin();
00141                 itor != boidSet.end();
00142                 itor++)
00143         {
00144                 Boid2 *boid = (*itor);
00145                 boid->update(frameTime, boidSet, (movementNumber_ % 10) == 0);
00146         }
00147 }
00148 
00149 bool TargetMovementEntryBoids::writeMessage(NetBuffer &buffer)
00150 {
00151         buffer.addToBuffer(movementNumber_);
00152         return true;
00153 }
00154 
00155 bool TargetMovementEntryBoids::readMessage(NetBufferReader &reader)
00156 {
00157         if (!reader.getFromBuffer(movementNumber_)) return false;
00158         return true;
00159 }
00160 
00161 void TargetMovementEntryBoids::draw()
00162 {
00163 }

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