LuaLibraryTracker.wiki
author sheepluva
Thu, 23 Mar 2023 20:47:32 +0000
changeset 2221 4bf8172a73a1
parent 1397 63655141bb8f
permissions -rw-r--r--
TableOfContents: TableOfContents: Adjust two Entries

#summary Lua library documentation of Tracker; for keeping track of gears
#labels !LuaLibrary
= Lua library: `Tracker` =
<wiki:toc max_depth="2" />

== Introduction ==
This Lua library is intended to be used if you need to keep track of gears. It can also keep track of teams and clans and as an extra functionality it can also store variables for a gear, team or clan.

To set it up you need to add some hooks in different events. The hooks `trackGear` and `trackDeletion` to `onGearAdd` and `onGearDelete` respectively. It is strongly recommended to only track the gears you are interested in as, especially with snow on, the amount of gears can go up high and that will slow down the script. To keep track of teams you need to keep track of `gtHedgehog` and `gtResurrector` (if resurrection might be used) and add `trackTeams` to `onGameStart`.

If you want to call a function on a couple of gears you will have to call the “`runOn`” functions. To these you will pass the function you want to be run as a variable to the function.

=== Examples ===
Here's a minimal example which tracks all hedgehogs and kills them all when a target is destroyed:
<code language="lua">HedgewarsScriptLoad("Scripts/Tracker.lua")

function onGearAdd(gear)
    if GetGearType(gear) == gtHedgehog then
        trackGear(gear)
    end
end

local function killAll(gear)
    SetHealth(gear, 0)
end

function onGearDelete(gear)
    if GetGearType(gear) == gtTarget then
        runOnGears(killAll)
    end
end</code>

To see a commented example of `Tracker` in use by a script you can look at
[https://hg.hedgewars.org/hedgewars/file/default/share/hedgewars/Data/Scripts/Multiplayer/Random_Weapon.lua Random Weapons].

== Tracking functions ==
These functions must be called to start tracking stuff. *IMPORTANT:* The other functions will only work on tracked objects. So make sure you track the things you care about.

=== `trackGear(gear)` ===
Will keep track of the gear.

=== `trackDeletion(gear)` ===
Will stop keeping track of the gear (gears not tracked will be ignored).

=== `trackHiding(gear)` ===
Will keep track of the given hedgehog gear when it becomes hidden (e.g. with `HideHog`).

=== `trackRestoring(gear)` ===
Will keep track of the given hedgehog gear when it becomes restored, i.e. no longer hidden (e.g. with `RestoreHog`).

=== `trackTeams()` ===
Will start the tracking of teams, clans and hedgehogs (individual hogs can be tracked without this). If you want to use any of the team or clan-related functions below, you *must* call this function in `onGameStart`.



== “`runOn`” functions ==
These functions are used to run a function on _tracked_ gears, teams and clans. The passed function is assumed to take a single argument, which is a gear ID.

=== `runOnGears(func)` ===
Runs the function `func` on all tracked gears.

=== `runOnHogs(func)` ===
Runs the function `func` on all tracked hedgehogs.

Requires `trackTeams` to be called beforehand.

=== `runOnHogsInTeam(func, team)` ===
Runs the function `func` on all tracked hedgehogs in the specified team (`team` = team name).

Requires `trackTeams` to be called beforehand.

=== `runOnHogsInOtherTeams(func, team)` ===
Runs the function `func` on all tracked hedgehogs but those in the specified team (`team` = team name).

Requires `trackTeams` to be called beforehand.

=== `runOnHogsInClan(func, clan)` ===
Runs the function `func` on all tracked hedgehogs in the specified clan (`clan` = clan ID).

Requires `trackTeams` to be called beforehand.

=== `runOnHogsInOtherClans(func, clan)` ===
Runs the function `func` on all tracked hedgehogs but those in the specified clan (`clan` = clan ID).

Requires `trackTeams` to be called beforehand.



== Helper functions ==
=== `getFirstHogOfClan(clan)` ===
Returns the first hedgehog (alive or not) in the clan with clan ID `clan`.



== Key-value storage ==
For tracked gears, teams and clans, you can assign an arbitrary number of values. Each tracked object has a simple key-value storage. This means, a tracked object has an arbitrary number of keys, each of which holds one arbitrary value. Any data type (besides `nil`) can be used for keys and values. Initially, all keys have the value `nil`.

=== `getGearValue(gear, key)` ===
Returns the gear value of the given `gear` for `key`.

=== `setGearValue(gear, key, value)` ===
Sets the gear value for given `key` to `value`.

=== `increaseGearValue(gear, key)` ===
Increases the gear value for `key` by 1. This function *must not* be called if the current value is not a number.

=== `decreaseGearValue(gear, key)` ===
Decreases the gear value for `key` by 1. This function *must not* be called if the current value is not a number.

=== `getTeamValue(team, key)` ===
Returns the value of the given team (`team` is the team name) for the specified `key`.

=== `setTeamValue(team, key, value)` ===
Sets the team value with given `key` to `value`. `team` is the team name.

=== `increaseTeamValue(team, key)` ===
Increases the team value for `key` by 1. `team` is the team name. This function *must not* be called if the current value is not a number.

=== `decreaseTeamValue(team, key)` ===
Decreases the team value for `key` by 1. `team` is the team name. This function *must not* be called if the current value is not a number.

=== `getClanValue(clan, key)` ===
Returns the value of the given clan (`clan` is the clan ID) for the specified `key`.

=== `setClanValue(clan, key, value)` ===
Sets the clan value with given `key` to `value`. `clan` is the clan ID.

=== `increaseClanValue(clan, key)` ===
Increases the clan value for `key` by 1. `clan` is the clan ID. This function *must not* be called if the current value is not a number.

=== `decreaseClanValue(clan, key)` ===
Decreases the clan value for `key` by 1. `clan` is the clan ID. This function *must not* be called if the current value is not a number.

=== Examples ===
Assuming `gear` is a tracked gear, here are some simple usage examples:

<code lang="lua">
setGearValue(gear, "mutant", true)
local isMutant = getGearValue(gear, "mutant")
print(isMutant) -- prints "true"
</code>

<code lang="lua">
setGearValue(gear, "score", 0)
increaseGearValue(gear, "score")
local score = getGearValue(gear, "score")
print(score) -- prints "1"
</code>