📄 game.java
字号:
/* * MegaMek - * Copyright (C) 2000,2001,2002,2003,2004,2005 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;/* Do not use the Sun collections (com.sun.java.util.collections.*) framework * in this class until Java 1.1 compatibility is abandoned or a * non-serialization based save feature is implemented. */import megamek.common.actions.ArtilleryAttackAction;import megamek.common.actions.AttackAction;import megamek.common.actions.EntityAction;import megamek.common.actions.LayMinefieldAction;import megamek.common.event.GameBoardChangeEvent;import megamek.common.event.GameBoardNewEvent;import megamek.common.event.GameEndEvent;import megamek.common.event.GameEntityChangeEvent;import megamek.common.event.GameEntityNewEvent;import megamek.common.event.GameEntityNewOffboardEvent;import megamek.common.event.GameEntityRemoveEvent;import megamek.common.event.GameEvent;import megamek.common.event.GameListener;import megamek.common.event.GameMapQueryEvent;import megamek.common.event.GameNewActionEvent;import megamek.common.event.GamePhaseChangeEvent;import megamek.common.event.GamePlayerChangeEvent;import megamek.common.event.GamePlayerChatEvent;import megamek.common.event.GamePlayerConnectedEvent;import megamek.common.event.GamePlayerDisconnectedEvent;import megamek.common.event.GameReportEvent;import megamek.common.event.GameSettingsChangeEvent;import megamek.common.event.GameTurnChangeEvent;import megamek.common.options.GameOptions;import java.io.Serializable;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import java.util.ArrayList;/** * The game class is the root of all data about the game in progress. * Both the Client and the Server should have one of these objects and it * is their job to keep it synched. */public class Game implements Serializable, IGame{ /** * Define constants to describe the condition a * unit was in when it wass removed from the game. */ private GameOptions options = new GameOptions(); public IBoard board = new Board(); private Vector entities = new Vector(); private Hashtable entityIds = new Hashtable(); /** Track entities removed from the game (probably by death) */ private Vector vOutOfGame = new Vector(); private Vector players = new Vector(); private Vector teams = new Vector(); // DES private Hashtable playerIds = new Hashtable(); /** have the entities been deployed? */ private boolean deploymentComplete = false; /** how's the weather? */ private int windDirection = -1; private int windStrength = -1; private String stringWindDirection; private String stringWindStrength; /** what round is it? */ private int roundCount = 0; /** The current turn list */ private Vector turnVector = new Vector(); private int turnIndex = 0; /** The present phase */ private int phase = PHASE_UNKNOWN; /** The past phase */ private int lastPhase = PHASE_UNKNOWN; // phase state private Vector actions = new Vector(); private Vector pendingCharges = new Vector(); private Vector pendingLayMinefieldActions = new Vector(); private Vector pilotRolls = new Vector(); private Vector extremeGravityRolls = new Vector(); private Vector initiativeRerollRequests = new Vector(); // reports private GameReports gameReports = new GameReports(); private boolean forceVictory = false; private int victoryPlayerId = Player.PLAYER_NONE; private int victoryTeam = Player.TEAM_NONE; private Hashtable deploymentTable = new Hashtable(); private int lastDeploymentRound = 0; private Hashtable minefields = new Hashtable(); private Vector vibrabombs = new Vector(); private Vector offboardArtilleryAttacks = new Vector(); private int lastEntityId; private Vector tagInfoForTurn = new Vector(); private Vector flares = new Vector(); /** * Constructor */ public Game() { } public IBoard getBoard() { return board; } public void setBoard(IBoard board) { IBoard oldBoard = this.board; this.board = board; processGameEvent(new GameBoardNewEvent(this, oldBoard, board)); } public boolean containsMinefield(Coords coords) { return minefields.containsKey(coords); } public Vector getMinefields(Coords coords) { Vector mfs = (Vector) minefields.get(coords); if (mfs == null) { return new Vector(); } return mfs; } public int getNbrMinefields(Coords coords) { Vector mfs = (Vector) minefields.get(coords); if (mfs == null) { return 0; } return mfs.size(); } /** * Get the coordinates of all mined hexes in the game. * * @return an <code>Enumeration</code> of the <code>Coords</code> * containing minefilds. This will not be <code>null</code>. */ public Enumeration getMinedCoords() { return minefields.keys(); } public void addMinefield(Minefield mf) { addMinefieldHelper(mf); processGameEvent(new GameBoardChangeEvent(this)); } public void addMinefields(Vector minefields) { for (int i = 0; i < minefields.size(); i++) { Minefield mf = (Minefield) minefields.elementAt(i); addMinefieldHelper(mf); } processGameEvent(new GameBoardChangeEvent(this)); } public void setMinefields(Vector minefields) { clearMinefieldsHelper(); for (int i = 0; i < minefields.size(); i++) { Minefield mf = (Minefield) minefields.elementAt(i); addMinefieldHelper(mf); } processGameEvent(new GameBoardChangeEvent(this)); } protected void addMinefieldHelper(Minefield mf) { Vector mfs = (Vector) minefields.get(mf.getCoords()); if (mfs == null) { mfs = new Vector(); mfs.addElement(mf); minefields.put(mf.getCoords(), mfs); return; } mfs.addElement(mf); } public void removeMinefield(Minefield mf) { removeMinefieldHelper(mf); processGameEvent(new GameBoardChangeEvent(this)); } public void removeMinefieldHelper(Minefield mf) { Vector mfs = (Vector) minefields.get(mf.getCoords()); if (mfs == null) { return; } Enumeration e = mfs.elements(); while (e.hasMoreElements()) { Minefield mftemp = (Minefield) e.nextElement(); if (mftemp.equals(mf)) { mfs.removeElement(mftemp); break; } } if (mfs.isEmpty()) { minefields.remove(mf.getCoords()); } } public void clearMinefields() { clearMinefieldsHelper(); processGameEvent(new GameBoardChangeEvent(this)); } protected void clearMinefieldsHelper() { minefields.clear(); } public Vector getVibrabombs() { return vibrabombs; } public void addVibrabomb(Minefield mf) { vibrabombs.addElement(mf); } public void removeVibrabomb(Minefield mf) { vibrabombs.removeElement(mf); } public boolean containsVibrabomb(Minefield mf) { return vibrabombs.contains(mf); } public GameOptions getOptions() { return options; } public void setOptions(GameOptions options) { if ( null == options ) { System.err.println( "Can't set the game options to null!" ); } else { this.options = options; processGameEvent(new GameSettingsChangeEvent(this)); } } /** * Return an enumeration of teams in the game */ public Enumeration getTeams() { return teams.elements(); } /** * Return the current number of teams in the game. */ public int getNoOfTeams() { return teams.size(); } public Vector getTeamsVector() { return (Vector)teams.clone(); } /** * Return a players team * Note: may return null if player has no team */ public Team getTeamForPlayer(Player p) { for (Enumeration i = teams.elements(); i.hasMoreElements();) { final Team team = (Team)i.nextElement(); for (Enumeration j = team.getPlayers(); j.hasMoreElements();) { final Player player = (Player)j.nextElement(); if (p == player) { return team; } } } return null; } /** Set up the teams vector. Each player on a team (Team 1 .. Team X) is placed in the appropriate vector. Any player on 'No Team', is placed in their own object */ public void setupTeams() { Vector teams = new Vector(); boolean useTeamInit = getOptions().getOption("team_initiative").booleanValue(); // Get all NO_TEAM players. If team_initiative is false, all // players are on their own teams for initiative purposes. for (Enumeration i = getPlayers(); i.hasMoreElements();) { final Player player = (Player)i.nextElement(); if ( !useTeamInit || player.getTeam() == Player.TEAM_NONE ) { Team new_team = new Team(Player.TEAM_NONE); new_team.addPlayer(player); teams.addElement(new_team); } } if (useTeamInit) { // Now, go through all the teams, and add the apropriate player for (int t = Player.TEAM_NONE + 1; t < Player.MAX_TEAMS; t++) { Team new_team = null; for (Enumeration i = getPlayers(); i.hasMoreElements();) { final Player player = (Player)i.nextElement(); if (player.getTeam() == t) { if (new_team == null) { new_team = new Team(t); } new_team.addPlayer(player); } } if (new_team != null) { teams.addElement(new_team); } } } this.teams = teams; } /** * Return an enumeration of player in the game */ public Enumeration getPlayers() { return players.elements(); } /** * Return the players vector */ public Vector getPlayersVector() { return players; } /** * Return the current number of active players in the game. */ public int getNoOfPlayers() { return players.size(); } /** * Returns the individual player assigned the id parameter. */ public Player getPlayer(int id) { if ( Player.PLAYER_NONE == id ) { return null; } return (Player)playerIds.get(new Integer(id)); } public void addPlayer(int id, Player player) { player.setGame(this); players.addElement(player); playerIds.put(new Integer(id), player); updatePlayer(player); } public void setPlayer(int id, Player player) { final Player oldPlayer = getPlayer(id); player.setGame(this); players.setElementAt(player, players.indexOf(oldPlayer));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -