Introduction

Data\LuaAPI\System\Library.lua is a library written in Lua, built on top of the GameServer functions. This file is managed by the system (do not edit). This page only lists the function sets and their general usage; for details of each function in the Player, Coin, Item... groups, see the function reference pages.

Helpers and Item.RandomLevel

FunctionEffect
Helpers.RGB(r, g, b)Combines a color into one number: r + g * 256 + b * 65536.
Helpers.MakeItemId(group, index)Item code = group * 512 + index.
Helpers.GetItemType(itemId), Helpers.GetItemIndex(itemId)Split an item code into the group / the index within the group.
Item.RandomLevel(min, max)A random item level from min to max. This function is provided by GameServer (Library.lua does not redefine it): min and max are clamped to the range 0 to 15 (the maximum item level), and if min is greater than max the two numbers are swapped. See Items (Item).

Currency by type (Coin.*)

Use the constants COIN_ZEN (0), COIN_WCOIN (1), COIN_GOBLIN_POINT (2), COIN_RUUD (3) (see Constants).

FunctionEffectReturns
Coin.IsValidType(coinType)Whether the currency type is valid.boolean
Coin.GetName(coinType)The currency name ("Zen", "WCoin", "Goblin Point", "Ruud").string; empty if the type is wrong
Coin.GetByType(aIndex, coinType)The character's amount of that currency.number
Coin.Check(aIndex, coinType, amount)Whether there is enough currency (amount must be >= 0).boolean
Coin.Charge(aIndex, coinType, amount)Deducts the currency if there is enough.true if deducted
Coin.Give(aIndex, coinType, amount)Adds currency (amount must be > 0).true if added

ActionTick (3 time-marker slots for each character)

Each character has 3 slots (slot = 0, 1, 2) to store a number and a short name, commonly used as a timestamp to prevent repeated clicking (cooldown). The data is cleared when the character exits.

FunctionEffect
ActionTick.Set(aIndex, slot, tick, name)Writes the tick marker (a number) and a name (truncated to at most 15 characters). Returns false if slot is wrong or the character is invalid.
ActionTick.Get(aIndex, slot)Reads the stored number; 0 if none.
ActionTick.GetName(aIndex, slot)Reads the stored name; empty string if none.
ActionTick.Clear(aIndex, slot)Clears the slot.

Scheduler (event schedule from an XML file)

Reads the schedule from an XML file, checks every second and emits the events onScheduleNotice, onScheduleStart, onScheduleEnd to the attached modules (see Attaching a module to events).

<EventScheduler>
	<Event Type="0" Name="Boss Hunt" NoticePeriod="300" Duration="600">
		<Start Year="-1" Month="-1" Day="-1" DayOfWeek="-1" Hour="20" Minute="0" Second="0"/>
	</Event>
</EventScheduler>
  • -1 = any. DayOfWeek: 0 = Sunday.
  • Type: the event identifier number (set by you); NoticePeriod: the number of seconds to warn before the start (0 = no advance notice); Duration: duration in seconds, must be greater than 0.
  • Events the module receives: OnScheduleNotice(type, secondsLeft), OnScheduleStart(type), OnScheduleEnd(type).
  • Check mode: by default the Scheduler checks only once per minute (near the start of the minute) and matches only down to the minute (ignoring Second). If any <Start> tag of any event has Second other than -1, the whole schedule switches to second mode: it checks every second, and a <Start> tag with Second="-1" is interpreted as second 0.
  • The advance notice is exact only when NoticePeriod is a multiple of 60 (minute mode), or in second mode with any value. In minute mode, a NoticePeriod not divisible by 60 still emits the notice once but rounded down to the minute (for example 90 seconds is emitted when 60 seconds remain, and under 60 seconds is emitted at the same time as the start), while the secondsLeft parameter is still the declared NoticePeriod value. To get second-accurate notices, use a multiple of 60 or set Second in a <Start> tag.
  • An event that is in progress does not start again until it ends (Duration runs out or Scheduler.StopEvent is called).
FunctionEffect
Scheduler.LoadFromXML(path)Loads the schedule from an XML file (path as in File.ReadXml), replacing the whole existing schedule. Returns true; false (with an error log) if the file cannot be read.
Scheduler.GetEventCount(), Scheduler.GetEventName(type)Number of loaded events; event name.
Scheduler.IsEventActive(type)Whether the event is in progress.
Scheduler.GetElapsedTime(type), Scheduler.GetRemainingTime(type), Scheduler.GetProgress(type)Elapsed time, remaining time, progress of an event in progress.
Scheduler.StopEvent(type)Ends the event in progress immediately (emits OnScheduleEnd).
Scheduler.CheckEventNotices(), CheckEventStarts(), CheckEventEnds(), Scheduler.Process()Internal functions called by Timers.lua every second; normally there is no need to call them.

