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

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



三.良种山羊(用闪电羊角种良种山羊,会生小羊,能跟随、产羊奶)

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

local function OnDeploy (inst, pt)
	local goodgoat = SpawnPrefab("lightninggoathorn")
	goodgoat.Transform:SetPosition(pt.x, pt.y, pt.z)
	goodgoat.AnimState:SetBank("lightning_goat")
	goodgoat.AnimState:SetBuild("lightning_goat_build")
	goodgoat.AnimState:PlayAnimation("idle_loop", true)
	goodgoat.Transform:SetFourFaced()
	goodgoat.entity:AddSoundEmitter()
	local shadow = goodgoat.entity:AddDynamicShadow()
	shadow:SetSize(1.75,.75)
	MakeCharacterPhysics(goodgoat, 100, 1)
	goodgoat.entity:AddLight()
	goodgoat.Light:Enable(false)
	local minimap = goodgoat.entity:AddMiniMapEntity()
	minimap:SetIcon( "toothtrap.png" )
	goodgoat:RemoveComponent("stackable")
	goodgoat:RemoveComponent("inventoryitem")
	goodgoat:RemoveComponent("deployable")
	goodgoat:AddComponent("knownlocations")
	goodgoat:AddComponent("leader")
	goodgoat:AddComponent("follower")
	goodgoat.components.follower:SetLeader(GetPlayer())
	goodgoat.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
	goodgoat:AddTag("followme")
	goodgoat:AddComponent("locomotor")
	goodgoat.components.locomotor.walkspeed = 4
	goodgoat.components.locomotor.runspeed = 8
	goodgoat:SetStateGraph("SGlightninggoat")
	local brain = require "brains/chesterbrain"
	goodgoat:SetBrain(brain)
	goodgoat:AddComponent("health")
	goodgoat.components.health:SetMaxHealth(200)
	goodgoat:AddComponent("combat")
	goodgoat.components.combat.hiteffectsymbol = "lightning_goat_body"
	goodgoat.components.combat:SetAttackPeriod(1)
	goodgoat.components.combat:SetRange(1, 2)
	goodgoat.components.combat:SetDefaultDamage(10)
	goodgoat:AddComponent("lootdropper")
	goodgoat.components.lootdropper:SetLoot({"meat","meat","meat","meat","meat","meat"})
	goodgoat:ListenForEvent("attacked", function(goodgoat, data)
		if data.attacker ~= GetPlayer() then
		   goodgoat.components.combat:SetTarget(data.attacker)
		   goodgoat.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("goodgoats") and not dude.components.health:IsDead() end, 10)
		else
		   goodgoat.components.health:Kill()
		end
	end )
	goodgoat.components.inspectable.getstatus = function(goodgoat)
		if not goodgoat:HasTag("followme") then
		   local brain = require "brains/chesterbrain"
		   goodgoat:SetBrain(brain)
		   goodgoat:RestartBrain()
		   goodgoat.components.follower:SetLeader(GetPlayer())
		   goodgoat.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
		   goodgoat:AddTag("followme")
		else
		   local brain = require "brains/frogbrain"
		   goodgoat:SetBrain(brain)
		   goodgoat:RestartBrain()
		   goodgoat.components.follower:SetLeader(nil)
		   goodgoat.AnimState:SetBloomEffectHandle("")
		   goodgoat:RemoveTag("followme")
		end
	end
	goodgoat:DoPeriodicTask(math.random(180, 360), function(goodgoat)
		SpawnPrefab("goatmilk").Transform:SetPosition(goodgoat.Transform:GetWorldPosition())
	end )
	goodgoat:ListenForEvent( "nighttime", function()
		if math.random()<.3 then
		   local pt0 = goodgoat:GetPosition()
		   local smallgoat = SpawnPrefab("lightninggoathorn")
		   smallgoat.Transform:SetPosition(pt0.x, pt0.y, pt0.z)
		   smallgoat.AnimState:SetBank("lightning_goat")
		   smallgoat.AnimState:SetBuild("lightning_goat_build")
		   smallgoat.AnimState:PlayAnimation("idle_loop", true)
		   smallgoat.Transform:SetFourFaced()
		   smallgoat.Transform:SetScale(0.6, 0.6, 0.6)
		   smallgoat.entity:AddSoundEmitter()
		   local shadow = smallgoat.entity:AddDynamicShadow()
		   shadow:SetSize(1,.5)
		   MakeCharacterPhysics(smallgoat, 60, 0.6)
		   smallgoat.entity:AddLight()
		   smallgoat.Light:Enable(false)
		   local minimap = smallgoat.entity:AddMiniMapEntity()
		   minimap:SetIcon( "toothtrap.png" )
		   smallgoat:RemoveComponent("stackable")
		   smallgoat:RemoveComponent("inventoryitem")
		   smallgoat:RemoveComponent("deployable")
		   smallgoat:AddComponent("knownlocations")
		   smallgoat:AddComponent("follower")
		   smallgoat:AddComponent("locomotor")
		   smallgoat.components.locomotor.walkspeed = 4
		   smallgoat.components.locomotor.runspeed = 8
		   smallgoat:SetStateGraph("SGlightninggoat")
		   smallgoat:AddComponent("health")
		   smallgoat.components.health:SetMaxHealth(100)
		   smallgoat:AddComponent("combat")
		   smallgoat.components.combat.hiteffectsymbol = "lightning_goat_body"
		   smallgoat.components.combat:SetAttackPeriod(1)
		   smallgoat.components.combat:SetRange(1, 2)
		   smallgoat.components.combat:SetDefaultDamage(5)
		   smallgoat:AddComponent("lootdropper")
		   smallgoat.components.lootdropper:SetLoot({"meat","meat","meat","lightninggoathorn"})
		   smallgoat:ListenForEvent("attacked", function(smallgoat, data)
			   if data.attacker ~= GetPlayer() then
				  smallgoat.components.combat:SetTarget(data.attacker)
				  smallgoat.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("goodgoats") and not dude.components.health:IsDead() end, 10)
			   else
				  smallgoat.components.health:Kill()
			   end
		   end )
		   smallgoat:DoPeriodicTask(math.random(180, 360), function(smallgoat)
			   SpawnPrefab("poop").Transform:SetPosition(smallgoat.Transform:GetWorldPosition())
		   end )
		   smallgoat:DoTaskInTime(1, function(smallgoat)
			   local mama = FindEntity(smallgoat, 100, function(guy)
				   return guy:HasTag("goodgoats") and guy.components.leader and guy.components.health and not guy.components.health:IsDead()
			   end)
			   if mama then
				  local brain = require "brains/chesterbrain"
				  smallgoat:SetBrain(brain)
				  smallgoat:RestartBrain()
				  smallgoat.components.follower:SetLeader(mama)
			   else
				  local brain = require "brains/frogbrain"
				  smallgoat:SetBrain(brain)
				  smallgoat:RestartBrain()
				  smallgoat.components.follower:SetLeader(nil)
		   end
		   end )
		   smallgoat.components.inspectable.getstatus = function(smallgoat)
			   local mama = FindEntity(smallgoat, 100, function(guy)
				   return guy:HasTag("goodgoats") and guy.components.leader and guy.components.health and not guy.components.health:IsDead()
			   end)
			   if mama then
				  local brain = require "brains/chesterbrain"
				  smallgoat:SetBrain(brain)
				  smallgoat:RestartBrain()
				  smallgoat.components.follower:SetLeader(mama)
			   else
				  local brain = require "brains/frogbrain"
				  smallgoat:SetBrain(brain)
				  smallgoat:RestartBrain()
				  smallgoat.components.follower:SetLeader(nil)
		   end
		   end
		   smallgoat:AddTag("smallgoats")
		end
	end , GetWorld())
	goodgoat:AddTag("goodgoats")
	inst.components.stackable:Get():Remove()
