All pastes #903212 Raw Edit

Something

public text v1 · immutable
·published 2008-02-14 02:02 UTC
rendered paste body
function gadget:GetInfo()
  return {
    name      = "Auras",
    desc      = "ZOMG AURAZ",
    author    = "lurker",
    date      = "Feb 12, 2008",
    license   = "GNU GPL, v2 or later",
    layer     = 1,
    enabled   = true  --  loaded by default?
  }
end

if (gadgetHandler:IsSyncedCode()) then

local newcmdid = 35600
local auras = {}
local default = {
	emmitterStartCMD =
	  function(aura, unitID)
		aura.emitterList[unitID] = {enabled = true}
	  end,
	emmitterStopCMD =
	  function(aura, unitID)
	  end,
	emmitterStopCMD =
	  function(aura, unitID)
	    aura.emitterList[unitID] = nil
	  end,
	isEmitter =
	  function(aura, unitID)
		local unitDefID = Spring.GetUnitDefID(unitID)
		for _,name in ipairs(aura.emitterDefs.UnitDefNames) do
		  if unitDefID == UnitDefNames[name] then
			return true
		  end
		end
		return false
	  end,
	command =
	  function(aura, unitID, UnitDefID, teamID, cmdID, cmdParams, cmdOptions)
	    Spring.Echo(aura.emitterList[unitID].state, cmdParams[1])
		local cmdDescID = Spring.FindUnitCmdDesc(unitID, cmdID)
		if (cmdParams[1] == 1) then --check for current state later
		  Spring.Echo('A')
		  aura.emitterList[unitID].state = 1
		  Spring.EditUnitCmdDesc(unitID, cmdDescID, { params = {'1', 'Aura Off', 'Aura On' } })
		  Spring.SetUnitRulesParam(unitID, aura.name, 0)
		else
		  Spring.Echo('B')
		  aura.emitterList[unitID].state = 0
		  Spring.EditUnitCmdDesc(unitID, cmdDescID, { params = {'0', 'Aura Off', 'Aura On' } })
		  Spring.SetUnitRulesParam(unitID, aura.name, 1)
		end		
	  end,
	emitterDefs = {
	  eengineer = {
		radius = 1000,
	  },
	  eengineer2 = {
		radius = 1000,
	  },
	  tengineer = {
		radius = 1000,
	  },
	  tengineer2 = {
		radius = 1000,
	  },
	},
	description = 'it\'s an aura',
	params  = {'0', 'Aura Off', 'Aura On' },
	RunOnLoad =
	  function(aura, def)
		for unitName,def in pairs(aura.emitterDefs) do
	      if UnitDefNames[unitName] ~= nil then
			local id = UnitDefNames[unitName].id
			aura.emitterDefs[id] = def
			aura.emitterDefs[unitName] = nil
	      end
		end
		if def.hasCommand ~= false then
		  if def.cmdid then
			aura.cmdid = def.cmdid
		  else
			newcmdid = newcmdid + 1
			aura.cmdid = newcmdid
		  end
		  gadgetHandler:RegisterCMDID(aura.cmdid)
		  aura.cmdDescription = {
			id      = aura.cmdid,
			type    = CMDTYPE.ICON_MODE,
			name    = aura.name,
			cursor  = 'aura', --check if this line is needed
			action  = aura.name,
			tooltip = aura.description,
			params  = aura.params,
		  }
		end
	  end,
}


