Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List

Wormhole.cpp

00001 /*
00002 Copyright 2003 - 2005 Elliott Kleinrock, Dan Neely, Kurt W. Over, Damon Domjan
00003 
00004 This file is part of FreeStars, a free clone of the Stars! game.
00005 
00006 FreeStars 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 FreeStars 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 FreeStars; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 The full GPL Copyright notice should be in the file COPYING.txt
00021 
00022 Contact:
00023 Email Elliott at 9jm0tjj02@sneakemail.com
00024 */
00025 #include "FSServer.h"
00026 #include "Location.h"
00027 #include "Wormhole.h"
00028 #include <deque>
00029 
00030 #ifdef _DEBUG
00031 #define new DEBUG_NEW
00032 #endif
00033 
00034 Wormhole::Wormhole()
00035 {
00036         Init();
00037         TheGalaxy->GetWormholeID();
00038 }
00039 
00040 Wormhole::Wormhole(const Wormhole &hole)
00041 {
00042         Init();
00043         mID = hole.mID;
00044         mStability = hole.mStability;
00045         mMaxStability = hole.mMaxStability;
00046         mMinStability = hole.mMinStability;
00047         mAttached = hole.mAttached;
00048         mAttachedID = hole.mAttachedID;
00049 }
00050 
00051 void Wormhole::Init()
00052 {
00053         mAttached = NULL;
00054         mAttachedID = 0;
00055         mStability = WORM_VERYSTABLE;
00056         mMaxStability = WORM_VERYSTABLE;
00057         mMinStability = WORM_VERYUNSTABLE;
00058         mID = 0;
00059         mHadSeen.insert(mHadSeen.begin(), TheGame->NumberPlayers(), false);
00060         mTraversed.insert(mTraversed.begin(), TheGame->NumberPlayers(), false);
00061 }
00062 
00063 bool Wormhole::ParseNode(const TiXmlNode * node)
00064 {
00065         CargoHolder::ParseNode(node);
00066 
00067         mStability = GetLong(node->FirstChild("Stability"));
00068         mMaxStability = GetLong(node->FirstChild("MaxStability"));
00069         mMinStability = GetLong(node->FirstChild("MinStability"));
00070         mAttachedID = GetLong(node->FirstChild("Attached"));
00071         Rules::ParseArrayBool(node->FirstChild("Traversed"), "Race", "Number", mTraversed);
00072         for (int i = 0; i < TheGame->NumberPlayers(); ++i) {
00073                 if (SeenBy(i))
00074                         mHadSeen[i] = true;
00075         }
00076 
00077         TheGame->AddAlsoHere(this);
00078 
00079         if(mID == 0)
00080                 return false;
00081         else
00082                 return true;
00083 }
00084 
00085 void Wormhole::AdjustTraverse()
00086 {
00087         for (int i = 0; i < TheGame->NumberPlayers(); ++i) {
00088                 if (!SeenBy(i))
00089                         mTraversed[i] = false;
00090                 else if (GetAttached() && GetAttached()->SeenBy(i) == 0)
00091                         mTraversed[i] = false;
00092         }
00093 }
00094 
00095 TiXmlNode * Wormhole::WriteNode(TiXmlNode * node, const Player * viewer) const
00096 {
00097         if (viewer == NULL || SeenBy(viewer)) {
00098                 CargoHolder::WriteNode(node, viewer);
00099                 AddLong(node, "Stability", mStability);
00100                 if (viewer == NULL || (mTraversed[viewer->GetID()-1] && GetAttached()->SeenBy(viewer->GetID()-1)))
00101                         AddLong(node, "Attached", mAttachedID);
00102 
00103                 if (viewer == NULL) {
00104                         AddLong(node, "MaxStability", mMaxStability);
00105                         AddLong(node, "MinStability", mMinStability);
00106                         node->LinkEndChild(Rules::WriteArrayBool("Traversed", "Race", "Number", mTraversed));
00107                 }
00108         }
00109 
00110         return node;
00111 }
00112 
00113 const Wormhole * Wormhole::GetAttached() const
00114 {
00115         if (mAttachedID == 0)
00116                 return NULL;
00117 
00118         if (mAttached == NULL)
00119                 const_cast<Wormhole *>(this)->mAttached = TheGalaxy->GetWormhole(mAttachedID);
00120 
00121         return mAttached;
00122 }
00123 
00124 void Wormhole::Enter(long id) const
00125 {
00126         const_cast<Wormhole *>(this)->mTraversed[id-1] = true;
00127         const_cast<Wormhole *>(this)->mHadSeen[id-1] = true;
00128 }
00129 
00130 void Wormhole::Exit(long id) const
00131 {
00132         const_cast<Wormhole *>(this)->mHadSeen[id-1] = true;
00133 }
00134 
00135 void Wormhole::Jiggle()
00136 {
00137         // write Jiggle (distance traveled dependent on myStability
00138         posX += Random(mStability * 4) - mStability * 2;
00139         posY += Random(mStability * 4) - mStability * 2;
00140 
00141         if (Random(50) < mStability)
00142                 Shift();
00143         else if (Random(10) == 0)
00144                 mStability = min(mStability++, mMinStability);
00145 }
00146 
00147 void Wormhole::Shift()
00148 {
00149         // reset the wormhole
00150         mID = TheGalaxy->GetWormholeID();
00151         mSeenBy.erase(mSeenBy.begin(), mSeenBy.end());
00152         mSeenBy.insert(mSeenBy.begin(), TheGame->NumberPlayers(), 0L);
00153 
00154 //      mID = TheGalaxy->GetWormholeID();
00155 //      GetAttached()->SetOtherEnd(this);
00156 
00157         mStability = mMaxStability;
00158 
00159         long dist = 0;
00160         long count = 0;
00161         while (count++ < 1000 || dist == 0) {   // hard coded max to planet placement for safety
00162                 SetPosX(Random(TheGalaxy->MinX(), TheGalaxy->MaxX()));
00163                 SetPosY(Random(TheGalaxy->MinY(), TheGalaxy->MaxY()));
00164 
00165                 dist = long(Distance(*TheGalaxy->ClosestPlanet(this)));
00166                 if (dist >= TheGame->GetWHMinDistance())
00167                         break;  // far enough apart, go with it
00168         }
00169 
00170         AvoidPlanets();
00171 }
00172 
00173 void Wormhole::AvoidPlanets()
00174 {
00175         //TODO: Planet grav-well avoidance code
00176 }

Generated on Mon Aug 8 21:33:45 2005 for Freestars by  doxygen 1.4.2-20050421