代码来源于“易宁”大佬的分享,仅供学习,不要直接复制粘贴。

原帖链接:http://bbs.3dmgame.com/thread-3859071-1-1.html



一九七.吸地牛(用大理石种吸地牛,地面物品自动吸入肚中,可开矿、砍树)

	用MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/inv_marble.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:

local slotpos = {}
for y = 4, 0, -1 do
	for x = 0, 14 do
		table.insert(slotpos, Vector3(75*x-75*2+75, 75*y-75*2+75,0))
	end
end
local sounds = 
{
	walk = "dontstarve/beefalo/walk",
	grunt = "dontstarve/beefalo/grunt",
	yell = "dontstarve/beefalo/yell",
	swish = "dontstarve/beefalo/tail_swish",
	curious = "dontstarve/beefalo/curious",
	angry = "dontstarve/beefalo/angry",
}
local function OnDeploy (inst, pt)
	local bull = SpawnPrefab("marble")
	bull.Transform:SetPosition(pt.x, pt.y, pt.z)
	bull.AnimState:SetBank("beefalo")
	bull.AnimState:SetBuild("beefalo_build")
	bull.AnimState:PlayAnimation("idle_loop", true)
	bull.Transform:SetFourFaced()
	bull.Transform:SetScale(0.8, 0.8, 0.8)
	local sound = bull.entity:AddSoundEmitter()
	bull.sounds = sounds
	local shadow = bull.entity:AddDynamicShadow()
	shadow:SetSize( 3, 1.25 )
	MakeCharacterPhysics(bull, 100, 1.5)
	bull:AddTag("bull")
	bull:AddTag("companion")
	bull:AddComponent("locomotor")
	bull.components.locomotor.walkspeed = 1.5
	bull.components.locomotor.runspeed = 15
	bull:SetStateGraph("SGBeefalo")
	bull:AddComponent("follower")
	bull:AddComponent("knownlocations")
	bull.components.container.canbeopened = true
	bull:AddComponent("combat")
	bull:AddComponent("health")
	bull.components.health:SetMaxHealth(10000)
	bull.components.health:SetInvincible(true)
	bull.components.health.nofadeout = true
	bull:RemoveComponent("stackable")
	bull:RemoveComponent("inventoryitem")
	bull:RemoveComponent("bait")
	bull:RemoveTag("molebait")
	bull:AddComponent("workable")
	bull.components.workable:SetWorkAction(ACTIONS.CHOP)
	bull.components.workable:SetWorkLeft(3)
	bull.components.workable:SetOnFinishCallback(function(bull)
		bull.AnimState:PlayAnimation("death")
		bull.SoundEmitter:PlaySound("dontstarve/beefalo/yell")
		bull:DoTaskInTime(1.5, function() bull.components.container:DropEverything() bull:Remove() end)
	end )
	bull:DoPeriodicTask(1, function(bull)
		if not bull.components.container:Has("cutgrass", 1) then
		   bull.components.locomotor:Stop()
		   bull:SetBrain(nil)
		   bull.components.follower:SetLeader(nil)
		   bull:RemoveTag("letgo")
		else
		   local brain = require "brains/chesterbrain"
		   bull:SetBrain(brain)
		   bull:RestartBrain()
		   bull.components.follower:SetLeader(GetPlayer())
		   bull:AddTag("letgo")
		end
	end )
	bull:DoPeriodicTask(180, function(bull)
		if bull.components.container:Has("cutgrass", 1) then
		   bull.components.container:ConsumeByName("cutgrass", 1)
		   bull.AnimState:PlayAnimation("graze_loop")
		end
	end )
	bull:DoPeriodicTask(.1, function(bull)
	  if bull:HasTag("letgo") then
		local pos = Vector3(bull.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 15)
		for k,v in pairs(ents) do
			local pt1 = v:GetPosition()
			if v.components.inventoryitem and v.components.inventoryitem.canbepickedup and v.components.inventoryitem.cangoincontainer
			   and not v.components.inventoryitem:IsHeld() and not v:HasTag("trap") and not v:HasTag("light") and not v:HasTag("blowdart")
			   and not v:HasTag("projectile") and not v:HasTag("helpers") then
			   if not bull.components.container:IsFull() then
				  SpawnPrefab("small_puff").Transform:SetPosition(pt1.x, pt1.y, pt1.z)
				  bull.components.container:GiveItem(v)
				  bull.SoundEmitter:PlaySound("dontstarve/HUD/research_available")
			   end
			end
		end
	  end
	end )
	bull.Physics:SetCollisionCallback(function(bull, other)
		if other and other.components.workable and other.components.workable.workleft > 0 and not other:HasTag("bull") then
		   SpawnPrefab("collapse_small").Transform:SetPosition(other:GetPosition():Get())
		   other.components.workable:Destroy(bull)
		end
	end)
	inst.components.stackable:Get():Remove()
end
	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = OnDeploy
local function onsave(inst, data)
	if inst:HasTag("bull") then
		data.bull = true
	end