function gadget:Initialize()
  local auraDefs = include("LuaRules/Configs/aura_defs.lua")
  if (not auraDefs) then
    gadgetHandler:RemoveGadget()
    return
  end
  for defName, def in pairs(auraDefs) do
    local newAura = {}
	newAura.name = defName
	newAura.affectedGameFrame	= def.affectedGameFrame
	newAura.useGlobalAffList	= false --not coded yet
	newAura.storeAffListPerE	= def.storeAffListPerE or false
	if def.useuseGlobalAffList then
	  newAura.affectedStart 	= def.affectedStart
	  newAura.affectedStop  	= def.affectedStop
	  newAura.affectedList  	= {}
	end
	newAura.emitterStartCMD	= def.emmitterStartCMD or default.emmitterStartCMD
	newAura.emitterGameFrame= def.emmitterGameFrame or default.emmitterGameFrame
	newAura.emitterStopCMD	= def.emmitterStopCMD or default.emmitterStopCMD
	newAura.isEmitter		= def.isEmitter or default.isEmitter
	newAura.emitterList		= {}
	newAura.emitterDefs		= def.emitterDefs or default.emitterDefs
	newAura.description		= def.description or default.description
	newAura.params			= def.params or default.params
	newAura.command			= def.command or default.command
	if def.RunOnLoad then
		def.RunOnLoad(newAura, def)
	else
		default.RunOnLoad(newAura, def)
	end
	auras[defName] = newAura
  end
  for _,unit in ipairs(Spring.GetAllUnits()) do
	local unitDefID = Spring.GetUnitDefID(unitID)
	for _, aura in pairs(auras) do
      if aura.emitterDefs[unitDefID] ~= nil then
		aura.emitterList[unit].state = ((aura.params[1] == '1') and 0) or 1
		if aura.cmdid ~= nil then
		  Spring.InsertUnitCmdDesc(unitID, aura.cmdid, aura.description)
		  Spring.CreateUnitRulesParams(unitID, { { [aura.name] = (aura.params[1] == '1') and 0 or 1} })
		end
	  end
	end
  end
end

function gadget:Shutdown()
  --IDIL
end


function gadget:UnitCreated(unitID, unitDefID, unitTeam)
  for _, aura in pairs(auras) do
	if aura.emitterDefs[unitDefID] ~= nil then
	  aura.emitterList[unitID] = {}
	  aura.emitterList[unitID].state = aura.defaultState
	  if aura.cmdid ~= nil then
		Spring.InsertUnitCmdDesc(unitID, aura.cmdid, aura.cmdDescription)
		Spring.CreateUnitRulesParams(unitID, { { [aura.name] = aura.defaultState } })
	  end
	end
  end
end


function gadget:UnitDestroyed(unitID, unitDefID, unitTeam)
  for _, aura in pairs(auras) do
	if aura.useGlobalAffList then
	  --it's more complex
	else
	  aura.emitterList[unitID] = nil
	end
  end
end


function gadget:UnitTaken(unitID, unitDefID, unitTeam)
  for _, aura in pairs(auras) do
	if aura.useGlobalAffList then
	  --it's more complex
	else
	  if aura.emitterDefs[unitDefID] ~= nil then
		aura.emitterList[unitID].state = aura.defaultState
		if aura.cmdid ~= nil then
		  SetUnitRulesParam(unitID, aura.name, aura.defaultState)
		end
	  end
	end
  end
end


local function AuraGameFrame(aura)
  if (aura.emitterList == nil) then return end
  for emitter,edata in pairs(aura.emitterList) do
    if edata.state ~= 0 then
	  local emitterTeam = Spring.GetUnitAllyTeam(emitter)
	  local emitterDef = aura.emitterDefs[Spring.GetUnitDefID(emitter)]
      local x, y, z = Spring.GetUnitPosition(emitter)
	  if (x == nil) then return end
	  local localAffected = Spring.GetUnitsInSphere(x, y, z, emitterDef.radius or 1000)
	  if (localAffected == nil) then return end
	  if aura.useGlobalAffList then
		--merge, etc.
	  else
		for _,affected in ipairs(localAffected) do
		  if (emitterTeam == Spring.GetUnitAllyTeam(affected)) then
			aura.affectedGameFrame(aura, affected)
		  end
		end
	  end
	  if aura.storeAffListPerE then
		aura.emitterList[emitter].affected = localAffected
	  end
	end
  end
end


function gadget:GameFrame(frameNum)
  for cmdid, aura in pairs(auras) do
	AuraGameFrame(aura) --possibly abstract later
  end
end


function gadget:AllowCommand(unitID, unitDefID, teamID,
                             cmdID, cmdParams, cmdOptions)
  for auraCmdID,aura in pairs(auras) do
    if cmdID == auraCmdID then
	  if aura.emitterList[emitter] ~= nil then
	    return true  --let it pass
	  else
		return false  -- command was used
	  end
	end
  end
  return true  -- command was not used
end


function gadget:CommandFallback(unitID, unitDefID, teamID,
                                cmdID, cmdParams, cmdOptions)
  for auraCmdID,aura in pairs(auras) do
    if cmdID == aura.cmdid then
	  if aura.emitterList[unitID] ~= nil then
		aura.command(aura, unitID, UnitDefID, teamID,
					cmdID, cmdParams, cmdOptions)
		return true, true  -- command was used, remove it
	  end
    end
  end
  return false  -- command was not used
end

end