Jump to content

Remove specific items on ground within given radius?


Recommended Posts

I guess by now most of you are already familiar with the lag that those homeless bees, twiggy trees and beefalos produce to over 1k servers. You could keep picking homeless' bee's flowers, beefalos' ****, and twiggy tree's twigs, but that can easily become tiresome.

Since anything is possible via Console, we should be able to come up with a command that can:

remove all the single items (specified with its prefab `prefabA`) on the ground without stacks (2 rocks and a stack of manure is useful and quite possibly not something that is lying around) within a given radius (say 1 screen radius around the player running the command)?

What I know by now is:

local playerPos = ThePlayer:GetPosition()

local x, y, z = ThePlayer.Transform:GetWorldPosition()

local ents = TheSim:FindEntities(x,y,z, radius, tags)

Actually, there is already code in simutil.lua that uses the lines above, so code below should *select* all flowers on ground, then remove them:

GetClosestInstWithTag('flower', ThePlayer, 10)

Now, add that with Remove(). There is already code even for this, so we dont have to manually do GetClosestInstWithTag('flower', ThePlayer, 10):Remove(), so the code is:

DeleteCloseEntsWithTag('flower', ThePlayer, 10)

 

Yay! A pity is, the shortcut command does not help us see if the prefab we are deleting is a single item or a stack of them. I am still not sure how to combine GetClosestInstWithTag() with a number of elements check (to make sure it is a single item) then remove it. Should be something like this, but needs improvement: (not quite sure about the #v==1 part)

    local x, y, z = inst.Transform:GetWorldPosition()
    local ents = TheSim:FindEntities(x, y, z, radius, type(tag) == "string" and { tag } or tag)
    for i, v in ipairs(ents) do

           if #v == 1; then v:Remove(); end
    end
 

 

Link to comment
Share on other sites

function c_delete(pref,n,radius)
	local e = TheInput:GetWorldEntityUnderMouse()
	if pref ~= nil then
		if n == nil or n == 1 then
			local deletthis = c_find(tostring(pref),radius or 80)
			if deletthis ~= nil then
				print("Deleting "..pref)
				if deletthis.components.stackable and deletthis.components.stackable.stacksize > 1 then
					deletthis.components.stackable:SetStackSize(deletthis.components.stackable.stacksize - 1)
					deletthis:PushEvent("stacksizechange")
				else
					deletthis:Remove()
				end
			else
				print("Can't find "..pref.." to delete")
			end
		else
			local times = math.abs(math.ceil(tonumber(n)))
			local delets = 0
			for i = 1, times do
				local deletthis = c_find(tostring(pref),radius or 80)
				if deletthis ~= nil then
					if deletthis.components.stackable and deletthis.components.stackable.stacksize > 1 then
						deletthis.components.stackable:SetStackSize(deletthis.components.stackable.stacksize - 1)
					else
						deletthis:Remove()
					end
					delets = delets + 1
				end
				if i == times then
				print((delets ~= 0) and ("Deleted "..delets.." instances of prefab "..pref) or ("Can't find "..pref.." to delete"))
				end
			end
		end
	elseif e ~= nil then
		print("Deleting "..e.name)
		e:Remove()
	else
		print("Nothing to delete")
	end
end

I have this in my consolecommands.lua, I guess you could modify it to do what you want.

I guess something like this would work:

function c_clean(pref,radius)
	local x,y,z = ConsoleCommandPlayer().Transform:GetWorldPosition()
	local ents = TheSim:FindEntities( x,y,z, radius or 9001)
	for k, v in ipairs(ents) do
		if v ~= nil and v.components.inventoryitem and v.components.inventoryitem.owner == nil then
			if v.components.stackable and v.components.stackable.stacksize == 1 then
				if pref == nil or pref == v.prefab then
					v:Remove()
				end
			end
		end
	end
end

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
×
  • Create New...