Basic Unit Shields

Basic Unit Shields

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

Moderator: Moderators

Post Reply
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Basic Unit Shields

Post by Argh »

This is a unit "shield" gameplay effect, that recharges over time, can have a weakness vs. a specific weapon type and nullifies Paralyze when on. This is somewhat similar to the very complex random-damage system I built for Merc Squad, but with a few twists here and there, to aid people in making more complex behaviors. You could add special effects (say, add the unitID to a table if Shield takes a hit, do a special effect with Lua or just call some CEGs) very easily, making this both gamecode-useful and pretty.

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 
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Basic Unit Shields

Post by zwzsg »

That's not a shield, that's just custom damage that wears/heal over shots/time.
User avatar
MidKnight
Posts: 2652
Joined: 10 Sep 2008, 03:11

Re: Basic Unit Shields

Post by MidKnight »

the projectile tracking code is the hard part >_>
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Basic Unit Shields

Post by Argh »

A "personal shield" and the gameplay effect described are exactly the same thing. I mean... what is a "shield"? It's damage mitigation / nullification. All a "shield" is... is some math that removes damage under whatever conditions you want.

Now... if you want big area shields, use Spring's bubbles, or use a spherical search around shield emitters every second or three, and put any Units inside that area "under the shield", and put them into the shieldDamageLocal table. The big disadvantage of doing big area shields without particle interception is that it will tend to get pretty darn slow, because you're going to have to wipe out the entries of shieldDamageLocal when Units leave the shielded area. I think that, for very large shields, it's best to use Spring's bubbles for now.

But hey, I thought that the Feature request was a series of complaints about "personal shields"- having to make large bubbles, etc. to generate good particle-interception behavior, etc.

And unlike Spring's built-in shields, this nullfies AOE damage from projectiles outside the shield itself. Pretty nice.

Also, if you really want the bounce behaviors, you could make that happen, but doing that in Lua would involve a number of searches every frame and tracking weapon projectiles, which as jK reminded us, is veeeeeeeery slow. You wouldn't want to use that for anything trivial, basically. I've thought up a few ways that that could be sped up (cubical area searches with large volumes, to avoid sphere searches on a regular basis) but I am not really interested in solving that problem today.
Last edited by Argh on 17 Feb 2009, 22:05, edited 1 time in total.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Basic Unit Shields

Post by zwzsg »

Also, it looks like I can unparalyze shielded units by shooting at them, and that when shields are down, they continue going deeper and deeper in the negative and then have to recharge all that negativness before being on again. There is also absolutly no way to check the state of a shield ingame, it'll be just shoot shoot shoot ooohhh now it damages.
Argh wrote:I mean... what is a "shield"?
A shield is not just a damage nullifier, it's a cool translucent bubble that deflect shots and has its own healtbar (unrelated to the unit healht).
I think that, for very large shields, it's best to use Spring's bubbles for now, imo.
My point exactly.

You know, back in the TA engine, you could also simulate personal shields by toggling the "ARMORED" state (and there was no Lua involved), but at least people who used this had cool animation to match.
imbaczek
Posts: 3629
Joined: 22 Aug 2006, 16:19

Re: Basic Unit Shields

Post by imbaczek »

if you want semi-efficient, lua-controllable shields, you need engine patches for:
- projectile entered/left shielded area call-out
- control of projectile physics in lua (including detonation)
- ability to selectively call the aforementioned lua call-outs for certain projectile types to minimize overhead

patches welcome...
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Basic Unit Shields

Post by zwzsg »

Oh I don't.

I just want team colored engine shield.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Basic Unit Shields

Post by Argh »

I just want team colored engine shield.
Then use an invisible shield (easy) and generate the visual effect with Lua (easy to really painful, depending on how fancy you get).
Also, it looks like I can unparalyze shielded units by shooting at them, and that when shields are down, they continue going deeper and deeper in the negative and...
Quit whining. This was a freebie, not a complete solution. You're expected to do some work yourself ;)
A shield is not just a damage nullifier, it's a cool translucent bubble that deflect shots and has its own healtbar (unrelated to the unit healht).
Oh, so you want it both ways, basically? Well, too bad.

