Page 1 of 2

Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 05:34
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?

Re: Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 06:11
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

Re: Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 06:33
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

Re: Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 07:11
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 :-)

Re: Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 07:27
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. :-/

Re: Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 11:00
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.

Re: Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 15:15
by FLOZi
Ideally reclaim behaviour would be separated from blocking behaviour engine side...
(I assume you already tried just forcing them reclaimable and autoReclaimable?)

Re: Gadget that orders features reclaimed when placing buildings

Posted: 11 Jul 2011, 16:12
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.

Re: Gadget that orders features reclaimed when placing buildings

Posted: 12 Jul 2011, 11:17
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?

Re: Gadget that orders features reclaimed when placing buildings

Posted: 13 Jul 2011, 06:58
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 

Re: Gadget that orders features reclaimed when placing buildings

Posted: 13 Jul 2011, 08:10
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

Re: Gadget that orders features reclaimed when placing buildings

Posted: 13 Jul 2011, 15:36
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

Re: Gadget that orders features reclaimed when placing buildings

Posted: 13 Jul 2011, 23:15
by Forboding Angel
Doesn't work flozi. No errors tho :-)

Re: Gadget that orders features reclaimed when placing buildings

Posted: 13 Jul 2011, 23:48
by FLOZi
Try now? :(

Re: Gadget that orders features reclaimed when placing buildings

Posted: 15 Jul 2011, 00:02
by Forboding Angel
Nope. :?

Re: Gadget that orders features reclaimed when placing buildings

Posted: 16 Jul 2011, 18:15
by knorke
knorke wrote:in 0.83
?

Re: Gadget that orders features reclaimed when placing buildings

Posted: 16 Jul 2011, 18:41
by FLOZi
Yeah, but now featureDef.geoThermal should just return nil.

Re: Gadget that orders features reclaimed when placing buildings

Posted: 16 Jul 2011, 19:51
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

Re: Gadget that orders features reclaimed when placing buildings

Posted: 16 Jul 2011, 21:24
by FLOZi
Ah yes. I always forget that 'not' has such low precedence. :oops:

Re: Gadget that orders features reclaimed when placing buildings

Posted: 17 Jul 2011, 06:01
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)