# Class Ship

java.lang.Object
it.polimi.ingsw.is25am31.model.player.Ship

All Implemented Interfaces:
  `Serializable`

public class Ship extends Object implements Serializable
Represents a player's spaceship in the game, managing components placement, grid layout, combat mechanics, and ship validation. The ship consists of a 5x7 grid where components can be placed. It includes functionality for: Component placement and removal, Combat mechanics (cannon, meteor impacts), Resource management (batteries, goods, crew), Ship validation and connectivity checks, Reserved component management. The ship always contains a MainCabin at the center position (2,3) which serves as the core component for connectivity validation.

See Also:
  - Serialized Form

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `Ship()`
  Creates an empty ship with default grid size 5x7 and empty component lists.
  `Ship(Gameboard gameboard)`
  Constructs a ship initialized for the given gameboard level.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `activateCannon(int x1, int y1, int x2, int y2)`
  Activates a cannon at (x1,y1) consuming one battery from (x2,y2).
  `void`
  `activateEngine(int x1, int y1, int x2, int y2)`
  Activates an engine at (x1,y1) consuming one battery from (x2,y2).
  `void`
  `activateShield(int x1, int y1, int x2, int y2)`
  Activates a shield generator at (x1,y1) consuming one battery from (x2,y2).
  `boolean`
  `addComponent(int line, int column, Component component)`
  Attempts to add a component at specified grid position if slot is usable.
  `boolean`
  `addComponentFromShip(int line, int column, int x, int y)`
  Moves a reserved component from (x,y) into a usable slot (line,column).
  `int`
  `getAlienCount(AlienColor color)`
  Counts aliens of given color currently in all cabins except main cabin.
  `int`
  `getBatteryCount()`
  Sums remaining battery units from all battery components.
  `double`
  `getCannonPower()`
  Calculates ship's total cannon firepower, boosted by purple aliens.
  `Component`
  `getComponent(int line, int colomn)`
  Retrieves component at given coordinates.
  `List<Component>`
  `getComponents()`
  Returns list of all active components on ship.
  `int`
  `getCrewNumber()`
  Calculates total crew aboard (humans + aliens).
  `int`
  `getCrewNumberWithoutAlien()`
  Calculates crew number excluding aliens.
  `int`
  `getEnginePower()`
  Calculates ship's total engine thrust, boosted by brown aliens.
  `int`
  `getExposedConnectors()`
  Counts the total number of exposed non-engine, non-cannon connectors on ship perimeter.
  `int`
  `getGoodCount()`
  Counts total goods stored across all cargo holds.
  `Component[][]`
  `getGrid()`
  Returns the 2D array of components on the ship grid.
  `Component`
  `getReservedComponent(int index)`
  Retrieves a reserved component by reserve index (0 or 1).
  `List<Component>`
  `getReservedComponentsList()`
  Returns list of currently reserved components.
  `boolean`
  `getUsableSlot(int line, int column)`
  Checks if a slot at given coordinates is available for placement or reservation.
  `void`
  `hitByCannon(int x, Side side, Size size)`
  Handles a cannon hit result: determines if a component is destroyed and removes it.
  `void`
  `hitByMeteor(int x, Side side, Size size)`
  Handles a meteor impact: determines component destruction based on size and side.
  `void`
  `removeBattery(int line, int column)`
  Decreases battery capacity by one unit at specified slot.
  `boolean`
  `removeComponent(int line, int column)`
  Removes the last-added component at specified slot if it matches removal order.
  `void`
  `removeGoods(int line, int column, Good good)`
  Removes one unit of specified good from cargo hold at (line,column) following priority order.
  `void`
  `setColor(Color color)`
  Sets the color of the ship's main cabin for player identification.
  `void`
  `validateShipNextPhase()`
  Validates ship structure at end of phase and removes disconnected or conflicting components and reserved components

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### Ship

    public Ship()
    Creates an empty ship with default grid size 5x7 and empty component lists.

  - ### Ship

    public Ship(Gameboard gameboard)
    Constructs a ship initialized for the given gameboard level. Loads usable slots configuration from JSON resources and places the MainCabin at center.
    Parameters:
      `gameboard` - Gameboard on which the ship is placed.