You can't have the spherical stuff and shot deflections without very expensive particle detection code, and Spring's compromise on this is to check for that each frame, so some stuff may be moving too fast to detect collisions properly. It's just how it goes.

However... why go to that much trouble, when you can just fake it, by generating a CEG at the "point of impact", that looks like the projectile? Oh... that would involve lots of work on your end, though.
You know, back in the TA engine, you could also simulate personal shields by toggling the "ARMORED" state (and there was no Lua involved), but at least people who used this had cool animation to match.
1. There wasn't anything really like Spring's shields in OTA. Don't even try to tell me about the hackish, COB-based stuff, it sucked.

2. You can toggle the ARMORED state and do an animation very easily via Lua. Hell, it still works in COB.

In short... there are hard choices, always, and I'm getting increasingly annoyed with people who aren't willing to see that gameplay effects != what you see.

Removing those hard choices through what imbaczek is saying won't make you happy, either, when you see the speed hits involved. Even "semi-efficient" will mean "don't use this for some weapon types at all".
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Basic Unit Shields

Post by zwzsg »

Argh wrote:1. There wasn't anything really like Spring's shields in OTA. Don't even try to tell me about the hackish, COB-based stuff, it sucked.
Yes, I know how much they sucked. And that is exactly why I compare them to your Lua code! :P The only improvment between the Lua you posted and the old Cob way of TA days is that in TA, you could only count how many shots and time between shots, but not the shot power, while with the Lua call-in you know how much health the shot wanted to eat. But both are miles away from the Spring engine shield. And like I said, at least the TA shields usually came with clever visual animation tricks, of which your code is totally devoid of.

Argh wrote:You're expected to do some work yourself [...] Oh... that would involve lots of work on your end, though.
Like I said, let's ditch all the C++ of Spring.exe and just provide a Lua interpreter, and call lazy any modder who doesn't program his own RTS engine in Lua.

Argh wrote:I'm getting increasingly annoyed with people who aren't willing to see that gameplay effects != what you see.
Funny, my philosophy is that in video games what you see and the gameplay must match. But then of course it doesn't mean that they technically they have to, smoke and mirror is fine as long as the illusion doesn't break. For instance, your code is horrible in the sense that it implants some underlying shield mechanic without giving ANY clue to the player about it.

Well, too bad. [...] You can't have [...] compromise [...] It's just how it goes. [...] the speed hits involved
So basically we agree with what I wanted to point out, althought you won't admit as your position is to prove the contrary, which is that no, Lua shields can not replace the engine shield.

Argh wrote:However... why go to that much trouble, when you can just fake it, by generating a CEG at the "point of impact", that looks like the projectile? Oh... that would involve lots of work on your end, though.
So, if I have a mod with hundreds of weapons, I must write hundreds CEG to fake those hundreds of weapons, and then write some more Lua code to make the fake shots behave like real shot (the shots deflected by engine shield for instance can do damage and everything else shots do as they are actual shots), which is basically rewriting the whole weapon projectile handler in Lua. If I haven't done it yet, it's of coure pure lazyness on my part.

Quit whining. This was a freebie, not a complete solution.
I suppose you were expecting us to kneel and worship you for this holy offering of code. Truth is, I never asked for that code, and just wanted to expose the fraud into trying to make some more gullible forum readers believe this code bit be anything remotely like the current engine shield.

2. You can toggle the ARMORED state and do an animation very easily via Lua. Hell, it still works in COB.
Oh, and stop trying to teach me how to mod, even if Pure is nice and must have required tremendous amout of work, while I produced about naught content for Spring, I'm still contemptuous of your coding skills. You keep acting like you're the coding messiah, but over and over you display lots of misconception about how programming work and other technical aspect of Spring modding. Makes me wonder how you manage to build such a nice mod. Until I remember how about half of TA modders thought the if(true) were essential in the walkscripts and would strongly argue about it.

