api.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. -- support for i18n
  2. local S = armor_i18n.gettext
  3. local skin_previews = {}
  4. local use_player_monoids = minetest.global_exists("player_monoids")
  5. local use_armor_monoid = minetest.global_exists("armor_monoid")
  6. local use_pova_mod = minetest.get_modpath("pova")
  7. local armor_def = setmetatable({}, {
  8. __index = function()
  9. return setmetatable({
  10. groups = setmetatable({}, {
  11. __index = function()
  12. return 0
  13. end})
  14. }, {
  15. __index = function()
  16. return 0
  17. end
  18. })
  19. end,
  20. })
  21. local armor_textures = setmetatable({}, {
  22. __index = function()
  23. return setmetatable({}, {
  24. __index = function()
  25. return "blank.png"
  26. end
  27. })
  28. end
  29. })
  30. armor = {
  31. timer = 0,
  32. elements = {"head", "torso", "legs", "feet"},
  33. physics = {"jump", "speed", "gravity"},
  34. attributes = {"heal", "fire", "water", "block"},
  35. formspec = "image[2.5,0;2,4;armor_preview]"..
  36. default.gui_bg..
  37. default.gui_bg_img..
  38. default.gui_slots..
  39. default.get_hotbar_bg(0, 4.7)..
  40. "list[current_player;main;0,4.7;8,1;]"..
  41. "list[current_player;main;0,5.85;8,3;8]",
  42. def = armor_def,
  43. textures = armor_textures,
  44. default_skin = "character",
  45. materials = {
  46. wood = "group:wood",
  47. },
  48. fire_nodes = {
  49. {"default:lava_source", 5, 8},
  50. {"default:lava_flowing", 5, 8},
  51. {"nether:lava_source", 5, 8},
  52. {"fire:basic_flame", 3, 4},
  53. {"fire:permanent_flame", 3, 4},
  54. {"epic:permanent_flame", 3, 4},
  55. {"nether:lava_crust", 3, 4},
  56. },
  57. registered_groups = {["fleshy"]=100},
  58. registered_callbacks = {
  59. on_update = {},
  60. on_equip = {},
  61. on_unequip = {},
  62. on_damage = {},
  63. on_destroy = {},
  64. },
  65. migrate_old_inventory = true,
  66. version = "0.4.13",
  67. }
  68. armor.config = {
  69. init_delay = 2,
  70. init_times = 10,
  71. bones_delay = 1,
  72. update_time = 1,
  73. drop = minetest.get_modpath("bones") ~= nil,
  74. destroy = false,
  75. level_multiplier = 1,
  76. heal_multiplier = 1,
  77. material_wood = true,
  78. material_cactus = true,
  79. material_steel = true,
  80. material_bronze = true,
  81. material_diamond = true,
  82. material_gold = true,
  83. material_mithril = true,
  84. material_crystal = true,
  85. water_protect = true,
  86. fire_protect = minetest.get_modpath("ethereal") ~= nil,
  87. punch_damage = true,
  88. }
  89. -- Armor Registration
  90. armor.register_armor = function(self, name, def)
  91. minetest.register_tool(name, def)
  92. end
  93. armor.register_armor_group = function(self, group, base)
  94. base = base or 100
  95. self.registered_groups[group] = base
  96. if use_armor_monoid then
  97. armor_monoid.register_armor_group(group, base)
  98. end
  99. end
  100. -- Armor callbacks
  101. armor.register_on_update = function(self, func)
  102. if type(func) == "function" then
  103. table.insert(self.registered_callbacks.on_update, func)
  104. end
  105. end
  106. armor.register_on_equip = function(self, func)
  107. if type(func) == "function" then
  108. table.insert(self.registered_callbacks.on_equip, func)
  109. end
  110. end
  111. armor.register_on_unequip = function(self, func)
  112. if type(func) == "function" then
  113. table.insert(self.registered_callbacks.on_unequip, func)
  114. end
  115. end
  116. armor.register_on_damage = function(self, func)
  117. if type(func) == "function" then
  118. table.insert(self.registered_callbacks.on_damage, func)
  119. end
  120. end
  121. armor.register_on_destroy = function(self, func)
  122. if type(func) == "function" then
  123. table.insert(self.registered_callbacks.on_destroy, func)
  124. end
  125. end
  126. armor.run_callbacks = function(self, callback, player, index, stack)
  127. if stack then
  128. local def = stack:get_definition() or {}
  129. if type(def[callback]) == "function" then
  130. def[callback](player, index, stack)
  131. end
  132. end
  133. local callbacks = self.registered_callbacks[callback]
  134. if callbacks then
  135. for _, func in pairs(callbacks) do
  136. func(player, index, stack)
  137. end
  138. end
  139. end
  140. armor.update_player_visuals = function(self, player)
  141. if not player then
  142. return
  143. end
  144. local name = player:get_player_name()
  145. if self.textures[name] then
  146. default.player_set_textures(player, {
  147. self.textures[name].skin,
  148. self.textures[name].armor,
  149. self.textures[name].wielditem,
  150. })
  151. end
  152. self:run_callbacks("on_update", player)
  153. end
  154. armor.set_player_armor = function(self, player)
  155. local name, armor_inv = self:get_valid_player(player, "[set_player_armor]")
  156. if not name then
  157. return
  158. end
  159. local state = 0
  160. local count = 0
  161. local material = {count=1}
  162. local preview = armor:get_preview(name)
  163. local texture = "3d_armor_trans.png"
  164. local textures = {}
  165. local physics = {}
  166. local attributes = {}
  167. local levels = {}
  168. local groups = {}
  169. local change = {}
  170. for _, phys in pairs(self.physics) do
  171. physics[phys] = 1
  172. end
  173. for _, attr in pairs(self.attributes) do
  174. attributes[attr] = 0
  175. end
  176. for group, _ in pairs(self.registered_groups) do
  177. change[group] = 1
  178. levels[group] = 0
  179. end
  180. local list = armor_inv:get_list("armor")
  181. if type(list) ~= "table" then
  182. return
  183. end
  184. for i, stack in pairs(list) do
  185. if stack:get_count() == 1 then
  186. local def = stack:get_definition()
  187. for _, element in pairs(self.elements) do
  188. if def.groups["armor_"..element] then
  189. if def.armor_groups then
  190. for group, level in pairs(def.armor_groups) do
  191. if levels[group] then
  192. levels[group] = levels[group] + level
  193. end
  194. end
  195. else
  196. local level = def.groups["armor_"..element]
  197. levels["fleshy"] = levels["fleshy"] + level --Level is 90% of armor_group added value.
  198. end
  199. break
  200. end
  201. -- DEPRECATED, use armor_groups instead
  202. if def.groups["armor_radiation"] and levels["radiation"] then
  203. levels["radiation"] = def.groups["armor_radiation"]
  204. end
  205. end
  206. local item = stack:get_name()
  207. local tex = def.texture or item:gsub("%:", "_")
  208. tex = tex:gsub(".png$", "")
  209. local prev = def.preview or tex.."_preview"
  210. prev = prev:gsub(".png$", "")
  211. texture = texture.."^"..tex..".png"
  212. preview = preview.."^"..prev..".png"
  213. state = state + stack:get_wear()
  214. count = count + 1
  215. for _, phys in pairs(self.physics) do
  216. local value = def.groups["physics_"..phys] or 0
  217. physics[phys] = physics[phys] + value
  218. end
  219. for _, attr in pairs(self.attributes) do
  220. local value = def.groups["armor_"..attr] or 0
  221. attributes[attr] = attributes[attr] + value
  222. end
  223. local mat = string.match(item, "%:.+_(.+)$")
  224. if material.name then
  225. if material.name == mat then
  226. material.count = material.count + 1
  227. end
  228. else
  229. material.name = mat
  230. end
  231. end
  232. end
  233. for group, level in pairs(levels) do
  234. if level > 0 then
  235. level = level * armor.config.level_multiplier
  236. if material.name and material.count == #self.elements then
  237. level = level * 1.1
  238. end
  239. end
  240. local base = self.registered_groups[group]
  241. self.def[name].groups[group] = level
  242. if level > base then
  243. level = base
  244. end
  245. groups[group] = base - level
  246. change[group] = groups[group] / base
  247. end
  248. for _, attr in pairs(self.attributes) do
  249. local mult = attr == "heal" and self.config.heal_multiplier or 1
  250. self.def[name][attr] = attributes[attr] * mult
  251. end
  252. for _, phys in pairs(self.physics) do
  253. self.def[name][phys] = physics[phys]
  254. end
  255. if use_armor_monoid then
  256. armor_monoid.monoid:add_change(player, change, "3d_armor:armor")
  257. else
  258. player:set_armor_groups(groups)
  259. end
  260. if use_player_monoids then
  261. player_monoids.speed:add_change(player, physics.speed,
  262. "3d_armor:physics")
  263. player_monoids.jump:add_change(player, physics.jump,
  264. "3d_armor:physics")
  265. player_monoids.gravity:add_change(player, physics.gravity,
  266. "3d_armor:physics")
  267. elseif use_pova_mod then
  268. -- only add the changes, not the default 1.0 for each physics setting
  269. pova.add_override(name, "3d_armor", {
  270. speed = physics.speed - 1,
  271. jump = physics.jump - 1,
  272. gravity = physics.gravity - 1,
  273. })
  274. pova.do_override(player)
  275. else
  276. player:set_physics_override(physics)
  277. end
  278. self.textures[name].armor = texture
  279. self.textures[name].preview = preview
  280. self.def[name].level = self.def[name].groups.fleshy or 0
  281. self.def[name].state = state
  282. self.def[name].count = count
  283. self:update_player_visuals(player)
  284. end
  285. armor.punch = function(self, player, hitter, time_from_last_punch, tool_capabilities)
  286. local name, armor_inv = self:get_valid_player(player, "[punch]")
  287. if not name then
  288. return
  289. end
  290. local state = 0
  291. local count = 0
  292. local recip = true
  293. local default_groups = {cracky=3, snappy=3, choppy=3, crumbly=3, level=1}
  294. local list = armor_inv:get_list("armor")
  295. for i, stack in pairs(list) do
  296. if stack:get_count() == 1 then
  297. local name = stack:get_name()
  298. local use = minetest.get_item_group(name, "armor_use") or 0
  299. local damage = use > 0
  300. local def = stack:get_definition() or {}
  301. if type(def.on_punched) == "function" then
  302. damage = def.on_punched(player, hitter, time_from_last_punch,
  303. tool_capabilities) ~= false and damage == true
  304. end
  305. if damage == true and tool_capabilities then
  306. local damage_groups = def.damage_groups or default_groups
  307. local level = damage_groups.level or 0
  308. local groupcaps = tool_capabilities.groupcaps or {}
  309. local uses = 0
  310. damage = false
  311. for group, caps in pairs(groupcaps) do
  312. local maxlevel = caps.maxlevel or 0
  313. local diff = maxlevel - level
  314. if diff == 0 then
  315. diff = 1
  316. end
  317. if diff > 0 and caps.times then
  318. local group_level = damage_groups[group]
  319. if group_level then
  320. local time = caps.times[group_level]
  321. if time then
  322. local dt = time_from_last_punch or 0
  323. if dt > time / diff then
  324. if caps.uses then
  325. uses = caps.uses * math.pow(3, diff)
  326. end
  327. damage = true
  328. break
  329. end
  330. end
  331. end
  332. end
  333. end
  334. if damage == true and recip == true and hitter and
  335. def.reciprocate_damage == true and uses > 0 then
  336. local item = hitter:get_wielded_item()
  337. if item and item:get_name() ~= "" then
  338. item:add_wear(65535 / uses)
  339. hitter:set_wielded_item(item)
  340. end
  341. -- reciprocate tool damage only once
  342. recip = false
  343. end
  344. end
  345. if damage == true and hitter == "fire" then
  346. damage = minetest.get_item_group(name, "flammable") > 0
  347. end
  348. if damage == true and hitter == "water" then
  349. damage = minetest.get_item_group(name, "armor_water") > 0
  350. end
  351. if damage == true then
  352. self:damage(player, i, stack, use)
  353. end
  354. state = state + stack:get_wear()
  355. count = count + 1
  356. end
  357. end
  358. self.def[name].state = state
  359. self.def[name].count = count
  360. end
  361. armor.damage = function(self, player, index, stack, use)
  362. local old_stack = ItemStack(stack)
  363. stack:add_wear(use)
  364. self:run_callbacks("on_damage", player, index, stack)
  365. self:set_inventory_stack(player, index, stack)
  366. if stack:get_count() == 0 then
  367. self:run_callbacks("on_unequip", player, index, old_stack)
  368. self:run_callbacks("on_destroy", player, index, old_stack)
  369. self:set_player_armor(player)
  370. end
  371. end
  372. armor.get_player_skin = function(self, name)
  373. if (self.skin_mod == "skins" or self.skin_mod == "simple_skins") and skins.skins[name] then
  374. return skins.skins[name]..".png"
  375. elseif self.skin_mod == "u_skins" and u_skins.u_skins[name] then
  376. return u_skins.u_skins[name]..".png"
  377. elseif self.skin_mod == "wardrobe" and wardrobe.playerSkins and wardrobe.playerSkins[name] then
  378. return wardrobe.playerSkins[name]
  379. elseif self.skin_mod == "custom_skin" then
  380. return custom_skin.texture[name]
  381. end
  382. return armor.default_skin..".png"
  383. end
  384. armor.add_preview = function(self, preview)
  385. skin_previews[preview] = true
  386. end
  387. armor.get_preview = function(self, name)
  388. local preview = string.gsub(armor:get_player_skin(name), ".png", "_preview.png")
  389. if skin_previews[preview] then
  390. return preview
  391. end
  392. return "character_preview.png"
  393. end
  394. armor.get_armor_formspec = function(self, name, listring)
  395. if armor.def[name].init_time == 0 then
  396. return "label[0,0;Armor not initialized!]"
  397. end
  398. local formspec = armor.formspec..
  399. "list[detached:"..name.."_armor;armor;0,0.5;2,3;]"
  400. if listring == true then
  401. formspec = formspec.."listring[current_player;main]"..
  402. "listring[detached:"..name.."_armor;armor]"
  403. end
  404. formspec = formspec:gsub("armor_preview", armor.textures[name].preview)
  405. formspec = formspec:gsub("armor_level", armor.def[name].level)
  406. for _, attr in pairs(self.attributes) do
  407. formspec = formspec:gsub("armor_attr_"..attr, armor.def[name][attr])
  408. end
  409. for group, _ in pairs(self.registered_groups) do
  410. formspec = formspec:gsub("armor_group_"..group,
  411. armor.def[name].groups[group])
  412. end
  413. return formspec
  414. end
  415. armor.get_element = function(self, item_name)
  416. for _, element in pairs(armor.elements) do
  417. if minetest.get_item_group(item_name, "armor_"..element) > 0 then
  418. return element
  419. end
  420. end
  421. end
  422. armor.serialize_inventory_list = function(self, list)
  423. local list_table = {}
  424. for _, stack in ipairs(list) do
  425. table.insert(list_table, stack:to_string())
  426. end
  427. return minetest.serialize(list_table)
  428. end
  429. armor.deserialize_inventory_list = function(self, list_string)
  430. local list_table = minetest.deserialize(list_string)
  431. local list = {}
  432. for _, stack in ipairs(list_table or {}) do
  433. table.insert(list, ItemStack(stack))
  434. end
  435. return list
  436. end
  437. armor.load_armor_inventory = function(self, player)
  438. local _, inv = self:get_valid_player(player, "[load_armor_inventory]")
  439. if inv then
  440. local armor_list_string = player:get_attribute("3d_armor_inventory")
  441. if armor_list_string then
  442. inv:set_list("armor",
  443. self:deserialize_inventory_list(armor_list_string))
  444. return true
  445. end
  446. end
  447. end
  448. armor.save_armor_inventory = function(self, player)
  449. local _, inv = self:get_valid_player(player, "[save_armor_inventory]")
  450. if inv then
  451. player:set_attribute("3d_armor_inventory",
  452. self:serialize_inventory_list(inv:get_list("armor")))
  453. end
  454. end
  455. armor.update_inventory = function(self, player)
  456. -- DEPRECATED: Legacy inventory support
  457. end
  458. armor.set_inventory_stack = function(self, player, i, stack)
  459. local _, inv = self:get_valid_player(player, "[set_inventory_stack]")
  460. if inv then
  461. inv:set_stack("armor", i, stack)
  462. self:save_armor_inventory(player)
  463. end
  464. end
  465. armor.get_valid_player = function(self, player, msg)
  466. msg = msg or ""
  467. if not player then
  468. minetest.log("warning", S("3d_armor: Player reference is nil @1", msg))
  469. return
  470. end
  471. local name = player:get_player_name()
  472. if not name then
  473. minetest.log("warning", S("3d_armor: Player name is nil @1", msg))
  474. return
  475. end
  476. local inv = minetest.get_inventory({type="detached", name=name.."_armor"})
  477. if not inv then
  478. minetest.log("warning", S("3d_armor: Detached armor inventory is nil @1", msg))
  479. return
  480. end
  481. return name, inv
  482. end
  483. armor.drop_armor = function(pos, stack)
  484. local node = minetest.get_node_or_nil(pos)
  485. if node then
  486. local obj = minetest.add_item(pos, stack)
  487. if obj then
  488. obj:setvelocity({x=math.random(-1, 1), y=5, z=math.random(-1, 1)})
  489. end
  490. end
  491. end