end
local function onload(inst, data)
  if data and data.bull then
	inst.AnimState:SetBank("beefalo")
	inst.AnimState:SetBuild("beefalo_build")
	inst.AnimState:PlayAnimation("idle_loop", true)
	inst.Transform:SetFourFaced()
	inst.Transform:SetScale(0.8, 0.8, 0.8)
	local sound = inst.entity:AddSoundEmitter()
	inst.sounds = sounds
	local shadow = inst.entity:AddDynamicShadow()
	shadow:SetSize( 3, 1.25 )
	MakeCharacterPhysics(inst, 100, 1.5)
	inst:AddTag("bull")
	inst:AddTag("companion")
	inst:AddComponent("locomotor")
	inst.components.locomotor.walkspeed = 1.5
	inst.components.locomotor.runspeed = 15
	inst:SetStateGraph("SGBeefalo")
	inst:AddComponent("follower")
	inst:AddComponent("knownlocations")
	inst.components.container.canbeopened = true
	inst:AddComponent("combat")
	inst:AddComponent("health")
	inst.components.health:SetMaxHealth(10000)
	inst.components.health:SetInvincible(true)
	inst.components.health.nofadeout = true
	inst:RemoveComponent("stackable")
	inst:RemoveComponent("inventoryitem")
	inst:RemoveComponent("bait")
	inst:RemoveTag("molebait")
	inst:AddComponent("workable")
	inst.components.workable:SetWorkAction(ACTIONS.CHOP)
	inst.components.workable:SetWorkLeft(3)
	inst.components.workable:SetOnFinishCallback(function(inst)
		inst.AnimState:PlayAnimation("death")
		inst.SoundEmitter:PlaySound("dontstarve/beefalo/yell")
		inst:DoTaskInTime(1.5, function() inst.components.container:DropEverything() inst:Remove() end)
	end )
	inst:DoPeriodicTask(1, function(inst)
		if not inst.components.container:Has("cutgrass", 1) then
		   inst.components.locomotor:Stop()
		   inst:SetBrain(nil)
		   inst.components.follower:SetLeader(nil)
		   inst:RemoveTag("letgo")
		else
		   local brain = require "brains/chesterbrain"
		   inst:SetBrain(brain)
		   inst:RestartBrain()
		   inst.components.follower:SetLeader(GetPlayer())
		   inst:AddTag("letgo")
		end
	end )
	inst:DoPeriodicTask(180, function(inst)
		if inst.components.container:Has("cutgrass", 1) then
		   inst.components.container:ConsumeByName("cutgrass", 1)
		   inst.AnimState:PlayAnimation("graze_loop")
		end
	end )
	inst:DoPeriodicTask(.1, function(inst)
	  if inst:HasTag("letgo") then
		local pos = Vector3(inst.Transform:GetWorldPosition())
		local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 15)
		for k,v in pairs(ents) do
			local pt1 = v:GetPosition()
			if v.components.inventoryitem and v.components.inventoryitem.canbepickedup and v.components.inventoryitem.cangoincontainer
			   and not v.components.inventoryitem:IsHeld() and not v:HasTag("trap") and not v:HasTag("light") and not v:HasTag("blowdart")
			   and not v:HasTag("projectile") and not v:HasTag("helpers") then
			   if not inst.components.container:IsFull() then
				  SpawnPrefab("small_puff").Transform:SetPosition(pt1.x, pt1.y, pt1.z)
				  inst.components.container:GiveItem(v)
				  inst.SoundEmitter:PlaySound("dontstarve/HUD/research_available")
			   end
			end
		end
	  end
	end )
	inst.Physics:SetCollisionCallback(function(inst, other)
		if other and other.components.workable and other.components.workable.workleft > 0 and not other:HasTag("bull") then
		   SpawnPrefab("collapse_small").Transform:SetPosition(other:GetPosition():Get())
		   other.components.workable:Destroy(inst)
		end
	end)
  end
end
	inst.OnSave = onsave
	inst.OnLoad = onload
	inst:AddComponent("container")
	inst.components.container:SetNumSlots(#slotpos)
	inst.components.container.widgetslotpos = slotpos
	inst.components.container.widgetpos = Vector3(-300,200,0)
	inst.components.container.side_align_tip = 160
	inst.components.container.canbeopened = false
	inst:AddTag("fridge")

	即可用大理石种吸地牛,鼠标左键点它可开启格子,放入草后会跟随你,吸取沿路所有物品(吸进肚中的格子里,鼠标左键点击可拿取)。拿出草后即停止跟随,在原地等待。吸地牛会消化肚中的草,每天消化2-3根,如果格子中的草被吃完了,就不会跟随你,直到再给它草为止。吸地牛可撞碎石头、撞倒树,并将掉落物品直接吸入肚中,带着它去开矿、砍树吧。不要让吸地牛靠近你的建筑物,它会将其撞毁,除非你想用它拆迁。不想要吸地牛时,用斧子砍它三下即可,肚中物品会掉在地上