TargetGroup.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 <target/TargetGroup.h>
00022 #include <target/Target.h>
00023 #include <target/TargetLife.h>
00024 #include <engine/ScorchedContext.h>
00025 #include <landscapemap/TargetGroupsSetEntry.h>
00026 #include <landscapemap/LandscapeMaps.h>
00027 #include <net/NetBuffer.h>
00028 #include <set>
00029 
00030 TargetGroup::TargetGroup(ScorchedContext &context) :
00031         context_(context)
00032 {
00033 }
00034 
00035 TargetGroup::~TargetGroup()
00036 {
00037 }
00038 
00039 FixedVector &TargetGroup::getPosition()
00040 {
00041         return target_->getLife().getTargetPosition();
00042 }
00043 
00044 bool TargetGroup::writeMessage(NetBuffer &buffer)
00045 {
00046         buffer.addToBuffer((int) groups_.size());
00047         std::set<TargetGroupsSetEntry *>::iterator itor;
00048         for (itor = groups_.begin();
00049                 itor != groups_.end();
00050                 itor++)
00051         {
00052                 TargetGroupsSetEntry *group = *itor;
00053                 buffer.addToBuffer(group->getName());
00054         }       
00055         return true;
00056 }
00057 
00058 bool TargetGroup::readMessage(NetBufferReader &reader)
00059 {
00060         std::set<std::string> goodGroups;
00061 
00062         int groupNo = 0;
00063         if (!reader.getFromBuffer(groupNo)) return false;
00064         for (int g=0; g<groupNo; g++)
00065         {
00066                 std::string groupName;
00067                 if (!reader.getFromBuffer(groupName)) return false;
00068 
00069                 TargetGroupsSetEntry *group = 
00070                         context_.getLandscapeMaps().getGroundMaps().getGroups().getGroup(groupName.c_str());
00071                 if (group && !group->hasObject(this))
00072                 {
00073                         group->addObject(this, false);
00074                 }
00075                 goodGroups.insert(groupName);
00076         }
00077 
00078         std::set<TargetGroupsSetEntry *> groupsCopy = groups_;
00079         std::set<TargetGroupsSetEntry *>::iterator itor;
00080         for (itor = groupsCopy.begin();
00081                 itor != groupsCopy.end();
00082                 itor++)
00083         {
00084                 TargetGroupsSetEntry *group = *itor;
00085                 if (goodGroups.find(group->getName()) == goodGroups.end())
00086                 {
00087                         if (group->hasObject(this))
00088                         {
00089                                 group->removeObject(this);
00090                         }
00091                 }
00092         }
00093 
00094         return true;
00095 }
00096 
00097 void TargetGroup::addToGroup(TargetGroupsSetEntry *group)
00098 {
00099         groups_.insert(group);
00100 }
00101 
00102 void TargetGroup::removeFromGroup(TargetGroupsSetEntry *group)
00103 {
00104         groups_.erase(group);
00105 }
00106 
00107 void TargetGroup::removeFromAllGroups()
00108 {
00109         if (groups_.empty()) return;
00110 
00111         std::set<TargetGroupsSetEntry *> groupsCopy = groups_;
00112         std::set<TargetGroupsSetEntry *>::iterator itor;
00113         for (itor = groupsCopy.begin();
00114                 itor != groupsCopy.end();
00115                 itor++)
00116         {
00117                 TargetGroupsSetEntry *group = *itor;
00118                 if (group->hasObject(this))
00119                 {
00120                         group->removeObject(this);
00121                 }
00122         }
00123 }

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