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

WayOrder.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 "Packet.h"
00029 #include "TempFleet.h"
00030 
00031 #ifdef _DEBUG
00032 #define new DEBUG_NEW
00033 #endif
00034 
00035 /*
00036                 <Waypoint>
00037                         <Fleet>123</Fleet>
00038                         <Mine>123</Mine>
00039                         <Packet>123</Packet>
00040                         <Player>5</Player>
00041                         <Scrap>123</Scrap>
00042                         <Space>123 123</Space>
00043             <Trader>1</Trader>
00044                         <Planet>Cosine</Planet>
00045                         <Speed>0</Speed><!-- -1 for gating -->
00046                         <Order>
00047                                 Colonize
00048                                 Remote Mine
00049                                 Scrap
00050                                 Route
00051                                 <Transport>
00052                                         <Cargo>
00053                                                 <Type>Population</Type> <!-- Fuel, Population, or minerals (normally Ironium, Boranium, or Germanium) -->
00054                                                 <Action>LoadAll</Action> <!-- LoadAll, UnloadAll, LoadExactly, UnloadExactly, FillToPercent, WaitForPercent, LoadOptimal, SetAmountTo, SetWaypointTo -->
00055                                                 <Value>1000</Value>
00056                                         </Cargo>
00057                                 </Transport>
00058                                 <Merge>123</Merge>
00059                                 <LayMine>5</LayMine> <!-- Lay mines for this many years.  -1 for indefinately. -->
00060                                 <Patrol>
00061                                         <Range>50</Range>
00062                                         <Speed>0</Speed>
00063                                 </Patrol>
00064                                 <Transfer>5</Transfer> <!-- Transfers to this player -->
00065                         </Order>
00066                 </Waypoint>
00067 */
00068 
00069 WayOrder::~WayOrder()
00070 {
00071         if (mForMeOnly)
00072                 delete mLoc;
00073 }
00074 
00075 WayOrder::WayOrder(const WayOrder & copy)
00076         : mSpeed(copy.mSpeed),
00077         mOrder(copy.mOrder)
00078 {
00079         if (copy.mForMeOnly) {
00080                 TempFleet * tf = dynamic_cast<TempFleet *>(copy.mLoc);
00081                 if (tf != NULL) {
00082                         assert(false);
00083                         mLoc = new TempFleet(*tf);
00084                 } else {
00085                         mLoc = new Location(*copy.mLoc);
00086                 }
00087                 mForMeOnly = true;
00088         } else {
00089                 mLoc = copy.mLoc;
00090                 mForMeOnly = false;
00091         }
00092 }
00093 
00094 WayOrder * WayOrder::Copy() const
00095 {
00096         if (typeid(*this) == typeid(WayOrder)) {
00097                 return new WayOrder(*this);
00098         } else if (typeid(*this) == typeid(WayOrderNumber)) {
00099                 return new WayOrderNumber(dynamic_cast<const WayOrderNumber &>(*this));
00100         } else if (typeid(*this) == typeid(WayOrderPatrol)) {
00101                 return new WayOrderPatrol(dynamic_cast<const WayOrderPatrol &>(*this));
00102         } else if (typeid(*this) == typeid(WayOrderTransport)) {
00103                 return new WayOrderTransport(dynamic_cast<const WayOrderTransport &>(*this));
00104         } else {
00105 //              assert(false);
00106                 return NULL;
00107         }
00108 }
00109 
00110 void WayOrder::SetLocation(Location * loc, bool fmo/*= false*/)
00111 {
00112         if (mForMeOnly)
00113                 delete mLoc;
00114         
00115         mLoc = loc;
00116         mForMeOnly = fmo;
00117 }
00118 
00119 TiXmlNode * WayOrder::WriteNode(TiXmlNode * node) const
00120 {
00121         TiXmlElement * wayorder = new TiXmlElement("Waypoint");
00122 
00123         const Planet * planet = dynamic_cast<const Planet *>(mLoc);
00124         if (planet != NULL) {
00125                 AddString(wayorder, "Planet", planet->GetName().c_str());
00126         } else {
00127                 const Fleet * fleet = dynamic_cast<const Fleet *>(mLoc);
00128                 if (fleet != NULL) {
00129                         AddLong(wayorder, "Fleet", fleet->GetID());
00130                         AddLong(wayorder, "Player", fleet->GetOwner()->GetID());
00131                 } else {
00132                         const Packet * packet = dynamic_cast<const Packet *>(mLoc);
00133                         if (packet != NULL) {
00134 //                              AddLong(wayorder, "Packet", packet->GetID());
00135                         } else {
00136 //                              const MineField * mine = dynamic_cast<const MineField *>(mLoc);
00137 //                              if (mine != NULL) {
00138 //                                      AddLong(wayorder, "Minefield", mine->GetID());
00139 //                              } else {
00140                                         const Salvage * scrap = dynamic_cast<const Salvage *>(mLoc);
00141                                         if (scrap != NULL) {
00142                                                 AddLong(wayorder, "Scrap", scrap->GetID());
00143                                         } else {
00144                                                 mLoc->WriteNode(wayorder);
00145                                         }
00146 //                              }
00147                         }
00148                 }
00149         }
00150 
00151         AddLong(wayorder, "Speed", mSpeed);
00152         switch (mOrder) {
00153         case OT_NONE:
00154                 AddString(wayorder, "Order", "No Task");
00155                 break;
00156         case OT_COLONIZE:
00157                 AddString(wayorder, "Order", "Colonize");
00158                 break;
00159         case OT_REMOTEMINE:
00160                 AddString(wayorder, "Order", "Remote Mine");
00161                 break;
00162         case OT_SCRAP:
00163                 AddString(wayorder, "Order", "Scrap");
00164                 break;
00165         case OT_ROUTE:
00166                 AddString(wayorder, "Order", "Route");
00167                 break;
00168         }
00169 
00170         node->LinkEndChild(wayorder);
00171         return wayorder;
00172 }
00173 
00174 WayOrderNumber::~WayOrderNumber()
00175 {
00176 }
00177 
00178 TiXmlNode * WayOrderNumber::WriteNode(TiXmlNode * node) const
00179 {
00180         TiXmlNode * wo = WayOrder::WriteNode(node);
00181         TiXmlElement order("Order");
00182         switch (mOrder) {
00183         case OT_MERGE:
00184                 AddLong(&order, "Merge", mNumber);
00185                 break;
00186         case OT_LAYMINE:
00187                 AddLong(&order, "LayMine", mNumber);
00188                 break;
00189         case OT_TRANSFER:
00190                 AddLong(&order, "Transfer", mNumber);
00191                 break;
00192         }
00193 
00194         wo->InsertEndChild(order);
00195 
00196         return wo;
00197 }
00198 
00199 WayOrderPatrol::~WayOrderPatrol()
00200 {
00201 }
00202 
00203 TiXmlNode * WayOrderPatrol::WriteNode(TiXmlNode * node) const
00204 {
00205         TiXmlNode * wo = WayOrder::WriteNode(node);
00206         TiXmlElement order("Order");
00207         TiXmlElement patrol("Patrol");
00208         AddLong(&patrol, "Speed", mPatrolSpeed);
00209         AddLong(&patrol, "Range", mRange);
00210         order.InsertEndChild(patrol);
00211         wo->InsertEndChild(order);
00212 
00213         return wo;
00214 }
00215 
00216 WayOrderTransport::WayOrderTransport(Location * loc, bool ForMeOnly /*= false*/)
00217 : WayOrder(loc, ForMeOnly)
00218 {
00219         actions.insert(actions.begin(), Rules::MaxMinType-FUEL, TRANSFER_NOORDER);
00220         values.insert(values.begin(), Rules::MaxMinType-FUEL, 0L);
00221 }
00222 
00223 WayOrderTransport::WayOrderTransport(const WayOrderTransport & copy) : WayOrder(copy)
00224 {
00225         actions.insert(actions.begin(), copy.actions.begin(), copy.actions.begin());
00226         values.insert(values.begin(), copy.values.begin(), copy.values.begin());
00227 }
00228 
00229 WayOrderTransport::~WayOrderTransport()
00230 {
00231 }
00232 
00233 /*
00234                                         <Cargo>
00235                                                 <Type>Population</Type> <!-- Fuel, Population, or minerals (normally Ironium, Boranium, or Germanium) -->
00236                                                 <Action>Load All</Action> <!-- Load All, Unload All, Load Exactly, Unload Exactly, Fill To Percent, Wait For Percent, Load Optimal/Load Dunnage, Set Amount To, Set Waypoint To, Set To Percent, Drop And Load -->
00237                                                 <Value>1000</Value>
00238                                         </Cargo>
00239 */
00240 bool WayOrderTransport::ParseNode(const TiXmlNode * node, Player * player)
00241 {
00242         const TiXmlNode * child1;
00243         const char * ptr;
00244 
00245         for (child1 = node->FirstChild("Cargo"); child1; child1 = child1->NextSibling("Cargo")) {
00246                 long type = Rules::GetCargoType(GetString(child1->FirstChild("Type")));
00247                 if (type < FUEL || type >= Rules::MaxMinType) {
00248                         Message * mess = player->AddMessage("Error: Invalid cargo type");
00249                         mess->AddItem("", GetString(child1->FirstChild("Type")));
00250                         return false;
00251                 }
00252 
00253                 type -= FUEL;   // make it 0 indexed
00254                 ptr = GetString(child1->FirstChild("Action"));
00255                 if (stricmp(ptr, "Load All") == 0)
00256                         actions[type] = TRANSFER_LOADALL;
00257                 else if (stricmp(ptr, "Unload All") == 0)
00258                         actions[type] = TRANSFER_UNLOADALL;
00259                 else if (stricmp(ptr, "Load Exactly") == 0)
00260                         actions[type] = TRANSFER_LOADAMT;
00261                 else if (stricmp(ptr, "Unload Exactly") == 0)
00262                         actions[type] = TRANSFER_UNLOADAMT;
00263                 else if (stricmp(ptr, "Fill To Percent") == 0)
00264                         actions[type] = TRANSFER_FILLPER;
00265                 else if (stricmp(ptr, "Wait For Percent") == 0)
00266                         actions[type] = TRANSFER_WAITPER;               // Affects movement too
00267                 else if (stricmp(ptr, "Load Optimal") == 0 || stricmp(ptr, "Load Dunnage") == 0)
00268                         actions[type] = TRANSFER_LOADDUNN;              // Load optimal for fuel
00269                 else if (stricmp(ptr, "Set Amount To") == 0)
00270                         actions[type] = TRANSFER_AMOUNTTO;
00271                 else if (stricmp(ptr, "Set Waypoint To") == 0)
00272                         actions[type] = TRANSFER_DESTTO;
00273                 else if (stricmp(ptr, "Set To Percent") == 0)
00274                         actions[type] = TRANSFER_SETTOPER;              // set to a % of fleet capacity
00275                 else if (stricmp(ptr, "Drop And Load") == 0)
00276                         actions[type] = TRANSFER_DROPNLOAD;             // drop all and then load all. only really useful for pop drops
00277                 else if (stricmp(ptr, "No Orders") == 0)
00278                         actions[type] = TRANSFER_NOORDER;
00279                 else {
00280                         actions[type] = TRANSFER_NOORDER;
00281                         Message * mess = player->AddMessage("Error: Invalid transfer order");
00282                         mess->AddItem("", ptr);
00283                 }
00284                 values[type] = GetLong(child1->FirstChild("Value"));
00285         }
00286 
00287         return true;
00288 }
00289 
00290 TiXmlNode * WayOrderTransport::WriteNode(TiXmlNode * node) const
00291 {
00292         TiXmlNode * wo = WayOrder::WriteNode(node);
00293         TiXmlElement order("Order");
00294         TiXmlElement transport("Transport");
00295 
00296         for (CargoType i = 0; i < Rules::MaxMinType-FUEL; ++i) {
00297                 if (actions[i] != TRANSFER_NOORDER) {
00298                         TiXmlElement cargo("Cargo");
00299                         AddString(&cargo, "Type", Rules::GetCargoName(i+FUEL).c_str());
00300 
00301                         switch (actions[i]) {
00302                         case TRANSFER_LOADALL:
00303                                 AddString(&cargo, "Action", "Load All");
00304                                 break;
00305                         case TRANSFER_UNLOADALL:
00306                                 AddString(&cargo, "Action", "Unload All");
00307                                 break;
00308                         case TRANSFER_LOADAMT:
00309                                 AddString(&cargo, "Action", "Load Exactly");
00310                                 AddLong(&cargo, "Value", values[i]);
00311                                 break;
00312                         case TRANSFER_UNLOADAMT:
00313                                 AddString(&cargo, "Action", "Unload Exactly");
00314                                 AddLong(&cargo, "Value", values[i]);
00315                                 break;
00316                         case TRANSFER_FILLPER:
00317                                 AddString(&cargo, "Action", "Fill To Percent");
00318                                 AddLong(&cargo, "Value", values[i]);
00319                                 break;
00320                         case TRANSFER_WAITPER:
00321                                 AddString(&cargo, "Action", "Wait For Percent");
00322                                 AddLong(&cargo, "Value", values[i]);
00323                                 break;
00324                         case TRANSFER_LOADDUNN:
00325                                 AddString(&cargo, "Action", "Load Optimal");
00326                                 break;
00327                         case TRANSFER_AMOUNTTO:
00328                                 AddString(&cargo, "Action", "Set Amount To");
00329                                 AddLong(&cargo, "Value", values[i]);
00330                                 break;
00331                         case TRANSFER_DESTTO:
00332                                 AddString(&cargo, "Action", "Set Waypoint To");
00333                                 AddLong(&cargo, "Value", values[i]);
00334                                 break;
00335                         case TRANSFER_SETTOPER:
00336                                 AddString(&cargo, "Action", "Set To Percent");
00337                                 AddLong(&cargo, "Value", values[i]);
00338                                 break;
00339                         case TRANSFER_DROPNLOAD:
00340                                 AddString(&cargo, "Action", "Drop And Load");
00341                                 break;
00342                         }
00343 
00344                         transport.InsertEndChild(cargo);
00345                 }
00346         }
00347 
00348         order.InsertEndChild(transport);
00349         wo->InsertEndChild(order);
00350 
00351         return wo;
00352 }

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