[OTS] Lists of unique numbers kept in ‘player storage’

Friend asked me about code to keep ‘auto loot lists’ in storage. So player can configure few lists of items and then just switch between them, when he goes to other spawn with different loot.

-- config
local listsStartStorage = 550000000
local maximumListsCount = 10
local maximumElementsPerList = 100

-- variables for calculations
local listOffset = maximumElementsPerList + 1

function lists_getLists(player)
    local result = {}
    for i = 1, maximumListsCount do
        result[i] = lists_getListElementsCount(player, i)
    end

    return result
end

function lists_getListElementsCount(player, listId)
    local startStorage = listsStartStorage + listOffset * listId
    return math.max(0, player:getStorage(startStorage))
end

function lists_getListElements(player, listId)
    local result = {}
    local startStorage = listsStartStorage + listOffset * listId
    local listElementsCount = lists_getListElementsCount(player, listId)
    for i = startStorage + 1, startStorage + listElementsCount do
        table.insert(result, player:getStorage(i))
    end

    return result
end

function lists_removeAllListElements(player, listId)
    local startStorage = listsStartStorage + listOffset * listId
    local listElementsCount = lists_getListElementsCount(player, listId)
    for i = startStorage + 1, startStorage + listElementsCount + 1 do
        player:setStorage(i, -1)
    end
    player:setStorage(startStorage, -1)
end

function lists_getListElement(player, listId, elementKey)
    local elementStorage = listsStartStorage + listOffset * listId + elementKey
    return player:getStorage(elementStorage)
end

local function lists_assertListId(listId)
    assert(listId >= 1, 'minimum list id is 1')
    assert(listId <= maximumListsCount, 'maximum list id is 1' .. maximumListsCount)
end

local function lists_assertElementKey(elementKey)
    assert(elementKey >= 1, 'minimum list element id is 1')
    assert(elementKey <= maximumElementsPerList, 'maximum list element id is ' .. maximumElementsPerList)
end

-- this is for internal use only, should not be used by user
local function lists_setListElement(player, listId, elementKey, elementValue)
    lists_assertListId(listId)
    lists_assertElementKey(elementKey)
    local elementStorage = listsStartStorage + listOffset * listId + elementKey
    player:setStorage(elementStorage, elementValue)
end

-- this is for internal use only, should not be used by user
local function lists_setListElementsCount(player, listId, count)
    lists_assertListId(listId)
    lists_assertElementKey(count)
    local startStorage = listsStartStorage + listOffset * listId
    player:setStorage(startStorage, count)
end

function lists_addListElement(player, listId, elementValue)
    local elements = lists_getListElements(player, listId)
    for k, v in pairs(elements) do
        if v == elementValue then
            return
        end
    end

    local elementsCount = lists_getListElementsCount(player, listId)
    lists_setListElement(player, listId, elementsCount + 1, elementValue)
    lists_setListElementsCount(player, listId, elementsCount + 1)
end

function lists_removeListElement(player, listId, elementValue)
    local elements = lists_getListElements(player, listId)
    local elementToRemovePosition = nil
    for k, v in pairs(elements) do
        if v == elementValue then
            elementToRemovePosition = k
            break
        end
    end

    if elementToRemovePosition then
        local elementsCount = lists_getListElementsCount(player, listId)
        for i = elementToRemovePosition + 1, elementsCount  do
            local nextElementValue = lists_getListElement(player, listId, i)
            lists_setListElement(player, listId, i - 1, nextElementValue)
        end
        lists_setListElement(player, listId, elementsCount, -1)
        lists_setListElementsCount(player, listId, elementsCount - 1)
    end
end

TESTS:

Player = {}
function Player:new()
    o = {storages = {}}
    setmetatable(o, self)
    self.__index = self
    return o
end

function Player.getStorage(self, k)
    if (self.storages[k]) then
        return self.storages[k]
    end
    return -1
end

function Player.setStorage(self, k, v)
    if (v == -1) then
        v = nil
    end
    self.storages[k] = v
end

function Player.dumpStorages(self)
    local storages = 'storages: '
    for k, v in pairs(self.storages) do
        storages = storages .. k .. '=' .. v .. ', '
    end
    print(storages)
end

local function printPlayerListsElementsCount(player)
    local playerLists = lists_getLists(player)
    for k, v in pairs(playerLists) do
        local listElementsString = ''
        for k2, v2 in pairs(lists_getListElements(player, k)) do
            listElementsString = listElementsString .. ' ' .. k2 .. '=' .. v2 .. ', '
        end
        print ('list', k, 'elements', v, 'values', listElementsString)
    end
end

player = Player:new()

print()
player:dumpStorages()
print('start - empty list')
printPlayerListsElementsCount(player)

print()
player:dumpStorages()
lists_addListElement(player, 2, 2001)
lists_addListElement(player, 2, 2002)
print('added 2 elements to list 2')
player:dumpStorages()
printPlayerListsElementsCount(player)

print()
player:dumpStorages()
lists_addListElement(player, 1, 1001)
lists_addListElement(player, 1, 1002)
lists_addListElement(player, 1, 1003)
lists_addListElement(player, 1, 1004)
lists_addListElement(player, 1, 1005)
print('added 5 elements to list 1')
player:dumpStorages()
printPlayerListsElementsCount(player)

print()
player:dumpStorages()
lists_removeListElement(player, 1, 1001)
print('removed first element from list 1')
player:dumpStorages()
printPlayerListsElementsCount(player)

print()
player:dumpStorages()
lists_removeListElement(player, 1, 1005)
print('removed last element from list 1')
player:dumpStorages()
printPlayerListsElementsCount(player)

