代码来源于“易宁”大佬的分享,仅供学习,不要直接复制粘贴。
原帖链接:http://bbs.3dmgame.com/thread-3859071-1-1.html
二一五.猪族崛起(大陆上不定期出现猪族部落,他们将与你争夺资源,如何与之相处呢?) 1.用MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/forest.lua文件,在inst:AddComponent("clock")的下一行插入以下内容: local function createpigtent(inst) local pt = Vector3(GetPlayer().Transform:GetWorldPosition()) local ground = GetWorld() local result_offset = FindValidPositionByFan(math.random()*2*PI, math.random(50, 80), 100, function(offset) local x,y,z = (pt + offset):Get() local ents = TheSim:FindEntities(x,y,z , 1) return not next(ents) end) if result_offset and ground.Map:GetTileAtPoint((pt + result_offset):Get()) ~= GROUND.IMPASSABLE then local pigtent = SpawnPrefab("mosquitosack") pigtent.Transform:SetPosition((pt + result_offset):Get()) pigtent.AnimState:SetBank("tent") pigtent.AnimState:SetBuild("tent") pigtent.AnimState:PlayAnimation("idle", true) local minimap = pigtent.entity:AddMiniMapEntity() minimap:SetIcon( "tent.png" ) pigtent.AnimState:SetMultColour(255/255,0/255,0/255,1) pigtent.Transform:SetScale(2, 2, 2) pigtent:RemoveComponent("stackable") pigtent:RemoveComponent("inventoryitem") pigtent:RemoveComponent("healer") pigtent:RemoveComponent("burnable") pigtent:RemoveComponent("propagator") pigtent:RemoveTag("cattoy") pigtent:AddTag("pigtents") pigtent:AddComponent("workable") pigtent.components.workable:SetWorkAction(ACTIONS.HAMMER) pigtent.components.workable:SetWorkLeft(15) pigtent.components.workable:SetOnFinishCallback(function(pigtent) SpawnPrefab("collapse_big").Transform:SetPosition(pigtent.Transform:GetWorldPosition()) GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood") pigtent:Remove() end ) pigtent.components.workable:SetOnWorkCallback(function(pigtent, worker, workleft) local pos = Vector3(pigtent.Transform:GetWorldPosition()) local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 100) for k,v in pairs(ents) do if v:HasTag("pignpcs") then v.components.combat:SetTarget(worker) end end end) pigtent:ListenForEvent( "daytime", function() for k = 1,math.random(7,15) do local pt1 = pigtent:GetPosition() local pignpc = SpawnPrefab("mosquitosack") pignpc.Transform:SetPosition(pt1.x+(math.random(20)-math.random(20)), 0, pt1.z+(math.random(20)-math.random(20))) pignpc.entity:AddSoundEmitter() local shadow = pignpc.entity:AddDynamicShadow() shadow:SetSize( 1.5, .75 ) pignpc.AnimState:SetBank("pigman") pignpc.AnimState:SetBuild("pig_guard_build") pignpc.Transform:SetFourFaced() pignpc.Transform:SetScale(1.2, 1.2, 1.2) MakeCharacterPhysics(pignpc, 50, .5) local hats = {"hat_bee","hat_beefalo","hat_bush","hat_earmuffs","hat_feather","hat_flower","hat_football","hat_miner","hat_ruins","hat_slurper","hat_slurtle","hat_spider","hat_straw","hat_top","hat_walrus","hat_winter"} local hat = hats[math.random(#hats)] pignpc.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat") pignpc.AnimState:Show("hat") pignpc.AnimState:PlayAnimation("idle_loop") local brain = require "brains/leifbrain" pignpc:SetBrain(brain) local minimap = pignpc.entity:AddMiniMapEntity() minimap:SetIcon( "pigking.png" ) pignpc:AddComponent("locomotor") pignpc.components.locomotor.walkspeed = 5 pignpc.components.locomotor.runspeed = 10 pignpc:SetStateGraph("SGpig") pignpc:RemoveComponent("stackable") pignpc:RemoveComponent("inventoryitem") pignpc:RemoveComponent("healer") pignpc:RemoveComponent("burnable") pignpc:RemoveComponent("propagator") pignpc:RemoveTag("cattoy") pignpc:AddComponent("inventory") pignpc:AddComponent("follower") pignpc:AddComponent("knownlocations") pignpc:AddComponent("lootdropper") pignpc.components.lootdropper:SetLoot({"meat", "meat"}) pignpc:AddComponent("health") pignpc.components.health:SetMaxHealth(1000) pignpc:AddComponent("combat") pignpc.components.combat:SetDefaultDamage(20) pignpc.components.combat:SetAttackPeriod(1) pignpc.components.combat:SetRetargetFunction(3, function(pignpc) if not pignpc.components.health:IsDead() then return FindEntity(pignpc, 20, function(guy) if guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("npcs") then return guy:HasTag("monster") or guy:HasTag("smallcreature") end end ) end end ) pignpc.components.combat:SetKeepTargetFunction(function(pignpc, target) return target and target:IsValid() end ) pignpc:ListenForEvent("attacked", function(pignpc, data) pignpc.components.combat:SetTarget(data.attacker) pignpc.components.combat:ShareTarget(data.attacker, 50, function(dude) return dude:HasTag("pignpcs") and not dude.components.health:IsDead() end, 10) end ) pignpc:DoPeriodicTask(1, function(pignpc) if not pignpc.components.health:IsDead() then local pos = Vector3(pignpc.Transform:GetWorldPosition()) local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3) for k,v in pairs(ents) do if v.components.pickable and v.components.pickable:CanBePicked() then v.components.pickable:Pick(pignpc) end if v.components.crop then v.components.crop:Harvest(pignpc) end if v:HasTag("tree") and v.components.workable and v.components.workable.workleft > 0 then v.components.workable:Destroy(pignpc) end if v.components.edible and v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then v:Remove() end end end end ) pignpc:ListenForEvent("death", function() for k = 1,math.random(1,3) do local pt2 = pignpc:GetPosition() local gold = SpawnPrefab("goldnugget") gold.Transform:SetPosition(pt2.x+(math.random(3)-math.random(3)), 0, pt2.z+(math.random(3)-math.random(3))) end end ) pignpc:ListenForEvent( "daytime", function() pignpc:Remove() end , GetWorld()) pignpc:AddTag("pignpcs") end end , GetWorld()) end end inst:ListenForEvent( "nighttime", function() if math.random()<0.2 then createpigtent(inst) end end , GetWorld()) 2.用MT管理器打开游戏目录/assets/scripts/prefabs/mosquitosack.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容: local function onsave(inst, data) if inst:HasTag("pigtents") then data.pigtents = true end if inst:HasTag("pignpcs") then data.pignpcs = true end end local function onload(inst, data) if data and data.pigtents then inst.AnimState:SetBank("tent") inst.AnimState:SetBuild("tent") inst.AnimState:PlayAnimation("idle", true) local minimap = inst.entity:AddMiniMapEntity() minimap:SetIcon( "tent.png" ) inst.AnimState:SetMultColour(255/255,0/255,0/255,1) inst.Transform:SetScale(2, 2, 2) inst:RemoveComponent("stackable") inst:RemoveComponent("inventoryitem") inst:RemoveComponent("healer") inst:RemoveComponent("burnable") inst:RemoveComponent("propagator") inst:RemoveTag("cattoy") inst:AddTag("pigtents") inst:AddComponent("workable") inst.components.workable:SetWorkAction(ACTIONS.HAMMER) inst.components.workable:SetWorkLeft(15) inst.components.workable:SetOnFinishCallback(function(inst) SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition()) GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood") inst:Remove() end ) inst.components.workable:SetOnWorkCallback(function(inst, worker, workleft) local pos = Vector3(inst.Transform:GetWorldPosition()) local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 100) for k,v in pairs(ents) do if v:HasTag("pignpcs") then v.components.combat:SetTarget(worker) end end end) inst:ListenForEvent( "daytime", function() for k = 1,math.random(7,15) do local pt1 = inst:GetPosition() local pignpc = SpawnPrefab("mosquitosack") pignpc.Transform:SetPosition(pt1.x+(math.random(20)-math.random(20)), 0, pt1.z+(math.random(20)-math.random(20))) pignpc.entity:AddSoundEmitter() local shadow = pignpc.entity:AddDynamicShadow() shadow:SetSize( 1.5, .75 ) pignpc.AnimState:SetBank("pigman") pignpc.AnimState:SetBuild("pig_guard_build") pignpc.Transform:SetFourFaced() pignpc.Transform:SetScale(1.2, 1.2, 1.2) MakeCharacterPhysics(pignpc, 50, .5) local hats = {"hat_bee","hat_beefalo","hat_bush","hat_earmuffs","hat_feather","hat_flower","hat_football","hat_miner","hat_ruins","hat_slurper","hat_slurtle","hat_spider","hat_straw","hat_top","hat_walrus","hat_winter"} local hat = hats[math.random(#hats)] pignpc.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat") pignpc.AnimState:Show("hat") pignpc.AnimState:PlayAnimation("idle_loop") local brain = require "brains/leifbrain" pignpc:SetBrain(brain) local minimap = pignpc.entity:AddMiniMapEntity() minimap:SetIcon( "pigking.png" ) pignpc:AddComponent("locomotor") pignpc.components.locomotor.walkspeed = 5 pignpc.components.locomotor.runspeed = 10 pignpc:SetStateGraph("SGpig") pignpc:RemoveComponent("stackable") pignpc:RemoveComponent("inventoryitem") pignpc:RemoveComponent("healer") pignpc:RemoveComponent("burnable") pignpc:RemoveComponent("propagator") pignpc:RemoveTag("cattoy") pignpc:AddComponent("inventory") pignpc:AddComponent("follower") pignpc:AddComponent("knownlocations") pignpc:AddComponent("lootdropper") pignpc.components.lootdropper:SetLoot({"meat", "meat"}) pignpc:AddComponent("health") pignpc.components.health:SetMaxHealth(1000) pignpc:AddComponent("combat") pignpc.components.combat:SetDefaultDamage(20) pignpc.components.combat:SetAttackPeriod(1) pignpc.components.combat:SetRetargetFunction(3, function(pignpc) if not pignpc.components.health:IsDead() then return FindEntity(pignpc, 20, function(guy) if guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("npcs") then return guy:HasTag("monster") or guy:HasTag("smallcreature") end end ) end end ) pignpc.components.combat:SetKeepTargetFunction(function(pignpc, target) return target and target:IsValid() end ) pignpc:ListenForEvent("attacked", function(pignpc, data) pignpc.components.combat:SetTarget(data.attacker) pignpc.components.combat:ShareTarget(data.attacker, 50, function(dude) return dude:HasTag("pignpcs") and not dude.components.health:IsDead() end, 10) end ) pignpc:DoPeriodicTask(1, function(pignpc) if not pignpc.components.health:IsDead() then local pos = Vector3(pignpc.Transform:GetWorldPosition()) local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3) for k,v in pairs(ents) do if v.components.pickable and v.components.pickable:CanBePicked() then v.components.pickable:Pick(pignpc) end if v.components.crop then v.components.crop:Harvest(pignpc) end if v:HasTag("tree") and v.components.workable and v.components.workable.workleft > 0 then v.components.workable:Destroy(pignpc) end if v.components.edible and v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then v:Remove() end end end end ) pignpc:ListenForEvent("death", function() for k = 1,math.random(1,3) do local pt2 = pignpc:GetPosition() local gold = SpawnPrefab("goldnugget") gold.Transform:SetPosition(pt2.x+(math.random(3)-math.random(3)), 0, pt2.z+(math.random(3)-math.random(3))) end end ) pignpc:ListenForEvent( "daytime", function() pignpc:Remove() end , GetWorld()) pignpc:AddTag("pignpcs") end end , GetWorld()) end if data and data.pignpcs then inst.entity:AddSoundEmitter() local shadow = inst.entity:AddDynamicShadow() shadow:SetSize( 1.5, .75 ) inst.AnimState:SetBank("pigman") inst.AnimState:SetBuild("pig_guard_build") inst.Transform:SetFourFaced() inst.Transform:SetScale(1.2, 1.2, 1.2) MakeCharacterPhysics(inst, 50, .5) local hats = {"hat_bee","hat_beefalo","hat_bush","hat_earmuffs","hat_feather","hat_flower","hat_football","hat_miner","hat_ruins","hat_slurper","hat_slurtle","hat_spider","hat_straw","hat_top","hat_walrus","hat_winter"} local hat = hats[math.random(#hats)] inst.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat") inst.AnimState:Show("hat") inst.AnimState:PlayAnimation("idle_loop") local brain = require "brains/leifbrain" inst:SetBrain(brain) local minimap = inst.entity:AddMiniMapEntity() minimap:SetIcon( "pigking.png" ) inst:AddComponent("locomotor") inst.components.locomotor.walkspeed = 5 inst.components.locomotor.runspeed = 10 inst:SetStateGraph("SGpig") inst:RemoveComponent("stackable") inst:RemoveComponent("inventoryitem") inst:RemoveComponent("healer") inst:RemoveComponent("burnable") inst:RemoveComponent("propagator") inst:RemoveTag("cattoy") inst:AddComponent("inventory") inst:AddComponent("follower") inst:AddComponent("knownlocations") inst:AddComponent("lootdropper") inst.components.lootdropper:SetLoot({"meat", "meat"}) inst:AddComponent("health") inst.components.health:SetMaxHealth(1000) inst:AddComponent("combat") inst.components.combat:SetDefaultDamage(20) inst.components.combat:SetAttackPeriod(1) inst.components.combat:SetRetargetFunction(3, function(inst) if not inst.components.health:IsDead() then return FindEntity(inst, 20, function(guy) if guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("npcs") then return guy:HasTag("monster") or guy:HasTag("smallcreature") end end ) end end ) inst.components.combat:SetKeepTargetFunction(function(inst, target) return target and target:IsValid() end ) inst:ListenForEvent("attacked", function(inst, data) inst.components.combat:SetTarget(data.attacker) inst.components.combat:ShareTarget(data.attacker, 50, function(dude) return dude:HasTag("pignpcs") and not dude.components.health:IsDead() end, 10) end ) inst:DoPeriodicTask(1, function(inst) if not inst.components.health:IsDead() then local pos = Vector3(inst.Transform:GetWorldPosition()) local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3) for k,v in pairs(ents) do if v.components.pickable and v.components.pickable:CanBePicked() then v.components.pickable:Pick(inst) end if v.components.crop then v.components.crop:Harvest(inst) end if v:HasTag("tree") and v.components.workable and v.components.workable.workleft > 0 then v.components.workable:Destroy(inst) end if v.components.edible and v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then v:Remove() end end end end ) inst:ListenForEvent("death", function() for k = 1,math.random(1,3) do local pt2 = inst:GetPosition() local gold = SpawnPrefab("goldnugget") gold.Transform:SetPosition(pt2.x+(math.random(3)-math.random(3)), 0, pt2.z+(math.random(3)-math.random(3))) end end ) inst:ListenForEvent( "daytime", function() inst:Remove() end , GetWorld()) inst:AddTag("pignpcs") end end inst.OnSave = onsave inst.OnLoad = onload 即可在大陆上不定期出现猪族人的营地(红色的大帐篷),他们是新进化的亚人类,不会主动攻击你(会攻击怪物),但会与你争夺大陆上的资源,他们会砍伐森林、打猎、采集一切可以采集的植物(包括你的农田)。如果你进攻他们,将遭到抵抗,如果你拆毁他们的营地,也将被视为敌人,如果你用围墙将他们圈起来,猪族人也将毫不犹豫地拆毁它。是与猪族人和平相处,任由他们的不断壮大,还是与他们为敌,不断地进行猎杀,是摆在你面前的一道选择题。猪族营地在小地图上显示为帐篷图标,猪族人显示为猪王图标。用锤子砸15下可拆毁红色帐篷,被拆毁了帐篷的猪族部落,将在第二天天亮时离去