LuaLibraryUtils.wiki
author Wuzzy <Wuzzy@disroot.org>
Mon, 27 May 2024 19:59:44 +0200
changeset 2270 22a964a1fe85
parent 2264 e7c3385d41a0
permissions -rw-r--r--
Add Worms/Hedgewars taunt conversions

#summary Lua library documentation of Utils; contains assorted helper functions
#labels !LuaLibrary
= Lua library: `Utils` =

This library includes miscellaneous functions to use, they are all independent of each other and can be used everywhere.

== `gearIsInBox(gear, x, y, w, h)` ==

Returns whether the gear is inside (centre point of the gear) a box with x and y as the top left corner and having the width and height of w and h respectively.


== `gearIsInCircle(gear, x, y, r, useRadius)` ==

Returns whether the gear is inside a circle with x and y being the centre point and r being the radius of the circle. The boolean useRadius determine whether only the centre point of the gear will be used or the radius of the gear will be checked too.

== `fillMap([flush])` ==
Completely fill the current map with land. Requires `MapGen` to be set to `mgDrawn`. `FlushPoints()` will be called at the end, unless `flush` is set to `false`.

== `eraseMap([flush])` ==
Completely erase all land from current map. Requires `MapGen` to be set to `mgDrawn`. `FlushPoints()` will be called at the end, unless `flush` is set to `false`.

== `updateChallengeRecord(recordType, value[, stat])` ==
Report a new team record attempt in a [Missions mission] (preferably challenge missions). In missions, the team file of the mission team (see `AddMissionTeam` for more infos) can store several records (such as highscores) which will be shown in the frontend when the mission is selected. Note this only works for non-campaign missions so far.

Calling this function means the team has made a record attempt of a given record type (e.g. fastest time or highscore) with a given `value`. If the team's own record has been beaten, or is has set the first record, the team file is being updated.

Usually you want to call this function at the end of a mission, when it has completed and the final score/time/whatever is known.

Parameters:

 * `recordType`: Type of record (see [ConfigurationFiles#%5BMission%2520%3CMISSION_NAME%3E%5D here] for a list), as a string
 * `value`: Value that has been achieved by the team
 * `stat`: If `true` (default), will also write a message in the stats screen (whether record has been beaten, or current record if not). Otherwise, this writes nothing.

Example:
<code lang="lua">
-- Report a team score of 12000 to the game
updateChallengeRecord("Highscore", 12000)
</code>

== `getReadableChallengeRecord(recordType)` ==
Returns a human-readable description of a team record of the mission team (see also: `AddMissionTeam`) for the current challenge [Missions mission]. Team records are read from the team file. `recordType` is a record type. Possible record types are listed [ConfigurationFiles#%5BMission%2520%3CMISSION_NAME%3E%5D here]. This can be useful to display something in `ShowMission`.

If the team does not have a record for the given record type yet, the empty string is returned. If the record type is invalid, `nil` is returned.

For example, for `"TimeRecord"`, the returned string is something like `"Team's best time: 14.340s"`.

Example:
<code lang="lua">
ShowMission("MegaRace", "Race Challenge", "Try to finish the race as fast you can!\n" .. getReadableChallengeRecord("TimeRecord"))
-- Shows the mission panel and the current time record.
</code>

== `integerSqrt(num)` ==
Calculates the square root of `num`, but rounded to an integer. This function is desync-safe.

== `integerHypotenuse(x, y)` ==
Calculates the hypotenuse of a triangle with legs x, y, but rounded to an integer. This function is desync-safe.

== `formatEngineString(text, ...)` (1.1.0-dev) ==
Insert parameters %1 to %9 into an engine string and returns the result.

 * `text`: engine string with parameters (from `GetEngineString`)
 * `...`: Variable number of arguments to insert into the string, if the engine string needs them (placeholders like `%1`). The number of arguments *must* match the number of available arguments of the engine string

Example:

<code language="lua">
formatEngineString(GetEngineString("TMsgStrId", sidWinner), "My Team")
-- ... to create a string showing the winning team. In English, this will return "My Team wins!".</code>

== Standardized land tint colors ==
There are some “standard” tint colors for placed girders and other sprites which are defined to allow more consistency, if it is desired. These values are used in !TechRacer, !HedgeEditor and the basic movement training to colorize girders consistently:

 * `U_LAND_TINT_NORMAL`: Normal land
 * `U_LAND_TINT_ICE`: Icy land
 * `U_LAND_TINT_INDESTRUCTIBLE`: Indestructible land
 * `U_LAND_TINT_BOUNCY`: Bouncy land