print()
player:dumpStorages()
lists_removeListElement(player, 1, 1003)
print('removed middle element from list 1')
player:dumpStorages()
printPlayerListsElementsCount(player)

print()
player:dumpStorages()
lists_removeAllListElements(player, 1)
print('removed all elements from list 1')
player:dumpStorages()

You can paste code and tests to online tool like: https://www.tutorialspoint.com/execute_lua_online.php

Compile otservbr and TFS 1.3 on Ubuntu 20.04 and Debian 10/11

Dockerfiles tested on 2021-08-24. You can comment otservbr / forgottenserver part to compile just one sources.

Ubuntu 20.04

FROM ubuntu:20.04

RUN apt-get update

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/London

RUN apt-get -y install tzdata

# 1: forgottenserver (TFS)
RUN apt-get -y install git cmake build-essential libluajit-5.1-dev zip ca-certificates pkg-config autoconf libmariadb-dev-compat libboost-date-time-dev libboost-filesystem-dev libboost-system-dev libboost-iostreams-dev libpugixml-dev libcrypto++-dev libfmt-dev

RUN cd /home/ && git clone --depth 1 https://github.com/otland/forgottenserver.git

RUN cd /home/forgottenserver/ && mkdir build && cd build && cmake ..
RUN cd /home/forgottenserver/build/ && make -j 16

# 2: otservbr-global
RUN apt-get install -y git cmake build-essential libluajit-5.1-dev zip ca-certificates curl zip unzip tar pkg-config yasm autoconf

RUN cd /home/ && git clone https://github.com/microsoft/vcpkg && cd vcpkg && ./bootstrap-vcpkg.sh
RUN cd /home/vcpkg/ && ./vcpkg --triplet x64-linux install boost-asio boost-filesystem boost-iostreams boost-lockfree boost-system boost-variant cryptopp curl jsoncpp libmariadb pugixml spdlog

RUN cd /home/ && git clone --depth 1 https://github.com/opentibiabr/otservbr-global.git && cd otservbr-global && git checkout develop
RUN cd /home/otservbr-global/ && mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../../vcpkg/scripts/buildsystems/vcpkg.cmake ..
RUN cd /home/otservbr-global/build/ && make -j 16

Debian 11

FROM debian:11

RUN apt-get update

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/London

RUN apt-get -y install tzdata

# 1: forgottenserver (TFS)
RUN apt-get -y install git cmake build-essential libluajit-5.1-dev zip wget ca-certificates pkg-config autoconf libmariadb-dev-compat libboost-date-time-dev libboost-filesystem-dev libboost-system-dev libboost-iostreams-dev libpugixml-dev libcrypto++-dev libfmt-dev

RUN apt remove -y libfmt-dev
RUN cd /root/ && wget https://github.com/fmtlib/fmt/releases/download/7.1.3/fmt-7.1.3.zip && unzip fmt-7.1.3.zip
RUN cd /root/fmt-7.1.3/ && mkdir build && cd build && cmake .. && make -j 16 && make install

RUN cd /home/ && git clone --depth 1 https://github.com/otland/forgottenserver.git

RUN cd /home/forgottenserver/ && mkdir build && cd build && cmake ..
RUN cd /home/forgottenserver/build/ && make -j 16

# 2: otservbr-global
RUN apt-get install -y git cmake build-essential libluajit-5.1-dev wget zip ca-certificates curl zip unzip tar pkg-config yasm autoconf

RUN apt remove -y cmake
RUN cd /root/ && wget https://github.com/Kitware/CMake/releases/download/v3.21.1/cmake-3.21.1.tar.gz
RUN cd /root/ && tar -zxvf cmake-3.21.1.tar.gz && cd cmake-3.21.1 && ./bootstrap -- -DCMAKE_USE_OPENSSL=OFF && make -j 16 && make install

RUN cd /home/ && git clone https://github.com/microsoft/vcpkg && cd vcpkg && ./bootstrap-vcpkg.sh
RUN cd /home/vcpkg/ && ./vcpkg --triplet x64-linux install boost-asio boost-filesystem boost-iostreams boost-lockfree boost-system boost-variant cryptopp curl jsoncpp libmariadb pugixml spdlog

RUN cd /home/ && git clone --depth 1 https://github.com/opentibiabr/otservbr-global.git && cd otservbr-global && git checkout develop
RUN cd /home/otservbr-global/ && mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../../vcpkg/scripts/buildsystems/vcpkg.cmake ..
RUN cd /home/otservbr-global/build/ && make -j 16

Debian 10

FROM debian:10

RUN apt-get update

ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/London

RUN apt-get -y install tzdata

# 1: forgottenserver (TFS)
RUN apt-get -y install git cmake build-essential libluajit-5.1-dev zip wget ca-certificates pkg-config autoconf libmariadb-dev-compat libboost-date-time-dev libboost-filesystem-dev libboost-system-dev libboost-iostreams-dev libpugixml-dev libcrypto++-dev

RUN apt remove -y libfmt-dev
RUN cd /root/ && wget https://github.com/fmtlib/fmt/releases/download/7.1.3/fmt-7.1.3.zip && unzip fmt-7.1.3.zip
RUN cd /root/fmt-7.1.3/ && mkdir build && cd build && cmake .. && make -j 16 && make install

RUN cd /home/ && git clone --depth 1 https://github.com/otland/forgottenserver.git

RUN cd /home/forgottenserver/ && mkdir build && cd build && cmake ..
RUN cd /home/forgottenserver/build/ && make -j 16

# 2: otservbr-global
RUN apt-get install -y git cmake build-essential libluajit-5.1-dev wget zip ca-certificates curl zip unzip tar pkg-config yasm autoconf