end
	inst:AddComponent("deployable")
	inst.components.deployable.ondeploy = OnDeploy
local function onsave(inst, data)
	if inst:HasTag("goodgoats") then
		data.goodgoats = true
	end
	if inst:HasTag("followme") then
		data.followme = true
	end
	if inst:HasTag("smallgoats") then
		data.smallgoats = true
	end
end
local function onload(inst, data)
	if data and data.goodgoats then
	   inst.AnimState:SetBank("lightning_goat")
	   inst.AnimState:SetBuild("lightning_goat_build")
	   inst.AnimState:PlayAnimation("idle_loop", true)
	   inst.Transform:SetFourFaced()
	   inst.entity:AddSoundEmitter()
	   local shadow = inst.entity:AddDynamicShadow()
	   shadow:SetSize(1.75,.75)
	   MakeCharacterPhysics(inst, 100, 1)
	   inst.entity:AddLight()
	   inst.Light:Enable(false)
	   local minimap = inst.entity:AddMiniMapEntity()
	   minimap:SetIcon( "toothtrap.png" )
	   inst:RemoveComponent("stackable")
	   inst:RemoveComponent("inventoryitem")
	   inst:RemoveComponent("deployable")
	   inst:AddComponent("knownlocations")
	   inst:AddComponent("leader")
	   inst:AddComponent("follower")
	   inst:AddComponent("locomotor")
	   inst.components.locomotor.walkspeed = 4
	   inst.components.locomotor.runspeed = 8
	   inst:SetStateGraph("SGlightninggoat")
	   local brain = require "brains/frogbrain"
	   inst:SetBrain(brain)
	   inst:AddComponent("health")
	   inst.components.health:SetMaxHealth(200)
	   inst:AddComponent("combat")
	   inst.components.combat.hiteffectsymbol = "lightning_goat_body"
	   inst.components.combat:SetAttackPeriod(1)
	   inst.components.combat:SetRange(1, 2)
	   inst.components.combat:SetDefaultDamage(10)
	   inst:AddComponent("lootdropper")
	   inst.components.lootdropper:SetLoot({"meat","meat","meat","meat","meat","meat"})
	   inst:ListenForEvent("attacked", function(inst, data)
		   if data.attacker ~= GetPlayer() then
			  inst.components.combat:SetTarget(data.attacker)
			  inst.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("goodgoats") and not dude.components.health:IsDead() end, 10)
		   else
			  inst.components.health:Kill()
		   end
	   end )
	   inst.components.inspectable.getstatus = function(inst)
		   if not inst:HasTag("followme") then
			  local brain = require "brains/chesterbrain"
			  inst:SetBrain(brain)
			  inst:RestartBrain()
			  inst.components.follower:SetLeader(GetPlayer())
			  inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
			  inst:AddTag("followme")
		   else
			  local brain = require "brains/frogbrain"
			  inst:SetBrain(brain)
			  inst:RestartBrain()
			  inst.components.follower:SetLeader(nil)
			  inst.AnimState:SetBloomEffectHandle("")
			  inst:RemoveTag("followme")
		   end
	   end
	   inst:DoPeriodicTask(math.random(180, 360), function(inst)
		   SpawnPrefab("goatmilk").Transform:SetPosition(inst.Transform:GetWorldPosition())
	   end )
	   inst:ListenForEvent( "nighttime", function()
		   if math.random()<.3 then
			  local pt0 = inst:GetPosition()
			  local smallgoat = SpawnPrefab("lightninggoathorn")
			  smallgoat.Transform:SetPosition(pt0.x, pt0.y, pt0.z)
			  smallgoat.AnimState:SetBank("lightning_goat")
			  smallgoat.AnimState:SetBuild("lightning_goat_build")
			  smallgoat.AnimState:PlayAnimation("idle_loop", true)
			  smallgoat.Transform:SetFourFaced()
			  smallgoat.Transform:SetScale(0.6, 0.6, 0.6)
			  smallgoat.entity:AddSoundEmitter()
			  local shadow = smallgoat.entity:AddDynamicShadow()
			  shadow:SetSize(1,.5)
			  MakeCharacterPhysics(smallgoat, 60, 0.6)
			  smallgoat.entity:AddLight()
			  smallgoat.Light:Enable(false)
			  local minimap = smallgoat.entity:AddMiniMapEntity()
			  minimap:SetIcon( "toothtrap.png" )
			  smallgoat:RemoveComponent("stackable")
			  smallgoat:RemoveComponent("inventoryitem")
			  smallgoat:RemoveComponent("deployable")
			  smallgoat:AddComponent("knownlocations")
			  smallgoat:AddComponent("follower")
			  smallgoat:AddComponent("locomotor")
			  smallgoat.components.locomotor.walkspeed = 4
			  smallgoat.components.locomotor.runspeed = 8
			  smallgoat:SetStateGraph("SGlightninggoat")
			  smallgoat:AddComponent("health")
			  smallgoat.components.health:SetMaxHealth(100)
			  smallgoat:AddComponent("combat")
			  smallgoat.components.combat.hiteffectsymbol = "lightning_goat_body"
			  smallgoat.components.combat:SetAttackPeriod(1)
			  smallgoat.components.combat:SetRange(1, 2)
			  smallgoat.components.combat:SetDefaultDamage(5)
			  smallgoat:AddComponent("lootdropper")
			  smallgoat.components.lootdropper:SetLoot({"meat","meat","meat","lightninggoathorn"})
			  smallgoat:ListenForEvent("attacked", function(smallgoat, data)
				  if data.attacker ~= GetPlayer() then
					 smallgoat.components.combat:SetTarget(data.attacker)
					 smallgoat.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("goodgoats") and not dude.components.health:IsDead() end, 10)
				  else
					 smallgoat.components.health:Kill()
				  end
			  end )
			  smallgoat:DoPeriodicTask(math.random(180, 360), function(smallgoat)
				  SpawnPrefab("poop").Transform:SetPosition(smallgoat.Transform:GetWorldPosition())
			  end )
			  smallgoat:DoTaskInTime(1, function(smallgoat)
				  local mama = FindEntity(smallgoat, 100, function(guy)
					  return guy:HasTag("goodgoats") and guy.components.leader and guy.components.health and not guy.components.health:IsDead()
				  end)
				  if mama then
					 local brain = require "brains/chesterbrain"
					 smallgoat:SetBrain(brain)
					 smallgoat:RestartBrain()
					 smallgoat.components.follower:SetLeader(mama)
				  else
					 local brain = require "brains/frogbrain"
					 smallgoat:SetBrain(brain)
					 smallgoat:RestartBrain()
					 smallgoat.components.follower:SetLeader(nil)
			  end
			  end )
			  smallgoat.components.inspectable.getstatus = function(smallgoat)
				  local mama = FindEntity(smallgoat, 100, function(guy)
					  return guy:HasTag("goodgoats") and guy.components.leader and guy.components.health and not guy.components.health:IsDead()
				  end)
				  if mama then
					 local brain = require "brains/chesterbrain"
					 smallgoat:SetBrain(brain)
					 smallgoat:RestartBrain()
					 smallgoat.components.follower:SetLeader(mama)
				  else
					 local brain = require "brains/frogbrain"
					 smallgoat:SetBrain(brain)
					 smallgoat:RestartBrain()
					 smallgoat.components.follower:SetLeader(nil)
			  end
			  end
			  smallgoat:AddTag("smallgoats")
		   end
	   end , GetWorld())
	   inst:AddTag("goodgoats")
	end
	if data and data.followme then
	   local brain = require "brains/chesterbrain"
	   inst:SetBrain(brain)
	   inst:RestartBrain()
	   inst.components.follower:SetLeader(GetPlayer())
	   inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
	   inst:AddTag("followme")
	end
	if data and data.smallgoats then
	   inst.AnimState:SetBank("lightning_goat")
	   inst.AnimState:SetBuild("lightning_goat_build")
	   inst.AnimState:PlayAnimation("idle_loop", true)
	   inst.Transform:SetFourFaced()
	   inst.Transform:SetScale(0.6, 0.6, 0.6)
	   inst.entity:AddSoundEmitter()
	   local shadow = inst.entity:AddDynamicShadow()
	   shadow:SetSize(1,.5)
	   MakeCharacterPhysics(inst, 60, 0.6)
	   inst.entity:AddLight()
	   inst.Light:Enable(false)
	   local minimap = inst.entity:AddMiniMapEntity()
	   minimap:SetIcon( "toothtrap.png" )
	   inst:RemoveComponent("stackable")
	   inst:RemoveComponent("inventoryitem")
	   inst:RemoveComponent("deployable")
	   inst:AddComponent("knownlocations")
	   inst:AddComponent("follower")
	   inst:AddComponent("locomotor")
	   inst.components.locomotor.walkspeed = 4
	   inst.components.locomotor.runspeed = 8
	   inst:SetStateGraph("SGlightninggoat")
	   inst:AddComponent("health")
	   inst.components.health:SetMaxHealth(100)
	   inst:AddComponent("combat")
	   inst.components.combat.hiteffectsymbol = "lightning_goat_body"
	   inst.components.combat:SetAttackPeriod(1)
	   inst.components.combat:SetRange(1, 2)
	   inst.components.combat:SetDefaultDamage(5)
	   inst:AddComponent("lootdropper")
	   inst.components.lootdropper:SetLoot({"meat","meat","meat","lightninggoathorn"})
	   inst:ListenForEvent("attacked", function(inst, data)
		   if data.attacker ~= GetPlayer() then
			  inst.components.combat:SetTarget(data.attacker)
			  inst.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("goodgoats") and not dude.components.health:IsDead() end, 10)
		   else
			  inst.components.health:Kill()
		   end
	   end )
	   inst:DoPeriodicTask(math.random(180, 360), function(inst)
		   SpawnPrefab("poop").Transform:SetPosition(inst.Transform:GetWorldPosition())
	   end )
	   inst:DoTaskInTime(1, function(inst)
		   local mama = FindEntity(inst, 100, function(guy)
			   return guy:HasTag("goodgoats") and guy.components.leader and guy.components.health and not guy.components.health:IsDead()
		   end)
		   if mama then
			  local brain = require "brains/chesterbrain"
			  inst:SetBrain(brain)
			  inst:RestartBrain()
			  inst.components.follower:SetLeader(mama)
		   else
			  local brain = require "brains/frogbrain"
			  inst:SetBrain(brain)
			  inst:RestartBrain()
			  inst.components.follower:SetLeader(nil)
		   end
	   end )
	   inst.components.inspectable.getstatus = function(inst)
		   local mama = FindEntity(inst, 100, function(guy)
			   return guy:HasTag("goodgoats") and guy.components.leader and guy.components.health and not guy.components.health:IsDead()
		   end)
		   if mama then
			  local brain = require "brains/chesterbrain"
			  inst:SetBrain(brain)
			  inst:RestartBrain()
			  inst.components.follower:SetLeader(mama)
		   else
			  local brain = require "brains/frogbrain"
			  inst:SetBrain(brain)
			  inst:RestartBrain()
			  inst.components.follower:SetLeader(nil)
	   end
	   end
	   inst:AddTag("smallgoats")
	end
end
	inst.OnSave = onsave
	inst.OnLoad = onload

	即可用闪电羊角种良种山羊,鼠标左键点山羊(手里不要拿武器,以免误伤)可跟随,再次点击取消跟随。良种山羊每天可产1-2份羊奶,夜里还有一定概率生下小羊,杀掉良种山羊可得6块大肉。小羊只跟随大羊,如果你杀掉了小羊的妈妈,它会乱跑,这时带一只大羊,用鼠标左键点小羊,可让它认新的妈妈。小羊会产便便,不会长大,可作为你的肉食来源,屠宰后得到3块大肉和闪电羊角,其中闪电羊角可用于再种良种山羊,逐步壮大你的羊群。良种山羊在小地图上显示为犬牙陷阱图标。闪电羊角可打闪电羊获得,如修改了“巨型超市”,也可花1-3个黄金购得