00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00079
00080
00081
00082
00083
00084 TiXmlNode * Location::WriteLocation(TiXmlNode * node) const
00085 {
00086 TiXmlElement *loc = new TiXmlElement("Location");
00087
00088
00089
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
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 }