Skip to content

Commit

Permalink
Rework the AI and AI_M files to use LUA_PATH instead of hooking the r…
Browse files Browse the repository at this point in the history
…equire function.

Remove unnecessary .lua from RAIL module names.
Add a few checks for lua.exe environment (vs. ragexe.exe environment). This simplifies debugging without loading Ragnarok.
  • Loading branch information
faithful613 committed Mar 13, 2010
1 parent ff9fa24 commit f7e3d1f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 50 deletions.
14 changes: 3 additions & 11 deletions AI.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ end

-- Now auto-detect where RAIL is located
do
local req = require

function FileExists(filename)
-- Try to open the file
local file = io.open(filename)
Expand Down Expand Up @@ -71,20 +69,14 @@ do
RAIL.Version = penv.RAIL.Version
RAIL.FullVersion = penv.RAIL.FullVersion

-- Replace the require function with one that uses RAIL's autodetected location
require = function(filename)
if FileExists(filename) then
return req(filename)
end

return req(ScriptLocation .. filename)
end
-- Set LUA_PATH to include the autodetected location
LUA_PATH = ScriptLocation .. "?;" .. ScriptLocation .. "?.lua;?;?.lua"
end
end

-- Only continue if autodetection worked
if not RAIL.CantRun then
-- The only difference between AI_M.lua and AI.lua is this following line
RAIL.Mercenary = false
require "MainRAIL.lua"
require "MainRAIL"
end
14 changes: 3 additions & 11 deletions AI_M.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ end

-- Now auto-detect where RAIL is located
do
local req = require

function FileExists(filename)
-- Try to open the file
local file = io.open(filename)
Expand Down Expand Up @@ -71,20 +69,14 @@ do
RAIL.Version = penv.RAIL.Version
RAIL.FullVersion = penv.RAIL.FullVersion

-- Replace the require function with one that uses RAIL's autodetected location
require = function(filename)
if FileExists(filename) then
return req(filename)
end

return req(ScriptLocation .. filename)
end
-- Set LUA_PATH to include the autodetected location
LUA_PATH = ScriptLocation .. "?;" .. ScriptLocation .. "?.lua;?;?.lua"
end
end

-- Only continue if autodetection worked
if not RAIL.CantRun then
-- The only difference between AI_M.lua and AI.lua is this following line
RAIL.Mercenary = true
require "MainRAIL.lua"
require "MainRAIL"
end
4 changes: 3 additions & 1 deletion Debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ do
local log_out = TraceAI

-- If not using TraceAI, generate a function to output to a file
if not RAIL.UseTraceAI then
-- Note: Ragnarok client doesn't provide the "debug" API table,
-- so we use this to force lua.exe to output to console.
if not RAIL.UseTraceAI and not RAIL._G.debug then
log_out = function(str)
-- Get the filename
local filename = RAIL.State.DebugFile
Expand Down
58 changes: 31 additions & 27 deletions MainRAIL.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@
RAIL._G = getfenv(0)

-- Load the configuration options
require "Config.lua"
require "Config"

-- Load Utils.lua and State.lua before all other code
require "Utils.lua" -- run CheckAPI() before others start using Ragnarok API
require "State.lua" -- allow other files to add state validation options
require "Utils" -- run CheckAPI() before others start using Ragnarok API
require "State" -- allow other files to add state validation options

-- Alphabetical
require "ActorOpts.lua"
require "Const.lua"
require "Debug.lua"
require "History.lua"
require "SkillAIs.lua"
require "Table.lua"
require "Timeout.lua"
--require "Version.lua" -- Note: Version.lua is pre-loaded by AI.lua and AI_M.lua
require "ActorOpts"
require "Const"
require "Debug"
require "History"
require "SkillAIs"
require "Table"
require "Timeout"
--require "Version" -- Note: Version.lua is pre-loaded by AI.lua and AI_M.lua

-- Load-time Dependency
require "Actor.lua" -- depends on History.lua
require "Commands.lua" -- depends on Table.lua
require "DecisionSupport.lua" -- depends on Table.lua
require "Skills.lua" -- depends on Table.lua
require "SkillSupport.lua" -- depends on Actor.lua
require "Actor" -- depends on History.lua
require "Commands" -- depends on Table.lua
require "DecisionSupport" -- depends on Table.lua
require "Skills" -- depends on Table.lua
require "SkillSupport" -- depends on Actor.lua

-- State validation options
RAIL.Validate.Information = {is_subtable = true,
Expand Down Expand Up @@ -72,8 +72,8 @@ function AI(id)
RAIL.LogT(0," --> Full Version ID = {1}",RAIL.FullVersion)

-- Check for some features of Lua
RAIL.LogT(0," --> Lua: _VERSION = {1}; debug = {2}; coroutine = {3}; collectgarbage = {4}",
RAIL._G._VERSION, RAIL._G.debug, RAIL._G.coroutine, RAIL._G.collectgarbage)
RAIL.LogT(0," --> Lua: _VERSION = {1}; _LOADED = {2}; coroutine = {3};",
RAIL._G._VERSION, RAIL._G._LOADED, RAIL._G.coroutine)

-- Load persistent state data again
-- Note: Redundant, but will show up in the log now
Expand Down Expand Up @@ -168,16 +168,20 @@ function AI(id)
RAIL.Owner.ExpireTimeout[1] = false
RAIL.Self.ExpireTimeout[1] = false

-- Periodically save state data
RAIL.Timeouts:New(2500,true,function()
-- Only load data if the "update" flag is on in the file
RAIL.State:Load(false)

-- Save data (if any data was loaded, it won't be dirty and won't save)
RAIL.State:Save()
end)
-- Check for the global variable "debug" (should be a table), to determine
-- if we're running inside lua.exe or ragexe.exe
if not RAIL._G.debug then
-- Periodically save state data
RAIL.Timeouts:New(2500,true,function()
-- Only load data if the "update" flag is on in the file
RAIL.State:Load(false)

-- Save data (if any data was loaded, it won't be dirty and won't save)
RAIL.State:Save()
end)
end

-- Profile the AI() function
-- Profile the AI() function (and include memory information)
AI = ProfilingHook("RAIL.AI",RAIL.AI,50,true)

-- Get memory usage after initialization
Expand Down

0 comments on commit f7e3d1f

Please sign in to comment.