RUN apt remove -y cmake
RUN cd /root/ && wget https://github.com/Kitware/CMake/releases/download/v3.21.1/cmake-3.21.1.tar.gz
RUN cd /root/ && tar -zxvf cmake-3.21.1.tar.gz && cd cmake-3.21.1 && ./bootstrap -- -DCMAKE_USE_OPENSSL=OFF && make -j 16 && make install

RUN cd /home/ && git clone https://github.com/microsoft/vcpkg && cd vcpkg && ./bootstrap-vcpkg.sh
RUN cd /home/vcpkg/ && ./vcpkg --triplet x64-linux install boost-asio boost-filesystem boost-iostreams boost-lockfree boost-system boost-variant cryptopp curl jsoncpp libmariadb pugixml spdlog

RUN cd /home/ && git clone --depth 1 https://github.com/opentibiabr/otservbr-global.git && cd otservbr-global && git checkout develop
RUN cd /home/otservbr-global/ && mkdir build && cd build && cmake -DCMAKE_TOOLCHAIN_FILE=../../vcpkg/scripts/buildsystems/vcpkg.cmake ..
RUN cd /home/otservbr-global/build/ && make -j 16

[OTS] Check monsters spawn time in spawn.xml

This script reads spawn.xml file and list monsters sorted by spawn time. You can easily detect monsters with too low or too high spawn times.

<?php

$spawnsFile = 'ots-spawn.xml';

function element_attributes($element_name, $xml)
{
    if ($xml == false) {
        return false;
    }
    $found = preg_match(
        '#<' . $element_name .
        '\s+([^>]+(?:"|\'))\s?/?>#',
        $xml,
        $matches
    );
    if ($found == 1) {
        $attribute_array = array();
        $attribute_string = $matches[1];
        $found = preg_match_all(
            '#([^\s=]+)\s*=\s*(\'[^<\']*\'|"[^<"]*")#',
            $attribute_string,
            $matches,
            PREG_SET_ORDER
        );
        if ($found != 0) {
            foreach ($matches as $attribute) {
                $attribute_array[$attribute[1]] =
                    substr($attribute[2], 1, -1);
            }
            return $attribute_array;
        }
    }
    return false;
}

$monsters = [];

foreach (file($spawnsFile) as $lineNo => $line) {
    $line = trim($line);
    if (substr($line, 0, 8) === '<monster') {
        $monsterAttributes = element_attributes('monster', $line);
        if (is_array($monsterAttributes)) {
            if (isset($monsterAttributes['name']) && isset($monsterAttributes['spawntime'])) {
                $name = $monsterAttributes['name'];
                $spawnTime = $monsterAttributes['spawntime'];

                $monsters[] = [
                    'line' => $lineNo,
                    'name' => $monsterAttributes['name'],
                    'spawnTime' => (int)$monsterAttributes['spawntime'],
                ];
            }
        }
    }
}

usort($monsters, function ($a, $b) {
    return $a['spawnTime'] < $b['spawnTime'];
});

foreach ($monsters as $monster) {
    echo $monster['line'] . ',' . $monster['name'] . ',' . $monster['spawnTime'] . PHP_EOL;
}

[TFS 1.x+] How to NOT write LUA scripts or how to crash server by LUA script

There are many tutorials about writing scripts for TFS 1.x, so I decided to write short tutorial how to NOT write scripts for TFS 1.x.
In this tutorial I will describe two common mistakes that result in server crash!

First thing that everyone notice when they change engine from TFS 0.x to 1.x are objects in LUA.
There are many of them, but most popular are: Player, Monster, Npc, Creature, Tile, Item, Container, Position

Thing that most scripters don’t know, is that, these objects are directly bind to address in RAM memory (to C++ objects).
If you try to use C++ object (ex. get name of player) that was deleted, it results in server crash. Bad thing about it is: you cannot check, if object was deleted in C++!
C++ programmers are used to take care of not using deleted objects. LUA scripters in all old engines always get nice error message, when they use variable that does not exist (ex. Creature not found).

There are 2 common mistakes that result in crash. Worst thing about them is that, they don’t crash server always. You can write code like that, test it, put it on your server, many players will use it.. and after week you get random crash.

PROBLEM 1

Passing object to addEvent – addEvent is function that execute given function with some delay. Example – print text after 5 seconds:

function executeMeLater()
    print('printed after 5 seconds!')
end

addEvent(executeMeLater, 5000)

You can pass arguments to function executed with delay:

function executeMeLater(param1, param2)
    print(param1)
    print(param2)
end

addEvent(executeMeLater, 5000, 'test string', 12345)

It will print after 5 seconds:

test string
12345

What is a problem with passing object to it? Object – in RAM memory – can be already deleted!
Example talkaction that crash server:

function buggedEvent1(player)
   print('Execute bugged event 1')
   print(player)
end

function buggedEvent2(hiddenPlayerObject)
   print('Execute bugged event 2')
   print(hiddenPlayerObject.myPlayerVariable:getName())
end

function onSay(player, words, param)
   print('onSay, player name: ' .. player:getName())
-- TRY 1
   addEvent(buggedEvent1, 4000, player)

-- TRY 2
   local hiddenPlayerObject = { myPlayerVariable = player }
   addEvent(buggedEvent2, 5000, hiddenPlayerObject)
   return true
end

What it does, if player stays online for 5 seconds after executing this talkaction:

onSay, player name: Gesior

Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/crashAddEvent.lua:onSay
luaAddEvent(). Argument #3 is unsafe
stack traceback:
    [C]: in function 'addEvent'
    data/talkactions/scripts/crashAddEvent.lua:14: in function <data/talkactions/scripts/crashAddEvent.lua:11>
