Advanced Strategic Command
vehicleattack.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 "vehicleattack.h"
23 #include "action-registry.h"
24 
25 #include "../vehicle.h"
26 #include "../gamemap.h"
27 #include "../attack.h"
28 #include "../spfst.h"
29 #include "../viewcalculation.h"
30 
31 #include "changeunitmovement.h"
32 #include "inflictdamage.h"
33 
34 #include "../mapdisplayinterface.h"
35 
36 
37 VehicleAttackAction::VehicleAttackAction( GameMap* gamemap, int vehicleID, const MapCoordinate& target, int weapon )
38  : UnitAction( gamemap, vehicleID )
39 {
40  this->target = target;
41  this->weapon = weapon;
42 }
43 
44 
46 {
47  ASCString res = "Attack at " + target.toString();
48  if ( getUnit(false) )
49  res += " with unit " + getUnit(false)->getName();
50  return res;
51 }
52 
53 
55 {
56  UnitAction::readData( stream );
57  int version = stream.readInt();
58  if ( version != 1 )
59  throw tinvalidversion ( "VehicleAttack", 1, version );
60 
61  weapon = stream.readInt();
62  target.read( stream );
63 };
64 
65 
67 {
68  UnitAction::writeData( stream );
69  stream.writeInt( 1 );
70  stream.writeInt( weapon );
71  target.write( stream );
72 };
73 
74 
76 {
78 }
79 
81 {
82  auto_ptr<tfight> battle;
83 
84  MapField* fld = getMap()->getField( target );
85 
86  Vehicle* attacker = getUnit();
87  Vehicle* attackee = fld->vehicle;
88 
89  if ( fld->vehicle ) {
90  battle.reset( new tunitattacksunit ( attacker, attackee, true, weapon ) );
91  } else {
92  if ( fld->building )
93  battle.reset( new tunitattacksbuilding ( getUnit(), target.x, target.y , weapon ) );
94  else
95  battle.reset( new tunitattacksobject ( getUnit(), target.x, target.y, weapon ) );
96  }
97 
98  bool shown;
99  if ( context.display && fieldvisiblenow ( getMap()->getField (target), context.viewingPlayer ) ) {
100  context.display->displayActionCursor ( getUnit()->getPosition(), target );
101  context.display->showBattle( *battle );
102  context.display->removeActionCursor ( );
103  shown = true;
104  } else {
105  battle->calc();
106  shown = false;
107  }
108 
109  /*
110  if ( !getUnit()->typ->hasFunction( ContainerBaseType::MoveAfterAttack )) {
111  GameAction* a = new ChangeUnitMovement( getMap(), getUnit()->networkid, 0 );
112  a->execute( context );
113  }
114  */
115 
116 
117  bool somethingDestroyed = (battle->dv.damage >= 100 || battle->av.damage >= 100);
118 
119  RecalculateAreaView rav ( getMap(), target, maxViewRange / maxmalq + 1, &context );
120  if ( somethingDestroyed )
121  rav.removeView();
122 
123  battle->setresult ( context );
124 
125  if ( somethingDestroyed )
126  // computeview( getMap(), 0, false, &context );
127  rav.addView();
128 
129 
130  if ( context.display && shown )
131  context.display->displayMap();
132 
133  return ActionResult(0);
134 }
135 
136 
138 {
140  if ( context.display )
141  context.display->displayMap();
142  return ActionResult(0);
143 }
144 
145 
146 
147 namespace {
148  const bool r1 = registerAction<VehicleAttackAction> ( ActionRegistry::VehicleAttack );
149 }
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
int GameActionID
Definition: action.h:35
virtual GameActionID getID() const
virtual void readData(tnstream &stream)
Vehicle * vehicle
Definition: mapfield.h:89
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
void writeData(tnstream &stream) const
Definition: unitaction.cpp:75
virtual void displayActionCursor(int x1, int y1, int x2, int y2)=0
void read(tnstream &stream)
Definition: typen.h:213
GameMap * getMap()
Definition: action.h:92
ASCString toString(bool coordinates=false) const
Definition: typen.cpp:304
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
bool fieldvisiblenow(const MapField *pe, Vehicle *veh, int player)
{@
Definition: spfst.cpp:399
#define maxmalq
Constants that specify the layout of ASC.
Definition: typen.h:429
virtual void writeData(tnstream &stream) const
void write(tnstream &stream) const
Definition: typen.h:212
Vehicle * getUnit(bool dontThrow=false)
Definition: unitaction.cpp:47
Coordinate on the twodimensional map.
Definition: typen.h:202
ASCString getName() const
returns the units name or, if it does not exist, the unit type's name or description ...
Definition: vehicle.cpp:1569
const int maxViewRange
Definition: typen.h:558
virtual void displayMap(void)=0
int evaluateviewcalculation(GameMap *gamemap, int player_fieldcount_mask, bool disableShareView, const Context *context)
evaluates the view on the whole map.
int viewingPlayer
Definition: context.h:40
MapDisplayInterface * display
Definition: context.h:39
virtual ActionResult undoAction(const Context &context)
virtual void removeActionCursor(void)=0
Building * building
Definition: mapfield.h:102
ASCString getDescription() const
virtual ActionResult runAction(const Context &context)
virtual void showBattle(tfight &battle)=0
void readData(tnstream &stream)
Definition: unitaction.cpp:65
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465