Removing those hard choices through what imbaczek is saying won't make you happy, either, when you see the speed hits involved. Even "semi-efficient" will mean "don't use this for some weapon types at all".
I'll be happy when it becomes acknownledged that there are problems where Lua is not the solution. So telling me how Lua won't ever make perfect shields is actually what I want, just not phrased with "be happy with that sucky Lua code", but when phrased as "hard coded features are essentials!"

According to the morons in there, it would now be perfectly normal if Pure RC4 displayed "error youradick" when player name == zwzsg, and if Pure RC5 was replaced by a badly packaged C.A. corrupting Spring so as to make it unusable until a complete wipe and reinstall.
User avatar
lurker
Posts: 3842
Joined: 08 Jan 2007, 06:13

Re: Basic Unit Shields

Post by lurker »

zwzsg wrote:The only improvment between the Lua you posted and the old Cob way of TA days is that in TA, you could only count how many shots and time between shots, but not the shot power, while with the Lua call-in you know how much health the shot wanted to eat.
But since Spring also has improved cob you can do that without lua, with HitByWeaponID.
zwzsg wrote:According to the morons in there, it would now be perfectly normal if Pure RC4 displayed "error youradick" when player name == zwzsg, and if Pure RC5 was replaced by a badly packaged C.A. corrupting Spring so as to make it unusable until a complete wipe and reinstall.
Imagine if you were plagued by desyncs starting at random times in games of Pure.
trepan
Former Engine Dev
Posts: 1200
Joined: 17 Nov 2005, 00:52

Re: Basic Unit Shields

Post by trepan »

A ShieldCollision() call-in is easy to implement, it's been done before :)
(also note that the synced version can force the collision to be ignored)
User avatar
1v0ry_k1ng
Posts: 4656
Joined: 10 Mar 2006, 10:24

Re: Basic Unit Shields

Post by 1v0ry_k1ng »

I think the ability to have a unit as 'shielded' by means of a second healthbar that recharges which can have some graphic attatched would be great, can finally have small units have small personal shields without having to worry about AOE weapons, weapon velocities, shield to hitsphere ratio etc
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Basic Unit Shields

Post by Argh »

at least the TA shields usually came with clever visual animation tricks, of which your code is totally devoid of
That's a part you're supposed to supply yourself.

And it's not hard, and there are plenty of pieces of code you can look at, if you need ideas. In every other way that actually matters, the shield code is equivalent. It gets the damage, it even gets the WeaponID, so you can do really fancy stuff with it, like in Merc Squad, where I have random headshots and other randomness, just because I felt like it.

So, basically... your entire argument is about bounces. Which is the least-important part of how shields work. You're basically arguing that since I can't deliver perfection, it's rubbish. That's funny!
Like I said, let's ditch all the C++ of Spring.exe and just provide a Lua interpreter, and call lazy any modder who doesn't program his own RTS engine in Lua.
Why do you always bring up arguments that are totally absurd whenever we point out that Lua is a powerful and flexible way to create brand-new behaviors, or improve upon engine code? Nobody thinks the entire engine should be Lua, that's too damn slow for anything that's mission-critical.

