Advanced Strategic Command
discoverresources.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 "discoverresources.h"
23 #include "action-registry.h"
24 
25 #include "../gamemap.h"
26 
28  : UnitAction( unit)
29 {
30  shareview = 1 << ( unit->color / 8);
31  for ( int i = 0; i < 8; i++ )
32  if ( i*8 != unit->color )
33  if ( getMap()->player[i].exist() )
34  if ( getMap()->getPlayer(unit).diplomacy.sharesView(i) )
35  shareview += 1 << i;
36 
37 }
38 
39 
41 {
42  return "Discover Resources";
43 }
44 
45 
47 {
48  pos.read( stream );
49  fuel = stream.readInt();
50  material = stream.readInt();
51  visible = stream.readInt();
52  player = stream.readInt();
53 }
54 
56 {
57  pos.write( stream );
58  stream.writeInt( fuel );
59  stream.writeInt( material );
60  stream.writeInt( visible );
61  stream.writeInt( player );
62 }
63 
65 {
66  this->player = player;
67  if ( fld->resourceview ) {
68  fuel = fld->resourceview->fuelvisible[player];
69  material = fld->resourceview->materialvisible[player];
70  visible = fld->resourceview->visible & (1 << player);
71  } else {
72  fuel = 0;
73  material = 0;
74  visible = 0;
75  }
76 }
77 
79 {
80  if ( !fld->resourceview )
82  fld->resourceview->fuelvisible[player] = fuel;
83  fld->resourceview->materialvisible[player] = material;
84  if ( visible )
85  fld->resourceview->visible |= (1 << player);
86  else
87  fld->resourceview->visible &= ~(1 << player);
88 }
89 
90 
92 {
93  int version = stream.readInt();
94  if ( version != 1 )
95  throw tinvalidversion ( "DiscoverResources", 1, version );
96 
97  readClassContainer( newState, stream );
98  readClassContainer( oldState, stream );
99 };
100 
101 
103 {
104  stream.writeInt( 1 );
105  writeClassContainer( newState, stream );
106  writeClassContainer( oldState, stream );
107 };
108 
109 
111 {
113 }
114 
115 
116 
117 void DiscoverResources::testField( const MapCoordinate& mc )
118 {
119  MapField* fld = getMap()->getField ( mc );
120  if ( !fld->building || fld->building->color == getMap()->actplayer*8 || fld->building->color == 8*8)
121  if ( !fld->vehicle || fld->vehicle->color == getMap()->actplayer*8 || fld->vehicle->color == 8*8) {
122  if ( !fld->resourceview )
124 
125  for ( int c = 0; c < 8; c++ )
126  if ( shareview & (1 << c) ) {
127 
128  if ( !(fld->resourceview->visible & (1 << c)) || fld->resourceview->materialvisible[c] != fld->material || fld->resourceview->fuelvisible[c] != fld->fuel ) {
129  ResourceViewState rvs( fld, c);
130  rvs.pos = mc;
131  oldState.push_back( rvs );
132 
133 
134  ResourceViewState newView;
135  newView.fuel = fld->fuel;
136  newView.material = fld->material;
137  newView.visible = true;
138  newView.player = c;
139  newView.pos = mc;
140 
141  newState.push_back( newView );
142  newView.writeToField ( fld );
143  }
144  }
145  }
146 }
147 
148 
150 {
151  if ( !getUnit()->typ->hasFunction( ContainerBaseType::DetectsMineralResources ) )
152  return ActionResult(311);
153 
154  circularFieldIterator( getMap(), getUnit()->getPosition(), 0, getUnit()->typ->digrange, FieldIterationFunctor(this, &DiscoverResources::testField ));
155 
156  return ActionResult(0);
157 }
158 
159 
161 {
162  for ( ViewState::iterator i = oldState.begin(); i != oldState.end(); ++i ) {
163  MapField* fld = getMap()->getField( i->pos );
164  /*
165  if ( fld->visible != newState[i->first] ) {
166  ASCString msg;
167  msg.format( "; expected: %x ; found %x" , newState[i->first], fld->visible );
168  throw ActionResult(21207, "Position is " + i->first.toString(true) + msg );
169  }
170  */
171  i->writeToField( fld );
172  }
173  return ActionResult(0);
174 }
175 
176 namespace {
177  const bool r1 = registerAction<DiscoverResources> ( ActionRegistry::DiscoverResources );
178 }
179 
Uint8 materialvisible[8]
Definition: mapfield.h:110
virtual GameActionID getID() const
void initFromField(const MapField *fld, int player)
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
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 ActionResult runAction(const Context &context)
ASCString getDescription() const
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
Uint8 material
Definition: mapfield.h:54
virtual void writeData(tnstream &stream) const
DiplomaticStateVector diplomacy
Definition: player.h:209
void read(tnstream &stream)
Definition: typen.h:213
bool sharesView(PlayerID receivingPlayer) const
Definition: player.h:81
GameMap * getMap()
Definition: action.h:92
The interface for all kinds of IO stream.
a single field of the map
Definition: mapfield.h:26
virtual void readData(tnstream &stream)
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
Vehicle * getUnit(bool dontThrow=false)
Definition: unitaction.cpp:47
Uint8 fuel
mineral resources on this field.
Definition: mapfield.h:54
Coordinate on the twodimensional map.
Definition: typen.h:202
void circularFieldIterator(GameMap *gamemap, const MapCoordinate &center, int startDist, int stopDist, FieldIterationFunctor functor)
void readClassContainer(C &c, tnstream &stream)
Definition: basestrm.h:752
Player player[9]
Definition: gamemap.h:253
virtual ActionResult undoAction(const Context &context)
int color
The owner of the container.
Building * building
Definition: mapfield.h:102
void writeClassContainer(const C &c, tnstream &stream)
Definition: basestrm.h:742
DiscoverResources(Vehicle *unit)
void write(tnstream &stream) const
bool exist() const
does the player exist at all
Definition: player.cpp:313
Loki::Functor< void, LOKI_TYPELIST_1(const MapCoordinate &) > FieldIterationFunctor
Definition: mapalgorithms.h:43
Resourceview * resourceview
the mineral resources that were seen by a player on this field; since the actual amount may have decr...
Definition: mapfield.h:114
MapField * getField(int x, int y)
Definition: gamemap.h:465