Gadget that orders features reclaimed when placing buildings

Gadget that orders features reclaimed when placing buildings

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

Moderator: Moderators

User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

ok, basically, I have a bunch of features that are non-blocking. In many cases trees and such. I need a gadget that will cause anything in the unit to be built's footprint to be reclaimed by the builder, or just lolpwned (spontaneous removal), I really don't care, I just need this. Spontaneous removal would be fine... I already have an effect in mind for it, but yes please, any thoughts? Help?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Gadget that orders features reclaimed when placing buildings

Post by knorke »

destroy all features in a square around new buildings:

Code: Select all

local a = 200
gadget::UnitCreated(unitID, unitDefID, teamID, builderID)
 if Spring.GetUnitIsBuilding (unitID then
   x,y,z=Spring.GetUnitPosition (unitID)
   featuresToKill = Spring.GetFeaturesInRectangle (x-a,z-a, x+a, z+a)
   for i in pairs(featuresToKill) do
    Spring.DestroyFeature (featuresToKill [i])
   end
 end
end
untested
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

You sir, are my hero! gadget hotfixes thanks you :-)

Edit: Crashes the gadget handler. I'm guessing because I didn't set up the gadget right:
http://pastebin.com/gNpG7jN1
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

This is the error I get:

[f=0000000] Lua LoadCode pcall error = 2, LuaRules/main.lua, error = 2, LuaRules/gadgets.lua, [string "LuaRules/gadgets.lua"]:492: attempt to index local 'info' (a nil value)