But in your case, if I take you at your word, you just needed to code a new visual effect to get team-colored shields. That's all doable with Lua... it won't be significantly faster in C++, because that's probably some very basic OpenGL that even an idiot like myself has figured out how to do... so do it.
Lua shields can not replace the engine shield.
I never said otherwise. I'm simply pointing out that, for most purposes, you could extend the code I gave you, add some visuals, and call it a day. If you're having problems with fast-moving shots, for example, but don't want huge shields (say, a space game, with little fighters for example) ... well, what I just gave you gives you a way to solve those problems.
So, if I have a mod with hundreds of weapons... snip... the shots deflected by engine shield for instance can do damage...
No offense, but most of the time, the shots bouncing off might as well be visual effects. Unless you're building a game design that's heavily dependent on that as a gameplay feature, players won't be upset. And since you haven't built any games with "hundreds of weapons", you're just being silly. I could probably do it for P.U.R.E. in an evening.
just wanted to expose the fraud
Well, no, I didn't offer a "fraud"- I offered a solution for several practical problems. That it doesn't completely satisfy you is pretty much irrelevant to me.
Oh, and stop trying to teach me how to mod, even if Pure is nice and must have required tremendous amout of work, while I produced about naught content for Spring, I'm still contemptuous of your coding skills.
Um... let's see... who needed me to hand him free code to do something simple, like custom Tooltips?

Oh yeah... that was me. Yup, I suck. Keep believing that, it means that you're too dumb to read the source I make available, and thereby will spend months reinventing things that I wrote in a couple of hours (this has already happened many times around here, btw).
You keep acting like you're the coding messiah
Since when? Did I claim godlike powers? Hell, I didn't even claim it would compile. Funny, I didn't say it was a panacea, and I didn't even claim it would work, and I'm supposedly a messiah. Hmm, guess that's what I get for helping, lol. Not that I intend to stop, it makes me laugh when people get pissed off because I've made and given away something useful.
I'll be happy when it becomes acknownledged that there are problems where Lua is not the solution.
Again, you're holding up a nice straw man there. When Lua can't do a given job, we need to admit that, and decide if it's important enough to be a priority.

However, before saying "I want this", I think it's perfectly fair to ask, "hey, did you try Lua first?". Maybe you've prototyped it, and it's too slow in Lua (like my revamp of LOS- haha, I'm such a shitty coder, I built a LOS system in Lua that was fast up to a reasonable threshold of Units, and only really broke because of CEGs and ALOS issues within Spring... lol, you're such a retard sometimes man)... or maybe you just can't get to a certain variable (like my request to do TypeMap changes) and need it to be exposed.

To sum up... you've called me a number of silly names, accused me of deliberately misleading people, and have used a bunch of straw-man arguments that are easy enough to see through. All because I decided to sit down and use some of my valuable time writing something free for everybody to use.

Thanks man, I appreciate that ;)
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Basic Unit Shields

Post by zwzsg »

Argh wrote:That's a part you're supposed to supply yourself.

And it's not hard, and there are plenty of pieces of code you can look at, if you need ideas.
No, I'm not asking for you to code the visual because I can't, I'm just explaining why the code bit you posted is so bare and stripped it would actually have to grow to a hundred lines of code before being good enough to use.


Argh wrote:You're basically arguing that since I can't deliver perfection, it's rubbish.
The point is, the engine shield are already near perfection, and your Lua shield will never even be able to come close*. Your shields are rubbish compared to the engine shields, so don't say us to abandon engine shields.(* at least with current Spring version, maybe they'll expose the projectile code to Lua or add shield hooks in Lua in next Spring.)


Argh wrote:Nobody thinks the entire engine should be Lua, that's too damn slow for anything that's mission-critical.
Nowadays "Lua it!" is the answer to every feature request, and more and more stuff gets exposed through Lua for modders to tweak and reimplement their way, I see threads about people asking that even the most CPU consuming thing for RTS, pathfinding, be Lua'ed, so even if nobody wants to replace the engine with a Lua interpreter, it's nonetheless the direction the Spring project appears to be slowly heading to.

Argh wrote:But in your case, if I take you at your word, you just needed to code a new visual effect to get team-colored shields. That's all doable with Lua... it won't be significantly faster in C++, because that's probably some very basic OpenGL that even an idiot like myself has figured out how to do... so do it.
Yeah, for the team colored shield request, it's most probably doable by having an invisible engine shield and Lua-drawing a colored sphere.

Argh wrote:I never said otherwise. I'm simply pointing out that, for most purposes, you could extend the code I gave you, add some visuals, and call it a day. If you're having problems with fast-moving shots, for example, but don't want huge shields (say, a space game, with little fighters for example) ... well, what I just gave you gives you a way to solve those problems.
Well, I could agree that Lua could be passable personal shields. But they initial request was for shields that cover multiple unit to be able to merge into even larger shields.

Argh wrote:So, basically... your entire argument is about bounces. Which is the least-important part of how shields work.
Argh wrote:No offense, but most of the time, the shots bouncing off might as well be visual effects. Unless you're building a game design that's heavily dependent on that as a gameplay feature, players won't be upset.
Well, the thing is, maybe they won't be upset, but they won't be flabbergasted by the awesomeness of SJ's shields either. There are many threads and videos about how it's cool to make plasma carousel and to kill comm by deflected bertha shots. And I prefer my video games to have that touch of outstanding than to be merely acceptably bland.

Argh wrote:Well, no, I didn't offer a "fraud"- I offered a solution for several practical problems. That it doesn't completely satisfy you is pretty much irrelevant to me.
Your code is irrelevant to that feature request:
1v0ry_k1ng wrote:shields dont overlap when two are touching, they link
But then I agree it answers to:
1v0ry_k1ng wrote:some work around so shields only slightly larger than hitsphere still function (and fast projectiles cant sneak through)
For instance I had used a similar Lua code to complement KP's ONS shield to ensure you couldn't bypass the shields by building mines inside the shield or using AoE weapons. But it's not a replacement, just a complement.


Not that I intend to stop, it makes me laugh when people get pissed off because I've made and given away something useful.
I'm not pissed because you gave away something, but because you linked to it in thread about making shields bubble merge, like if it was solution about it, while it's not. But then it can be the beginning of a solution for the blocking of fast shots in hitsphere-sized shields, so it's okay.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Re: Basic Unit Shields

Post by AF »

Perhaps we should add counters telling posters how many characters theyve typed
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Basic Unit Shields

Post by zwzsg »

trepan wrote:A ShieldCollision() call-in is easy to implement, it's been done before :)
(also note that the synced version can force the collision to be ignored)
Right now I have a need for a widget detecting that the player is attacking a shield. I don't think Spring.GetUnitShieldState will do as these are infinite power shields.
User avatar
Argh
Posts: 10920
Joined: 21 Feb 2005, 03:38

Re: Basic Unit Shields

Post by Argh »

[EDIT]Nevermind, that won't work, most of the time it won't show up in damaged. You can't just make the Shield recharge really, really fast?[/EDIT]
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Basic Unit Shields

Post by zwzsg »

Well, what I'm trying to do is a widget that tells people who are attacking a green shield in KP ONS "You're a noob, go for red!". I've just written load of code that detect when any order is given in the radius of a building, but right now I'm noticing that I don't even know how to check if weapon2 shield is on or off (I turn shield off by returning false in the aiming script.).

Ok, I tried about all that Spring.GetUnitWeaponState can return, even with the tags to ask something specific, and there nothing that would tell me if the shield-weapon aimed succesfully or not.

Then I tried Spring.GetUnitShieldState and luckily for me it picked the right one of the two shields.

So my widget is done.

Except, I'm apparently not allowed to get the shield states of enemies in a widget, even when they are in plain view. I guess I really need to add a gadget part to it then.

Oh... silly me, I had already exposed the shield state with some Spring.SetUnitRulesParam(UnitID,"ONS_Shielded",1) as I needed it for the tooltip. So now works for real. I just have to find a wording clear and concise for the explanation text.

Image
Attachments
ONS_help_Widget.jpg
(280.29 KiB) Downloaded 183 times
Post Reply

Return to “Lua Scripts”