Execute bugged event 1
268435457
Execute bugged event 2
Gesior

As you can see, there is a simple protection against passing objects as arguments to addEvent. Passing it directly as argument, results in changing object Player to old ‘cid’ version (player temporary ID number).
In second event example I put object Player inside table to bypass protection code. That way I could use ‘:getName()’ on object, as it was not converted to ‘cid’ number.

What it does, if player relog within 5 seconds after executing this talkaction:

onSay, player name: Gesior

Lua Script Error: [TalkAction Interface]
data/talkactions/scripts/crashAddEvent.lua:onSay
luaAddEvent(). Argument #3 is unsafe
stack traceback:
    [C]: in function 'addEvent'
    data/talkactions/scripts/crashAddEvent.lua:14: in function <data/talkactions/scripts/crashAddEvent.lua:11>
Gesior has logged out.
Execute bugged event 1
268435456
Execute bugged event 2
Segmentation fault (core dumped)

SERVER CRASHED!
It tried to load passed Player object (use ‘:getName()’ on it). Player did relog, so given instance of Player was removed in C++.
gdb report after crash:

#0  0x0000000000000000 in ?? ()
No symbol table info available.
#1  0x000055d4fed50615 in LuaScriptInterface::luaCreatureGetName(lua_State*) ()
No symbol table info available.
#2  0x00007f8865d55e37 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
No symbol table info available.
#3  0x00007f8865da327c in lua_pcall () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
No symbol table info available.
#4  0x000055d4fedc9274 in LuaScriptInterface::protectedCall(lua_State*, int, int) ()
No symbol table info available.
#5  0x000055d4fedca368 in LuaScriptInterface::callFunction(int) ()
No symbol table info available.
#6  0x000055d4fed39af2 in LuaEnvironment::executeTimerEvent(unsigned int) ()
No symbol table info available.
#7  0x000055d4fed1eb6e in void std::__invoke_impl<void, void (LuaEnvironment::*&)(unsigned int), LuaEnvironment*&, unsigned int&>(std::__invoke_memfun_deref, void (LuaEnvironment::*&)(unsigned int), LuaEnvironment*&, unsigned int&) ()
No symbol table info available.
#8  0x000055d4fed1bcac in std::__invoke_result<void (LuaEnvironment::*&)(unsigned int), LuaEnvironment*&, unsigned int&>::type std::__invoke<void (LuaEnvironment::*&)(unsigned int), LuaEnvironment*&, unsigned int&>(void (LuaEnvironment::*&)(unsigned int), LuaEnvironment*&, unsigned int&) ()
No symbol table info available.
#9  0x000055d4fed16be2 in void std::_Bind<void (LuaEnvironment::*(LuaEnvironment*, unsigned int))(unsigned int)>::__call<void, , 0ul, 1ul>(std::tuple<>&&, std::_Index_tuple<0ul, 1ul>) ()
No symbol table info available.
#10 0x000055d4fed1048d in void std::_Bind<void (LuaEnvironment::*(LuaEnvironment*, unsigned int))(unsigned int)>::operator()<, void>() ()
No symbol table info available.
#11 0x000055d4fed09916 in std::_Function_handler<void (), std::_Bind<void (LuaEnvironment::*(LuaEnvironment*, unsigned int))(unsigned int)> >::_M_invoke(std::_Any_data const&) ()
No symbol table info available.
#12 0x000055d4febfaec8 in std::function<void ()>::operator()() const ()
No symbol table info available.
#13 0x000055d4febfa9fe in Task::operator()() ()
No symbol table info available.
#14 0x000055d4febfac2c in Dispatcher::threadMain() ()

PROBLEM 2

Keeping object in variable for future use in script.
There is example of common usage in script – keeping last player who executed script (who went into some room, who used some lever):

local lastPlayer = nil

function onSay(player, words, param)
   print('onSay, player name: ' .. player:getName())
   if lastPlayer ~= nil then
      print('onSay, last player name: ' .. lastPlayer:getName())
   end
   lastPlayer = player
   return true
end

You can execute this code as many times you want and it will work fine.. until you relog or player that used it as last, logout and someone else execute it.
gdb report after crash:

#0  0x00007ff55a471d20 in ?? ()
No symbol table info available.
#1  0x000055dffa717615 in LuaScriptInterface::luaCreatureGetName(lua_State*) ()
No symbol table info available.
#2  0x00007ff560fd0e37 in ?? () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
No symbol table info available.
#3  0x00007ff56101e27c in lua_pcall () from /usr/lib/x86_64-linux-gnu/libluajit-5.1.so.2
No symbol table info available.
#4  0x000055dffa790274 in LuaScriptInterface::protectedCall(lua_State*, int, int) ()
No symbol table info available.
#5  0x000055dffa791368 in LuaScriptInterface::callFunction(int) ()
No symbol table info available.
#6  0x000055dffa5f8554 in TalkAction::executeSay(Player*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, SpeakClasses) const ()
No symbol table info available.
#7  0x000055dffa5f81c1 in TalkActions::playerSaySpell(Player*, SpeakClasses, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const ()
No symbol table info available.
#8  0x000055dffa827c4f in Game::playerSaySpell(Player*, SpeakClasses, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
No symbol table info available.
#9  0x000055dffa827910 in Game::playerSay(unsigned int, unsigned short, SpeakClasses, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
No symbol table info available.

How to make scripts safe? Pass cid/guid/itemid/position, not object.
Problem 1 script – safe version:

function buggedEvent1(player)
   print('Execute bugged event 1')
   print(player)
