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

Location.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 
00026 #include "FSServer.h"
00027 
00028 #include <stdlib.h>
00029 #include <math.h>
00030 
00031 #ifdef _DEBUG
00032 #define new DEBUG_NEW
00033 #endif
00034 
00035 
00036 Location::~Location()
00037 {
00038 }
00039 
00040 double Location::Distance(const Location & other) const
00041 {
00042         return sqrt(double((posX - other.posX) * (posX - other.posX) + (posY - other.posY) * (posY - other.posY)));
00043 }
00044 
00045 double Location::Distance(double px, double py) const
00046 {
00047         return sqrt((double(posX) - px) * (double(posX) - px) + (double(posY) - py) * (double(posY) - py));
00048 }
00049 
00050 bool Location::ParseNode(const TiXmlNode * node)
00051 {
00052         const TiXmlNode * child = node->FirstChild("XCoord");
00053         if (child) {
00054                 posX = GetLong(child);
00055                 posY = GetLong(node->FirstChild("YCoord"));
00056 
00057                 if (posX <= 0 || posY <= 0) {
00058                         Message * mess = TheGame->AddMessage("Error: Invalid Location");
00059                         mess->AddItem("", this);
00060                         return false;
00061                 }
00062         } else {
00063                 Planet * p = TheGalaxy->GetPlanet(GetString(node->FirstChild("Planet")));
00064                 if (p == NULL) {
00065                         Message * mess = TheGame->AddMessage("Error: Invalid Location");
00066                         mess->AddItem("", this);
00067                         return false;
00068                 }
00069 
00070                 posX = p->GetPosX();
00071                 posY = p->GetPosY();
00072         }
00073 
00074         return true;
00075 }
00076 
00077 /*
00078                 <Location>
00079                     <XCoord>1012</XCoord>
00080                     <YCoord>1045</YCoord>
00081                 </Location>
00082 */
00083 
00084 TiXmlNode * Location::WriteLocation(TiXmlNode * node) const
00085 {
00086         TiXmlElement *loc = new TiXmlElement("Location");
00087 //      if (mAlsoHere && mAlsoHere->at(0) != this && dynamic_cast<Planet *>(mAlsoHere->at(0)) != NULL) {
00088 //              AddString(loc, "Planet", mAlsoHere->at(0)->GetName(NULL).c_str());
00089 //      } else {
00090                 AddLong(loc, "XCoord", posX);
00091                 AddLong(loc, "YCoord", posY);
00092 //      }
00093 
00094         node->LinkEndChild(loc);
00095         return loc;
00096 }
00097 
00098 void Location::MoveToward(const Location &start, const Location & dest, double * px, double * py, long distance)
00099 {
00100         double totdist = start.Distance(dest);
00101         double travdist = double(distance) + 1.0 - epsilon;
00102 
00103         if (totdist < travdist) {
00104                 *px = dest.posX;
00105                 *py = dest.posY;
00106         } else {
00107                 *px = start.posX + (dest.posX - start.posX) * travdist / totdist;
00108                 *py = start.posY + (dest.posY - start.posY) * travdist / totdist;
00109         }
00110 /*
00111                 // adjust by up to 1ly to get some additional distance (allow 100.99ly at warp 10)
00112                 long signx = dest.posX == start.posX ? 0 : dest.posX > start.posX ? 1 : -1;
00113                 long signy = dest.posY == start.posY ? 0 : dest.posY > start.posY ? 1 : -1;
00114                 *px += signx;
00115                 if (long(sqrt((*px - start.posX)**2 + (*py - start.posY)**2)) > distance)
00116                         point->posX -= signx;
00117                 else {
00118                         point->posY += signy;
00119                         if (long(start.Distance(*point)) > distance)
00120                                 point->posY -= signy;
00121                 }
00122 */
00123 }

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