Building pre-requisites mod rule/gadget
Moderator: Moderators
Building pre-requisites mod rule/gadget
Extract into your mods main folder and repackage.
Allows you to disable buildings untill a pre-requisite has been built.
Pre-requisites are defined in the /LuaRules/Configs/prerequisiteDefs.lua file. This file has XTA example definitions.
note: once a pre-requisite has been built that si it, the unit can then be built even if the prerequisit building is destroyed/reclaimed.
Also buildigns with multiple pre-requisites work based on a or b not a and b, so if the factory has a mex and a solar as pre-requisites, then building a solar unlocks the factory, or building a mex unlocks the factory.
Download here
Allows you to disable buildings untill a pre-requisite has been built.
Pre-requisites are defined in the /LuaRules/Configs/prerequisiteDefs.lua file. This file has XTA example definitions.
note: once a pre-requisite has been built that si it, the unit can then be built even if the prerequisit building is destroyed/reclaimed.
Also buildigns with multiple pre-requisites work based on a or b not a and b, so if the factory has a mex and a solar as pre-requisites, then building a solar unlocks the factory, or building a mex unlocks the factory.
Download here
Hi!
I give a look at XTA example, and I saw only factories.
Can I extend to plan the building of units like the examples below?
I understood correctly?
Like Berthas only after fusions, escorts before light carriers(in my mod),
sonars before torpedo launchers, radars before SAMs etc..
OMG!
It can enhance a lot the AI gameplay according to the mod.
VonGratz
I give a look at XTA example, and I saw only factories.
Can I extend to plan the building of units like the examples below?
I understood correctly?
Like Berthas only after fusions, escorts before light carriers(in my mod),
sonars before torpedo launchers, radars before SAMs etc..
OMG!
It can enhance a lot the AI gameplay according to the mod.
VonGratz

Yes, it isnt specific to buildings.
As for AI, I'm not so sure. Remember, when you cross the lua divide you wont be taking any AIs with you, so if you have a highly complx prerequisite system the AI might not like it because itll try to build something and fail and it wont have any idea why.
With it being pre-requisites, the effect isnt as bad as say the magical superpower lua script that AIs arent told about and have now ay to access, thats critical to the gameplay.
As for AI, I'm not so sure. Remember, when you cross the lua divide you wont be taking any AIs with you, so if you have a highly complx prerequisite system the AI might not like it because itll try to build something and fail and it wont have any idea why.
With it being pre-requisites, the effect isnt as bad as say the magical superpower lua script that AIs arent told about and have now ay to access, thats critical to the gameplay.
AF -
Can you somehow make it so a set number of units must be built to pass the prerequisite threshold, too? I want to use scientists of different types, with units unlocked as set numbers of scientists are created. One unfortunate problem I can see if my mod would hopefully require the set number to be alive at the time and not just simply have been built. I want the various scientists to be valuable assets to protect.
Can you somehow make it so a set number of units must be built to pass the prerequisite threshold, too? I want to use scientists of different types, with units unlocked as set numbers of scientists are created. One unfortunate problem I can see if my mod would hopefully require the set number to be alive at the time and not just simply have been built. I want the various scientists to be valuable assets to protect.
So basically I can test for the unit prerequisite (scientist type in this case) then do some sort of test for the active quantity? I'd probably have a problem if in my examples the enemy knocked off scientists then, because the quantity would then drop below the threshold and this would effectively take away an already once satisfied demand. I could see compelling reasons for keeping the ability once the threshold was broken. Is this in your opinion going to be terribly difficult to retain that triggered prerequisite in this kind of case?
The allowed table has boolean values. If it instead stored integer values representing how many more of that unit were needed to enable it then the check would change from Allowed[key] == false to Allowed[key] >0
Then on gadget:untidestroyed you would increment the value of Allowed[key] and if the incremented value is above zero you'd disable the unit.
on gadget:Unitfinished you would decrement Allowed[key]
And in the def table rturned by th other lua file instead of storing the unitnames you'd store tables containing a unitname and a pre-requisite number for the initial value of allowed[key]
Then on gadget:untidestroyed you would increment the value of Allowed[key] and if the incremented value is above zero you'd disable the unit.
on gadget:Unitfinished you would decrement Allowed[key]
And in the def table rturned by th other lua file instead of storing the unitnames you'd store tables containing a unitname and a pre-requisite number for the initial value of allowed[key]
- Forboding Angel
- Evolution RTS Developer
- Posts: 14673
- Joined: 17 Nov 2005, 02:43
- Forboding Angel
- Evolution RTS Developer
- Posts: 14673
- Joined: 17 Nov 2005, 02:43
THis is the error btw, sorry for that:
Lua LoadCode pcall error = 2, LuaRules/main.lua, error = 2, LuaRules/gadgets.lua, [string "LuaRules/Gadgets/prerequisites.lua"]:56: attempt to index field '?' (a nil value)
A pastebin of the gadget code:
http://pastebin.com/d6fbf61f1
or if you're lazy
Lua LoadCode pcall error = 2, LuaRules/main.lua, error = 2, LuaRules/gadgets.lua, [string "LuaRules/Gadgets/prerequisites.lua"]:56: attempt to index field '?' (a nil value)
A pastebin of the gadget code:
http://pastebin.com/d6fbf61f1
or if you're lazy

