api.lua 14 KB

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