📄 building.java
字号:
/* * MegaMek - Copyright (C) 2000-2002 Ben Mazur (bmazur@sev.org) * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. */package megamek.common;import java.util.ArrayList;import java.util.Vector;import java.util.Enumeration;import java.io.Serializable;/** * This class represents a single, possibly multi-hex building on the board. * * @author Suvarov454@sourceforge.net (James A. Damour ) * @version $Revision: 4726 $ */public class Building implements Serializable { // Private attributes and helper functions. /** * The ID of this building. */ private int id = Building.UNKNOWN; /** * The coordinates of every hex of this building. */ private Vector coordinates = new Vector(); /** * The construction type of the building. */ private int type = Building.UNKNOWN; /** * The current construction factor of the building. * Any damage immediately updates this value. */ private int currentCF = Building.UNKNOWN; /** * The construction factor of the building at the start of this * attack phase. Damage that is received during the phase is * applied at the end of the phase. */ private int phaseCF = Building.UNKNOWN; /** * The name of the building. */ private String name = null; /** * Flag that indicates whether this building is burning */ private boolean burning = false; public class DemolitionCharge implements Serializable { public int damage; public int playerId; public DemolitionCharge(int playerId, int damage) { this.damage = damage; this.playerId = playerId; } } private ArrayList demolitionCharges = new ArrayList(); // Public and Protected constants, constructors, and methods. /** * Update this building to include the new hex (and all hexes off * the new hex, which aren't already included). * * @param coords - the <code>Coords</code> of the new hex. * @param board - the game's <code>IBoard</code> object. * @exception an <code>IllegalArgumentException</code> will be thrown if * the given coordinates do not contain a building, or if the * building covers multiple hexes with different CF. */ protected void include( Coords coords, IBoard board, int structureType ) { // If the hex is already in the building, we've covered it before. if ( this.isIn( coords ) ) { return; } // Get the nextHex hex. IHex nextHex = board.getHex( coords ); if(null == nextHex || !(nextHex.containsTerrain(structureType))) return; if(structureType == Terrains.BUILDING) { // Error off if the building type or CF is off. if ( this.type != nextHex.terrainLevel( Terrains.BUILDING ) ) { throw new IllegalArgumentException ( "The coordinates, " + coords.getBoardNum() + ", should contain the same type of building as " + ( (Coords) this.coordinates.elementAt(0)).getBoardNum() ); } boolean hexHasCF = nextHex.containsTerrain( Terrains.BLDG_CF ); if ( (hexHasCF && this.currentCF != nextHex.terrainLevel( Terrains.BLDG_CF )) || (!hexHasCF && this.currentCF != getDefaultCF( this.type )) ) { throw new IllegalArgumentException ( "The coordinates, " + coords.getBoardNum() + ", should contain a building with the same CF as " + ( (Coords) this.coordinates.elementAt(0)).getBoardNum() ); } } // We passed our tests, add the next hex to this building. this.coordinates.addElement( coords ); // Walk through the exit directions and // identify all hexes in this building. for ( int dir = 0; dir < 6; dir++ ) { // Does the building exit in this direction? if ( nextHex.containsTerrainExit( structureType, dir ) ) { this.include( coords.translated(dir), board, structureType ); } } } // End void protected include( Coords, Board ) /** * Generic flag for uninitialized values. */ protected static final int UNKNOWN = -1; /** * Various construction types. */ public static final int LIGHT = 1; public static final int MEDIUM = 2; public static final int HEAVY = 3; public static final int HARDENED= 4; /** * Construct a building for the given coordinates from the * board's information. If the building covers multiple hexes, * every hex will be included in the building. * * @param coords - the <code>Coords</code> of a hex of the building. * If the building covers multiple hexes, this constructor will * include them all in this building automatically. * @param board - the game's <code>Board</code> object. * @exception an <code>IllegalArgumentException</code> will be thrown if * the given coordinates do not contain a building, or if the * building covers multiple hexes with different CFs. */ public Building( Coords coords, IBoard board, int structureType ) { // The ID of the building will be the hashcode of the coords. // ASSUMPTION: this will be unique ID across ALL the building's // hexes for ALL the clients of this board. this.id = coords.hashCode(); // The building occupies the given coords, at least. this.coordinates.addElement( coords ); // Get the Hex for those coords. IHex startHex = board.getHex( coords ); // Read our construction type from the hex. if ( !startHex.containsTerrain( structureType ) ) { throw new IllegalArgumentException( "The coordinates, " + coords.getBoardNum() + ", do not contain a building." ); } this.type = startHex.terrainLevel( structureType ); // Insure that we've got a good type (and initialize our CF). this.currentCF = getDefaultCF( this.type ); if ( this.currentCF == Building.UNKNOWN ) { throw new IllegalArgumentException( "Unknown construction type: " + this.type + ". The board is invalid." ); } // Now read the *real* CF, if the board specifies one. if ( structureType == Terrains.BUILDING && startHex.containsTerrain( Terrains.BLDG_CF ) ) { this.currentCF = startHex.terrainLevel( Terrains.BLDG_CF ); } if ( structureType == Terrains.BRIDGE && startHex.containsTerrain( Terrains.BRIDGE_CF ) ) { this.currentCF = startHex.terrainLevel( Terrains.BRIDGE_CF ); } this.phaseCF = this.currentCF; // Walk through the exit directions and // identify all hexes in this building. for ( int dir = 0; dir < 6; dir++ ) { // Does the building exit in this direction? if ( startHex.containsTerrainExit( structureType, dir ) ) { this.include( coords.translated(dir), board, structureType ); } } // Set the building's name. StringBuffer buffer = new StringBuffer(); if (structureType == Terrains.FUEL_TANK) { buffer.append("Fuel Tank #"); } else if (structureType == Terrains.BUILDING) { buffer.append("Building #"); } else if (structureType == Terrains.BRIDGE) { buffer.append("Bridge #"); } else { buffer.append("Structure #"); } buffer.append( this.id ); this.name = buffer.toString(); } // End public Building( Coords, Board ) /** * Creates a new building of the specified type, name, ID, and * coordinates. Do *not* use this method unless you have carefully * examined this class. The construction factors for the building
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -