trader.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. local S = mobs_npc.S
  2. -- define table containing names for use and shop items for sale
  3. mobs.human = {
  4. names = {
  5. "Bob", "Duncan", "Bill", "Tom", "James", "Ian", "Lenny",
  6. "Dylan", "Ethan"
  7. },
  8. items = {
  9. --{item for sale, price, chance of appearing in trader's inventory}
  10. {"default:apple 10", "default:gold_ingot 2", 10},
  11. {"farming:bread 10", "default:gold_ingot 4", 5},
  12. {"default:clay 10", "default:gold_ingot 2", 12},
  13. {"default:brick 10", "default:gold_ingot 4", 17},
  14. {"default:glass 10", "default:gold_ingot 4", 17},
  15. {"default:obsidian 10", "default:gold_ingot 15", 50},
  16. {"default:diamond 1", "default:gold_ingot 5", 40},
  17. {"farming:wheat 10", "default:gold_ingot 2", 17},
  18. {"default:tree 5", "default:gold_ingot 4", 20},
  19. {"default:stone 10", "default:gold_ingot 8", 17},
  20. {"default:desert_stone 10", "default:gold_ingot 8", 27},
  21. {"default:sapling 1", "default:gold_ingot 1", 7},
  22. {"default:pick_steel 1", "default:gold_ingot 2", 7},
  23. {"default:sword_steel 1", "default:gold_ingot 2", 17},
  24. {"default:shovel_steel 1", "default:gold_ingot 1", 17},
  25. {"default:cactus 2", "default:gold_ingot 2", 40},
  26. {"default:papyrus 2", "default:gold_ingot 2", 40},
  27. {"default:mese_crystal_fragment 1", "default:dirt_with_grass 10", 90},
  28. {"default:mese_crystal_fragment 1", "default:gold_ingot 5", 90}
  29. }
  30. }
  31. -- Trader (same as NPC but with right-click shop)
  32. mobs:register_mob("mobs_npc:trader", {
  33. type = "npc",
  34. passive = false,
  35. damage = 3,
  36. attack_type = "dogfight",
  37. attacks_monsters = true,
  38. attack_animals = false,
  39. attack_npcs = false,
  40. pathfinding = false,
  41. hp_min = 10,
  42. hp_max = 20,
  43. armor = 100,
  44. collisionbox = {-0.35,-1.0,-0.35, 0.35,0.8,0.35},
  45. visual = "mesh",
  46. mesh = "mobs_character.b3d",
  47. textures = {
  48. {"mobs_trader.png"}, -- by Frerin
  49. {"mobs_trader2.png"},
  50. {"mobs_trader3.png"},
  51. {"mobs_trader4.png"} -- female by Astrobe
  52. },
  53. makes_footstep_sound = true,
  54. sounds = {},
  55. walk_velocity = 2,
  56. run_velocity = 3,
  57. jump = false,
  58. drops = {},
  59. water_damage = 0,
  60. lava_damage = 4,
  61. light_damage = 0,
  62. follow = {"default:diamond"},
  63. view_range = 15,
  64. owner = "",
  65. order = "stand",
  66. fear_height = 3,
  67. animation = {
  68. speed_normal = 30,
  69. speed_run = 30,
  70. stand_start = 0,
  71. stand_end = 79,
  72. walk_start = 168,
  73. walk_end = 187,
  74. run_start = 168,
  75. run_end = 187,
  76. punch_start = 200,
  77. punch_end = 219
  78. },
  79. -- stop attacking on right-click and open shop
  80. on_rightclick = function(self, clicker)
  81. self.attack = nil
  82. mobs_npc.shop_trade(self, clicker, mobs.human)
  83. end,
  84. -- show that npc is a trader once spawned
  85. on_spawn = function(self)
  86. self.nametag = S("Trader")
  87. self.object:set_properties({
  88. nametag = self.nametag,
  89. nametag_color = "#FFFFFF"
  90. })
  91. return true -- return true so on_spawn is run once only
  92. end
  93. })
  94. -- add spawn egg
  95. mobs:register_egg("mobs_npc:trader", S("Trader"), "default_sandstone.png", 1)
  96. -- this is only required for servers that previously used the old mobs mod
  97. mobs:alias_mob("mobs:trader", "mobs_npc:trader")
  98. local trader_lists = {}
  99. -- global function to add to list
  100. mobs_npc.add_trader_list = function(def)
  101. table.insert(trader_lists, def)
  102. end
  103. mobs_npc.add_trader_list({
  104. block = "default:tinblock",
  105. nametag = "Castro",
  106. textures = {"mobs_trader2.png"},
  107. item_list = {
  108. {"default:gold_lump 2", "default:gold_ingot 3"},
  109. {"default:iron_lump 2", "default:steel_ingot 2"},
  110. {"default:copper_lump 2", "default:copper_ingot 3"},
  111. {"default:tin_lump 2", "default:tin_ingot 3"}
  112. }
  113. })
  114. -- helper function
  115. local function place_trader(pos, node)
  116. local face = node.param2
  117. local pos2, def
  118. -- find which way block is facing
  119. if face == 0 then
  120. pos2 = {x = pos.x, y = pos.y, z = pos.z - 1}
  121. elseif face == 1 then
  122. pos2 = {x = pos.x - 1, y = pos.y, z = pos.z}
  123. elseif face == 2 then
  124. pos2 = {x = pos.x, y = pos.y, z = pos.z + 1}
  125. elseif face == 3 then
  126. pos2 = {x = pos.x + 1, y = pos.y, z = pos.z}
  127. else
  128. return
  129. end
  130. -- do we already have a trader spawned?
  131. local objs = minetest.get_objects_inside_radius(pos2, 1)
  132. if objs and #objs > 0 then
  133. return
  134. end
  135. -- get block below
  136. local bnode = minetest.get_node({x = pos2.x, y = pos2.y - 1, z = pos2.z})
  137. pos2.y = pos2.y + 0.5
  138. -- add new trader
  139. local obj = minetest.add_entity(pos2, "mobs_npc:trader")
  140. local ent = obj and obj:get_luaentity()
  141. if not ent then return end -- nil check
  142. for n = 1, #trader_lists do
  143. def = trader_lists[n]
  144. if bnode.name == def.block then
  145. ent.trades = def.item_list
  146. ent.nametag = def.nametag
  147. ent.game_name = def.nametag
  148. ent.base_texture = def.textures
  149. ent.textures = def.textures
  150. obj:set_properties({
  151. textures = ent.textures
  152. })
  153. break
  154. end
  155. end
  156. -- pop sound
  157. minetest.sound_play("default_place_node_hard", {
  158. pos = pos, gain = 1.0, max_hear_distance = 5, pitch = 2.0})
  159. end
  160. -- trader block (punch to spawn trader)
  161. minetest.register_node(":mobs:trader_block", {
  162. description = S("Place this and punch to spawn Trader"),
  163. groups = {cracky = 3},
  164. paramtype = "light",
  165. paramtype2 = "facedir",
  166. tiles = {
  167. "default_stone.png", "default_stone.png", "default_stone.png",
  168. "default_stone.png", "default_stone.png", "default_stone.png^mobs_npc_shop_icon.png"
  169. },
  170. -- punch block to spawn trader
  171. on_punch = function(pos, node, puncher, pointed_thing)
  172. place_trader(pos, node)
  173. end,
  174. on_rotate = screwdriver.rotate_simple,
  175. on_blast = function() end
  176. })
  177. -- trader block recipe
  178. minetest.register_craft({
  179. output = "mobs:trader_block",
  180. recipe = {
  181. {"group:stone", "group:stone", "group:stone"},
  182. {"group:stone", "default:diamondblock", "group:stone"},
  183. {"group:stone", "default:tinblock", "group:stone"}
  184. }
  185. })