Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Feature intial luajit implementation #626

Merged
merged 21 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
4f2f130
feature/luajit
MUN1Z Dec 18, 2024
875092f
Merge branch 'develop' into feature/luajit
MUN1Z Dec 21, 2024
1a8a670
feature: move some things to LuaJit project, refact way how LuaGameMa…
MUN1Z Dec 21, 2024
1b76dab
feat: refact on all lua jit loaders functions, remove unused code, an…
MUN1Z Dec 21, 2024
164f04e
feat: some functions implementations to load actions works from luajit.
MUN1Z Dec 24, 2024
b66d26a
Merge branch 'develop' into feature/luajit
MUN1Z Dec 24, 2024
54cc20c
feat: some refacts, moved luajit data to data folder, added link from…
MUN1Z Dec 24, 2024
65786ad
feat: some refacts, removed unused/commented code, and some lua funct…
MUN1Z Dec 24, 2024
023c3cd
feat: merge with develop
MUN1Z Dec 24, 2024
07ae481
feat: renamed and moved all Function classes to LuaMapping
MUN1Z Dec 24, 2024
15c010d
feat: merge with develop
MUN1Z Dec 24, 2024
d5813b1
feat: removed all unused usings
MUN1Z Dec 24, 2024
230de4b
feat: some lua funcions mapping implementations.
MUN1Z Dec 27, 2024
b09f2c3
feat: refact renaming LuaMappings to Functions
MUN1Z Dec 27, 2024
5518c8f
Merge branch 'develop' into feature/luajit
MUN1Z Dec 27, 2024
6444d87
fix: adjustments on code
MUN1Z Dec 28, 2024
7dba3d5
fix: code adjustments
MUN1Z Dec 28, 2024
f45a6ed
feat: create more talkactions (/up, /down, /goto, /m, /s, !position).
MUN1Z Dec 28, 2024
a5dceaa
feat: create Container, Npc and Teleport functions.
MUN1Z Dec 28, 2024
6d96129
feat: renamed some lua script files
MUN1Z Dec 28, 2024
66c1159
feat: create item talkaction implementation. new functions (Player.Ad…
MUN1Z Dec 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions data/LuaJit/core.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
logger.debug('Starting lua.')
logger.debug(tostring(os.getenv('LOCAL_LUA_DEBUGGER_VSCODE')))
if os.getenv('LOCAL_LUA_DEBUGGER_VSCODE') == '1' then
require('lldebugger').start()
logger.debug('Started LUA debugger.')
end

CORE_DIRECTORY = configKeys.BASE_DIRECTORY

dofile(CORE_DIRECTORY .. '/global.lua')
dofile(CORE_DIRECTORY .. '/libs/libs.lua')
41 changes: 41 additions & 0 deletions data/LuaJit/global.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- for use of: data\scripts\globalevents\customs\save_interval.lua

SAVE_INTERVAL_TYPE = configManager.getString(configKeys.SAVE_INTERVAL_TYPE)
SAVE_INTERVAL_CONFIG_TIME = configManager.getNumber(configKeys.SAVE_INTERVAL_TIME)
SAVE_INTERVAL_TIME = 0
if SAVE_INTERVAL_TYPE == "second" then
SAVE_INTERVAL_TIME = 1000
elseif SAVE_INTERVAL_TYPE == "minute" then
SAVE_INTERVAL_TIME = 60 * 1000
elseif SAVE_INTERVAL_TYPE == "hour" then
SAVE_INTERVAL_TIME = 60 * 60 * 1000
end

table.contains = function(array, value)
for _, targetColumn in pairs(array) do
if targetColumn == value then
return true
end
end
return false
end

string.split = function(str, sep)
local res = {}
for v in str:gmatch("([^" .. sep .. "]+)") do
res[#res + 1] = v
end
return res
end

string.splitTrimmed = function(str, sep)
local res = {}
for v in str:gmatch("([^" .. sep .. "]+)") do
res[#res + 1] = v:trim()
end
return res
end

string.trim = function(str)
return str:match'^()%s*$' and '' or str:match'^%s*(.*%S)'
end
35 changes: 35 additions & 0 deletions data/LuaJit/libs/functions/creature.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function Creature.getMonster(self)
return self:isMonster() and self or nil
end

function Creature.getPlayer(self)
return self:isPlayer() and self or nil
end

function Creature.isContainer(self)
return false
end

function Creature.isItem(self)
return false
end

function Creature.isMonster(self)
return false
end

function Creature.isNpc(self)
return false
end

function Creature.isPlayer(self)
return false
end

function Creature.isTeleport(self)
return false
end

function Creature.isTile(self)
return false
end
31 changes: 31 additions & 0 deletions data/LuaJit/libs/functions/item.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function Item.getType(self)
return ItemType(self:getId())
end

function Item.isContainer(self)
return false
end

function Item.isCreature(self)
return false
end

function Item.isMonster(self)
return false
end

function Item.isNpc(self)
return false
end

function Item.isPlayer(self)
return false
end

function Item.isTeleport(self)
return false
end

function Item.isTile(self)
return false
end
7 changes: 7 additions & 0 deletions data/LuaJit/libs/functions/load.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Load core functions
dofile(CORE_DIRECTORY .. "/libs/functions/creature.lua")
dofile(CORE_DIRECTORY .. "/libs/functions/item.lua")
dofile(CORE_DIRECTORY .. "/libs/functions/player.lua")
dofile(CORE_DIRECTORY .. "/libs/functions/position.lua")
dofile(CORE_DIRECTORY .. "/libs/functions/revscriptsys.lua")
dofile(CORE_DIRECTORY .. "/libs/functions/tile.lua")
6 changes: 6 additions & 0 deletions data/LuaJit/libs/functions/player.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function Player.sendCancelMessage(self, message)
if type(message) == "number" then
message = Game.getReturnMessage(message)
end
return self:sendTextMessage(MESSAGE_STATUS_SMALL, message)
end
72 changes: 72 additions & 0 deletions data/LuaJit/libs/functions/position.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
Position.directionOffset = {
[DIRECTION_NORTH] = {x = 0, y = -1},
[DIRECTION_EAST] = {x = 1, y = 0},
[DIRECTION_SOUTH] = {x = 0, y = 1},
[DIRECTION_WEST] = {x = -1, y = 0},
[DIRECTION_SOUTHWEST] = {x = -1, y = 1},
[DIRECTION_SOUTHEAST] = {x = 1, y = 1},
[DIRECTION_NORTHWEST] = {x = -1, y = -1},
[DIRECTION_NORTHEAST] = {x = 1, y = -1}
}

function Position:getNextPosition(direction, steps)
local offset = Position.directionOffset[direction]
if offset then
steps = steps or 1
self.x = self.x + offset.x * steps
self.y = self.y + offset.y * steps
end
end

function Position:moveUpstairs()
local swap = function (lhs, rhs)
lhs.x, rhs.x = rhs.x, lhs.x
lhs.y, rhs.y = rhs.y, lhs.y
lhs.z, rhs.z = rhs.z, lhs.z
end

self.z = self.z - 1

local defaultPosition = self + Position.directionOffset[DIRECTION_SOUTH]
local toTile = Tile(defaultPosition)
if not toTile or not toTile:isWalkable() then
for direction = DIRECTION_NORTH, DIRECTION_NORTHEAST do
if direction == DIRECTION_SOUTH then
direction = DIRECTION_WEST
end

local position = self + Position.directionOffset[direction]
toTile = Tile(position)
if toTile and toTile:isWalkable() then
swap(self, position)
return self
end
end
end
swap(self, defaultPosition)
return self
end

function Position:isInRange(from, to)
-- No matter what corner from and to is, we want to make
-- life easier by calculating north-west and south-east
local zone = {
nW = {
x = (from.x < to.x and from.x or to.x),
y = (from.y < to.y and from.y or to.y),
z = (from.z < to.z and from.z or to.z)
},
sE = {
x = (to.x > from.x and to.x or from.x),
y = (to.y > from.y and to.y or from.y),
z = (to.z > from.z and to.z or from.z)
}
}

if self.x >= zone.nW.x and self.x <= zone.sE.x
and self.y >= zone.nW.y and self.y <= zone.sE.y
and self.z >= zone.nW.z and self.z <= zone.sE.z then
return true
end
return false
end
104 changes: 104 additions & 0 deletions data/LuaJit/libs/functions/revscriptsys.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
-- Create functions revscriptsys
function createFunctions(class)
local exclude = { [2] = { "is" }, [3] = { "get", "set", "add", "can" }, [4] = { "need" } }
local temp = {}
for name, func in pairs(class) do
local add = true
for strLen, strTable in pairs(exclude) do
if table.contains(strTable, name:sub(1, strLen)) then
add = false
end
end
if add then
local str = name:sub(1, 1):upper() .. name:sub(2)
local getFunc = function(self)
return func(self)
end
local setFunc = function(self, ...)
return func(self, ...)
end
local get = "get" .. str
local set = "set" .. str
if not (rawget(class, get) and rawget(class, set)) then
table.insert(temp, { set, setFunc, get, getFunc })
end
end
end
for _, func in ipairs(temp) do
rawset(class, func[1], func[2])
rawset(class, func[3], func[4])
end
end

-- Creature index
do
local function CreatureIndex(self, key)
local methods = getmetatable(self)
if key == "uid" then
return methods.getId(self)
elseif key == "type" then
local creatureType = 0
if methods.isPlayer(self) then
creatureType = THING_TYPE_PLAYER
elseif methods.isMonster(self) then
creatureType = THING_TYPE_MONSTER
elseif methods.isNpc(self) then
creatureType = THING_TYPE_NPC
end
return creatureType
elseif key == "itemid" then
return 1
elseif key == "actionid" then
return 0
end
return methods[key]
end
rawgetmetatable("Player").__index = CreatureIndex
rawgetmetatable("Monster").__index = CreatureIndex
rawgetmetatable("Npc").__index = CreatureIndex
end

-- Item index
do
local function ItemIndex(self, key)
local methods = getmetatable(self)
if key == "itemid" then
return methods.getId(self)
elseif key == "actionid" then
return methods.getActionId(self)
elseif key == "uid" then
return methods.getUniqueId(self)
elseif key == "type" then
return methods.getSubType(self)
end
return methods[key]
end
rawgetmetatable("Item").__index = ItemIndex
rawgetmetatable("Container").__index = ItemIndex
rawgetmetatable("Teleport").__index = ItemIndex
end

-- Action revscriptsys
do
local function ActionNewIndex(self, key, value)
if key == "onUse" then
self:onUse(value)
return
end
rawset(self, key, value)
end
rawgetmetatable("Action").__newindex = ActionNewIndex
end

-- TalkAction revscriptsys
do
local function TalkActionNewIndex(self, key, value)
if key == "onSay" then
self:onSay(value)
return
end
rawset(self, key, value)
end
local meta = rawgetmetatable("TalkAction")
meta.__newindex = TalkActionNewIndex
end
55 changes: 55 additions & 0 deletions data/LuaJit/libs/functions/tile.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
function Tile.isCreature(self)
return false
end

function Tile.isItem(self)
return false
end

function Tile.isTile(self)
return true
end

function Tile.isContainer(self)
return false
end

function Tile.relocateTo(self, toPosition)
if self:getPosition() == toPosition or not Tile(toPosition) then
return false
end

for i = self:getThingCount() - 1, 0, -1 do
local thing = self:getThing(i)
if thing then
if thing:isItem() then
if thing:getFluidType() ~= 0 then
thing:remove()
elseif ItemType(thing:getId()):isMovable() then
thing:moveTo(toPosition)
end
elseif thing:isCreature() then
thing:teleportTo(toPosition)
end
end
end

return true
end

function Tile.isWalkable(self)
local ground = self:getGround()
if not ground or ground:hasProperty(CONST_PROP_BLOCKSOLID) then
return false
end

local items = self:getItems()
for i = 1, self:getItemCount() do
local item = items[i]
local itemType = item:getType()
if itemType:getType() ~= ITEM_TYPE_MAGICFIELD and not itemType:isMovable() and item:hasProperty(CONST_PROP_BLOCKSOLID) then
return false
end
end
return true
end
2 changes: 2 additions & 0 deletions data/LuaJit/libs/libs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Load core functions
dofile(CORE_DIRECTORY .. "/libs/functions/load.lua")
Loading
Loading