• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

OOP Classes for older TFS versions (0.3.x / 0.4x / ...)

update Game functions
Code:
-- Game
function Game.getStorageValue(key) return getGlobalStorageValue(key) end
function Game.setStorageValue(key, value) setGlobalStorageValue(key, value) end
function Game.getChannelUsers(channelId) return getChannelUsers(channelId) end
function Game.setWorldType(type) return setWorldType(type) end
function Game.getWorldType() return getWorldType() end
function Game.getSpectators(centerPos, rangex, rangey, multifloor) return getSpecators(centerPos, rangex, rangey, multifloor or false) end
function Game.getPlayers() return getPlayersOnline() end
function Game.getExperienceStage(level) return getExperienceStage(level) end
function Game.getGameState() return getGameState() end
function Game.setGameState(state) return doSetGameState(state) end
function Game.startRaid(raid) return doExecuteRaid(raid) end
function Game.createItem(itemId, count, position) return doCreateItem(itemId, count, position) end
function Game.createMonster(name, pos, extend, force, displayError) return doCreateMonster(name, pos, extend or false, force or false, displayError or true) end
function Game.createNpc(name, pos, displayError) return doCreateNpc(name, pos, displayError or true) end
rest of the Game 1.x functions cannot be converted as of now.
 
Where does all of this go in the files?
(Yes I know that I'm a noob)
 
0.4 -> create a lua file and place it into the lib folder
0.3 -> create a lua file and place it into the same folder as global.lua, then paste into global.lua
Code:
dofile('NAMEOFFILE.lua')
at top
 
For what are they?
to simplify it, this lib is there to keep scripting compatibility between the older tfs versions (0.2.x / 0.3.x / 0.4) and the newer tfs version (1.x)
while we do not use OOP classes in the older ones we do use them nowdays.
So this lib basicly converts the OOP functions to be readable by the old scripting environment.
Basicly it means you can use 1.x scripts in a 0.4 server (if all of the functions exist ofcourse)
 
to simplify it, this lib is there to keep scripting compatibility between the older tfs versions (0.2.x / 0.3.x / 0.4) and the newer tfs version (1.x)
while we do not use OOP classes in the older ones we do use them nowdays.
So this lib basicly converts the OOP functions to be readable by the old scripting environment.
Basicly it means you can use 1.x scripts in a 0.4 server (if all of the functions exist ofcourse)
With Old functions too? so i can use 0.4, 1.x functions at same time right? :D
 
@Evil Hero
Code:
_G[Position] = nil
Position = {}
setmetatable(Position,
	{__call = function(self, x, y, z, stackpos)
		if not x then
			return nil
		end
		
		if type(x) == "table" then
			if x[1] and x[2] and x[3] then
				return {x = x[1], y = x[2], z = x[3], stackpos = x[4]}
			elseif x.x and x.y and x.z then
				return {x = x.x, y = x.y, z = x.z, stackpos = x.stackpos}
			else
				return nil
			end
		elseif tonumber(x) and tonumber(y) and tonumber(z) then
			return {x = x, y = y, z = z, stackpos = stackpos}
		else
			return nil
		end
	end,
	__index = Position})

Tile = {}
setmetatable(Tile,
	{__call = function(self, pos)
		if not Position(pos) then
			return nil
		end
		
		doAreaCombatHealth(0,0,pos,0,0,0,255)
		return getTileInfo(pos)
	end,
	__index = Tile})

function newClass(constructor, parent)
    local methods = { }
    if parent then
        methods.__parent = parent
    end
    local MT = {__index = function(a, b) if methods[b] then return methods[b] elseif methods.__parent and methods.__parent[b] then return methods.__parent[b] end end}

    local call = function(self, ...)
        local obj = constructor(...)
        if not obj then
            return nil
        end

        return setmetatable(obj, MT)
    end

    return setmetatable(methods, {__call = call})
end

function creatureCreate(cid)
    -- Creature(id or name or table)
    local creature = 0
    if type(cid) == "number" then
        creature = cid
    elseif type(cid) == "string" then
        creature = getCreatureByName(cid)
    elseif type(cid) == "table" then
        creature = cid.cid
    end

    if creature and isCreature(creature) then
        return {cid = creature}
    end

    return false
end

function playerCreate(cid)
    -- Player(id or name or table)
    local player = 0
    if type(cid) == "number" then
        player = cid
    elseif type(cid) == "string" then
        player = getPlayerByNameWildcard(cid)
    elseif type(cid) == "table" then
        player = cid.cid
    end

    if player and isPlayer(player) then
        return {cid = player}
    end

    return false
end

function monsterCreate(cid)
    -- Monster(id or name or table)
    local monster = 0
    if type(cid) == "number" then
        monster = cid
    elseif type(cid) == "string" then
        monster = getCreatureByName(cid)
    elseif type(cid) == "table" then
        monster = cid.cid
    end

    if monster and isMonster(monster) then
        return {cid = monster}
    end

    return false
end

function npcCreate(cid)
    -- Npc(id or name or table)
    local npc = 0
    if type(cid) == "number" then
        npc = cid
    elseif type(cid) == "string" then
        npc = getCreatureByName(cid)
    elseif type(cid) == "table" then
        npc = cid.cid
    end

    if npc and isNpc(npc) then
        return {cid = npc}
    end

    return false
end

Creature = newClass(creatureCreate)
Player = newClass(playerCreate, Creature)
Monster = newClass(monsterCreate, Creature)
Npc = newClass(npcCreate, Creature)
Game = {}










------------------------------------------
------------------------------------------
---------------FUNCTIONS------------------
------------------------------------------
------------------------------------------

-- access metatable functions from creature
function Creature:getPlayer() return Player(self.cid) end
function Creature:getMonster() return Monster(self.cid) end
function Creature:getNpc() return Npc(self.cid) end

-- Creature
-- Creature get functions
function Creature:getId() return self.cid end
function Creature:getName() return getCreatureName(self.cid) end
function Creature:getHealth() return getCreatureHealth(self.cid) end
function Creature:getMaxHealth() return getCreatureMaxHealth(self.cid) end
function Creature:getMana() return getCreatureMana(self.cid) end
function Creature:getMaxMana() return getCreatureMaxMana(self.cid) end
function Creature:getStorageValue(id) return getCreatureStorageValue(self.cid, id) end
function Creature:getHiddenHealth() return getCreatureHideHealth(self.cid) end
function Creature:getSpeakType() return getCreatureSpeakType(self.cid) end
function Creature:getLookDirection() return getCreatureLookDirection(self.cid) end

-- Creature set functions
function Creature:setMaxHealth(health) return setCreatureMaxHealth(self.cid, health) end
function Creature:setMaxMana(mana) return setCreatureMaxMana(self.cid, mana) end
function Creature:setStorageValue(storage, value) return setCreatureStorageValue(self.cid, storage, value) end
function Creature:setHiddenHealth(hide) doCreatureSetHideHealth(self.cid, hide) end
function Creature:setSpeakType(type) doCreatureSetSpeakType(self.cid, type) end

-- Creature misc. functions
function Creature:teleportTo(newpos, pushmove) doTeleportThing(self.cid, newpos, pushmove or false) end
function Creature:addHealth(health, hitEffect, hitColor, force) doCreatureAddHealth(self.cid, health, hitEffect, hitColor, force) end
function Creature:addMana(mana) doCreatureAddMana(self.cid, mana) end
function Creature:say(text, type, ghost, cid, pos) doCreatureSay(self.cid, text, type or SPEAK_SAY, ghost or false, cid or 0, pos) end
function Creature:addCondition(condition) doAddCondition(self.cid, condition) end
function Creature:removeCondition(onlyPersistent, type, subId) if onlyPersistent then doRemoveConditions(self.cid, onlyPersistent) else doRemoveCondition(self.cid, type, subId) end end
function Creature:moveCreature(direction) doMoveCreature(self.cid, direction) end
function Creature:isCreature() return isCreature(self.cid) end
function Creature:isPlayer() return isPlayer(self.cid) end
function Creature:isMonster() return isMonster(self.cid) end
function Creature:isNpc() return isNpc(self.cid) end
function Creature:remove(forceLogout) doRemoveCreature(self.cid, forceLogout or true) end

-- Player
-- Player get functions
function Player:getLevel() return getPlayerLevel(self.cid) end
function Player:getExperience() return getPlayerExperience(self.cid) end
function Player:getMagicLevel(ignoreBuffs) return getPlayerMagLevel(self.cid, ignoreBuffs) end
function Player:getManaSpent() return getPlayerSpentMana(self.cid) end
function Player:getFood() return getPlayerFood(self.cid) end
function Player:getAccess() return getPlayerAccess(self.cid) end
function Player:getGhostAccess() return getPlayerGhostAccess(self.cid) end
function Player:getSkillLevel(skillid) return getPlayerSkillLevel(self.cid, skillid) end
function Player:getSkillTries(skillid) return getPlayerSkillTries(self.cid, skillid) end
function Player:getTown() return getPlayerTown(self.cid) end
function Player:getVocation() return getPlayerVocation(self.cid) end
function Player:getBaseVocation() local vocation = self:getVocation(); return vocation > 4 and vocation - 4 or vocation end
function Player:getIp() return getPlayerIp(self.cid) end
function Player:getRequiredMana(magicLevel) return getPlayerRequiredMana(self.cid, magicLevel) end
function Player:getRequiredSkillTries(skillId, skillLevel) return getPlayerRequiredSkillTries(self.cid, skillId, skillLevel) end
function Player:getItemCount(itemid, subType) return getPlayerItemCount(self.cid, itemid, subType or -1) end
function Player:getMoney() return getPlayerMoney(self.cid) end
function Player:getSoul() return getPlayerSoul(self.cid) end
function Player:getCap() return getPlayerFreeCap(self.cid) end
function Player:getLight() return getPlayerLight(self.cid) end
function Player:getSlotItem(slot) return getPlayerSlotItem(self.cid, slot) end

-- Player set functions
function Player:setMaxCap(cap) doPlayerSetMaxCapacity(self.cid, cap) end
function Player:setTown(townid) doPlayerSetTown(self.cid, townid) end
function Player:setVocation(vocation) doPlayerSetVocation(self.cid, vocation) end

-- Player send functions
function Player:sendCancelMsg(text) doPlayerSendCancel(self.cid, text) end
function Player:sendDefaultCancelMsg(ReturnValue) doPlayerSendDefaultCancel(self.cid, ReturnValue) end
function Player:sendTextMessage(MessageClasses, message) doPlayerSendTextMessage(self.cid, MessageClasses, message) end
function Player:sendChannelMessage(author, message, SpeakClasses, channel) doPlayerSendChannelMessage(self.cid, author, message, SpeakClasses, channel) end
function Player:sendToChannel(targetId, SpeakClasses, message, channel, time) doPlayerSendToChannel(self.cid, targetId, SpeakClasses, message, channel, time) end
function Player:addMoney(money) doPlayerAddMoney(self.cid, money) end
function Player:removeMoney(money) doPlayerRemoveMoney(self.cid, money) end
function Player:transferMoney(target, money) doPlayerTransferMoneyTo(self.cid, target, money) end
function Player:showTextDialog(itemid, text) doShowTextDialog(self.cid, itemid, text) end
function Player:addSkillTries(skillid, n, useMultiplier) doPlayerAddSkillTry(self.cid, skillid, n, useMultiplier or false) end

-- Player misc. functions
function Player:feed(food) doPlayerFeed(self.cid, food) end
function Player:addSpentMana(amount, useMultiplier) doPlayerAddSpentMana(self.cid, amount, useMultiplier) end
function Player:addSoul(soul) doPlayerAddSoul(self.cid, soul) end
function Player:addItem(itemid, count, canDropOnMap, subtype) return doPlayerAddItem(self.cid, itemid, count, canDropOnMap, subtype) end
function Player:addItemEx(uid, canDropOnMap) return doPlayerAddItemEx(self.cid, uid, canDropOnMap or false) end
function Player:addExperience(amount) doPlayerAddExperience(self.cid, amount) end
function Player:savePlayer(shallow) doPlayerSave(self.cid, shallow or false) end
function Player:isPzLocked() return isPlayerPzLocked(self.cid) end

-- Game
function Game.getStorageValue(key) return getGlobalStorageValue(key) end
function Game.setStorageValue(key, value) setGlobalStorageValue(key, value) end
function Game.getChannelUsers(channelId) return getChannelUsers(channelId) end
function Game.setWorldType(type) return setWorldType(type) end
function Game.getWorldType() return getWorldType() end
function Game.getSpectators(centerPos, rangex, rangey, multifloor) return getSpecators(centerPos, rangex, rangey, multifloor or false) end
function Game.getPlayers() return getPlayersOnline() end
function Game.getExperienceStage(level) return getExperienceStage(level) end
function Game.getGameState() return getGameState() end
function Game.setGameState(state) return doSetGameState(state) end
function Game.startRaid(raid) return doExecuteRaid(raid) end
function Game.createItem(itemId, count, position) return doCreateItem(itemId, count, position) end
function Game.createMonster(name, pos, extend, force, displayError) return doCreateMonster(name, pos, extend or false, force or false, displayError or true) end
function Game.createNpc(name, pos, displayError) return doCreateNpc(name, pos, displayError or true) end
function Game.createTile(pos) return doAreaCombatHealth(0,0,pos,0,0,0,255) end
 
Last edited:
I'm using otx 2 i think is based on 0.3.7, in my folder don't have this folder global.lua
 
Back
Top