battlecore::weapon

The weaponmodule controls firing and reloading, reports trigger and busy state, equips weapons and manages the weapons a player carries, and reads or writes the weapon's ammo and shot configuration. Include its declarations with include "./battlecore/weapon.bst";.

Firing and reloading

native bool battlecore::weapon.fire (int rounds = 1, int shotPower = 0, bool isCritical = false);
native bool battlecore::weapon.getCanFire (int rounds = 1);
native void battlecore::weapon.reload ();
native void battlecore::weapon.effectPulse (int ms = 20);
  • fire — sends a shot. Returns true when a shot left the barrel. Returns false when the weapon refused: too few rounds in the magazine, the weapon busy reloading or swapping, or the trigger pulled again before shotIntervalMs has passed. A refused shot spends nothing and sends nothing.
  • getCanFire — returns whether a shot costing rounds would fire right now, for the same three reasons.
  • reload — refills the magazine. A weapon with limited ammo only refills to what its supply still holds.
  • effectPulse — pulses the shot effect output (the solenoid or rumble motor) for ms milliseconds, then switches it off. It does not wait for the pulse to end. The firmware caps the length to protect the solenoid.

rounds is what the shot costs, 1 when left out. 0 is a free shot, which fires even from an empty magazine. 3 might be a burst, and needs three rounds left: with fewer the shot is refused rather than fired short. Either way it counts as one shot fired. shotPower0 means the weapon's own shot power.

The script plays the sound, the muzzle flash and the solenoid pulse around fire, so check what it returns before playing them. Otherwise the player gets every sign of a shot when no shot was sent:

if (!battlecore::weapon.fire()) {
	return;
}

battlecore::weapon.effectPulse(20);

This is better than asking getCanFire first. It is one call instead of two, and nothing can change between the question and the answer.

Status

native bool battlecore::weapon.getIsTriggerDown ();
native bool battlecore::weapon.getIsReloadDown ();
native bool battlecore::weapon.getIsBusy ();
  • getIsTriggerDown — returns whether the trigger is held down.
  • getIsReloadDown — returns whether the reload control is held down.
  • getIsBusy — returns whether the weapon is busy.

Busy count

native bool battlecore::weapon.getBusyCount ();
native void battlecore::weapon.setBusyCount (bool newVal);
native bool battlecore::weapon.incrementBusyCount ();
native bool battlecore::weapon.decrementBusyCount ();
  • getBusyCount / setBusyCount — gets or sets the busy count.
  • incrementBusyCount — increments the busy count.
  • decrementBusyCount — decrements the busy count.

Equipping a weapon

native bool battlecore::weapon.setWeaponId (int weaponId);
native int  battlecore::weapon.getWeaponId ();
native bool battlecore::weapon.getWeaponDefined (int weaponId);
  • setWeaponId — equips a weapon by id. Its whole definition applies at once: shot type, shot power, fire rate, reload delay and magazine, with the magazine filled. The definition comes from the game config or from a scanned NFC tag. Returns false when nothing has defined that id.
  • getWeaponId — returns the id of the weapon held. 0 means none, and an unarmed player cannot fire.
  • getWeaponDefined — returns whether the config or a tag has defined a weapon id. Worth asking before equipping something a tag named.

Inventory

The inventory is what this player carries. Slots are numbered from 1, and 0 means "none" throughout: no slot, no weapon, nothing to swap to.

native any  battlecore::weapon.pickUp (int weaponId, string tagId = "");
native int  battlecore::weapon.swapToNext ();
native bool battlecore::weapon.swapTo (int weaponId, int slot = 0);
native bool battlecore::weapon.resetWeapons ();
native int  battlecore::weapon.getCount ();
native int  battlecore::weapon.getIdAt (int slot);
native int  battlecore::weapon.getSlotOf (int weaponId);
native bool battlecore::weapon.getIsCarried (int weaponId);
native int  battlecore::weapon.addWeapon (int weaponId, int slot = 0);
native bool battlecore::weapon.removeWeapon (int weaponId);
  • pickUp— does everything a scanned weapon tag should. It refuses a weapon the config never defined, follows the game's pickup rule, tops up a weapon already carried, takes an empty slot if there is one, and otherwise replaces the weapon in hand. With a tagId it also refuses a tag already spent this game, and marks the tag spent when it succeeds. Returns an object: {"result": ..., "id": ..., "slot": ..., "replacedId": ...}, where result is one of rejected, tagSpent, replenished, added or replaced.
  • swapToNext — equips the next weapon carried, wrapping round and skipping empty slots. Returns the new weapon id, or 0 when there is nowhere to go.
  • swapTo — equips a weapon, taking a slot for it if it is not already carried. With no slot given, a full inventory loses the weapon held. Give a slot and that one is replaced instead.
  • resetWeapons — goes back to the loadout the config granted, with full magazines and supplies. It does not reset the slot count.
  • getCount — returns how many slots are occupied.
  • getIdAt — returns the weapon in a slot, or 0 for an empty or out-of-range slot.
  • getSlotOf — returns which slot holds a weapon, or 0 when it is not carried.
  • getIsCarried — returns whether a weapon is carried.
  • addWeapon — puts a weapon in the inventory without equipping it. Returns the slot it landed in, or 0 when there was no room. Give a slot to replace what is there.
  • removeWeapon — takes a weapon out. If it was the one in hand, the player is left holding nothing.

