Micropolis
ToolEffects Class Reference

#include <tool.h>

Public Member Functions

 ToolEffects (Micropolis *sim)
 
void clear ()
 
void modifyWorld ()
 
bool modifyIfEnoughFunding ()
 
MapValue getMapValue (const Position &pos) const
 
MapValue getMapValue (int x, int y) const
 
MapTile getMapTile (const Position &pos) const
 
MapTile getMapTile (int x, int y) const
 
int getCost () const
 
void addCost (int amount)
 
void setMapValue (const Position &pos, MapValue mapVal)
 
void setMapValue (int x, int y, MapValue mapVal)
 
void addFrontendMessage (FrontendMessage *msg)
 

Private Attributes

Micropolissim
 Simulator to get map values from, and to apply changes.
 
int cost
 Accumulated costs.
 
WorldModificationsMap modifications
 Collected world modifications.
 
FrontendMessages frontendMessages
 Collected messages to send.
 

Detailed Description

Class for storing effects of applying a tool to the world.

When applying a tool, two things change:

  • The world map.
  • The funds of the player.
  • Messages sent to the player and the front-end.
  • Sounds played for the player.

The funds gives a decision problem. To decide whether the tool can be applied, you need to know the cost. To know the cost you need to know the exact changes being made. The simplest way to compute the exact changes is to simply apply the tool to the world. This holds especially when tools get stacked on top of each other.

This class provides an easy way out, greatly simplifying the problem. All tools do not modify the world directly, but instead put their results in an instance of this class, thus collecting all the modifications. After the whole operation is 'done', the ToolEffects instance can tell the precise cost and what has been changed in the world. At that moment, the yes/no decision can be made, and the effects can be copied to the real map and funds.

Todo:
Extend the class for storing messages and sounds.

Definition at line 203 of file tool.h.

Constructor & Destructor Documentation

◆ ToolEffects()

ToolEffects::ToolEffects ( Micropolis mpolis)

Constructor.

Definition at line 86 of file tool.cpp.

References clear(), and sim.

Member Function Documentation

◆ addCost()

void ToolEffects::addCost ( int  amount)
inline

Add some amount to the total.

Definition at line 275 of file tool.h.

References cost.

Referenced by Micropolis::buildBuilding(), Micropolis::bulldozerTool(), Micropolis::prepareBuildingSite(), Micropolis::putDownLand(), and Micropolis::putDownNetwork().

◆ addFrontendMessage()

void ToolEffects::addFrontendMessage ( FrontendMessage msg)
inline

◆ clear()

void ToolEffects::clear ( )

Reset all effects.

Definition at line 103 of file tool.cpp.

References cost, frontendMessages, and modifications.

Referenced by modifyWorld(), and ToolEffects().

◆ getCost()

int ToolEffects::getCost ( ) const
inline

Get the total cost collected so far.

Returns
Total cost.

Definition at line 266 of file tool.h.

References cost.

◆ getMapTile() [1/2]

MapTile ToolEffects::getMapTile ( const Position pos) const
inline

Get the tile of a map position.

Parameters
posPosition being queried.
Returns
Tile at the specified position.
Precondition
Position must be within map limits

Definition at line 242 of file tool.h.

References getMapValue(), and LOMASK.

Referenced by Micropolis::fixSingle(), Micropolis::layRail(), Micropolis::layRoad(), Micropolis::layWire(), Micropolis::prepareBuildingSite(), Micropolis::putDownLand(), Micropolis::putDownNetwork(), Micropolis::putDownWater(), and Micropolis::putRubble().

◆ getMapTile() [2/2]

MapValue ToolEffects::getMapTile ( int  x,
int  y 
) const
inline

Get the tile of a map position.

Parameters
xHorizontal coordinate of position being queried.
yVertical coordinate of position being queried.
Returns
Tile at the specified position.
Precondition
Position must be within map limits

Definition at line 256 of file tool.h.

References getMapValue(), and LOMASK.

◆ getMapValue() [1/2]

MapValue ToolEffects::getMapValue ( const Position pos) const

Get a map value from the world. Unlike the simulator world, this method takes modifications made previously by (other) tools into account.

Parameters
posPosition of queried map value. Position must be on-map.
Returns
Map value of the queried position.

Definition at line 169 of file tool.cpp.

References Micropolis::map, modifications, Position::posX, Position::posY, and sim.

Referenced by Micropolis::bulldozerTool(), Micropolis::connectTile(), Micropolis::forestTool(), getMapTile(), getMapValue(), Micropolis::layDoze(), and Micropolis::smoothTreesAt().

◆ getMapValue() [2/2]

MapValue ToolEffects::getMapValue ( int  x,
int  y 
) const
inline

Get the value of a map position.

Parameters
xHorizontal coordinate of position being queried.
yVertical coordinate of position being queried.
Returns
Map value at the specified position.
Precondition
Position must be within map limits

Definition at line 290 of file tool.h.

References getMapValue().

◆ modifyIfEnoughFunding()

bool ToolEffects::modifyIfEnoughFunding ( )

If there are enough funds, apply the effects.

Returns
The effects have been applied.
Note
If funding was not adequate, the object is not modified (that is, the effects are still available).

Definition at line 152 of file tool.cpp.

References modifyWorld(), sim, and Micropolis::totalFunds.

◆ modifyWorld()

void ToolEffects::modifyWorld ( )

Perform the effects stored in the object to the simulator world.

Parameters
simSimulator object to modify.
Postcondition
Effects have been copied to the simulator world, and the ToolEffects object is ready for the next use.

Definition at line 121 of file tool.cpp.

References clear(), cost, frontendMessages, Micropolis::map, modifications, Position::posX, Position::posY, sim, Micropolis::spend(), and Micropolis::updateFunds().

Referenced by modifyIfEnoughFunding(), and Micropolis::smoothTreesAt().

◆ setMapValue() [1/2]

void ToolEffects::setMapValue ( const Position pos,
MapValue  mapVal 
)

Set a new map value.

Parameters
posPosition to set.
mapValValue to set.

Definition at line 186 of file tool.cpp.

References modifications.

Referenced by Micropolis::layRail(), Micropolis::layRoad(), Micropolis::layWire(), Micropolis::prepareBuildingSite(), Micropolis::putDownForest(), Micropolis::putDownLand(), and setMapValue().

◆ setMapValue() [2/2]

void ToolEffects::setMapValue ( int  x,
int  y,
MapValue  mapVal 
)
inline

Set a new map value.

Parameters
posPosition to set.
xHorizontal coordinate of position to set.
yVertical coordinate of position to set.
mapValValue to set.

Definition at line 303 of file tool.h.

References setMapValue().


The documentation for this class was generated from the following files: