Advanced Strategic Command
recycleunitcommand.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Advanced Strategic Command; http://www.asc-hq.de
3  Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; see the file COPYING. If not, write to the
17  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  Boston, MA 02111-1307 USA
19 */
20 
21 
22 #include "recycleunitcommand.h"
23 
24 #include "../vehicle.h"
25 #include "../mapfield.h"
26 #include "../gamemap.h"
27 #include "../viewcalculation.h"
28 #include "../spfst.h"
29 #include "../mapdisplayinterface.h"
30 #include "action-registry.h"
31 #include "../itemrepository.h"
32 #include "../containercontrols.h"
33 #include "consumeresource.h"
34 #include "servicecommand.h"
35 #include "destructcontainer.h"
36 
37 bool RecycleUnitCommand :: avail ( const ContainerBase* carrier, const Vehicle* unit )
38 {
39  if ( !carrier || !unit )
40  return false;
41 
42  if ( !carrier->findUnit( unit->networkid, false ) )
43  return false;
44 
45  if ( !carrier->isBuilding() )
46  return false;
47 
48  return true;
49 }
50 
51 
53 {
54  if ( !avail( carrier, unit ))
55  return Resources();
56 
57  int output;
59  output = recyclingoutput;
60  else
61  output = destructoutput;
62 
63  Resources res;
64 
65  for ( ContainerBase::Cargo::const_iterator i = unit->getCargo().begin(); i != unit->getCargo().end(); i++)
66  if ( *i )
67  res += getOutput ( carrier, *i );
68 
69  res.material += unit->typ->productionCost.material * (100 - unit->damage/2 ) * output / (100*100);
70  return res;
71 
72 }
73 
74 
75 RecycleUnitCommand :: RecycleUnitCommand ( ContainerBase* container )
76  : ContainerCommand ( container ), unitID(-1)
77 {
78 
79 }
80 
81 
82 
83 
84 
86 {
87  if ( getState() != SetUp )
88  return ActionResult(22000);
89 
90  Vehicle* unit = getMap()->getUnit( unitID );
91  if ( !unit )
92  return ActionResult( 22100 );
93 
94  if ( !avail( getContainer(), unit ))
95  return ActionResult( 22101 );
96 
97  auto_ptr<ServiceCommand> service ( new ServiceCommand( getContainer()));
98 
99  const ServiceTargetSearcher::Targets& targets = service->getDestinations();
100  if ( find( targets.begin(), targets.end(), unit ) != targets.end() ) {
101  service->setDestination( unit );
102 
103  service->getTransferHandler( ).emptyDest();
104  service->saveTransfers();
105  ActionResult res = service->execute( context );
106  if ( res.successful() )
107  service.release();
108  else
109  return res;
110  }
111 
112  Resources resource = getOutput ( getContainer(), unit );
113 
114  resource = getContainer()->putResource( resource, true, 1, context.actingPlayer->getPosition() );
115 
116  ActionResult res = (new ConsumeResource(getContainer(), -resource))->execute(context);
117 
118  if ( res.successful() )
119  res = (new DestructContainer( unit))->execute( context );
120 
121  if ( res.successful() )
122  setState( Finished );
123  else
124  setState( Failed );
125 
126  return res;
127 }
128 
129 
130 
131 static const int RecycleUnitCommandVersion = 1;
132 
134 {
135  ContainerCommand::readData( stream );
136  int version = stream.readInt();
137  if ( version > RecycleUnitCommandVersion )
138  throw tinvalidversion ( "RecycleUnitCommand", RecycleUnitCommandVersion, version );
139  unitID = stream.readInt();
140 }
141 
143 {
144  ContainerCommand::writeData( stream );
146  stream.writeInt( unitID );
147 }
148 
150 {
151  unitID = unit->networkid;
152  setState( SetUp );
153 }
154 
155 
157 {
158  ASCString c;
159  c.format("recycleUnit ( map, %d, %d )", getContainerID(), unitID );
160  return c;
161 
162 }
163 
165 {
167 }
168 
170 {
171  ASCString s = "Recycle ";
172 
173  if ( getMap()->getUnit( unitID ))
174  s += getMap()->getUnit( unitID )->getName();
175 
176  if ( getContainer(true) ) {
177  s += " by " + getContainer()->getName();
178  }
179  return s;
180 }
181 
182 namespace
183 {
184  const bool r1 = registerAction<RecycleUnitCommand> ( ActionRegistry::RecycleUnitCommand );
185 }
186 
Vehicle * findUnit(int nwid, bool recursive=true) const
searches for a the unit in carrier and optionally all inner carriers
Player * actingPlayer
Definition: context.h:37
virtual void writeInt(int i)
Writes a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is p...
Definition: basestrm.cpp:363
virtual int putResource(int amount, int resourcetype, bool queryonly, int scope=1, int player=-1)=0
scope: 0 = local 1 = resource network 2 = global in all buildings 3 = map wide pool( used only intern...
int GameActionID
Definition: action.h:35
the command is totally done
Definition: command.h:120
int damage
Damage. 0 is no damage, when damage reaches 100 the container is destroyed.
static const int RecycleUnitCommandVersion
virtual ASCString getName() const =0
static bool avail(const ContainerBase *carrier, const Vehicle *unit)
virtual int readInt(void)
Reads a 32 bit signed Integer. In the stream little-endian byte order is used and a translation is pe...
Definition: basestrm.cpp:284
int getPosition() const
Definition: player.h:112
bool hasFunction(ContainerFunctions function) const
const int recyclingoutput
the percentage of a units production cost that is reclaimed when the unit is recycled ...
Definition: typen.h:495
State getState() const
Definition: command.h:125
virtual bool isBuilding() const =0
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
ASCString & format(const charT *pFormat,...)
Definition: ascstring.cpp:78
static Resources getOutput(const ContainerBase *carrier, const Vehicle *unit)
ASCString getDescription() const
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
int getContainerID() const
ActionResult execute(const Context &context)
Definition: action.cpp:41
void readData(tnstream &stream)
const int destructoutput
the percentage of a units production cost that is reclaimed when the unit is salvaged ...
Definition: typen.h:498
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
void setUnit(Vehicle *unit)
int material
Definition: typen.h:100
void setState(State state)
Definition: command.cpp:44
bool successful() const
ASCString getCommandString() const
Vehicle * getUnit(int x, int y, int nwid)
Definition: gamemap.cpp:1215
void writeData(tnstream &stream) const
ContainerBase * getContainer(bool dontThrow=false)
const VehicleType * typ
Definition: vehicle.h:83
vector< ContainerBase * > Targets
Definition: servicing.h:129
const ContainerBaseType * baseType
the type descriping all non-instance specific properties of the container
Definition: containerbase.h:80
void writeData(tnstream &stream) const
ActionResult go(const Context &context)
void readData(tnstream &stream)
The parent class of Vehicle and Building; The name Container originates from Battle Isle...
Definition: containerbase.h:40
Resources are basically the currency of ASC.
Definition: typen.h:97
const Cargo & getCargo() const
GameActionID getID() const
int networkid
a unique identification of the unit that is used everywhere in ASC (and not only the network protocol...
Definition: vehicle.h:140