代码来源于“易宁”大佬的分享,仅供学习,不要直接复制粘贴。
原帖链接:http://bbs.3dmgame.com/thread-3859071-1-1.html
1 2二一五.猪族崛起(大陆上不定期出现猪族部落,他们将与你争夺资源,如何与之相处呢?) 3 4 1.用MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/forest.lua文件,在inst:AddComponent("clock")的下一行插入以下内容: 5 6local function createpigtent(inst) 7 local pt = Vector3(GetPlayer().Transform:GetWorldPosition()) 8 local ground = GetWorld() 9 local result_offset = FindValidPositionByFan(math.random()*2*PI, math.random(50, 80), 100, function(offset) 10 local x,y,z = (pt + offset):Get() 11 local ents = TheSim:FindEntities(x,y,z , 1) 12 return not next(ents) 13 end) 14 if result_offset and ground.Map:GetTileAtPoint((pt + result_offset):Get()) ~= GROUND.IMPASSABLE then 15 local pigtent = SpawnPrefab("mosquitosack") 16 pigtent.Transform:SetPosition((pt + result_offset):Get()) 17 pigtent.AnimState:SetBank("tent") 18 pigtent.AnimState:SetBuild("tent") 19 pigtent.AnimState:PlayAnimation("idle", true) 20 local minimap = pigtent.entity:AddMiniMapEntity() 21 minimap:SetIcon( "tent.png" ) 22 pigtent.AnimState:SetMultColour(255/255,0/255,0/255,1) 23 pigtent.Transform:SetScale(2, 2, 2) 24 pigtent:RemoveComponent("stackable") 25 pigtent:RemoveComponent("inventoryitem") 26 pigtent:RemoveComponent("healer") 27 pigtent:RemoveComponent("burnable") 28 pigtent:RemoveComponent("propagator") 29 pigtent:RemoveTag("cattoy") 30 pigtent:AddTag("pigtents") 31 pigtent:AddComponent("workable") 32 pigtent.components.workable:SetWorkAction(ACTIONS.HAMMER) 33 pigtent.components.workable:SetWorkLeft(15) 34 pigtent.components.workable:SetOnFinishCallback(function(pigtent) 35 SpawnPrefab("collapse_big").Transform:SetPosition(pigtent.Transform:GetWorldPosition()) 36 GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood") 37 pigtent:Remove() 38 end ) 39 pigtent.components.workable:SetOnWorkCallback(function(pigtent, worker, workleft) 40 local pos = Vector3(pigtent.Transform:GetWorldPosition()) 41 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 100) 42 for k,v in pairs(ents) do 43 if v:HasTag("pignpcs") then 44 v.components.combat:SetTarget(worker) 45 end 46 end 47 end) 48 pigtent:ListenForEvent( "daytime", function() 49 for k = 1,math.random(7,15) do 50 local pt1 = pigtent:GetPosition() 51 local pignpc = SpawnPrefab("mosquitosack") 52 pignpc.Transform:SetPosition(pt1.x+(math.random(20)-math.random(20)), 0, pt1.z+(math.random(20)-math.random(20))) 53 pignpc.entity:AddSoundEmitter() 54 local shadow = pignpc.entity:AddDynamicShadow() 55 shadow:SetSize( 1.5, .75 ) 56 pignpc.AnimState:SetBank("pigman") 57 pignpc.AnimState:SetBuild("pig_guard_build") 58 pignpc.Transform:SetFourFaced() 59 pignpc.Transform:SetScale(1.2, 1.2, 1.2) 60 MakeCharacterPhysics(pignpc, 50, .5) 61 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"} 62 local hat = hats[math.random(#hats)] 63 pignpc.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat") 64 pignpc.AnimState:Show("hat") 65 pignpc.AnimState:PlayAnimation("idle_loop") 66 local brain = require "brains/leifbrain" 67 pignpc:SetBrain(brain) 68 local minimap = pignpc.entity:AddMiniMapEntity() 69 minimap:SetIcon( "pigking.png" ) 70 pignpc:AddComponent("locomotor") 71 pignpc.components.locomotor.walkspeed = 5 72 pignpc.components.locomotor.runspeed = 10 73 pignpc:SetStateGraph("SGpig") 74 pignpc:RemoveComponent("stackable") 75 pignpc:RemoveComponent("inventoryitem") 76 pignpc:RemoveComponent("healer") 77 pignpc:RemoveComponent("burnable") 78 pignpc:RemoveComponent("propagator") 79 pignpc:RemoveTag("cattoy") 80 pignpc:AddComponent("inventory") 81 pignpc:AddComponent("follower") 82 pignpc:AddComponent("knownlocations") 83 pignpc:AddComponent("lootdropper") 84 pignpc.components.lootdropper:SetLoot({"meat", "meat"}) 85 pignpc:AddComponent("health") 86 pignpc.components.health:SetMaxHealth(1000) 87 pignpc:AddComponent("combat") 88 pignpc.components.combat:SetDefaultDamage(20) 89 pignpc.components.combat:SetAttackPeriod(1) 90 pignpc.components.combat:SetRetargetFunction(3, function(pignpc) 91 if not pignpc.components.health:IsDead() then 92 return FindEntity(pignpc, 20, function(guy) 93 if guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("npcs") then 94 return guy:HasTag("monster") or guy:HasTag("smallcreature") 95 end 96 end ) 97 end 98 end ) 99 pignpc.components.combat:SetKeepTargetFunction(function(pignpc, target) return target and target:IsValid() end ) 100 pignpc:ListenForEvent("attacked", function(pignpc, data) 101 pignpc.components.combat:SetTarget(data.attacker) 102 pignpc.components.combat:ShareTarget(data.attacker, 50, function(dude) return dude:HasTag("pignpcs") and not dude.components.health:IsDead() end, 10) 103 end ) 104 pignpc:DoPeriodicTask(1, function(pignpc) 105 if not pignpc.components.health:IsDead() then 106 local pos = Vector3(pignpc.Transform:GetWorldPosition()) 107 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3) 108 for k,v in pairs(ents) do 109 if v.components.pickable and v.components.pickable:CanBePicked() then 110 v.components.pickable:Pick(pignpc) 111 end 112 if v.components.crop then 113 v.components.crop:Harvest(pignpc) 114 end 115 if v:HasTag("tree") and v.components.workable and v.components.workable.workleft > 0 then 116 v.components.workable:Destroy(pignpc) 117 end 118 if v.components.edible and v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then 119 v:Remove() 120 end 121 end 122 end 123 end ) 124 pignpc:ListenForEvent("death", function() 125 for k = 1,math.random(1,3) do 126 local pt2 = pignpc:GetPosition() 127 local gold = SpawnPrefab("goldnugget") 128 gold.Transform:SetPosition(pt2.x+(math.random(3)-math.random(3)), 0, pt2.z+(math.random(3)-math.random(3))) 129 end 130 end ) 131 pignpc:ListenForEvent( "daytime", function() pignpc:Remove() end , GetWorld()) 132 pignpc:AddTag("pignpcs") 133 end 134 end , GetWorld()) 135 end 136end 137 inst:ListenForEvent( "nighttime", function() if math.random()<0.2 then createpigtent(inst) end end , GetWorld()) 138 139 140 2.用MT管理器打开游戏目录/assets/scripts/prefabs/mosquitosack.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容: 141 142local function onsave(inst, data) 143 if inst:HasTag("pigtents") then 144 data.pigtents = true 145 end 146 if inst:HasTag("pignpcs") then 147 data.pignpcs = true 148 end 149end 150local function onload(inst, data) 151 if data and data.pigtents then 152 inst.AnimState:SetBank("tent") 153 inst.AnimState:SetBuild("tent") 154 inst.AnimState:PlayAnimation("idle", true) 155 local minimap = inst.entity:AddMiniMapEntity() 156 minimap:SetIcon( "tent.png" ) 157 inst.AnimState:SetMultColour(255/255,0/255,0/255,1) 158 inst.Transform:SetScale(2, 2, 2) 159 inst:RemoveComponent("stackable") 160 inst:RemoveComponent("inventoryitem") 161 inst:RemoveComponent("healer") 162 inst:RemoveComponent("burnable") 163 inst:RemoveComponent("propagator") 164 inst:RemoveTag("cattoy") 165 inst:AddTag("pigtents") 166 inst:AddComponent("workable") 167 inst.components.workable:SetWorkAction(ACTIONS.HAMMER) 168 inst.components.workable:SetWorkLeft(15) 169 inst.components.workable:SetOnFinishCallback(function(inst) 170 SpawnPrefab("collapse_big").Transform:SetPosition(inst.Transform:GetWorldPosition()) 171 GetPlayer().SoundEmitter:PlaySound("dontstarve/common/destroy_wood") 172 inst:Remove() 173 end ) 174 inst.components.workable:SetOnWorkCallback(function(inst, worker, workleft) 175 local pos = Vector3(inst.Transform:GetWorldPosition()) 176 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 100) 177 for k,v in pairs(ents) do 178 if v:HasTag("pignpcs") then 179 v.components.combat:SetTarget(worker) 180 end 181 end 182 end) 183 inst:ListenForEvent( "daytime", function() 184 for k = 1,math.random(7,15) do 185 local pt1 = inst:GetPosition() 186 local pignpc = SpawnPrefab("mosquitosack") 187 pignpc.Transform:SetPosition(pt1.x+(math.random(20)-math.random(20)), 0, pt1.z+(math.random(20)-math.random(20))) 188 pignpc.entity:AddSoundEmitter() 189 local shadow = pignpc.entity:AddDynamicShadow() 190 shadow:SetSize( 1.5, .75 ) 191 pignpc.AnimState:SetBank("pigman") 192 pignpc.AnimState:SetBuild("pig_guard_build") 193 pignpc.Transform:SetFourFaced() 194 pignpc.Transform:SetScale(1.2, 1.2, 1.2) 195 MakeCharacterPhysics(pignpc, 50, .5) 196 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"} 197 local hat = hats[math.random(#hats)] 198 pignpc.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat") 199 pignpc.AnimState:Show("hat") 200 pignpc.AnimState:PlayAnimation("idle_loop") 201 local brain = require "brains/leifbrain" 202 pignpc:SetBrain(brain) 203 local minimap = pignpc.entity:AddMiniMapEntity() 204 minimap:SetIcon( "pigking.png" ) 205 pignpc:AddComponent("locomotor") 206 pignpc.components.locomotor.walkspeed = 5 207 pignpc.components.locomotor.runspeed = 10 208 pignpc:SetStateGraph("SGpig") 209 pignpc:RemoveComponent("stackable") 210 pignpc:RemoveComponent("inventoryitem") 211 pignpc:RemoveComponent("healer") 212 pignpc:RemoveComponent("burnable") 213 pignpc:RemoveComponent("propagator") 214 pignpc:RemoveTag("cattoy") 215 pignpc:AddComponent("inventory") 216 pignpc:AddComponent("follower") 217 pignpc:AddComponent("knownlocations") 218 pignpc:AddComponent("lootdropper") 219 pignpc.components.lootdropper:SetLoot({"meat", "meat"}) 220 pignpc:AddComponent("health") 221 pignpc.components.health:SetMaxHealth(1000) 222 pignpc:AddComponent("combat") 223 pignpc.components.combat:SetDefaultDamage(20) 224 pignpc.components.combat:SetAttackPeriod(1) 225 pignpc.components.combat:SetRetargetFunction(3, function(pignpc) 226 if not pignpc.components.health:IsDead() then 227 return FindEntity(pignpc, 20, function(guy) 228 if guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("npcs") then 229 return guy:HasTag("monster") or guy:HasTag("smallcreature") 230 end 231 end ) 232 end 233 end ) 234 pignpc.components.combat:SetKeepTargetFunction(function(pignpc, target) return target and target:IsValid() end ) 235 pignpc:ListenForEvent("attacked", function(pignpc, data) 236 pignpc.components.combat:SetTarget(data.attacker) 237 pignpc.components.combat:ShareTarget(data.attacker, 50, function(dude) return dude:HasTag("pignpcs") and not dude.components.health:IsDead() end, 10) 238 end ) 239 pignpc:DoPeriodicTask(1, function(pignpc) 240 if not pignpc.components.health:IsDead() then 241 local pos = Vector3(pignpc.Transform:GetWorldPosition()) 242 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3) 243 for k,v in pairs(ents) do 244 if v.components.pickable and v.components.pickable:CanBePicked() then 245 v.components.pickable:Pick(pignpc) 246 end 247 if v.components.crop then 248 v.components.crop:Harvest(pignpc) 249 end 250 if v:HasTag("tree") and v.components.workable and v.components.workable.workleft > 0 then 251 v.components.workable:Destroy(pignpc) 252 end 253 if v.components.edible and v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then 254 v:Remove() 255 end 256 end 257 end 258 end ) 259 pignpc:ListenForEvent("death", function() 260 for k = 1,math.random(1,3) do 261 local pt2 = pignpc:GetPosition() 262 local gold = SpawnPrefab("goldnugget") 263 gold.Transform:SetPosition(pt2.x+(math.random(3)-math.random(3)), 0, pt2.z+(math.random(3)-math.random(3))) 264 end 265 end ) 266 pignpc:ListenForEvent( "daytime", function() pignpc:Remove() end , GetWorld()) 267 pignpc:AddTag("pignpcs") 268 end 269 end , GetWorld()) 270 end 271 if data and data.pignpcs then 272 inst.entity:AddSoundEmitter() 273 local shadow = inst.entity:AddDynamicShadow() 274 shadow:SetSize( 1.5, .75 ) 275 inst.AnimState:SetBank("pigman") 276 inst.AnimState:SetBuild("pig_guard_build") 277 inst.Transform:SetFourFaced() 278 inst.Transform:SetScale(1.2, 1.2, 1.2) 279 MakeCharacterPhysics(inst, 50, .5) 280 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"} 281 local hat = hats[math.random(#hats)] 282 inst.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat") 283 inst.AnimState:Show("hat") 284 inst.AnimState:PlayAnimation("idle_loop") 285 local brain = require "brains/leifbrain" 286 inst:SetBrain(brain) 287 local minimap = inst.entity:AddMiniMapEntity() 288 minimap:SetIcon( "pigking.png" ) 289 inst:AddComponent("locomotor") 290 inst.components.locomotor.walkspeed = 5 291 inst.components.locomotor.runspeed = 10 292 inst:SetStateGraph("SGpig") 293 inst:RemoveComponent("stackable") 294 inst:RemoveComponent("inventoryitem") 295 inst:RemoveComponent("healer") 296 inst:RemoveComponent("burnable") 297 inst:RemoveComponent("propagator") 298 inst:RemoveTag("cattoy") 299 inst:AddComponent("inventory") 300 inst:AddComponent("follower") 301 inst:AddComponent("knownlocations") 302 inst:AddComponent("lootdropper") 303 inst.components.lootdropper:SetLoot({"meat", "meat"}) 304 inst:AddComponent("health") 305 inst.components.health:SetMaxHealth(1000) 306 inst:AddComponent("combat") 307 inst.components.combat:SetDefaultDamage(20) 308 inst.components.combat:SetAttackPeriod(1) 309 inst.components.combat:SetRetargetFunction(3, function(inst) 310 if not inst.components.health:IsDead() then 311 return FindEntity(inst, 20, function(guy) 312 if guy.components.health and not guy.components.health:IsDead() and not guy:HasTag("npcs") then 313 return guy:HasTag("monster") or guy:HasTag("smallcreature") 314 end 315 end ) 316 end 317 end ) 318 inst.components.combat:SetKeepTargetFunction(function(inst, target) return target and target:IsValid() end ) 319 inst:ListenForEvent("attacked", function(inst, data) 320 inst.components.combat:SetTarget(data.attacker) 321 inst.components.combat:ShareTarget(data.attacker, 50, function(dude) return dude:HasTag("pignpcs") and not dude.components.health:IsDead() end, 10) 322 end ) 323 inst:DoPeriodicTask(1, function(inst) 324 if not inst.components.health:IsDead() then 325 local pos = Vector3(inst.Transform:GetWorldPosition()) 326 local ents = TheSim:FindEntities(pos.x,pos.y,pos.z, 3) 327 for k,v in pairs(ents) do 328 if v.components.pickable and v.components.pickable:CanBePicked() then 329 v.components.pickable:Pick(inst) 330 end 331 if v.components.crop then 332 v.components.crop:Harvest(inst) 333 end 334 if v:HasTag("tree") and v.components.workable and v.components.workable.workleft > 0 then 335 v.components.workable:Destroy(inst) 336 end 337 if v.components.edible and v.components.inventoryitem and not v.components.inventoryitem:IsHeld() then 338 v:Remove() 339 end 340 end 341 end 342 end ) 343 inst:ListenForEvent("death", function() 344 for k = 1,math.random(1,3) do 345 local pt2 = inst:GetPosition() 346 local gold = SpawnPrefab("goldnugget") 347 gold.Transform:SetPosition(pt2.x+(math.random(3)-math.random(3)), 0, pt2.z+(math.random(3)-math.random(3))) 348 end 349 end ) 350 inst:ListenForEvent( "daytime", function() inst:Remove() end , GetWorld()) 351 inst:AddTag("pignpcs") 352 end 353end 354 inst.OnSave = onsave 355 inst.OnLoad = onload 356 357 即可在大陆上不定期出现猪族人的营地(红色的大帐篷),他们是新进化的亚人类,不会主动攻击你(会攻击怪物),但会与你争夺大陆上的资源,他们会砍伐森林、打猎、采集一切可以采集的植物(包括你的农田)。如果你进攻他们,将遭到抵抗,如果你拆毁他们的营地,也将被视为敌人,如果你用围墙将他们圈起来,猪族人也将毫不犹豫地拆毁它。是与猪族人和平相处,任由他们的不断壮大,还是与他们为敌,不断地进行猎杀,是摆在你面前的一道选择题。猪族营地在小地图上显示为帐篷图标,猪族人显示为猪王图标。用锤子砸15下可拆毁红色帐篷,被拆毁了帐篷的猪族部落,将在第二天天亮时离去