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

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


  1
  2二四三.新移民(白天石头营火附近出现新移民,自动打怪,可与他们做买卖)
  3
  4	1.MT管理器打开游戏目录/assets/DLC0002/scripts/prefabs/firepit.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:
  5
  6local function createnpc(inst)
  7  for k = 1,math.random(10,25) do
  8	local pt = inst:GetPosition()
  9	local npc = SpawnPrefab("frog")
 10	npc.Transform:SetPosition(pt.x+(math.random(50)-math.random(50)), 0, pt.z+(math.random(50)-math.random(50)))
 11	npc.AnimState:SetBank("wilson")
 12	local names = {"wilson","wendy","wes","wickerbottom","willow","wolfgang","wx78"}
 13	local buildname = names[math.random(#names)]
 14	npc.AnimState:SetBuild(buildname)
 15	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"}
 16	local hat = hats[math.random(#hats)]
 17	npc.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat")
 18	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"}
 19	local armor = armors[math.random(#armors)]
 20	npc.AnimState:OverrideSymbol("swap_body", armor, "swap_body")
 21	npc.AnimState:Show("HAT")
 22	npc.AnimState:Show("HAT_HAIR")
 23	npc.AnimState:Hide("HAIR_NOHAT")
 24	npc.AnimState:Hide("HAIR")
 25	npc.AnimState:Hide("ARM_carry")
 26	npc.AnimState:Show("ARM_normal")
 27	npc.AnimState:PlayAnimation("idle")
 28	MakeCharacterPhysics(npc, 75, .5)
 29	local minimap = npc.entity:AddMiniMapEntity()
 30	minimap:SetIcon( "lighter.png" )
 31	npc:RemoveComponent("sleeper")
 32	npc:RemoveComponent("thief")
 33	npc:RemoveComponent("lootdropper")
 34	npc:RemoveTag("animal")
 35	npc:RemoveTag("prey")
 36	npc:RemoveTag("smallcreature")
 37	npc:RemoveTag("frog")
 38	npc:RemoveTag("canbetrapped")
 39	npc:RemoveAllEventCallbacks()
 40	npc:SetStateGraph("SGshadowwaxwell")
 41	npc.components.health:SetMaxHealth(1000)
 42	npc:RemoveComponent("combat")
 43	npc:AddComponent("combat")
 44	npc.components.combat:SetDefaultDamage(10)
 45	npc.components.combat:SetAttackPeriod(1)
 46	npc.components.combat:SetRetargetFunction(3, function(npc)
 47		if not npc.components.health:IsDead() then
 48			return FindEntity(npc, 20, function(guy)
 49				if guy.components.combat and guy.components.health and not guy.components.health:IsDead() then
 50				   return guy.components.combat.target == npc or npc.components.combat.target == guy or guy:HasTag("monster")
 51				end
 52			end )
 53		end
 54	end )
 55	npc.components.combat:SetKeepTargetFunction(function(npc, target) return target and target:IsValid() end )
 56	npc:ListenForEvent("attacked", function(npc, data)
 57		npc.components.combat:SetTarget(data.attacker)
 58		npc.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("npcs") and not dude.components.health:IsDead() end, 5)
 59	end )
 60	npc:AddComponent("trader")
 61	npc.components.trader:SetAcceptTest(function(npc, item) 
 62		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
 63		   return true
 64		end
 65		return false
 66	end )
 67	npc.components.trader.onaccept = function(npc, giver, item)
 68		for k = 1, 2 do
 69			local goldnugget = SpawnPrefab("goldnugget")
 70			GetPlayer().components.inventory:GiveItem(goldnugget)
 71		end
 72	end
 73	npc:ListenForEvent( "nighttime", function() npc:Remove() end , GetWorld())
 74	npc:AddComponent("talker")
 75	npc:DoPeriodicTask(math.random(30,60), function()
 76		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"}
 77		local word = words[math.random(#words)]
 78		npc.components.talker:Say(word, 4, false)
 79	end)
 80	npc:AddTag("npcs")
 81  end
 82end
 83	inst:ListenForEvent( "daytime", function() createnpc(inst) end , GetWorld())
 84
 85
 86	2.MT管理器打开游戏目录/assets/scripts/prefabs/frog.lua文件,在inst:AddComponent("inspectable")的下一行插入以下内容:
 87
 88local 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"}
 89local weapon = names[math.random(#names)]
 90local items = { SWORD = weapon }
 91local function EquipItem(inst, item)
 92	if item then
 93	   inst.AnimState:OverrideSymbol("swap_object", item, item)
 94	   inst.AnimState:Show("ARM_carry") 
 95	   inst.AnimState:Hide("ARM_normal")
 96	end
 97end
 98	inst.items = items
 99	inst.equipfn = EquipItem
100	EquipItem(inst)
101local function onsave(inst, data)
102	if inst:HasTag("npcs") then
103		data.npcs = true
104	end
105end
106local function onload(inst, data)
107  if data and data.npcs then
108	inst.AnimState:SetBank("wilson")
109	local names = {"wilson","wendy","wes","wickerbottom","willow","wolfgang","wx78"}
110	local buildname = names[math.random(#names)]
111	inst.AnimState:SetBuild(buildname)
112	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"}
113	local hat = hats[math.random(#hats)]
114	inst.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat")
115	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"}
116	local armor = armors[math.random(#armors)]
117	inst.AnimState:OverrideSymbol("swap_body", armor, "swap_body")
118	inst.AnimState:Show("HAT")
119	inst.AnimState:Show("HAT_HAIR")
120	inst.AnimState:Hide("HAIR_NOHAT")
121	inst.AnimState:Hide("HAIR")
122	inst.AnimState:Hide("ARM_carry")
123	inst.AnimState:Show("ARM_normal")
124	inst.AnimState:PlayAnimation("idle")
125	MakeCharacterPhysics(inst, 75, .5)
126	local minimap = inst.entity:AddMiniMapEntity()
127	minimap:SetIcon( "lighter.png" )
128	inst:RemoveComponent("sleeper")
129	inst:RemoveComponent("thief")
130	inst:RemoveComponent("lootdropper")
131	inst:RemoveTag("animal")
132	inst:RemoveTag("prey")
133	inst:RemoveTag("smallcreature")
134	inst:RemoveTag("frog")
135	inst:RemoveTag("canbetrapped")
136	inst:RemoveAllEventCallbacks()
137	inst:SetStateGraph("SGshadowwaxwell")
138	inst.components.health:SetMaxHealth(1000)
139	inst:RemoveComponent("combat")
140	inst:AddComponent("combat")
141	inst.components.combat:SetDefaultDamage(10)
142	inst.components.combat:SetAttackPeriod(1)
143	inst.components.combat:SetRetargetFunction(3, function(inst)
144		if not inst.components.health:IsDead() then
145			return FindEntity(inst, 20, function(guy)
146				if guy.components.combat and guy.components.health and not guy.components.health:IsDead() then
147				   return guy.components.combat.target == inst or inst.components.combat.target == guy or guy:HasTag("monster")
148				end
149			end )
150		end
151	end )
152	inst.components.combat:SetKeepTargetFunction(function(inst, target) return target and target:IsValid() end )
153	inst:ListenForEvent("attacked", function(inst, data)
154		inst.components.combat:SetTarget(data.attacker)
155		inst.components.combat:ShareTarget(data.attacker, 30, function(dude) return dude:HasTag("npcs") and not dude.components.health:IsDead() end, 5)
156	end )
157	inst:AddComponent("trader")
158	inst.components.trader:SetAcceptTest(function(inst, item) 
159		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
160		   return true
161		end
162		return false
163	end )
164	inst.components.trader.onaccept = function(inst, giver, item)
165		for k = 1, 2 do
166			local goldnugget = SpawnPrefab("goldnugget")
167			GetPlayer().components.inventory:GiveItem(goldnugget)
168		end
169	end
170	inst:ListenForEvent( "nighttime", function() inst:Remove() end , GetWorld())
171	inst:AddComponent("talker")
172	inst:DoPeriodicTask(math.random(30,60), function()
173		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"}
174		local word = words[math.random(#words)]
175		inst.components.talker:Say(word, 4, false)
176	end)
177	inst:AddTag("npcs")
178  end
179end
180	inst.OnSave = onsave
181	inst.OnLoad = onload
182
183	即可在白天时,石头营火附近有新移民走动,黑夜离去,会主动攻击怪物,有他们的地方是安全的。他们愿意购买你的肉类、蔬菜和宝石(拿着物品对新移民点鼠标左键),每个物品可以卖2个黄金。如果你攻击新移民,附近的其他人将围殴你。新移民会自言自语,听听他们都说了些什么吧。在地图各处建造石头营火及其他建筑,就会形成许多小镇,饥荒世界从此不再冷清。新移民在小地图上显示为打火机图标(代表着希望),通过查看小地图,可以了解各个小镇的人口情况