(and yes I've fixed the typo on line 16 :-)
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

ok, it was somehting else crashing the gadget handler. My mistake.

Here is the current iteration (thanks for the help abma!), http://pastebin.com/index/dsQy4UaM It isn't throwing any errors, but it also isn't working. :-/
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

Code: Select all

   function gadget:GetInfo()
      return {
        name      = "Engine Hotfixes",
        desc      = "Hotfixes for engine behavior",
        author    = "knorke",
        date      = "July 2011",
        license   = "PD",
        layer     = 0,
        enabled   = true,
      }
    end
     
-- Destroy features in the build rectangle of new units for blocking features
function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
	if UnitDefs[unitDefID]["canMove"] == false then
		x,y,z=Spring.GetUnitPosition (unitID)
		local footprintx = UnitDefs[unitDefID]["xsize"] * 4
		local footprintz = UnitDefs[unitDefID]["zsize"] * 4
		featuresToKill = Spring.GetFeaturesInRectangle (x-footprintx,z-footprintz, x+footprintx, z+footprintz)
		for i in pairs(featuresToKill) do
			-- Spring.Echo('featuresToKill :'..featuresToKill [i])
			local fx,fy,fz = Spring.GetFeaturePosition(featuresToKill [i])
			if featuresToKill [i] ~= 0 then
				Spring.PlaySoundFile("sounds/reclaimed.wav", 1, fx, fy, fz)
				Spring.SpawnCEG("sparklegreen", fx, fy, fz)
				Spring.DestroyFeature (featuresToKill [i])
			end
		end
	end
end 
Ok, a couple hours later, we have a perfectly functioning gadget.

Huge thanks to abma and jk for helping me understand simple lua logic and helping me get it running (actually abma got it working, at which point I had ot make a lot of corrections to get it to behave how I wanted, with much help form abma and jk).


This is how it works:
When you place a building to be built, this gadget checks in that building's footprint, if there is a feature in that footprint, it is destroyed. If a feature is destroyed, a sound and a ceg will fire off at the location of the feature that was removed. So you'll want to change reclaimed.wav and sparklegreen to something applicable for your game.

Why did I need this? Because evo makes features 2x2 and smaller non blocking, which also means that when placing buildings, the 2x2 and smaller features were never reclaimed, causing a lot of graphical yukiness. Imo, this acts the way spring should by default, but yeah, with this you no longer have to worry about non-blocking features giving you grief.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Gadget that orders features reclaimed when placing buildings

Post by FLOZi »

Ideally reclaim behaviour would be separated from blocking behaviour engine side...
(I assume you already tried just forcing them reclaimable and autoReclaimable?)
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Gadget that orders features reclaimed when placing buildings

Post by knorke »

Imo, this acts the way spring should by default
no.
Or what if you want features to show through certain buildings? Maybe because they mark buildable positions or something, like geovents.
Or what if all buildings are on pillars like seen below and clipping is never a problem?
Image
Then it would be super annoying to make Spring not delete the features.

Btw, might want to check that not to delete geovent features:

Code: Select all

if FeatureDefs[Spring.GetFeatureDefID(featuresToKill[i])].name == "geovent" then
If non-blocking features were reclaimable, you could never move constructors through stuff like bushes. On rightclick they would try to reclaim instead of move.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

FLOZi wrote:Ideally reclaim behaviour would be separated from blocking behaviour engine side...
(I assume you already tried just forcing them reclaimable and autoReclaimable?)
Non-blocking features are not reclaimed when a building is placed. That's the start of the entire problem.
knorke wrote:If non-blocking features were reclaimable, you could never move constructors through stuff like bushes. On rightclick they would try to reclaim instead of move.
What? All these features are reclaimable, and cons move through them just fine. What are you trying to say? You mean if the coverage was uberthick?
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

Fix for geothermal vents:

Code: Select all

   function gadget:GetInfo()
      return {
        name      = "Engine Hotfixes",
        desc      = "Hotfixes for engine behavior",
        author    = "knorke",
        date      = "July 2011",
        license   = "PD",
        layer     = 0,
        enabled   = true,
      }
    end
     
-- Destroy features in the build rectangle of new units for blocking features
function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
	if UnitDefs[unitDefID]["canMove"] == false then
		x,y,z=Spring.GetUnitPosition (unitID)
		local footprintx = UnitDefs[unitDefID]["xsize"] * 4
		local footprintz = UnitDefs[unitDefID]["zsize"] * 4
		featuresToKill = Spring.GetFeaturesInRectangle (x-footprintx,z-footprintz, x+footprintx, z+footprintz)
		for i in pairs(featuresToKill) do
			-- Spring.Echo('featuresToKill :'..featuresToKill [i])
			local fx,fy,fz = Spring.GetFeaturePosition(featuresToKill [i])
			local isGeo = FeatureDefs[Spring.GetFeatureDefID(featuresToKill[i])].name == "geovent"
			if featuresToKill [i] ~= 0 and not isGeo then
				Spring.PlaySoundFile("sounds/reclaimed.wav", 1, fx, fy, fz)
				Spring.SpawnCEG("sparklegreen", fx, fy, fz)
				Spring.DestroyFeature (featuresToKill [i])
			end
		end
	end
end 
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Gadget that orders features reclaimed when placing buildings

Post by knorke »

just checking if feature.name=="geovent" will not be enough in 0.83 to detect all geovent features.
instead check for the geoThermal tag
https://github.com/1231e7b84a5de93a/spr ... b073f36755
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Gadget that orders features reclaimed when placing buildings

Post by FLOZi »

Code: Select all

   function gadget:GetInfo()
      return {
        name      = "Engine Hotfixes",
        desc      = "Hotfixes for engine behavior",
        author    = "knorke",
        date      = "July 2011",
        license   = "PD",
        layer     = 0,
        enabled   = true,
      }
    end
     
if (gadgetHandler:IsSyncedCode()) then

-- LOCALISATIONS go here (I am too lazy to do this for you)

-- Destroy features in the build rectangle of new units for blocking features
function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
   if UnitDefs[unitDefID].isBuilding then
      local x, y, z = Spring.GetUnitPosition (unitID)
      local footprintX = UnitDefs[unitDefID]["xsize"] * 4
      local footprintZ = UnitDefs[unitDefID]["zsize"] * 4
      local featuresToKill = Spring.GetFeaturesInRectangle (x - footprintX, z - footprintZ, x + footprintX, z + footprintZ)
      for _, featureID in pairs(featuresToKill) do
         local featureDef = FeatureDefs[Spring.GetFeatureDefID(featureID)]
         if (not featureDef.name == "geovent") and (not featureDef.geoThermal) then
            local fx,fy,fz = Spring.GetFeaturePosition(featureID)
            Spring.PlaySoundFile("sounds/reclaimed.wav", 1, fx, fy, fz)
            Spring.SpawnCEG("sparklegreen", fx, fy, fz)
            Spring.DestroyFeature (featureID)
         end
      end
   end
end

end
Last edited by FLOZi on 13 Jul 2011, 23:49, edited 2 times in total.
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

Doesn't work flozi. No errors tho :-)
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Gadget that orders features reclaimed when placing buildings

Post by FLOZi »

Try now? :(
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

Nope. :?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Gadget that orders features reclaimed when placing buildings

Post by knorke »

knorke wrote:in 0.83
?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Gadget that orders features reclaimed when placing buildings

Post by FLOZi »

Yeah, but now featureDef.geoThermal should just return nil.
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: Gadget that orders features reclaimed when placing buildings

Post by Kloot »

Code: Select all

if (not featureDef.name == "geovent") and (not featureDef.geoThermal) then
should be

Code: Select all

if (featureDef.name ~= "geovent") and (not featureDef.geoThermal) then
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6242
Joined: 29 Apr 2005, 01:14

Re: Gadget that orders features reclaimed when placing buildings

Post by FLOZi »

Ah yes. I always forget that 'not' has such low precedence. :oops:
User avatar
Forboding Angel
Evolution RTS Developer
Posts: 14673
Joined: 17 Nov 2005, 02:43

Re: Gadget that orders features reclaimed when placing buildings

Post by Forboding Angel »

still doesn't work.

Code: Select all

   function gadget:GetInfo()
      return {
        name      = "Engine Hotfixes",
        desc      = "Hotfixes for engine behavior",
        author    = "knorke",
        date      = "July 2011",
        license   = "PD",
        layer     = 0,
        enabled   = true,
      }
    end
     
-- Destroy features in the build rectangle of new units for blocking features
function gadget:UnitCreated(unitID, unitDefID, teamID, builderID)
	if UnitDefs[unitDefID]["canMove"] == false then
		x,y,z=Spring.GetUnitPosition (unitID)
		local footprintx = UnitDefs[unitDefID]["xsize"] * 4
		local footprintz = UnitDefs[unitDefID]["zsize"] * 4
		featuresToKill = Spring.GetFeaturesInRectangle (x-footprintx,z-footprintz, x+footprintx, z+footprintz)
		for i in pairs(featuresToKill) do
			-- Spring.Echo('featuresToKill :'..featuresToKill [i])
			local fx,fy,fz = Spring.GetFeaturePosition(featuresToKill [i])
			local isGeo = FeatureDefs[Spring.GetFeatureDefID(featuresToKill[i])].name == "geovent" 
			if featuresToKill [i] ~= 0 and not isGeo then
				Spring.PlaySoundFile("sounds/reclaimed.wav", 1, fx, fy, fz)
				Spring.SpawnCEG("sparklegreen", fx, fy, fz)
				Spring.DestroyFeature (featuresToKill [i])
			end
		end
	end
end 
THis does but needs additions of r.83 geovents if I understand correctly. Also, flozi, the rectangle cannot be properly drawn if I were to put the localizations where you suggest anyway (because that's where they were originally and I had to learn the hard way)
Post Reply

Return to “Lua Scripts”