Building pre-requisites mod rule/gadget

Building pre-requisites mod rule/gadget

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Building pre-requisites mod rule/gadget

Post by AF »

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
User avatar
VonGratz
Posts: 471
Joined: 03 May 2005, 05:25

Post by VonGratz »

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 :wink:
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

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.
tinnut
Posts: 67
Joined: 09 Sep 2006, 08:17

Post by tinnut »

Thanks for this AF. I was going to have a look at doing this later and you've saved me a lot of time :)
User avatar
MadRat
Posts: 532
Joined: 24 Oct 2006, 13:45

Post by MadRat »

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.
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

His version wouldn't do that easily but you can of course write your own script.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

My gadgets quite simlpe. It doesnt track buildings once they're built however. So once a pre-requisites built that's it, the pre-requisite is full filled even if its then blown up.

Its till a very simple gadget, modifying it into whatever variation you need should not be hard at all.
User avatar
MadRat
Posts: 532
Joined: 24 Oct 2006, 13:45

Post by MadRat »

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?
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

Its easy to unlock and never lock again, it's a bit harder to relock it if the condition is no longer met.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

just relock it when a scientist dies and retest again when a scientist is built
User avatar
KDR_11k
Game Developer
Posts: 8293
Joined: 25 Jun 2006, 08:44

Post by KDR_11k »

There's no relocking code in your gadget though so he'd have to write that half himself.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

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]
User avatar
MadRat
Posts: 532
Joined: 24 Oct 2006, 13:45

Post by MadRat »

That's very helpful. Thanks AF.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Post by Forboding Angel »

This gadget bombs, any idea why?

And has anyone else had trouble with it?
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Post by Forboding Angel »

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 :P
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
--------------------------------------------------------------------------------
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

line 56

Code: Select all

   restrictedIDs[key] = UnitDefNames[key].id
Or more precisely:

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
Someone needs to check their unitnames. This could easily be caused by a misspelt unit name in prerequisite_defs.lua
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Post by Forboding Angel »

Yep that was the problem. My bad, I arsed up the config something decent. Sry :oops:
Post Reply

Return to “Lua Scripts”