LuaLibraryTracker.wiki
author Wuzzy
Wed, 02 May 2018 23:32:42 +0100
changeset 1364 a0547181393a
parent 1363 77bece04d563
child 1365 f7ca5312d2ff
permissions -rw-r--r--
LuaLibraryTracker: TOC 2

#summary Lua library documentation: Tracker
#labels !LuaLibrary
= Lua library: `Tracker` =
<wiki:toc max_depth="3" />

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

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

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

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

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

=== `decreaseGearValue(gear, value)` ===
Decrease a gear value 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)` ===
Increase a team value by 1. `team` is the team name. This function *must not* be called if the current value is not a number.

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

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

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

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

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

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