Creeping metal gadget
Posted: 25 Oct 2007, 20:23
What am I doing wrong here? Or am I just hopelessly lost?
This widget is supposed to display a textured groundquad under a unit. Eventually, it will be used to make a growing creep-style graphical effect.
http://pastebin.com/m539a6943
This widget is supposed to display a textured groundquad under a unit. Eventually, it will be used to make a growing creep-style graphical effect.
http://pastebin.com/m539a6943
Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- file: unit_metals.lua
-- brief: Adds metal creep junk
-- author: aegis
--
-- Copyright (C) 2007.
-- Licensed under the terms of the GNU GPL, v2 or later.
--
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function gadget:GetInfo()
return {
name = "UnitMetals",
desc = "Adds metal creep functionality",
author = "aegis",
date = "September 23, 2007",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true -- loaded by default?
}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--
-- Proposed Command ID Ranges:
--
-- all negative: Engine (build commands)
-- 0 - 999: Engine
-- 1000 - 9999: Group AI
-- 10000 - 19999: LuaUI
-- 20000 - 29999: LuaCob
-- 30000 - 39999: LuaRules
--------------------------------------------------------------------------------
-- COMMON
--------------------------------------------------------------------------------
function isFinished(UnitID)
local _,_,_,_,buildProgress = Spring.GetUnitHealth(UnitID)
if ((buildProgress) and (buildProgress>=1)) then
return true
end
return false
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
if (gadgetHandler:IsSyncedCode()) then
--------------------------------------------------------------------------------
-- SYNCED
--------------------------------------------------------------------------------
include("LuaRules/colors.h.lua")
local metalDefs = {}
local metalUnits = {} -- make it global in Initialize()
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function DefCost(paramName, udSrc, udDst)
local pSrc = udSrc[paramName]
local pDst = udDst[paramName]
if ((not pSrc) or (not pDst) or
(type(pSrc) ~= 'number') or
(type(pDst) ~= 'number')) then
return 0
end
local cost = (pDst - pSrc) * metalPenalty
if (cost < 0) then
cost = 0
end
return math.floor(cost)
end
local function ValidatemetalDefs(mds)
local newDefs = {}
for src,metalData in pairs(mds) do
local udSrc = UnitDefNames[string.lower(src)]
if (not udSrc) then
Spring.Echo('Bad metal src type: ' .. src)
end
if (udSrc and udDst) then
local unitDef = UnitDefs[udSrc.id]
local newData = {}
newData.into = udDst.id
newData.speed = metalData.speed or 1
newDefs[udSrc.id] = newData
end
end
return newDefs
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local function UpdateMorphPossibilities(teamID)
local teamTech = teamTechLevel[teamID] or 0
local units = Spring.GetTeamUnitsSorted(teamID)
for unitDefID,unitIDs in pairs(units) do
local metalDefs = metalDefs[unitDefID]
if (metalDefs) then
for _,unitID in ipairs(unitIDs) do
local cmdDescID = Spring.FindUnitCmdDesc(unitID, CMD_MORPH)
if (cmdDescID) then
local unitXP = Spring.GetUnitExperience(unitID)
local metalCmdDesc = {}
metalCmdDesc.disabled = (metalDefs.tech > teamTech)or(metalDefs.xp > unitXP)
metalCmdDesc.tooltip = GetMorphToolTip(unitID, metalDefs, teamTech, unitXP)
Spring.EditUnitCmdDesc(unitID, cmdDescID, metalCmdDesc)
end
end
end
end
end
--------------------------------------------------------------------------------
------------
-- REPLACE THESE WITH THE CORRECT FUNCTIONS ;)
------------
local function StartMetal(unitID, metalDefs)
-- paralyze the unit
Spring.SetUnitHealth(unitID, { paralyze = 1.0e9 }) -- turns mexes and mm off
Spring.SetUnitResourcing(unitID,"e",0) -- turns solars off
Spring.GiveOrderToUnit(unitID, CMD.ONOFF, { 0 }, { }) -- turns radars/jammers off
metalUnits[unitID] = {
def = metalDefs,
progress = 0.0,
increment = metalDefs.increment
}
end
local function UpdateMetal(unitID, metalData)
if (Spring.UseUnitResource(unitID, metalData.def.resTable)) then
metalData.progress = metalData.progress + metalData.increment
end
if (metalData.progress >= 1.0) then
FinishMorph(unitID, metalData)
return false -- remove from the list, all done
end
return true
end
--------------------------------------------------------------------------------
function gadget:Initialize()
--[[
if Spring.IsReplay() then
gadgetHandler:RemoveGadget()
return
end
--]]
_G.metalUnits = metalUnits -- make it global for unsynced access via SYNCED
-- get the metalDefs
metalDefs = include("LuaRules/Configs/metal_defs.lua")
if (not metalDefs) then
gadgetHandler:RemoveGadget()
return
end
metalDefs = ValidatemetalDefs(metalDefs)
-- add the Morph command to existing units
for _,unitID in ipairs(Spring.GetAllUnits()) do
local teamID = Spring.GetUnitTeam(unitID)
local unitDefID = Spring.GetUnitDefID(unitID)
local metalDefs = metalDefs[unitDefID]
if (metalDefs) then
--make code here :)
end
end
end
function gadget:UnitCreated(unitID, unitDefID, teamID)
local metalDefs = metalDefs[unitDefID]
if (metalDefs) then
--- dunno
end
end
function gadget:UnitDestroyed(unitID, unitDefID, teamID)
--use unitID to check if there's metal, remove, do something ;)
end
function gadget:UnitTaken(unitID, unitDefID, oldTeamID, teamID)
--this should work as is
self:UnitCreated(unitID, unitDefID, teamID)
end
function gadget:UnitGiven(unitID, unitDefID, newTeamID, teamID)
--dunno
end
function gadget:UnitFinished(unitID, unitDefID, teamID)
--add start function here?
if (metalDefs) then
local px, py, pz = Spring.GetUnitBasePosition(unitID)
_G.px1 = px + 4
_G.px2 = px - 4
_G.pz1 = pz + 4
_G.pz2 = pz - 4
end
end
function gadget:GameFrame(n)
if ((n+28) % 64)<1 then
local teamIDs = Spring.GetTeamList()
for _,teamID in ipairs(teamIDs) do
-- UpdateMetalPossibilities(teamID)
-- make this update the list of units with metal radii?
end
end
if (next(metalUnits) == nil) then
return -- no metal spot units
end
local killUnits = {}
for unitID, metalData in pairs(metalUnits) do
if (not UpdateMetal(unitID, metalData)) then
killUnits[unitID] = true
end
end
for unitID in pairs(killUnits) do
metalUnits[unitID] = nil
end
end
--------------------------------------------------------------------------------
-- SYNCED
--------------------------------------------------------------------------------
else
--------------------------------------------------------------------------------
-- UNSYNCED
--------------------------------------------------------------------------------
--
-- speed-ups
--
local SYNCED = SYNCED
local GetUnitTeam = Spring.GetUnitTeam
local GetUnitHeading = Spring.GetUnitHeading
local GetUnitBasePosition = Spring.GetUnitBasePosition
local GetGameFrame = Spring.GetGameFrame
local GetSpectatingState = Spring.GetSpectatingState
local AddWorldIcon = Spring.AddWorldIcon
local AddWorldText = Spring.AddWorldText
local glColor = gl.Color
local glPushMatrix = gl.PushMatrix
local glTranslate = gl.Translate
local glRotate = gl.Rotate
local glUnitShape = gl.UnitShape
local glPopMatrix = gl.PopMatrix
--------------------------------------------------------------------------------
local function SelectSwap(cmd, oldID, newID)
local selUnits = Spring.GetSelectedUnits()
for i, unitID in ipairs(selUnits) do
if (unitID == oldID) then
selUnits[i] = newID
Spring.SelectUnitArray(selUnits)
return true
end
end
return true
end
function gadget:Initialize()
gadgetHandler:AddSyncAction("unit_metal", SelectSwap)
end
function gadget:Shutdown()
gadgetHandler:RemoveSyncAction("unit_metal")
end
local teamColors = {}
local function SetTeamColor(teamID,a)
local color = teamColors[teamID]
if (color) then
color[4]=a
glColor(color)
return
end
local _,_,_,_,_,_,r,g,b = Spring.GetTeamInfo(teamID)
if (r and g and b) then
color = { r, g, b }
teamColors[teamID] = color
glColor(color)
return
end
end
local function DrawMetalUnit(unitID, metalData, localTeamID)
--[[ commented out until you work it out :)
local unitTeam = GetUnitTeam(unitID)
local h = GetUnitHeading(unitID)
if (not h) then
return -- bonus, heading is only available when the unit is in LOS
end
local px,py,pz = GetUnitBasePosition(unitID)
if (not px) then
return
end
local frac = math.mod(GetGameFrame() + unitID, 30) / 30
local alpha = 2.0 * math.abs(0.5 - frac)
--glColor(1.0, 1.0, 1.0, alpha*0.3)
SetTeamColor(unitTeam,alpha)
glPushMatrix()
glTranslate(px, py, pz)
glRotate(h * (360 / 65535), 0, 1, 0)
glUnitShape(metalData.def.into, unitTeam)
glPopMatrix()
-- cheesy progress indicator
if (localTeamID)and(unitTeam==localTeamID) then
glPushMatrix()
glTranslate(px, py-20, pz)
gl.Billboard()
local progStr = string.format("%.1f%%", 100 * metalData.progress)
gl.Text(progStr, 0, 0, 7, "oc")
glPopMatrix()
end
]]--
end
function gadget:DrawWorld()
local metalUnits = SYNCED.metalUnits
if ((not metalUnits) or (snext(metalUnits) == nil)) then
return -- no metals to draw
end
--gl.Blending(GL.SRC_ALPHA, GL.ONE)
--gl.DepthTest(GL.LEQUAL)
local texName = "Bitmaps/metalring.png"
if (metalDefs) then
local px1 = SYNCED.px1
local px2 = SYNCED.px2
local pz1 = SYNCED.pz1
local pz2 = SYNCED.pz2
gl.Texture(texName)
gl.DrawGroundQuad(px1, pz1, px2, pz2)
gl.Texture(false)
end
local spec, specFullView = GetSpectatingState()
local readTeam
if (specFullView) then
readTeam = Script.ALL_ACCESS_TEAM
else
readTeam = Spring.GetLocalTeamID()
end
CallAsTeam({ ['read'] = readTeam }, function()
for unitID, metalData in spairs(metalUnits) do
if (unitID and metalData) then -- FIXME: huh?
DrawMorphUnit(unitID, metalData,readTeam)
end
end
end)
--gl.DepthTest(false)
--gl.Blending(GL.SRC_ALPHA, GL.ONE_MINUS_SRC_ALPHA)
end
--------------------------------------------------------------------------------
-- UNSYNCED
--------------------------------------------------------------------------------
end
--------------------------------------------------------------------------------
-- COMMON
--------------------------------------------------------------------------------
Code: Select all
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
local metalDefs = {
-- units
aliensoldl = {
speed = 3
},
alienmsoldl = {
speed = 2.5
},
}
return metalDefs
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------