end

function buggedEvent2(hiddenPlayerObject)
   print('Execute bugged event 2')
   -- FIX: TRY TO CREATE OBJECT 'PLAYER' FROM IT'S CID
   local myPlayer = Player(hiddenPlayerObject.myPlayerVariable)
   -- FIX: IT WILL CREATE OBJECT 'PLAYER' OR RETURN 'nil'
   if myPlayer then
      -- FIX: 'if' CHECKED THAT 'myPlayer' IS NOT 'nil',
      -- SO CREATING OBJECT 'PLAYER' WORKED
      -- WE CAN USE 'getName'
      print(myPlayer:getName())
   end
end

function onSay(player, words, param)
   print('onSay, player name: ' .. player:getName())
   -- TRY 1
   addEvent(buggedEvent1, 4000, player)

   -- TRY 2
   -- FIX: PASS ID (cid) OF PLAYER, NOT OBJECT
   local hiddenPlayerObject = { myPlayerVariable = player:getId() }
   addEvent(buggedEvent2, 5000, hiddenPlayerObject)
   return true
end

Problem 2 script – safe version:
Version with player ID (in old scripts called ‘cid’). It’s temporary player ID. It changes when player relog/die:

local lastPlayerCid = nil

function onSay(player, words, param)
   print('onSay, player name: ' .. player:getName())
   -- FIX: TRY TO CREATE OBJECT 'PLAYER' FROM IT'S CID
   local lastPlayer = Player(lastPlayerCid)
   -- FIX: IT WILL CREATE OBJECT 'PLAYER' OR RETURN 'nil'
   if lastPlayer then
      print('onSay, last player name: ' .. lastPlayer:getName())
   end
   -- FIX: STORE ID OF PLAYER (cid), NOT OBJECT
   lastPlayerCid = player:getId()
   return true
end

Version with player GUID. It’s player ID from database. It never changes.

local lastPlayerGuid = nil

function onSay(player, words, param)
   print('onSay, player name: ' .. player:getName())
   -- FIX: TRY TO CREATE OBJECT 'PLAYER' FROM IT'S GUID
   local lastPlayer = Player(lastPlayerGuid)
   -- FIX: IT WILL CREATE OBJECT 'PLAYER' OR RETURN 'nil'
   if lastPlayer then
      print('onSay, last player name: ' .. lastPlayer:getName())
   end
   -- FIX: STORE ID OF PLAYER (cid), NOT OBJECT
   lastPlayerGuid = player:getGuid()
   return true
end

There are 2 main reasons for using ID version:

  • It’s sometimes useful to know that player did relog/die (change ID). If he went to no-logout zone and his ID changed, it means he died.
  • It also works for Monster, Npc and Creature objects. You can store ID of monster/npc/player and load it same way – some LUA scripts work for monsters (ex. movements).

There is one reason for using GUID:

  • Some scripts need to know if player is online, even if he did relog.

[TFS 1.2+] Give item on level advance

Someone asked me on Discord about script that gives configurable rewards on level advance.
It’s onAdvance event. Rewards are easy configurable and can be limited by vocations and level.

local rookVocations = { 0 }
local sorcererVocations = { 1, 5 }
local druidVocations = { 2, 6 }
local paladinVocations = { 3, 7 }
local knightVocations = { 4, 8 }
local mainVocations = { 1, 2, 3, 4, 5, 6, 7, 8 }
local allVocations = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }

local rewardsConfig = {
    -- 2cc and Magic Sword for every 20 level on main
    {
        level = 20,
        vocations = mainVocations,
        storage = 25100,
        items = {
            { 2160, 2 },
            { 2400, 1 },
        }
    },
    -- wand of inferno for 33 sorc
    {
        level = 33,
        vocations = sorcererVocations,
        storage = 25101,
        items = {
            { 2187, 1 },
        }
    }
}

function onAdvance(player, skill, oldLevel, newLevel)
    if skill ~= SKILL_LEVEL then
        return true
    end

    for i1, rewardConfig in pairs(rewardsConfig) do
        if newLevel == rewardConfig.level then
            if table.contains(rewardConfig.vocations, player:getVocation():getId()) then
                if player:getStorageValue(rewardConfig.storage) < 1 then
                    player:setStorageValue(rewardConfig.storage, os.time())
                    for i2, item in pairs(rewardConfig.items) do
                        player:addItem(item[1], item[2])
                    end
                    player:getPosition():sendMagicEffect(CONST_ME_CRAPS)
                    player:sendTextMessage(MESSAGE_INFO_DESCR, "You received reward for getting " .. rewardConfig.level .. " level.")
                end
            end
        end
    end

    return true
end

Gesior2012 – block using monster names as new player name

In acc. maker in file system/load.compat.php find function check_name_new_char and above return true add:

    if (stripos(",The Queen of Banshee,Rook,Orc,",
            ',' . $name . ',') !== false) {
        return false;
    }

Line where you need to paste it: https://github.com/gesior/Gesior2012/blob/268f477369c5c4a8ba88084224142941cba15654/system/load.compat.php#L142

List of monsters of your OTS you can generate using http://halp.skalski.pro/2020/05/21/ots-generating-list-of-monster-names/

TFS – Jak zrobić skrypt Loop, żeby wykonywał się co sekundę przez określoną w skrypcie ilość razy?

W językach skryptowych nie ma znanego z innych języków polecenia ‘sleep’, żeby poczekać np. sekundę – to zatrzymało by działanie całego serwera gry.
W takich językach najczęściej dostępne są funkcje do ‘planowania zdarzeń’ – to znaczy specjalna lista gdzie są spisane funkcje i czas po jakim mają być wykonane.