- ## Method Details

  - ### getUsableSlot

    public boolean getUsableSlot(int line, int column)
    Checks if a slot at given coordinates is available for placement or reservation.
    Parameters:
      `line` - Row index in grid.
      `column` - Column index in grid.
    Returns:
      true if slot is usable; false otherwise.

  - ### getGrid

    public Component[][] getGrid()
    Returns the 2D array of components on the ship grid.
    Returns:
      Component grid.

  - ### activateCannon

    public void activateCannon(int x1, int y1, int x2, int y2)
    Activates a cannon at (x1,y1) consuming one battery from (x2,y2). Restores battery if activation fails.
    Parameters:
      `x1` - Cannon's row.
      `y1` - Cannon's column.
      `x2` - Battery's row.
      `y2` - Battery's column.
    Throws:
      `RuntimeException` - if coordinates invalid or components mismatch.

  - ### activateEngine

    public void activateEngine(int x1, int y1, int x2, int y2)
    Activates an engine at (x1,y1) consuming one battery from (x2,y2). Restores battery if activation fails.
    Parameters:
      `x1` - Engine's row.
      `y1` - Engine's column.
      `x2` - Battery's row.
      `y2` - Battery's column.
    Throws:
      `RuntimeException` - if coordinates invalid or components mismatch.

  - ### activateShield

    public void activateShield(int x1, int y1, int x2, int y2)
    Activates a shield generator at (x1,y1) consuming one battery from (x2,y2). Restores battery if activation fails.
    Parameters:
      `x1` - Shield's row.
      `y1` - Shield's column.
      `x2` - Battery's row.
      `y2` - Battery's column.
    Throws:
      `RuntimeException` - if coordinates invalid or components mismatch.

  - ### setColor

    public void setColor(Color color)
    Sets the color of the ship's main cabin for player identification.
    Parameters:
      `color` - Color to apply.

  - ### addComponent

    public boolean addComponent(int line, int column, Component component)
    Attempts to add a component at specified grid position if slot is usable. Handles special reserve slots in first row columns 5 and 6.
    Parameters:
      `line` - Row index.
      `column` - Column index.
      `component` - Component to place.
    Returns:
      true if placement succeeds.
    Throws:
      `RuntimeException` - if slot invalid or already occupied.

  - ### getReservedComponent

    public Component getReservedComponent(int index)
    Retrieves a reserved component by reserve index (0 or 1).
    Parameters:
      `index` - Position in reservation list.
    Returns:
      Reserved Component or null.

  - ### getReservedComponentsList

    public List<Component> getReservedComponentsList()
    Returns list of currently reserved components.
    Returns:
      List of reserved components (size 2).

  - ### getAlienCount

    public int getAlienCount(AlienColor color)
    Counts aliens of given color currently in all cabins except main cabin.
    Parameters:
      `color` - AlienColor to count.
    Returns:
      Number of aliens of that color.

  - ### addComponentFromShip

    public boolean addComponentFromShip(int line, int column, int x, int y)
    Moves a reserved component from (x,y) into a usable slot (line,column).
    Parameters:
      `line` - Destination row.
      `column` - Destination column.
      `x` - Original row of reserved component.
      `y` - Original column of reserved component.
    Returns:
      true if move succeeds.
    Throws:
      `RuntimeException` - if invalid position or no reserved component at origin.

  - ### removeComponent

    public boolean removeComponent(int line, int column)
    Removes the last-added component at specified slot if it matches removal order.
    Parameters:
      `line` - Row index.
      `column` - Column index.
    Returns:
      false always.
    Throws:
      `RuntimeException` - if removal is not last component.

  - ### getComponent

    public Component getComponent(int line, int colomn)
    Retrieves component at given coordinates.
    Parameters:
      `line` - Row index.
      `colomn` - Column index.
    Returns:
      Component at that slot or throws if out of bounds.

  - ### getExposedConnectors

    public int getExposedConnectors()
    Counts the total number of exposed non-engine, non-cannon connectors on ship perimeter.
    Returns:
      Number of exposed connectors.

  - ### getCrewNumber

    public int getCrewNumber()
    Calculates total crew aboard (humans + aliens).
    Returns:
      Sum of crew members across components.

  - ### getCrewNumberWithoutAlien

    public int getCrewNumberWithoutAlien()
    Calculates crew number excluding aliens.
    Returns:
      Number of human crew members.

  - ### getEnginePower

    public int getEnginePower()
    Calculates ship's total engine thrust, boosted by brown aliens.
    Returns:
      Engine power value.

  - ### getCannonPower

    public double getCannonPower()
    Calculates ship's total cannon firepower, boosted by purple aliens.
    Returns:
      Cannon power as double.

  - ### getComponents

    public List<Component> getComponents()
    Returns list of all active components on ship.
    Returns:
      List of components.

  - ### getGoodCount

    public int getGoodCount()
    Counts total goods stored across all cargo holds.
    Returns:
      Number of goods.

  - ### getBatteryCount

    public int getBatteryCount()
    Sums remaining battery units from all battery components.
    Returns:
      Battery count.

  - ### removeGoods

    public void removeGoods(int line, int column, Good good)
    Removes one unit of specified good from cargo hold at (line,column) following priority order.
    Parameters:
      `line` - Row of cargo hold.
      `column` - Column of cargo hold.
      `good` - Good to remove.
    Throws:
      `RuntimeException` - if slot invalid or good not present.

  - ### removeBattery

    public void removeBattery(int line, int column)
    Decreases battery capacity by one unit at specified slot.
    Parameters:
      `line` - Row index.
      `column` - Column index.
    Throws:
      `RuntimeException` - if component missing, not a battery, or no capacity.

  - ### hitByCannon

    public void hitByCannon(int x, Side side, Size size)
    Handles a cannon hit result: determines if a component is destroyed and removes it.
    Parameters:
      `x` - Dice result indicating impact line/column index.
      `side` - Side from which shot arrives.
      `size` - Size of the cannonball (SMALL or LARGE).

  - ### hitByMeteor

    public void hitByMeteor(int x, Side side, Size size)
    Handles a meteor impact: determines component destruction based on size and side.
    Parameters:
      `x` - Impact coordinate adjusted by side.
      `side` - Side from which meteor arrives.
      `size` - Meteor size (SMALL or LARGE).

  - ### validateShipNextPhase

    public void validateShipNextPhase()
    Validates ship structure at end of phase and removes disconnected or conflicting components and reserved components