Class TrainAmmoSharing

java.lang.Object
megamek.common.equipment.TrainAmmoSharing

public final class TrainAmmoSharing extends Object
Stateless rules helper for ammunition shared along a tractor-and-trailer train. A unit may feed its weapons from the ammo bins of the unit it tows and the unit towing it, so an ammo bin is not necessarily owned by the unit firing it. Sharing reaches one coupling in each direction and no further; see canShareAmmoWith(megamek.common.units.Entity, megamek.common.units.Entity) for the ruling.

This is the single definition of which units may supply each other. The client offers exactly this set in the ammo dropdown and the server validates against it, so the two cannot drift apart.

Extracted from Entity and TWGameManager so the rule does not add to either of those already very large classes; this mirrors the codebase's other Entity-operating utilities (BridgeLayerLogic, megamek.common.compute.Compute).
  • Method Details

    • getSharedAmmo

      public static List<AmmoMounted> getSharedAmmo(Entity shooter)
      Parameters:
      shooter - the unit whose weapons are being loaded
      Returns:
      every ammo bin the unit may draw from, in display order: its own bins first, then those of the unit towing it, then those of the unit it tows. Units not in a train return only their own bins.
    • canShareAmmoWith

      public static boolean canShareAmmoWith(Entity shooter, Entity ammoCarrier)
      Whether one unit may fire another's ammo. The server must check this because the ammo bin named in an ammo change packet arrives from the client.

      Only the units on either side of a coupling share, not the whole train. Xotl ruled on this in the official rules Q&A (battletech.com forums, topic 74296): "Only vehicles directly coupled can share ammo", and in a trailer-tractor-trailer arrangement "the tractor may pull from either trailer, but one trailer may not pull from the other".

      This makes a Mobile Long Tom convoy weaker than its fluff: three carriages hold about 75 rounds between them, but the gun only ever reaches the 25 in the carriage hitched to it. That is the ruling, not an oversight. Do not widen this to the whole train on the strength of the convoy description or of TM's "Trailers act as part of the Tractor for movement, stacking and firing" - the Q&A was asked about exactly this case.
      Parameters:
      shooter - the unit firing the weapon
      ammoCarrier - the unit that owns the ammo bin
      Returns:
      true when the carrier is the shooter itself or a unit directly connected to it in the same train, false otherwise
    • dropUncoupledAmmoLinks

      public static void dropUncoupledAmmoLinks(Entity entity)
      Drops any weapon link pointing at ammo the unit may no longer fire, and reloads the weapon from a bin it may still use.

      A weapon linked to a trailer's ammo bin keeps that link when the train uncouples, and the firing path never re-checks the coupling: AmmoWeaponHandler.checkAmmo takes whatever getLinked returns, and the server only consults canShareAmmoWith(megamek.common.units.Entity, megamek.common.units.Entity) when the player changes ammo. Left alone, a tractor goes on firing a detached trailer's ammo from any distance. Call this on every unit whose train membership changed.

      The replacement is chosen before the stale link is cleared, so a unit that carries the same munition itself keeps firing that munition rather than falling back to whatever sits in its first bin.
      Parameters:
      entity - the unit whose weapon links should be re-checked
    • relinkExternalAmmo

      public static void relinkExternalAmmo(Entity entity, @Nullable Game game)
      Reconnects weapons linked to another unit's ammo bin after the owning unit has been transferred.

      Packets are Java-serialized object graphs and a Mounted holds a hard reference to its owning entity, so a weapon linked to a trailer's bin drags a copy of that trailer along with it. The receiving side would otherwise hold a detached duplicate rather than the unit the game knows about. Links to bins the unit owns itself are left alone, and a link whose carrier has left the game is cleared so nothing keeps firing a detached copy.

      Parameters:
      entity - the unit whose weapon links should be checked
      game - the game holding the canonical units, or null to do nothing