Przykład w LUA w serwerze gry TFS 1.3 (funkcja do tego służąca: addEvent ).
Do funkcji addEvent można przekazywać parametry, ale nie mogą to być obiekty zdefiniowane w silniku TFS jak Player, Creature, Item itp. – może to doprowadzić do crasha serwera i każde takie użycie generuje komunikat w konsoli.

Ta funkcja (akcja do dodania w actions) wykona się 3 sekundy po użyciu przedmiotu i wyświetli animacje ‘POFF’ na pozycji gracza.
Zamiast przekazywać jako parametr gracza (obiekt Player), przekazujemy jego ID (numer), a w momencie wykonania ‘zdarzenia’ zamieniamy go ponownie na obiekt Player.

function wyslijAnimacjeNaPozycjeGracza(playerId)
   -- zamieniamy liczbę playerId w obiekt player
   local player = Player(playerId)
   -- UWAGA: gracz moze w tym czasie zginac/wylogowac sie, wiec zawsze sprawdzamy czy udalo sie utworzyc obiekt 'player'
   if player then
      player:getPosition():sendMagicEffect(CONST_ME_POFF)
   end
end

function onUse(player, item, fromPosition, target, toPosition, isHotkey)
   addEvent(wyslijAnimacjeNaPozycjeGraczaZaSekunde, 3000, player:getId())
   return true
end

Ta funkcja zostanie wykonana 2 razy w odstępach sekundy:

function twojaFunkcjaDoWykonaniaKilkaRazy(iloscPowtorzen, czasMiedzyPowtorzeniami)
    -- kod odpowiadajacy za powtarzanie
    iloscPowtorzen = iloscPowtorzen - 1
    if (iloscPowtorzen > 0 ) then
        -- parametry przekazywane do funkcji 'addEvent' to kolejno:
        -- 1: nazwa funkcji ktora ma sie wykonac
        -- 2: czas po jakim ma sie wykonac, 1000 = 1 sekunda
        -- 3 i kolejne - sa opcjonalne - wszystkie kolejne parametry zostana przekazane do wykonywanej funkcji w takiej kolejnosci jak zostaly podane
        addEvent(twojaFunkcjaDoWykonaniaKilkaRazy, czasMiedzyPowtorzeniami, iloscPowtorzen, czasMiedzyPowtorzeniami)
    end

    -- kod ktory ma sie wykonywac (twoj wlasny):
    print('zostalo powtorzen:', iloscPowtorzen)
end

twojaFunkcjaDoWykonaniaKilkaRazy(2, 1000, 'testowy test', 12345)

Przykład z tą samą funkcją, ale teraz z przekazaniem parametrów. W funkcji wykonywanej ‘później’ nie masz dostępu do zmiennych które są dostępne w ‘aktualnie’ wykonywanym skrypcie (tym który ‘planuje’ wykonanie funkcji), więc trzeba je przekazać.

function twojaFunkcjaDoWykonaniaKilkaRazyZParametrami(iloscPowtorzen, czasMiedzyPowtorzeniami, parametr1, parametr2)
   -- kod odpowiadajacy za powtarzanie
   iloscPowtorzen = iloscPowtorzen - 1
   if (iloscPowtorzen > 0 ) then
      addEvent(twojaFunkcjaDoWykonaniaKilkaRazyZParametrami, czasMiedzyPowtorzeniami, iloscPowtorzen, czasMiedzyPowtorzeniami, parametr1, parametr2)
   end

   -- kod ktory ma sie wykonywac:
   print('parametr 1 ma wartosc:', parametr1)
   print('parametr 2 ma wartosc:', parametr2)
end

twojaFunkcjaDoWykonaniaKilkaRazyZParametrami(5, 1000, 'testowy test', 12345)

From Windows 10 to OTS development machine for dummies

I made this tutorial on clean machine just after installing Windows 10 Pro (64bit) on 2020-02-16.
Installing everything took around 1 hour.
In this tutorial we will use Visual Studio 2019, not 2017 recommended for TFS compilation. It will work!

What do you need before start?

  • Windows 10
  • Web Browser (tutorial created with Google Chrome)

What will you have after completing this tutorial?

  • GIT client integrated with Windows 10
  • ‘linux console’ integrated with Windows 10
  • Visual Studio 2019 Community with C++ dev tools – IDE that let you compile TFS/OTClient easily (generate ‘.exe’ file)
  • vcpkg – C++ Library Manager for Windows, it will let you compile TFS/OTClient without searching for C++ libraries for hours
  • IDEA Community Edition with EmmyLUA plugin – free version of IDE that let you manage ‘data’ folder easily (XML and LUA files)
  • MariaDB database, PHP 7.4 and Apache2 server – website for your OTS
  • The Forgotten Server 1.2+ running on your PC
  • Gesior2012 for TFS 1.2+ running on your PC

1. Install GIT for Windows with environment integration

Download it from site (version 64-bit Setup): https://git-scm.com/download/win
‘Setup’ version will integrate Git with Windows explorer.

Download 64-bit Git for Windows Setup.
Unselect GUI option. It’s useless. You will have better Git GUI in IDEA.
Change line ending to ‘as is’. On GitHub most of code uses Unix-style endings.

2. Install Visual Studio 2019 Community with C++

Download it from site: https://visualstudio.microsoft.com/vs/

Select “Community” version. It will start download. After download run installer.
Select packet “Programming classic applications in C++”.
Select packet “English language”. It’s required to install TFS compilation libraries.
After installation run Visual Studio for first time to make it configure itself.
You don’t need Microsoft Account to use it. Click ‘Not now, maybe later’.

