LuaLibraryTracker.wiki
author Wuzzy
Wed, 02 May 2018 23:44:01 +0100
changeset 1366 680a173ead44
parent 1365 f7ca5312d2ff
child 1367 831b860f835d
permissions -rw-r--r--
LuaLibraryTracker: TOC -1

#summary Lua library documentation: Tracker
#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. The function must take a gear as a parameter, nothing else, for example:
<code language="lua">function killall(gear)
    SetHealth(gear, 0)
end

function onGearDelete(gear)
    if GetGearType(gear) == gtTarget then
        runOnHogs(killall)
    end
end</code>
This will kill all hogs if a target is destroyed.

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).

=== `trackTeams()` ===
Will start the tracking of teams, clans and hedgehogs (hogs can be tracked without this).

=== `trackHiding(gear)` ===
Will keep track of the given hedgehog gear when it is hidden.

=== `trackRestoring(gear)` ===
Will keep track of the given hedgehog gear when it is un-hidden.

== “`runOn`” functions ==
These functions are used to run a function on tracked gears, teams and clans.

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

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

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

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

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

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

== 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 will have a simple key-value storage. 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.