function gadget:GetInfo()
return {
name = "Build Menu pre-requisites",
desc = "Disables build menu items untill pre-requisites have been built",
author = "AF/Tom Nowell",
date = "July 28, 2007",
license = "GNU GPL, v2 or later",
layer = 1,
enabled = true -- loaded by default?
}
end
--------------------------------------------------------------------------------
-- COMMON
--------------------------------------------------------------------------------
if (gadgetHandler:IsSyncedCode()) then
--------------------------------------------------------------------------------
-- SYNCED
--------------------------------------------------------------------------------
local FindUnitCmdDesc = Spring.FindUnitCmdDesc
local EditUnitCmdDesc = Spring.EditUnitCmdDesc
local restrictionData = {}
local restrictedIDs = {}
local allowed = {}
local function ContainsValue(mytable,value)
for key, tvalue in pairs(mytable) do
if(tvalue == value) then
return key
end
end
return nil
end
local function SetCommand(unitID, CommandId, isdisabled)
local editID = FindUnitCmdDesc(unitID, CommandId)
if(editID) then
Spring.EditUnitCmdDesc(unitID, editID, {disabled=isdisabled})
end
end
local function SetCommands(CommandId,isdisabled)
for _, uid in ipairs(Spring.GetAllUnits()) do
SetCommand(uid,CommandId,isdisabled)
end
end
function gadget:Initialize()
-- stuff
restrictionData = include("LuaRules/Configs/prerequisite_defs.lua")
for key, value in pairs(restrictionData) do
allowed[key]=false
restrictedIDs[key] = UnitDefNames[key].id
end
end
function gadget:UnitCreated(unitID, unitDefID)
for key, value in pairs(allowed) do
if(allowed[key]==false)then
SetCommand(unitID,restrictedIDs[key]*-1,true)
end
end
end
function gadget:UnitFinished(unitID, unitDefID)
local ud = UnitDefs[unitDefID]
local keys = {}
local i = 1
for key, value in pairs(restrictionData) do
local found = false
for j,tvalue in ipairs(value) do -- ContainsValue(value,ud.name)
if(tvalue == ud.name) then
found = true
break
end
end
if(found == true) then
keys = key
i = i + 1
end
end
if (i ~= 1) then
for j,key in ipairs(keys) do
if(allowed[key]==false)then
SetCommands(restrictedIDs[key]*-1,false)
allowed[key]=true
end
end
end
end
function gadget:AllowCommand(unitID, unitDefID, teamID,
cmdID, cmdParams, cmdOptions)
if (ContainsValue(restrictedIDs,cmdID) == nil) then
return true -- command was not used
end
return false -- command was used
end
--------------------------------------------------------------------------------
-- SYNCED
--------------------------------------------------------------------------------
else
--------------------------------------------------------------------------------
-- UNSYNCED
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- UNSYNCED
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
-- COMMON
--------------------------------------------------------------------------------
line 56
Or more precisely:
Someone needs to check their unitnames. This could easily be caused by a misspelt unit name in prerequisite_defs.lua
Code: Select all
restrictedIDs[key] = UnitDefNames[key].id
Code: Select all
function gadget:Initialize()
-- stuff
restrictionData = include("LuaRules/Configs/prerequisite_defs.lua")
for key, value in pairs(restrictionData) do
allowed[key]=false
restrictedIDs[key] = UnitDefNames[key].id
end
end
- Forboding Angel
- Evolution RTS Developer
- Posts: 14673
- Joined: 17 Nov 2005, 02:43