3. Install vcpkg – C++ library manager for Windows

Open Git Bash (‘linux console’) in folder ‘C:\’
Type in console: git clone https://github.com/Microsoft/vcpkg.git
git clone https://github.com/Microsoft/vcpkg.git
Type in console: cd vcpkg
cd vcpkg
Open website: https://github.com/Microsoft/vcpkg and find newest tagged version

Link: https://github.com/Microsoft/vcpkg
Newest tagged version should be stable. I tried master branch and it did not work with all libraries. You can always use old version, not newest. I used for this tutorial: 2020.01

Type in console: git checkout 2020.01
git checkout 2020.01

It will switch version of vcpkg to version from 2020.01

Type: ./bootstrap-vcpkg.bat
./bootstrap-vcpkg.bat

To build vcpkg. You must do it after downloading from GitHub and after every version change.

Run Windows PowerShell as Administrator (another black console..)
Change path in console to vcpkg path
Type: cd C:\vcpkg
cd C:\vcpkg
In console type: .\vcpkg integrate install
.\vcpkg integrate install

This will integrate vcpkg C++ library manager with Visual Studio we installed before.
After integrating with Visual Studio close PowerShell. We don’t need administrator rights anymore.

If you closed Git Bash console we opened at start, open it again in vcpkg folder.
Copy current libraries list from official TFS wiki (for 64-bit): https://github.com/otland/forgottenserver/wiki/Compiling-on-Windows-%28vcpkg%29

Libraries list at 2020-02-17:

vcpkg install boost-iostreams:x64-windows boost-asio:x64-windows boost-system:x64-windows boost-filesystem:x64-windows boost-variant:x64-windows boost-lockfree:x64-windows luajit:x64-windows libmariadb:x64-windows pugixml:x64-windows mpir:x64-windows cryptopp:x64-windows
Type in console: ./ and paste list

./ means ‘run’

We got all libraries for TFS!

4. Download TFS and compile engine

Create folder:

C:\ots
Open Git Bash in folder C:\ots
Download newest TFS. Type: git clone https://github.com/otland/forgottenserver.git
git clone https://github.com/otland/forgottenserver.git
C:\ots\forgottenserver\vc14\theforgottenserver.sln

It will open TFS project in Visual Studio.

Click ‘OK’ when VS ask you about changing target platform.

Change build type to Release and build target platform to x64.
Previously we installed C++ libraries for 64-bit platform – we can compile only 64-bit version.

If you plan to do many changes in sources and compile in often, you may try to use Debug build type.
It should link faster and reduce time of building solution.

Start compilation of TFS. It can take few minutes.
Open folder: C:\ots\forgottenserver\vc14\x64\Release
C:\ots\forgottenserver\vc14\x64\Release
Copy all .dll files and .exe file to folder: C:\ots\forgottenserver\
C:\ots\forgottenserver\
TFS is ready. Now we need to install database server and import TFS database schema.

5. Install MariaDB, PHP and Apache2 servers

Download newest XAMPP for Windows.

Link: https://www.apachefriends.org/download.html

During installation deselect components we won’t use for OTS development.
Start Apache, MySQL and open phpMyAdmin site (database administration site).

If Apache fails to start, it’s probably because some other application is already using port 80.
Common problem is Skype. Turn off Skype, start Apache, start again Skype – you got website and Skype working.

In phpMyAdmin open page with new user account configuration.
Type username forgottenserver
Type secure password for that user
Check ‘Create database with same name and grant all access.’
New database will appear in left panel. Click it and go to Import page.
Choose file: C:\ots\forgottenserver\schema.sql
C:\ots\forgottenserver\schema.sql

and click Import.
We got database for our OTS! Time to configure game server.

Open folder C:\ots\forgottenserver and copy file config.lua.dist to config.lua

6. Install IntelliJ IDEA, configure game server and run it for first time!

What is IntelliJ IDEA? It’s IDE for Java and Android development.
What is IDE? It’s ‘Integrated Development Environment’.
Why do I need it? To develop code faster, automatically detect simple bugs and make cleaner code.

Choose folder: C:\ots\forgottenserver
C:\ots\forgottenserver
Open IntelliJ IDEA Settings
Go to Plugins and install EmmyLua
Restart IDE to make it work with LUA
Configure your password to database user forgottenserver in file config.lua
Your server is running!

7. Install website (Gesior2012)

Open folder: C:\xampp\htdocs
 C:\xampp\htdocs
Remove all files from C:\xampp\htdocs
Open Git Bash in folder C:\xampp\htdocs
Type: git clone https://github.com/gesior/Gesior2012.git .
git clone https://github.com/gesior/Gesior2012.git .

Yes. There is space and dot at end. We want to download Gesior2012 to current folder. Not to folder C:\xampp\htdocs\Gesior2012

Type: git checkout TFS-1.2
git checkout TFS-1.2

Change Gesior2012 version to TFS-1.2 (exactly change git branch to TFS-1.2)
Same as we did during vcpkg installation. Download from GitHub with git clone and then change version.

From now you will edit every directory in IntelliJ IDEA 🙂

PHP files are not colored in IntelliJ IDEA Community edition 🙁
LUA plugin is free, but PHP is available only in IntelliJ IDEA Ultimate (paid / 30 days trial) or as separate application PHPStorm (it’s IntelliJ IDEA with PHP plugin).

Open website http://127.0.0.1 and click link to installation.
Refresh site. Error about IP should disappear.
There will be probably some errors about cache folder.
We got to set cache folder not read-only.
When it asks about ‘To all files and sub-directories’ click Yes.
Click link to Step 1 in left side menu.
Type path to TFS: C:\ots\forgottenserver
C:\ots\forgottenserver

