Fix undeclared global fail in villager.lua
[MineClone/MineClone2/MineClone2-Fixes.git] / mods / ENTITIES / mobs_mc / villager.lua
blob8709ecdb28440dcbae9037822a379fae603fad13
1 --MCmobs v0.4
2 --maikerumine
3 --made for MC like Survival game
4 --License for code WTFPL and otherwise stated in readmes
6 --###################
7 --################### VILLAGER
8 --###################
9 -- Summary: Villagers are complex NPCs, their main feature allows players to trade with them.
11 -- TODO: Particles
12 -- TODO: 4s Regeneration I after trade unlock
13 -- TODO: Breeding
14 -- TODO: Baby villagers
15 -- TODO: Spawning in villages
16 -- TODO: Behaviour:
17 -- TODO: Walk around village, but do not leave it intentionally
18 -- TODO: Run into house on rain or danger, open doors
19 -- TODO: Internal inventory, pick up items, trade with other villagers
20 -- TODO: Farm stuff
22 -- intllib
23 local MP = minetest.get_modpath(minetest.get_current_modname())
24 local S, NS = dofile(MP.."/intllib.lua")
26 -- playername-indexed table containing the previously used tradenum
27 local player_tradenum = {}
28 -- playername-indexed table containing the objectref of trader, if trading formspec is open
29 local player_trading_with = {}
31 local DEFAULT_WALK_CHANCE = 33 -- chance to walk in percent, if no player nearby
32 local PLAYER_SCAN_INTERVAL = 5 -- every X seconds, villager looks for players nearby
33 local PLAYER_SCAN_RADIUS = 4 -- scan radius for looking for nearby players
35 --[=======[ TRADING ]=======]
37 -- LIST OF VILLAGER PROFESSIONS AND TRADES
39 -- TECHNICAL RESTRICTIONS (FIXME):
40 -- * You can't use a clock as requested item
41 -- * You can't use a compass as requested item if its stack size > 1
42 -- * You can't use a compass in the second requested slot
43 -- This is a problem in the mcl_compass and mcl_clock mods,
44 -- these items should be implemented as single items, then everything
45 -- will be much easier.
47 local COMPASS = "mcl_compass:compass"
48 if minetest.registered_aliases[COMPASS] then
49 COMPASS = minetest.registered_aliases[COMPASS]
50 end
52 local E1 = { "mcl_core:emerald", 1, 1 } -- one emerald
54 -- Special trades for v6 only
55 local TRADE_V6_RED_SANDSTONE, TRADE_V6_DARK_OAK_SAPLING, TRADE_V6_ACACIA_SAPLING, TRADE_V6_BIRCH_SAPLING
56 if minetest.get_mapgen_setting("mg_name") == "v6" then
57 TRADE_V6_RED_SANDSTONE = { E1, { "mcl_core:redsandstone", 12, 16 } }
58 TRADE_V6_DARK_OAK_SAPLING = { { "mcl_core:emerald", 6, 9 }, { "mcl_core:darksapling", 1, 1 } }
59 TRADE_V6_ACACIA_SAPLING = { { "mcl_core:emerald", 14, 17 }, { "mcl_core:acaciasapling", 1, 1 } }
60 TRADE_V6_BIRCH_SAPLING = { { "mcl_core:emerald", 8, 11 }, { "mcl_core:birchsapling", 1, 1 } }
61 end
63 local professions = {
64 farmer = {
65 name = "Farmer",
66 texture = "mobs_mc_villager_farmer.png",
67 trades = {
69 { { "mcl_farming:wheat_item", 18, 22, }, E1 },
70 { { "mcl_farming:potato_item", 15, 19, }, E1 },
71 { { "mcl_farming:carrot_item", 15, 19, }, E1 },
72 { E1, { "mcl_farming:bread", 2, 4 } },
76 { { "mcl_farming:pumpkin_face", 8, 13 }, E1 },
77 { E1, { "mcl_farming:pumpkin_pie", 2, 3} },
81 { { "mcl_farming:melon", 7, 12 }, E1 },
82 { E1, { "mcl_core:apple", 5, 7 }, },
86 { E1, { "mcl_farming:cookie", 6, 10 } },
87 { E1, { "mcl_cake:cake", 1, 1 } },
88 TRADE_V6_BIRCH_SAPLING,
89 TRADE_V6_DARK_OAK_SAPLING,
90 TRADE_V6_ACACIA_SAPLING,
94 fisherman = {
95 name = "Fisherman",
96 texture = "mobs_mc_villager_farmer.png",
97 trades = {
99 { { "mcl_fishing:fish_raw", 6, 6, "mcl_core:emerald", 1, 1 }, { "mcl_fishing:fish_cooked", 6, 6 } },
100 { { "mcl_mobitems:string", 15, 20 }, E1 },
101 { { "mcl_core:coal_lump", 16, 24 }, E1 },
103 -- TODO: enchanted fishing rod
106 fletcher = {
107 name = "Fletcher",
108 texture = "mobs_mc_villager_farmer.png",
109 trades = {
111 { { "mcl_mobitems:string", 15, 20 }, E1 },
112 { E1, { "mcl_bows:arrow", 8, 12 } },
116 { { "mcl_core:gravel", 10, 10, "mcl_core:emerald", 1, 1 }, { "mcl_core:flint", 6, 10 } },
117 { { "mcl_core:emerald", 2, 3 }, { "mcl_bows:bow", 1, 1 } },
121 shepherd ={
122 name = "Shepherd",
123 texture = "mobs_mc_villager_farmer.png",
124 trades = {
126 { { "mcl_wool:white", 16, 22 }, E1 },
127 { { "mcl_core:emerald", 3, 4 }, { "mcl_tools:shears", 1, 1 } },
131 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:white", 1, 1 } },
132 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:grey", 1, 1 } },
133 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:silver", 1, 1 } },
134 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:black", 1, 1 } },
135 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:yellow", 1, 1 } },
136 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:orange", 1, 1 } },
137 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:red", 1, 1 } },
138 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:magenta", 1, 1 } },
139 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:purple", 1, 1 } },
140 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:blue", 1, 1 } },
141 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:cyan", 1, 1 } },
142 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:lime", 1, 1 } },
143 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:green", 1, 1 } },
144 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:pink", 1, 1 } },
145 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:light_blue", 1, 1 } },
146 { { "mcl_core:emerald", 1, 2 }, { "mcl_wool:brown", 1, 1 } },
150 librarian = {
151 name = "Librarian",
152 texture = "mobs_mc_villager_librarian.png",
153 trades = {
155 { { "mcl_core:paper", 24, 36 }, E1 },
156 -- TODO: enchanted book
157 { { "mcl_books:book", 8, 10 }, E1 },
158 { { "mcl_core:emerald", 10, 12 }, { "mcl_compass:compass", 1 ,1 }},
159 { { "mcl_core:emerald", 3, 4 }, { "mcl_books:bookshelf", 1 ,1 }},
163 { { "mcl_books:written_book", 2, 2 }, E1 },
164 { { "mcl_core:emerald", 10, 12 }, { "mcl_clock:clock", 1, 1 } },
165 { E1, { "mcl_core:glass", 3, 5 } },
169 { E1, { "mcl_core:glass", 3, 5 } },
172 -- TODO: 2 enchanted book tiers
175 { { "mcl_core:emerald", 20, 22 }, { "mcl_mobs:nametag", 1, 1 } },
179 cartographer = {
180 name = "Cartographer",
181 texture = "mobs_mc_villager_librarian.png",
182 trades = {
184 { { "mcl_core:paper", 24, 36 }, E1 },
188 -- subject to special checks
189 { { "mcl_compass:compass", 1, 1 }, E1 },
193 -- TODO: replace with empty map
194 { { "mcl_core:emerald", 7, 11}, { "mcl_maps:filled_map", 1, 1 } },
197 -- TODO: special maps
200 armorer = {
201 name = "Armorer",
202 texture = "mobs_mc_villager_smith.png",
203 trades = {
205 { { "mcl_core:coal_lump", 16, 24 }, E1 },
206 { { "mcl_core:emerald", 4, 6 }, { "3d_armor:helmet_iron", 1, 1 } },
210 { { "mcl_core:iron_ingot", 7, 9 }, E1 },
211 { { "mcl_core:emerald", 10, 14 }, { "3d_armor:chestplate_iron", 1, 1 } },
215 { { "mcl_core:diamond", 3, 4 }, E1 },
216 -- TODO: enchant
217 { { "mcl_core:emerald", 16, 19 }, { "3d_armor:chestplate_diamond", 1, 1 } },
221 { { "mcl_core:emerald", 5, 7 }, { "3d_armor:boots_chain", 1, 1 } },
222 { { "mcl_core:emerald", 9, 11 }, { "3d_armor:leggings_chain", 1, 1 } },
223 { { "mcl_core:emerald", 5, 7 }, { "3d_armor:helmet_chain", 1, 1 } },
224 { { "mcl_core:emerald", 11, 15 }, { "3d_armor:chestplate_chain", 1, 1 } },
228 leatherworker = {
229 name = "Leatherworker",
230 texture = "mobs_mc_villager_butcher.png",
231 trades = {
233 { { "mcl_mobitems:leather", 9, 12 }, E1 },
234 { { "mcl_core:emerald", 2, 4 }, { "3d_armor:leggings_leather", 2, 4 } },
238 -- TODO: enchant
239 { { "mcl_core:emerald", 7, 12 }, { "3d_armor:chestplate_leather", 1, 1 } },
243 { { "mcl_core:emerald", 8, 10 }, { "mcl_mobitems:saddle", 1, 1 } },
247 butcher = {
248 name = "Butcher",
249 texture = "mobs_mc_villager_butcher.png",
250 trades = {
252 { { "mcl_mobitems:beef", 14, 18 }, E1 },
253 { { "mcl_mobitems:chicken", 14, 18 }, E1 },
257 { { "mcl_core:coal_lump", 16, 24 }, E1 },
258 { E1, { "mcl_mobitems:cooked_beef", 5, 7 } },
259 { E1, { "mcl_mobitems:cooked_chicken", 6, 8 } },
263 weapon_smith = {
264 name = "Weapon Smith",
265 texture = "mobs_mc_villager_smith.png",
266 trades = {
268 { { "mcl_core:coal_lump", 16, 24 }, E1 },
269 { { "mcl_core:emerald", 6, 8 }, { "mcl_tools:axe_iron", 1, 1 } },
273 { { "mcl_core:iron_ingot", 7, 9 }, E1 },
274 -- TODO: enchant
275 { { "mcl_core:emerald", 9, 10 }, { "mcl_tools:sword_iron", 1, 1 } },
279 { { "mcl_core:diamond", 3, 4 }, E1 },
280 -- TODO: enchant
281 { { "mcl_core:emerald", 12, 15 }, { "mcl_tools:sword_diamond", 1, 1 } },
282 -- TODO: enchant
283 { { "mcl_core:emerald", 9, 12 }, { "mcl_tools:axe_diamond", 1, 1 } },
287 tool_smith = {
288 name = "Tool Smith",
289 texture = "mobs_mc_villager_smith.png",
290 trades = {
292 { { "mcl_core:coal_lump", 16, 24 }, E1 },
293 -- TODO: enchant
294 { { "mcl_core:emerald", 5, 7 }, { "mcl_tools:shovel_iron", 1, 1 } },
298 { { "mcl_core:iron_ingot", 7, 9 }, E1 },
299 -- TODO: enchant
300 { { "mcl_core:emerald", 9, 11 }, { "mcl_tools:pick_iron", 1, 1 } },
304 { { "mcl_core:diamond", 3, 4 }, E1 },
305 -- TODO: enchant
306 { { "mcl_core:emerald", 12, 15 }, { "mcl_tools:pick_diamond", 1, 1 } },
310 cleric = {
311 name = "Cleric",
312 texture = "mobs_mc_villager_priest.png",
313 trades = {
315 { { "mcl_mobitems:rotten_flesh", 36, 40 }, E1 },
316 { { "mcl_core:gold_ingot", 8, 10 }, E1 },
320 { E1, { "mesecons:redstone", 1, 4 } },
321 { E1, { "mcl_dye:blue", 1, 2 } },
325 TRADE_V6_RED_SANDSTONE,
326 { E1, { "mcl_nether:glowstone", 1, 3 } },
327 { { "mcl_core:emerald", 4, 7 }, { "mcl_throwing:ender_pearl", 1, 1 } },
330 -- TODO: Bottle 'o enchanting
333 nitwit = {
334 name = "Nitwit",
335 texture = "mobs_mc_villager.png",
336 -- No trades for nitwit
337 trades = nil,
341 local profession_names = {}
342 for id, _ in pairs(professions) do
343 table.insert(profession_names, id)
346 local stand_still = function(self)
347 self.walk_chance = 0
348 self.jump = false
351 local update_max_tradenum = function(self)
352 if not self._trades then
353 return
355 local trades = minetest.deserialize(self._trades)
356 for t=1, #trades do
357 local trade = trades[t]
358 if trade.tier > self._max_trade_tier then
359 self._max_tradenum = t - 1
360 return
363 self._max_tradenum = #trades
366 local init_trader_vars = function(self)
367 if not self._profession then
368 -- Select random profession from all professions with matching clothing
369 local texture = self.base_texture[1]
370 local matches = {}
371 for prof_id, prof in pairs(professions) do
372 if texture == prof.texture then
373 table.insert(matches, prof_id)
376 local p = math.random(1, #matches)
377 self._profession = matches[p]
379 if not self._max_trade_tier then
380 self._max_trade_tier = 1
382 if not self._locked_trades then
383 self._locked_trades = 0
385 if not self._trading_players then
386 self._trading_players = {}
390 local init_trades = function(self, inv)
391 local profession = professions[self._profession]
392 local trade_tiers = profession.trades
393 if trade_tiers == nil then
394 -- Empty trades
395 self._trades = false
396 return
399 local max_tier = #trade_tiers
400 local trades = {}
401 for tiernum=1, max_tier do
402 local tier = trade_tiers[tiernum]
403 for tradenum=1, #tier do
404 local trade = tier[tradenum]
405 local wanted1_item = trade[1][1]
406 local wanted1_count = math.random(trade[1][2], trade[1][3])
407 local offered_item = trade[2][1]
408 local offered_count = math.random(trade[2][2], trade[2][3])
410 local wanted = { wanted1_item .. " " ..wanted1_count }
411 if trade[1][4] then
412 local wanted2_item = trade[1][4]
413 local wanted2_count = math.random(trade[1][5], trade[1][6])
414 table.insert(wanted, wanted2_item .. " " ..wanted2_count)
417 table.insert(trades, {
418 wanted = wanted,
419 offered = offered_item .. " " .. offered_count,
420 tier = tiernum, -- tier of this trade
421 traded_once = false, -- true if trade was traded at least once
422 trade_counter = 0, -- how often the this trade was mate after the last time it got unlocked
423 locked = false, -- if this trade is locked. Locked trades can't be used
427 self._trades = minetest.serialize(trades)
430 local set_trade = function(trader, player, inv, concrete_tradenum)
431 local trades = minetest.deserialize(trader._trades)
432 if not trades then
433 init_trades(trader)
434 trades = minetest.deserialize(trader._trades)
435 if not trades then
436 minetest.log("error", "[mobs_mc] Failed to select villager trade!")
437 return
440 local name = player:get_player_name()
442 -- Stop tradenum from advancing into locked tiers or out-of-range areas
443 if concrete_tradenum > trader._max_tradenum then
444 concrete_tradenum = trader._max_tradenum
445 elseif concrete_tradenum < 1 then
446 concrete_tradenum = 1
448 player_tradenum[name] = concrete_tradenum
449 local trade = trades[concrete_tradenum]
450 inv:set_stack("wanted", 1, ItemStack(trade.wanted[1]))
451 inv:set_stack("offered", 1, ItemStack(trade.offered))
452 if trade.wanted[2] then
453 local wanted2 = ItemStack(trade.wanted[2])
454 inv:set_stack("wanted", 2, wanted2)
455 else
456 inv:set_stack("wanted", 2, "")
461 local function show_trade_formspec(playername, trader, tradenum)
462 if not trader._trades then
463 return
465 if not tradenum then
466 tradenum = 1
468 local trades = minetest.deserialize(trader._trades)
469 local trade = trades[tradenum]
470 local profession = professions[trader._profession].name
471 local disabled_img = ""
472 if trade.locked then
473 disabled_img = "image[4.3,2.52;1,1;mobs_mc_trading_formspec_disabled.png]"..
474 "image[4.3,1.1;1,1;mobs_mc_trading_formspec_disabled.png]"
476 local tradeinv_name = "mobs_mc:trade_"..playername
477 local tradeinv = minetest.formspec_escape("detached:"..tradeinv_name)
479 local b_prev, b_next = "", ""
480 if #trades > 1 then
481 if tradenum > 1 then
482 b_prev = "button[1,1;0.5,1;prev_trade;<]"
484 if tradenum < trader._max_tradenum then
485 b_next = "button[7.26,1;0.5,1;next_trade;>]"
489 local formspec =
490 "size[9,8.75]"
491 .."background[-0.19,-0.25;9.41,9.49;mobs_mc_trading_formspec_bg.png]"
492 ..disabled_img
493 ..mcl_vars.inventory_header
494 .."label[4,0;"..minetest.formspec_escape(profession).."]"
495 .."list[current_player;main;0,4.5;9,3;9]"
496 .."list[current_player;main;0,7.74;9,1;]"
497 ..b_prev..b_next
498 .."list["..tradeinv..";wanted;2,1;2,1;]"
499 .."list["..tradeinv..";offered;5.76,1;1,1;]"
500 .."list["..tradeinv..";input;2,2.5;2,1;]"
501 .."list["..tradeinv..";output;5.76,2.55;1,1;]"
502 .."listring["..tradeinv..";output]"
503 .."listring[current_player;main]"
504 .."listring["..tradeinv..";input]"
505 .."listring[current_player;main]"
506 minetest.sound_play("mobs_mc_villager_trade", {to_player = playername})
507 minetest.show_formspec(playername, tradeinv_name, formspec)
510 local update_offer = function(inv, player, sound)
511 local name = player:get_player_name()
512 local trader = player_trading_with[name]
513 local tradenum = player_tradenum[name]
514 if not trader or not tradenum then
515 return false
517 local trades = minetest.deserialize(trader._trades)
518 if not trades then
519 return false
521 local trade = trades[tradenum]
522 if not trade then
523 return false
525 local wanted1, wanted2 = inv:get_stack("wanted", 1), inv:get_stack("wanted", 2)
526 local input1, input2 = inv:get_stack("input", 1), inv:get_stack("input", 2)
528 -- BEGIN OF SPECIAL HANDLING OF COMPASS
529 -- These 2 functions are a complicated check to check if the input contains a
530 -- special item which we cannot check directly against their name, like
531 -- compass.
532 -- TODO: Remove these check functions when compass and clock are implemented
533 -- as single items.
534 local check_special = function(special_item, group, wanted1, wanted2, input1, input2)
535 if minetest.registered_aliases[special_item] then
536 special_item = minetest.registered_aliases[special_item]
538 if wanted1:get_name() == special_item then
539 local check_input = function(input, wanted, group)
540 return minetest.get_item_group(input:get_name(), group) ~= 0 and input:get_count() >= wanted:get_count()
542 if check_input(input1, wanted1, group) then
543 return true
544 elseif check_input(input2, wanted1, group) then
545 return true
546 else
547 return false
550 return false
552 -- Apply above function to all items which we consider special.
553 -- This function succeeds if ANY item check succeeds.
554 local check_specials = function(wanted1, wanted2, input1, input2)
555 return check_special(COMPASS, "compass", wanted1, wanted2, input1, input2)
557 -- END OF SPECIAL HANDLING OF COMPASS
559 if (
560 ((inv:contains_item("input", wanted1) and
561 (wanted2:is_empty() or inv:contains_item("input", wanted2))) or
562 -- BEGIN OF SPECIAL HANDLING OF COMPASS
563 check_specials(wanted1, wanted2, input1, input2)) and
564 -- END OF SPECIAL HANDLING OF COMPASS
565 (trade.locked == false)) then
566 inv:set_stack("output", 1, inv:get_stack("offered", 1))
567 if sound then
568 minetest.sound_play("mobs_mc_villager_accept", {to_player = name})
570 return true
571 else
572 inv:set_stack("output", 1, ItemStack(""))
573 if sound then
574 minetest.sound_play("mobs_mc_villager_deny", {to_player = name})
576 return false
580 -- Returns a single itemstack in the given inventory to the player's main inventory, or drop it when there's no space left
581 local function return_item(itemstack, dropper, pos, inv_p)
582 if dropper:is_player() then
583 -- Return to main inventory
584 if inv_p:room_for_item("main", itemstack) then
585 inv_p:add_item("main", itemstack)
586 else
587 -- Drop item on the ground
588 local v = dropper:get_look_dir()
589 local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
590 p.x = p.x+(math.random(1,3)*0.2)
591 p.z = p.z+(math.random(1,3)*0.2)
592 local obj = minetest.add_item(p, itemstack)
593 if obj then
594 v.x = v.x*4
595 v.y = v.y*4 + 2
596 v.z = v.z*4
597 obj:setvelocity(v)
598 obj:get_luaentity()._insta_collect = false
601 else
602 -- Fallback for unexpected cases
603 minetest.add_item(pos, itemstack)
605 return itemstack
608 local return_fields = function(player)
609 local name = player:get_player_name()
610 local inv_t = minetest.get_inventory({type="detached", name = "mobs_mc:trade_"..name})
611 local inv_p = player:get_inventory()
612 for i=1, inv_t:get_size("input") do
613 local stack = inv_t:get_stack("input", i)
614 return_item(stack, player, player:get_pos(), inv_p)
615 stack:clear()
616 inv_t:set_stack("input", i, stack)
618 inv_t:set_stack("output", 1, "")
621 minetest.register_on_player_receive_fields(function(player, formname, fields)
622 if string.sub(formname, 1, 14) == "mobs_mc:trade_" then
623 local name = player:get_player_name()
624 if fields.quit then
625 -- Get input items back
626 return_fields(player)
627 -- Reset internal "trading with" state
628 local trader = player_trading_with[name]
629 if trader then
630 trader._trading_players[name] = nil
632 player_trading_with[name] = nil
633 elseif fields.next_trade or fields.prev_trade then
634 local trader = player_trading_with[name]
635 if not trader or not trader.object:get_luaentity() then
636 return
638 local trades = trader._trades
639 if not trades then
640 return
642 local dir = 1
643 if fields.prev_trade then
644 dir = -1
646 local tradenum = player_tradenum[name] + dir
647 local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
648 set_trade(trader, player, inv, tradenum)
649 update_offer(inv, player, false)
650 show_trade_formspec(name, trader, player_tradenum[name])
653 end)
655 minetest.register_on_leaveplayer(function(player)
656 local name = player:get_player_name()
657 return_fields(player)
658 player_tradenum[name] = nil
659 local trader = player_trading_with[name]
660 if trader then
661 trader._trading_players[name] = nil
663 player_trading_with[name] = nil
665 end)
667 -- Return true if player is trading with villager, and the villager entity exists
668 local trader_exists = function(playername)
669 local trader = player_trading_with[playername]
670 return trader ~= nil and trader.object:get_luaentity() ~= nil
673 local trade_inventory = {
674 allow_take = function(inv, listname, index, stack, player)
675 if listname == "input" then
676 return stack:get_count()
677 elseif listname == "output" then
678 if not trader_exists(player:get_player_name()) then
679 return 0
681 -- Only allow taking full stack
682 local count = stack:get_count()
683 if count == inv:get_stack(listname, index):get_count() then
684 -- Also update output stack again.
685 -- If input has double the wanted items, the
686 -- output will stay because there will be still
687 -- enough items in input after the trade
688 local wanted1 = inv:get_stack("wanted", 1)
689 local wanted2 = inv:get_stack("wanted", 2)
690 local input1 = inv:get_stack("input", 1)
691 local input2 = inv:get_stack("input", 2)
692 wanted1:set_count(wanted1:get_count()*2)
693 wanted2:set_count(wanted2:get_count()*2)
694 -- BEGIN OF SPECIAL HANDLING FOR COMPASS
695 local special_checks = function(wanted1, input1, input2)
696 if wanted1:get_name() == COMPASS then
697 local compasses = 0
698 if (minetest.get_item_group(input1:get_name(), "compass") ~= 0) then
699 compasses = compasses + input1:get_count()
701 if (minetest.get_item_group(input2:get_name(), "compass") ~= 0) then
702 compasses = compasses + input2:get_count()
704 return compasses >= wanted1:get_count()
706 return false
708 -- END OF SPECIAL HANDLING FOR COMPASS
709 if (inv:contains_item("input", wanted1) and
710 (wanted2:is_empty() or inv:contains_item("input", wanted2)))
711 -- BEGIN OF SPECIAL HANDLING FOR COMPASS
712 or special_checks(wanted1, input1, input2) then
713 -- END OF SPECIAL HANDLING FOR COMPASS
714 return -1
715 else
716 -- If less than double the wanted items,
717 -- remove items from output (final trade,
718 -- input runs empty)
719 return count
721 else
722 return 0
724 else
725 return 0
727 end,
728 allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
729 if from_list == "input" and to_list == "input" then
730 return count
731 elseif from_list == "output" and to_list == "input" then
732 if not trader_exists(player:get_player_name()) then
733 return 0
735 local move_stack = inv:get_stack(from_list, from_index)
736 if inv:get_stack(to_list, to_index):item_fits(move_stack) then
737 return count
740 return 0
741 end,
742 allow_put = function(inv, listname, index, stack, player)
743 if listname == "input" then
744 if not trader_exists(player:get_player_name()) then
745 return 0
746 else
747 return stack:get_count()
749 else
750 return 0
752 end,
753 on_put = function(inv, listname, index, stack, player)
754 update_offer(inv, player, true)
755 end,
756 on_move = function(inv, from_list, from_index, to_list, to_index, count, player)
757 if from_list == "output" and to_list == "input" then
758 inv:remove_item("input", inv:get_stack("wanted", 1))
759 local wanted2 = inv:get_stack("wanted", 2)
760 if not wanted2:is_empty() then
761 inv:remove_item("input", inv:get_stack("wanted", 2))
763 minetest.sound_play("mobs_mc_villager_accept", {to_player = player:get_player_name()})
765 update_offer(inv, player, true)
766 end,
767 on_take = function(inv, listname, index, stack, player)
768 local accept
769 local name = player:get_player_name()
770 if listname == "output" then
771 local wanted1 = inv:get_stack("wanted", 1)
772 inv:remove_item("input", wanted1)
773 local wanted2 = inv:get_stack("wanted", 2)
774 if not wanted2:is_empty() then
775 inv:remove_item("input", inv:get_stack("wanted", 2))
777 -- BEGIN OF SPECIAL HANDLING FOR COMPASS
778 if wanted1:get_name() == COMPASS then
779 for n=1, 2 do
780 local input = inv:get_stack("input", n)
781 if minetest.get_item_group(input:get_name(), "compass") ~= 0 then
782 input:set_count(input:get_count() - wanted1:get_count())
783 inv:set_stack("input", n, input)
784 break
788 -- END OF SPECIAL HANDLING FOR COMPASS
789 local trader = player_trading_with[name]
790 local tradenum = player_tradenum[name]
791 local trades
792 if trader and trader._trades then
793 trades = minetest.deserialize(trader._trades)
795 if trades then
796 local trade = trades[tradenum]
797 local unlock_stuff = false
798 if not trade.traded_once then
799 -- Unlock all the things if something was traded
800 -- for the first time ever
801 unlock_stuff = true
802 trade.traded_once = true
803 elseif trade.trade_counter == 0 and math.random(1,5) == 1 then
804 -- Otherwise, 20% chance to unlock if used freshly reset trade
805 unlock_stuff = true
807 local update_formspec = false
808 if unlock_stuff then
809 -- First-time trade unlock all trades and unlock next trade tier
810 if trade.tier + 1 > trader._max_trade_tier then
811 trader._max_trade_tier = trader._max_trade_tier + 1
812 update_max_tradenum(trader)
813 update_formspec = true
815 for t=1, #trades do
816 trades[t].locked = false
817 trades[t].trade_counter = 0
819 trader._locked_trades = 0
820 -- Also heal trader for unlocking stuff
821 -- TODO: Replace by Regeneration I
822 trader.health = math.min(trader.hp_max, trader.health + 4)
824 trade.trade_counter = trade.trade_counter + 1
825 -- Semi-randomly lock trade for repeated trade (not if there's only 1 trade)
826 if trader._max_tradenum > 1 then
827 if trade.trade_counter >= 12 then
828 trade.locked = true
829 elseif trade.trade_counter >= 2 then
830 local r = math.random(1, math.random(4, 10))
831 if r == 1 then
832 trade.locked = true
837 if trade.locked then
838 inv:set_stack("output", 1, "")
839 update_formspec = true
840 trader._locked_trades = trader._locked_trades + 1
841 -- Check if we managed to lock ALL available trades. Rare but possible.
842 if trader._locked_trades >= trader._max_tradenum then
843 -- Emergency unlock! Unlock all other trades except the current one
844 for t=1, #trades do
845 if t ~= tradenum then
846 trades[t].locked = false
847 trades[t].trade_counter = 0
850 trader._locked_trades = 1
851 -- Also heal trader for unlocking stuff
852 -- TODO: Replace by Regeneration I
853 trader.health = math.min(trader.hp_max, trader.health + 4)
856 trader._trades = minetest.serialize(trades)
857 if update_formspec then
858 show_trade_formspec(name, trader, tradenum)
860 else
861 minetest.log("error", "[mobs_mc] Player took item from trader output but player_trading_with or player_tradenum is nil!")
864 accept = true
865 elseif listname == "input" then
866 update_offer(inv, player, false)
868 if accept then
869 minetest.sound_play("mobs_mc_villager_accept", {to_player = name})
870 else
871 minetest.sound_play("mobs_mc_villager_deny", {to_player = name})
873 end,
876 minetest.register_on_joinplayer(function(player)
877 local name = player:get_player_name()
878 player_tradenum[name] = 1
879 player_trading_with[name] = nil
881 -- Create or get player-specific trading inventory
882 local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
883 if not inv then
884 inv = minetest.create_detached_inventory("mobs_mc:trade_"..name, trade_inventory, name)
886 inv:set_size("input", 2)
887 inv:set_size("output", 1)
888 inv:set_size("wanted", 2)
889 inv:set_size("offered", 1)
890 end)
892 --[=======[ MOB REGISTRATION AND SPAWNING ]=======]
894 mobs:register_mob("mobs_mc:villager", {
895 type = "npc",
896 hp_min = 20,
897 hp_max = 20,
898 collisionbox = {-0.3, -0.01, -0.3, 0.3, 1.94, 0.3},
899 visual = "mesh",
900 mesh = "mobs_mc_villager.b3d",
901 textures = {
903 "mobs_mc_villager.png",
904 "mobs_mc_villager.png", --hat
907 "mobs_mc_villager_farmer.png",
908 "mobs_mc_villager_farmer.png", --hat
911 "mobs_mc_villager_priest.png",
912 "mobs_mc_villager_priest.png", --hat
915 "mobs_mc_villager_librarian.png",
916 "mobs_mc_villager_librarian.png", --hat
919 "mobs_mc_villager_butcher.png",
920 "mobs_mc_villager_butcher.png", --hat
923 "mobs_mc_villager_smith.png",
924 "mobs_mc_villager_smith.png", --hat
927 visual_size = {x=3, y=3},
928 makes_footstep_sound = true,
929 walk_velocity = 1.2,
930 run_velocity = 2.4,
931 drops = {},
932 sounds = {
933 random = "mobs_mc_villager_noise",
934 death = "mobs_mc_villager_death",
935 damage = "mobs_mc_villager_damage",
936 distance = 16,
938 animation = {
939 stand_speed = 25,
940 stand_start = 40,
941 stand_end = 59,
942 walk_speed = 25,
943 walk_start = 0,
944 walk_end = 40,
945 run_speed = 25,
946 run_start = 0,
947 run_end = 40,
948 die_speed = 15,
949 die_start = 210,
950 die_end = 220,
951 die_loop = false,
953 water_damage = 0,
954 lava_damage = 4,
955 light_damage = 0,
956 view_range = 16,
957 fear_height = 4,
958 jump = true,
959 walk_chance = DEFAULT_WALK_CHANCE,
960 on_rightclick = function(self, clicker)
961 -- Initiate trading
962 local name = clicker:get_player_name()
963 self._trading_players[name] = true
965 init_trader_vars(self)
966 if self._trades == nil then
967 init_trades(self)
969 update_max_tradenum(self)
970 if self._trades == false then
971 -- Villager has no trades, rightclick is a no-op
972 return
975 player_trading_with[name] = self
977 local inv = minetest.get_inventory({type="detached", name="mobs_mc:trade_"..name})
979 set_trade(self, clicker, inv, 1)
981 show_trade_formspec(name, self)
983 -- Behaviour stuff:
984 -- Make villager look at player and stand still
985 local selfpos = self.object:get_pos()
986 local clickerpos = clicker:get_pos()
987 local dir = vector.direction(selfpos, clickerpos)
988 self.object:set_yaw(minetest.dir_to_yaw(dir))
989 stand_still(self)
990 end,
992 _player_scan_timer = 0,
993 _trading_players = {}, -- list of playernames currently trading with villager (open formspec)
994 do_custom = function(self, dtime)
995 -- Stand still if player is nearby.
996 if not self._player_scan_timer then
997 self._player_scan_timer = 0
999 self._player_scan_timer = self._player_scan_timer + dtime
1000 -- Check infrequently to keep CPU load low
1001 if self._player_scan_timer > PLAYER_SCAN_INTERVAL then
1002 self._player_scan_timer = 0
1003 local selfpos = self.object:get_pos()
1004 local objects = minetest.get_objects_inside_radius(selfpos, PLAYER_SCAN_RADIUS)
1005 local has_player = false
1006 for o, obj in pairs(objects) do
1007 if obj:is_player() then
1008 has_player = true
1009 break
1012 if has_player then
1013 minetest.log("verbose", "[mobs_mc] Player near villager found!")
1014 stand_still(self)
1015 else
1016 minetest.log("verbose", "[mobs_mc] No player near villager found!")
1017 self.walk_chance = DEFAULT_WALK_CHANCE
1018 self.jump = true
1021 end,
1023 on_spawn = function(self)
1024 init_trader_vars(self)
1025 end,
1026 on_die = function(self, pos)
1027 -- Close open trade formspecs and give input back to players
1028 local trading_players = self._trading_players
1029 for name, _ in pairs(trading_players) do
1030 minetest.close_formspec(name, "mobs_mc:trade_"..name)
1031 local player = minetest.get_player_by_name(name)
1032 if player then
1033 return_fields(player)
1036 end,
1041 mobs:spawn_specific("mobs_mc:villager", mobs_mc.spawn.village, {"air"}, 0, minetest.LIGHT_MAX+1, 30, 8000, 4, mobs_mc.spawn_height.water+1, mobs_mc.spawn_height.overworld_max)
1043 -- compatibility
1044 mobs:alias_mob("mobs:villager", "mobs_mc:villager")
1046 -- spawn eggs
1047 mobs:register_egg("mobs_mc:villager", S("Villager"), "mobs_mc_spawn_icon_villager.png", 0)
1049 if minetest.settings:get_bool("log_mods") then
1050 minetest.log("action", "MC mobs loaded")