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

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



二四三.新移民(白天石头营火附近出现新移民,自动打怪,可与他们做买卖)

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

local function createnpc(inst)
  for k = 1,math.random(10,25) do
	local pt = inst:GetPosition()
	local npc = SpawnPrefab("frog")
	npc.Transform:SetPosition(pt.x+(math.random(50)-math.random(50)), 0, pt.z+(math.random(50)-math.random(50)))
	npc.AnimState:SetBank("wilson")
	local names = {"wilson","wendy","wes","wickerbottom","willow","wolfgang","wx78"}
	local buildname = names[math.random(#names)]
	npc.AnimState:SetBuild(buildname)
	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","hat_rain","hat_watermelon","hat_ice","hat_catcoon","hat_wathgrithr"}
	local hat = hats[math.random(#hats)]
	npc.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat")
	local armors = {"armor_grass","armor_marble","armor_onemanband","armor_ruins","armor_sanity","armor_slurper","armor_slurtleshell","armor_sweatervest","armor_trunkvest_summer","armor_trunkvest_winter","armor_wood","torso_rain"}
	local armor = armors[math.random(#armors)]
	npc.AnimState:OverrideSymbol("swap_body", armor, "swap_body")
	npc.AnimState:Show("HAT")
	npc.AnimState:Show("HAT_HAIR")
	npc.AnimState:Hide("HAIR_NOHAT")
	npc.AnimState:Hide("HAIR")
	npc.AnimState:Hide("ARM_carry")
	npc.AnimState:Show("ARM_normal")
	npc.AnimState:PlayAnimation("idle")
	MakeCharacterPhysics(npc, 75, .5)
	local minimap = npc.entity:AddMiniMapEntity()
	minimap:SetIcon( "lighter.png" )
	npc:RemoveComponent("sleeper")
	npc:RemoveComponent("thief")
	npc:RemoveComponent("lootdropper")
	npc:RemoveTag("animal")
	npc:RemoveTag("prey")
	npc:RemoveTag("smallcreature")
	npc:RemoveTag("frog")
	npc:RemoveTag("canbetrapped")
	npc:RemoveAllEventCallbacks()
	npc:SetStateGraph("SGshadowwaxwell")
	npc.components.health:SetMaxHealth(1000)
	npc:RemoveComponent("combat")
	npc:AddComponent("combat")
	npc.components.combat:SetDefaultDamage(10)
	npc.components.combat:SetAttackPeriod(1)
	npc.components.combat:SetRetargetFunction(3, function(npc)
		if not npc.components.health:IsDead() then
			return FindEntity(npc, 20, function(guy)
				if guy.components.combat and guy.components.health and not guy.components.health:IsDead() then
				   return guy.components.combat.target == npc or npc.components.combat.target == guy or guy:HasTag("monster")
				end
			end )
		end
	end )
	npc.components.combat:SetKeepTargetFunction(function(npc, target) return target and target:IsValid() end )
	npc:ListenForEvent("attacked", function(npc, data)
		npc.components.combat:SetTarget(data.attacker)
		npc.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("npcs") and not dude.components.health:IsDead() end, 5)
	end )
	npc:AddComponent("trader")
	npc.components.trader:SetAcceptTest(function(npc, item) 
		if item.prefab == "cave_banana" or item.prefab == "carrot" or item.prefab == "corn" or item.prefab == "pumpkin" or item.prefab == "eggplant" or item.prefab == "durian" or item.prefab == "pomegranate" or item.prefab == "dragonfruit" or item.prefab == "berries" or item.prefab == "cactus_meat" or item.prefab == "watermelon" or item.prefab == "acorn" or item.prefab == "meat" or item.prefab == "smallmeat" or item.prefab == "fish" or item.prefab == "eel" or item.prefab == "drumstick" or item.prefab == "bird_egg" or item.prefab == "froglegs" or item.prefab == "monstermeat" or item.prefab == "butter" or item.prefab == "butterflywings" or item.prefab == "cutlichen" or item.prefab == "foliage" or item.prefab == "honey" or item.prefab == "lightbulb" or item.prefab == "red_cap" or item.prefab == "green_cap" or item.prefab == "blue_cap" or item.prefab == "petals" or item.prefab == "petals_evil" or item.prefab == "redgem" or item.prefab == "bluegem" or item.prefab == "purplegem" or item.prefab == "greengem" or item.prefab == "orangegem" or item.prefab == "yellowgem" then
		   return true
		end
		return false
	end )
	npc.components.trader.onaccept = function(npc, giver, item)
		for k = 1, 2 do
			local goldnugget = SpawnPrefab("goldnugget")
			GetPlayer().components.inventory:GiveItem(goldnugget)
		end
	end
	npc:ListenForEvent( "nighttime", function() npc:Remove() end , GetWorld())
	npc:AddComponent("talker")
	npc:DoPeriodicTask(math.random(30,60), function()
		local words = {"Good day","I'm happy","I like here","Thank you","Where's the enemy","I'm hungry","Too hot","Too cold","Hello","Bye","Sorry","Good morning","Good afternoon","Good evening","I'll go home","I want to go hunting","I'm going to mining","I want to go to the farm","I want to pick","I want to go fishing","I'm going to catch","I want to fight","I want to have a rest","I was a good man","I want to be rich","I was lucky","I want to make friends"}
		local word = words[math.random(#words)]
		npc.components.talker:Say(word, 4, false)
	end)
	npc:AddTag("npcs")
  end
end
	inst:ListenForEvent( "daytime", function() createnpc(inst) end , GetWorld())


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

local names = {"swap_axe","swap_batbat","swap_cane","swap_diviningrod","swap_ruins_bat","swap_fishingrod","swap_goldenaxe","swap_goldenpickaxe","swap_goldenshovel","swap_ham_bat","swap_hammer","swap_lucy_axe","swap_nightmaresword","swap_pickaxe","swap_pitchfork","swap_ruins_bat","swap_shovel","swap_spear","swap_spike","swap_umbrella","swap_spear_wathgrithr","swap_parasol"}
local weapon = names[math.random(#names)]
local items = { SWORD = weapon }
local function EquipItem(inst, item)
	if item then
	   inst.AnimState:OverrideSymbol("swap_object", item, item)
	   inst.AnimState:Show("ARM_carry") 
	   inst.AnimState:Hide("ARM_normal")
	end
end
	inst.items = items
	inst.equipfn = EquipItem
	EquipItem(inst)
local function onsave(inst, data)
	if inst:HasTag("npcs") then
		data.npcs = true
	end
end
local function onload(inst, data)
  if data and data.npcs then
	inst.AnimState:SetBank("wilson")
	local names = {"wilson","wendy","wes","wickerbottom","willow","wolfgang","wx78"}
	local buildname = names[math.random(#names)]
	inst.AnimState:SetBuild(buildname)
	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","hat_rain","hat_watermelon","hat_ice","hat_catcoon","hat_wathgrithr"}
	local hat = hats[math.random(#hats)]
	inst.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat")
	local armors = {"armor_grass","armor_marble","armor_onemanband","armor_ruins","armor_sanity","armor_slurper","armor_slurtleshell","armor_sweatervest","armor_trunkvest_summer","armor_trunkvest_winter","armor_wood","torso_rain"}
	local armor = armors[math.random(#armors)]
	inst.AnimState:OverrideSymbol("swap_body", armor, "swap_body")
	inst.AnimState:Show("HAT")
	inst.AnimState:Show("HAT_HAIR")
	inst.AnimState:Hide("HAIR_NOHAT")
	inst.AnimState:Hide("HAIR")
	inst.AnimState:Hide("ARM_carry")
	inst.AnimState:Show("ARM_normal")
	inst.AnimState:PlayAnimation("idle")
	MakeCharacterPhysics(inst, 75, .5)
	local minimap = inst.entity:AddMiniMapEntity()
	minimap:SetIcon( "lighter.png" )
	inst:RemoveComponent("sleeper")
	inst:RemoveComponent("thief")
	inst:RemoveComponent("lootdropper")
	inst:RemoveTag("animal")
	inst:RemoveTag("prey")
	inst:RemoveTag("smallcreature")
	inst:RemoveTag("frog")
	inst:RemoveTag("canbetrapped")
	inst:RemoveAllEventCallbacks()
	inst:SetStateGraph("SGshadowwaxwell")
	inst.components.health:SetMaxHealth(1000)
	inst:RemoveComponent("combat")
	inst:AddComponent("combat")
	inst.components.combat:SetDefaultDamage(10)
	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.combat and guy.components.health and not guy.components.health:IsDead() then
				   return guy.components.combat.target == inst or inst.components.combat.target == guy or guy:HasTag("monster")
				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, 30, function(dude) return dude:HasTag("npcs") and not dude.components.health:IsDead() end, 5)
	end )
	inst:AddComponent("trader")
	inst.components.trader:SetAcceptTest(function(inst, item) 
		if item.prefab == "cave_banana" or item.prefab == "carrot" or item.prefab == "corn" or item.prefab == "pumpkin" or item.prefab == "eggplant" or item.prefab == "durian" or item.prefab == "pomegranate" or item.prefab == "dragonfruit" or item.prefab == "berries" or item.prefab == "cactus_meat" or item.prefab == "watermelon" or item.prefab == "acorn" or item.prefab == "meat" or item.prefab == "smallmeat" or item.prefab == "fish" or item.prefab == "eel" or item.prefab == "drumstick" or item.prefab == "bird_egg" or item.prefab == "froglegs" or item.prefab == "monstermeat" or item.prefab == "butter" or item.prefab == "butterflywings" or item.prefab == "cutlichen" or item.prefab == "foliage" or item.prefab == "honey" or item.prefab == "lightbulb" or item.prefab == "red_cap" or item.prefab == "green_cap" or item.prefab == "blue_cap" or item.prefab == "petals" or item.prefab == "petals_evil" or item.prefab == "redgem" or item.prefab == "bluegem" or item.prefab == "purplegem" or item.prefab == "greengem" or item.prefab == "orangegem" or item.prefab == "yellowgem" then
		   return true
		end
		return false
	end )
	inst.components.trader.onaccept = function(inst, giver, item)
		for k = 1, 2 do
			local goldnugget = SpawnPrefab("goldnugget")
			GetPlayer().components.inventory:GiveItem(goldnugget)
		end
	end
	inst:ListenForEvent( "nighttime", function() inst:Remove() end , GetWorld())
	inst:AddComponent("talker")
	inst:DoPeriodicTask(math.random(30,60), function()
		local words = {"Good day","I'm happy","I like here","Thank you","Where's the enemy","I'm hungry","Too hot","Too cold","Hello","Bye","Sorry","Good morning","Good afternoon","Good evening","I'll go home","I want to go hunting","I'm going to mining","I want to go to the farm","I want to pick","I want to go fishing","I'm going to catch","I want to fight","I want to have a rest","I was a good man","I want to be rich","I was lucky","I want to make friends"}
		local word = words[math.random(#words)]
		inst.components.talker:Say(word, 4, false)
	end)
	inst:AddTag("npcs")
  end
end
	inst.OnSave = onsave
	inst.OnLoad = onload

	即可在白天时,石头营火附近有新移民走动,黑夜离去,会主动攻击怪物,有他们的地方是安全的。他们愿意购买你的肉类、蔬菜和宝石(拿着物品对新移民点鼠标左键),每个物品可以卖2个黄金。如果你攻击新移民,附近的其他人将围殴你。新移民会自言自语,听听他们都说了些什么吧。在地图各处建造石头营火及其他建筑,就会形成许多小镇,饥荒世界从此不再冷清。新移民在小地图上显示为打火机图标(代表着希望),通过查看小地图,可以了解各个小镇的人口情况