loot.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. -- Localize for performance.
  2. local math_floor = math.floor
  3. local math_random = math.random
  4. local critical_loot = {
  5. -- The main problem with the `surface` challenge is keeping it from being too easy.
  6. -- This is especially due to travel being very swift, and too much food can go a long way.
  7. ["surface"] = {
  8. {item="default:stick", min=5, max=20},
  9. {item="farming:bread", min=5, max=20},
  10. {item="default:steel_ingot", min=5, max=20},
  11. {item="default:diamond", min=3, max=9},
  12. {item="bones:bones_type2", min=9, max=27},
  13. {item="torches:torch_floor", min=10, max=30},
  14. {item="bucket:bucket_water", min=1, max=4},
  15. {item="default:dirt", min=6, max=12},
  16. },
  17. -- The challenge of `cave` mode is building a farm to make food and keep going.
  18. -- Finding sources of iron and coal are critical. Farm supplies have to be provided
  19. -- right away because otherwise the gamemode would probably be impossible.
  20. ["cave"] = {
  21. {item="default:stick", min=10, max=30},
  22. {item="farming:bread", min=10, max=50},
  23. {item="basictrees:tree_apple", min=10, max=50},
  24. {item="pumpkin:bread", min=10, max=30},
  25. {item="default:steel_ingot", min=30, max=64},
  26. {item="default:coal_lump", min=30, max=64},
  27. {item="bones:bones_type2", min=10, max=40},
  28. {item="default:dirt", min=6, max=12},
  29. {item="torches:torch_floor", min=10, max=30},
  30. {item="bucket:bucket_water", min=4, max=12},
  31. {item="default:grass_dummy", min=5, max=20},
  32. {item="moreblocks:super_glow_glass", min=6, max=20},
  33. {item="rackstone:dauthsand", min=3, max=6},
  34. {item="firetree:sapling", min=2, max=3},
  35. {item="griefer:grieferstone", min=2, max=4},
  36. {item="titanium:crystal", min=3, max=16},
  37. },
  38. -- Like `cave` mode, in this gamemode building a farm and finding sources of iron and coal are critical.
  39. ["nether"] = {
  40. {item="default:stick", min=10, max=30},
  41. {item="farming:bread", min=10, max=50},
  42. {item="basictrees:tree_apple", min=10, max=50},
  43. {item="default:steel_ingot", min=30, max=64},
  44. {item="default:coal_lump", min=30, max=64},
  45. {item="gems:ruby_gem", min=3, max=13},
  46. {item="torches:kalite_torch_floor", min=10, max=25},
  47. {item="moreblocks:super_glow_glass", min=6, max=20},
  48. {item="rackstone:dauthsand", min=3, max=12},
  49. {item="firetree:sapling", min=2, max=3},
  50. {item="default:flint", min=3, max=5},
  51. {item="bluegrass:seed", min=3, max=16},
  52. {item="griefer:grieferstone", min=2, max=4},
  53. {item="titanium:crystal", min=3, max=16},
  54. {item="default:cobble", min=1, max=64},
  55. {item="default:cobble", min=1, max=64},
  56. {item="default:cobble", min=1, max=64},
  57. -- Nether challenge must have bed, because otherwise it is too
  58. -- difficult/impossible to craft one.
  59. {item="beds:fancy_bed_bottom", min=3, max=5},
  60. },
  61. }
  62. local bonus_loot = {
  63. {item="farming:seed_wheat", min=2, max=6, chance=40},
  64. {item="farming:seed_cotton", min=2, max=6, chance=40},
  65. {item="potatoes:seed", min=2, max=6, chance=40},
  66. {item="default:junglegrass", min=2, max=6, chance=40},
  67. {item="default:cactus", min=2, max=6, chance=40},
  68. {item="bandages:bandage_3", min=5, max=30, chance=40},
  69. {item="carbon_steel:ingot", min=5, max=30, chance=40},
  70. {item="default:mese_crystal", min=5, max=30, chance=40},
  71. }
  72. function survivalist.fill_loot_chest(inv, gamemode)
  73. if not inv then
  74. return
  75. end
  76. local loot = {}
  77. local critical = critical_loot[gamemode]
  78. if not critical then
  79. return
  80. end
  81. loot = table.copy(critical)
  82. table.shuffle(loot)
  83. local positions = {}
  84. local listsize = inv:get_size("main")
  85. for i = 1, listsize, 1 do
  86. positions[#positions + 1] = i
  87. end
  88. table.shuffle(positions)
  89. -- Add loot.
  90. for k, v in ipairs(loot) do
  91. local min = math_floor(v.min)
  92. local max = math.ceil(v.max)
  93. local stackmax = minetest.registered_items[v.item].stack_max or 64
  94. if max >= min then
  95. local count = math_floor(math_random(min, max))
  96. while count > 0 and #positions > 0 do
  97. local idx = positions[#positions]
  98. if count > stackmax then
  99. inv:set_stack("main", idx, ItemStack(v.item .. " " .. stackmax))
  100. count = count - stackmax
  101. else
  102. inv:set_stack("main", idx, ItemStack(v.item .. " " .. count))
  103. count = 0
  104. end
  105. positions[#positions] = nil
  106. end
  107. end
  108. end
  109. local bonus = table.copy(bonus_loot)
  110. table.shuffle(bonus)
  111. -- Add bonus loot.
  112. for k, v in ipairs(bonus) do
  113. local min = math_floor(v.min)
  114. local max = math.ceil(v.max)
  115. local stackmax = minetest.registered_items[v.item].stack_max or 64
  116. if max >= min then
  117. local count = math_floor(math_random(min, max))
  118. local chance = math_random(0, 100)
  119. if chance < v.chance then
  120. while count > 0 and #positions > 0 do
  121. local idx = positions[#positions]
  122. if count > stackmax then
  123. inv:set_stack("main", idx, ItemStack(v.item .. " " .. stackmax))
  124. count = count - stackmax
  125. else
  126. inv:set_stack("main", idx, ItemStack(v.item .. " " .. count))
  127. count = 0
  128. end
  129. positions[#positions] = nil
  130. end
  131. end
  132. end
  133. end
  134. end