GetElapsedTime returns the number of seconds elapsed (-1 if the event is not in progress). GetRemainingTime returns the number of seconds remaining (-1 if not in progress or Duration = 0). GetProgress returns a percentage 0..100 (-1 in the two cases above). Duration must be greater than 0. If left at 0 (or missing), the event does not end by itself: OnScheduleEnd is emitted only when you call Scheduler.StopEvent.

EventMonsterTracker (tracking the monsters / NPCs of an event)

Helps an event create monsters or NPCs and then manage and clean them up. Monsters are identified by index + monster type (an index can be reused after a monster is deleted).

FunctionEffect
EventMonsterTracker.Register(eventType, index, objectClass, objectType), RegisterNPC(eventType, index, npcClass)Adds a created monster / NPC to the event's tracking list.
EventMonsterTracker.Unregister(eventType, index), IsTracked, GetObjectInfo, GetMonsterClassStop tracking / query information.
EventMonsterTracker.GetMonsters(eventType, filter), GetCount(eventType, filter), GetActiveEvents()List / count.
EventMonsterTracker.SpawnAndRegister(eventType, monsterClass, map, x1, y1, x2, y2, element, dir)Creates 1 monster at a random point in the area and tracks it. Returns the monster index, -1 on failure.
EventMonsterTracker.SpawnWaveAndRegister(eventType, monsterClass, count, map, x1, y1, x2, y2, element, dir)Creates count monsters, returns the list of indexes.
EventMonsterTracker.CleanupEvent(eventType, filter), CleanupMonsters, CleanupNPCsDeletes the objects that still exist and stops tracking; returns the number deleted.
EventMonsterTracker.CleanupDead(eventType, filter), ClearAll()Stops tracking objects that are dead / already deleted (deletes nothing from the game) / clears the entire tracking list.

When LuaAPI is reloaded or GameServer is shut down, the library automatically deletes the monsters / NPCs being tracked (the new script does not know about them).

OptionBonus (adding stats from multiple modules)

OptionBonus.Set(aIndex, "MyModule", { AddDamage = 100, MulMaxHP = 5 })
OptionBonus.Set(aIndex, "MyModule", nil)        -- remove MyModule's part

Each module sets only its own part (source is the module name); the parts are added together into the character's stat table and then the stats are recalculated. OptionBonus.Set returns true if the stat table actually changed; OptionBonus.Get(aIndex, source) reads one module's part. The data is cleared when the character exits and when LuaAPI is reloaded. For the stat names (AddDamage, AddMaxHP, ...) see the function reference pages Player.GetOptionTable / Player.SetOptionTable.

The Player object (accessed through fields)

Instead of calling many functions, get an object and read / write its fields directly. Each read / write goes straight to GameServer (no stale copy is kept).

local p = Player.GetObject(aIndex)          -- or Player.GetObjectByName("Name")
if p then
	p.Money = p.Money + 1000
end
  • Player.GetObject(aIndex) returns the object, or nil if it is not an online character. Player.GetObjectByName(name) looks up by name. Player.IsObjectValid(obj) checks whether the object still points to that same character.
  • The object remembers the character it was created for: when the character exits and the index is reused by someone else, reads return nil and writes have no effect.
  • Readable fields include (excerpt): Level, MasterLevel, MajesticLevel, Reset, Money, Ruud, PKLevel, PKCount, PKTimer, Skin, Strength, Dexterity, Vitality, Energy, Leadership, Life, MaxLife, Mana, MaxMana, Shield, MaxShield, BP, MaxBP, GuildName, IsLive, IsOffline, IsGameMaster...
  • Writable fields: Level, MasterLevel, MajesticLevel, Reset, LevelUpPoint, MasterPoint, MajesticPoint, Money, Ruud, PKLevel, PKCount, PKTimer, Skin, the 5 stats, Life, MaxLife, Mana, MaxMana, Shield, MaxShield, BP, MaxBP.
  • Writing to a read-only or nonexistent field causes the error Player object: field '...' is read-only or unknown.