If you want a bubble shield, there are various ways to do that effect visually, but that's outside the scope here.
Hereby released PD.
I would guess that once people see how easy it is, a lot of people would like to make more interesting Unit / Weapon interactions with gamecode, instead of using Spring's hardcoded defaults.
I wrote this really fast, and I haven't tested it, so please let me know if you find any bugs. Feel free to post optimizations if you see any obvious fat.
Code: Select all
function gadget:GetInfo()
	return {
		name = "Basic Shield Gadget",
		desc = "Skeleton for complex shield behaviors.",
		author = "Argh",
		date = "February 17th, 2009",
		license = "Public Domain, or the least-restrictive rights in your country of residence.",
		layer = 1,
		enabled = true,
	}
end
local shieldIgnoreList = {}
local shieldDamage = {}
local shieldDamageLocal = {}
local shieldAmount = 0
if (gadgetHandler:IsSyncedCode()) then
function gadget:Initialize()
	-- Set up weapon classes, for special damage
	for i=1,#WeaponDefs do  --search all WeaponDef entries
		if WeaponDefs[i].customParams.shield_ignore == "yes" then
			table.insert(shieldIgnoreList,i,1)
		end
	end
	-- Set special status for weapons that cause team damage
	for ud,_ in pairs(UnitDefs) do  -- search all UnitDef entries
		if UnitDefs[ud].customParams.has_shield == 'yes' then
			table.insert(shieldDamage,ud,1)
		end
	end
end
function gadget:UnitCreated(unitID,unitDefID,teamID)
	if shieldDamage[unitDefID] then
		table.insert(shieldDamageLocal,unitID,{amount = 100,})
	end
end
function gadget:UnitDestroyed(unitID,unitDefID,teamID)
	if shieldDamage[unitDefID] then
		table.remove(shieldDamageLocal,unitID)
	end
end
function gadget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)
	if shieldDamage[unitDefID] then
		if paralyzer then
			SetUnitHealth(unitID,{paralyze = 0}) --no Paralyze for units with Shields on.
		else
			if shieldIgnoreList[weaponID] then
				--normal damage results
			else
				--shield may have nullified results, remove power from shield now
				shieldDamageLocal[unitID].amount = shieldDamageLocal[unitID].amount - damage
				shieldAmount = shieldDamageLocal[unitID].amount -- save a search
				--if shield > 0, then nullify some damage
				if shieldAmount > 0 then
					SetUnitHealth(unitID,{health = Health + shieldAmount})
				end
			end
		end
	end
end
function gadget:GameFrame(f)
	if f % 35 < 0.01 then  -- every 35 frames, operate
		for i=1,#shieldDamageLocal () do    -- iterate through the entire shieldDamageLocal
			if shieldDamageLocal[i].amount <= 100
				shieldDamageLocal[i].amount = shieldDamageLocal[i].amount + 10    -- recharge our shields
			else
				shieldDamageLocal[i].amount = 100
			end
		end
	end
end
-------------------------------------------------------------------------------------END
----------------------------------------------------------------------------------
------------------------------------------------------------------------------
end 