Slots

native int  battlecore::weapon.getSlots ();
native any  battlecore::weapon.setSlots (int newVal);
native int  battlecore::weapon.getSlotsInitial ();
native void battlecore::weapon.setSlotsInitial (int newVal);
native int  battlecore::weapon.getSlotsMax ();
native void battlecore::weapon.setSlotsMax (int newVal);
  • getSlots — returns how many weapons this player can carry. It belongs to the player, so two players in one game can differ.
  • setSlots — grants or takes away slots. Shrinking below what the player holds drops the highest slots and what is in them. Zero is allowed. Returns the ids dropped.
  • getSlotsInitial / setSlotsInitial — the slot count the config granted.
  • getSlotsMax / setSlotsMax — the most slots any upgrade may grant.

Limited ammo

native bool battlecore::weapon.getFiniteAmmo ();
native int  battlecore::weapon.getTotalAmmo ();
native void battlecore::weapon.setTotalAmmo (int newVal);
  • getFiniteAmmo — returns whether the weapon held draws from a limited supply.
  • getTotalAmmo / setTotalAmmo — gets or sets what is left of that supply. The supply includes the rounds in the magazine, so a reload moves nothing and costs nothing.

Spent tags

native bool battlecore::weapon.getTagUsed (string tagId);
native bool battlecore::weapon.setTagUsed (string tagId);
native void battlecore::weapon.clearTagsUsed ();

Whether a tag has been spent this game. The key is the tag's own id, not the weapon it describes, so a second tag carrying the same weapon still works.

Fields on every shot

native bool battlecore::weapon.setShotField (string key, int value);
native bool battlecore::weapon.clearShotFields ();

Adds a field to every shot this tagger fires, for the IR protocol to use. A protocol that has no such field ignores it. Infection sets lwZombieon a zombie, so in a LaserWar game its shots go out as LaserWar's own zombie. A value of 0 removes the field, and a new game starts with none. The shot's own fields (playerId, teamId, hitValue, bulletTypeId, isCritical) cannot be set this way, and return false.

Charge state

native bool battlecore::weapon.getIsChargeBased ();
native bool battlecore::weapon.getIsCharging ();
native void battlecore::weapon.setIsCharging (bool newVal);
  • getIsChargeBased — returns whether the weapon is charge based.
  • getIsCharging / setIsCharging — gets or sets whether the weapon is charging.

Ammo and shot configuration (getters and setters)

Each of the following exposes a getter and a setter. Setters take a single newVal argument of the type shown.

GetterSetter
native int battlecore::weapon.getAmmoMax ();native void battlecore::weapon.setAmmoMax (int newVal);
native int battlecore::weapon.getAmmo ();native void battlecore::weapon.setAmmo (int newVal);
native int battlecore::weapon.getShotType ();native void battlecore::weapon.setShotType (int newVal);
native int battlecore::weapon.getShotPower ();native void battlecore::weapon.setShotPower (int newVal);
native int battlecore::weapon.getShotIntervalMs ();native void battlecore::weapon.setShotIntervalMs (int newVal);
native int battlecore::weapon.getShotDelayMs ();native void battlecore::weapon.setShotDelayMs (int newVal);
native int battlecore::weapon.getShotChargeMinMs ();native void battlecore::weapon.setShotChargeMinMs (int newVal);
native int battlecore::weapon.getShotChargeMaxMs ();native void battlecore::weapon.setShotChargeMaxMs (int newVal);
native bool battlecore::weapon.getShotChargeAutoFire ();native void battlecore::weapon.setShotChargeAutoFire (bool newVal);
native int battlecore::weapon.getReloadDelayMs ();native void battlecore::weapon.setReloadDelayMs (int newVal);
native int battlecore::weapon.getShotsFired ();native void battlecore::weapon.setShotsFired (int newVal);