Continue steps 2-5.

Your website should be ready to use!

Reset TFS database without removing accounts, players and guilds

SQL queries to reset database to ‘server start’ without removing accounts, players and guilds. All players are set to level 30 with valid hp, mana and cap for their vocations.

UPDATE `players`
SET
`health` = 185 + 22 * 5,
`healthmax` = 185 + 22 * 5,
`mana` =  35 + 22 * 30,
`manamax` = 35 + 22 * 30,
`cap` = 470 + 22 * 10,
`level` = 30,
`experience` = 368300,
`maglevel` = 30,
`soul` = 100,
`manaspent` = 0,
`lookaddons` = 0,
`posx` = 0,
`posy` = 0,
`posz` = 0, 
`conditions` = '',
`skull` = 0,
`skulltime` = 0,
`blessings` = 0,
`offlinetraining_time` = 43200,
`offlinetraining_skill` = -1,
`stamina` = 2520,
`skill_fist` = 10,
`skill_club` = 10,
`skill_sword` = 10,
`skill_axe` = 10,
`skill_dist` = 10,
`skill_shielding` = 10,
`skill_fishing` = 10,
`skill_fist_tries` = 0,
`skill_club_tries` = 0,
`skill_sword_tries` = 0,
`skill_axe_tries` = 0,
`skill_dist_tries` = 0,
`skill_shielding_tries` = 0,
`skill_fishing_tries` = 0
WHERE `vocation` =  1 OR `vocation`= 5  OR `vocation`= 2 OR `vocation`= 6;

UPDATE `players`
SET
`health` = 185 + 22 * 10,
`healthmax` = 185 + 22 * 10,
`mana` =  35 + 22 * 15,
`manamax` = 35 + 22 * 15,
`cap` = 470 + 22 * 20,
`level` = 30,
`experience` = 368300,
`maglevel` = 5,
`soul` = 100,
`manaspent` = 0,
`lookaddons` = 0,
`posx` = 0,
`posy` = 0,
`posz` = 0, 
`conditions` = '',
`skull` = 0,
`skulltime` = 0,
`blessings` = 0,
`offlinetraining_time` = 43200,
`offlinetraining_skill` = -1,
`stamina` = 2520,
`skill_fist` = 10,
`skill_club` = 10,
`skill_sword` = 10,
`skill_axe` = 10,
`skill_dist` = 30,
`skill_shielding` = 10,
`skill_fishing` = 10,
`skill_fist_tries` = 0,
`skill_club_tries` = 0,
`skill_sword_tries` = 0,
`skill_axe_tries` = 0,
`skill_dist_tries` = 0,
`skill_shielding_tries` = 0,
`skill_fishing_tries` = 0
WHERE `vocation` =  3 OR `vocation`= 7;

UPDATE `players`
SET
`health` = 185 + 22 * 15,
`healthmax` = 185 + 22 * 15,
`mana` =  35 + 22 * 5,
`manamax` = 35 + 22 * 5,
`cap` = 470 + 22 * 25,
`level` = 30,
`experience` = 368300,
`maglevel` = 2,
`soul` = 100,
`manaspent` = 0,
`lookaddons` = 0,
`posx` = 0,
`posy` = 0,
`posz` = 0, 
`conditions` = '',
`skull` = 0,
`skulltime` = 0,
`blessings` = 0,
`offlinetraining_time` = 43200,
`offlinetraining_skill` = -1,
`stamina` = 2520,
`skill_fist` = 10,
`skill_club` = 30,
`skill_sword` = 30,
`skill_axe` = 30,
`skill_dist` = 10,
`skill_shielding` = 10,
`skill_fishing` = 10,
`skill_fist_tries` = 0,
`skill_club_tries` = 0,
`skill_sword_tries` = 0,
`skill_axe_tries` = 0,
`skill_dist_tries` = 0,
`skill_shielding_tries` = 0,
`skill_fishing_tries` = 0
WHERE `vocation` =  4 OR `vocation`= 8;

DELETE FROM `account_ban_history`;
DELETE FROM `account_bans`;
DELETE FROM `house_lists`;
DELETE FROM `houses`;
DELETE FROM `market_history`;
DELETE FROM `market_offers`;
DELETE FROM `player_deaths`;
DELETE FROM `player_depotitems`;
DELETE FROM `player_inboxitems`;
DELETE FROM `player_items`;
DELETE FROM `player_spells`;
DELETE FROM `player_storage`;
DELETE FROM `players_online`;
DELETE FROM `tile_store`;

[TFS 1.x+] Generate big depot

LUA code to generate backpack full of backpacks:

function fillContainer(container, level, innerContainerCount)
    if level > 0 then
        for i = 1, innerContainerCount do
            local innerContainer = Game.createItem(2000)
            container:addItemEx(innerContainer)
            fillContainer(innerContainer, level - 1, innerContainerCount)
        end
    else
        for i = 1, 20 do
            local item = Game.createItem(i % 2 == 0 and 2376 or 2400)
            container:addItemEx(item)
        end
    end
end

function generateBigContainer(player, innerContainerCount, deepness)
    local item = player:getTile():addItem(2000)
    fillContainer(item, deepness, innerContainerCount)
    return item
end

Code to call it:

!lua generateBigContainer(player, 20, 2)

Requires !lua debug:

https://otland.net/threads/debug-lua-scripts-with-talkaction.112027/

https://otland.net/threads/tfs-1-x-luajit-debug-lua-scripts-with-talkaction.260910/

https://otland.net/threads/tfs-1-3-lua-5-2-debug-lua-scripts-with-talkaction.266086/