init.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. stamina = {players = {}, mod = "redo"}
  2. STAMINA_TICK = tonumber(minetest.settings:get("stamina_tick")) or 800
  3. -- time in seconds after that 1 stamina point is taken
  4. STAMINA_TICK_MIN = 4 -- stamina ticks won't reduce stamina below this level
  5. STAMINA_HEALTH_TICK = 4 -- time in seconds after player gets healed/damaged
  6. STAMINA_MOVE_TICK = 0.5 -- time in seconds after the movement is checked
  7. STAMINA_POISON_TICK = 1.25 -- time in seconds after player is poisoned
  8. STAMINA_EXHAUST_DIG = 2 -- exhaustion increased this value after digged node
  9. STAMINA_EXHAUST_PLACE = 1 -- .. after digging node
  10. STAMINA_EXHAUST_MOVE = 1.5 -- .. if player movement detected
  11. STAMINA_EXHAUST_JUMP = 5 -- .. if jumping
  12. STAMINA_EXHAUST_CRAFT = 2 -- .. if player crafts
  13. STAMINA_EXHAUST_PUNCH = 40 -- .. if player punches another player
  14. STAMINA_EXHAUST_LVL = 160 -- at what exhaustion player saturation gets lowered
  15. STAMINA_HEAL = 1 -- number of HP player gets healed after STAMINA_HEALTH_TICK
  16. STAMINA_HEAL_LVL = 5 -- lower level of saturation needed to get healed
  17. STAMINA_STARVE = 1 -- number of HP player gets damaged by stamina after
  18. -- STAMINA_HEALTH_TICK
  19. STAMINA_STARVE_LVL = 3 -- level of staturation that causes starving
  20. STAMINA_VISUAL_MAX = 20 -- hud bar extends only to 20
  21. SPRINT_SPEED = 0.3 -- how much faster player can run if satiated
  22. SPRINT_JUMP = 0.1 -- how much higher player can jump if satiated
  23. SPRINT_DRAIN = 0.35 -- how fast to drain satation while sprinting (0-1)
  24. local function get_int_attribute(player)
  25. -- pipeworks fake player check
  26. if not player or not player.get_attribute then
  27. return nil
  28. end
  29. local meta = player:get_meta()
  30. local level = meta and meta:get_string("stamina:level")
  31. if level then
  32. return tonumber(level)
  33. end
  34. return nil
  35. end
  36. local stamina_enabled = minetest.settings:get_bool("enable_stamina") ~= false
  37. local function stamina_update_level(player, level)
  38. -- pipeworks fake player check
  39. if not player.get_attribute or not stamina_enabled then
  40. return nil
  41. end
  42. local old = get_int_attribute(player)
  43. if level == old then -- To suppress HUD update
  44. return
  45. end
  46. -- players without interact priv cannot eat
  47. if not minetest.check_player_privs(player, {interact = true}) then
  48. return
  49. end
  50. local meta = player and player:get_meta() ; if not meta then return end
  51. meta:set_string("stamina:level", level)
  52. player:hud_change(
  53. stamina.players[player:get_player_name()].hud_id,
  54. "number",
  55. math.min(STAMINA_VISUAL_MAX, level)
  56. )
  57. end
  58. -- global function for mods to amend stamina level
  59. stamina.change = function(player, change)
  60. local name = player:get_player_name()
  61. if not name or not change or change == 0 then
  62. return false
  63. end
  64. local level = get_int_attribute(player) + change
  65. if level < 0 then level = 0 end
  66. if level > STAMINA_VISUAL_MAX then level = STAMINA_VISUAL_MAX end
  67. stamina_update_level(player, level)
  68. return true
  69. end
  70. local function exhaust_player(player, v)
  71. if not player
  72. or not player.is_player
  73. or not player:is_player()
  74. or not player.set_attribute then
  75. return
  76. end
  77. local name = player:get_player_name()
  78. if not name then
  79. return
  80. end
  81. local exhaustion = stamina.players[name].exhaustion + v
  82. if exhaustion > STAMINA_EXHAUST_LVL then
  83. exhaustion = 0
  84. local h = get_int_attribute(player)
  85. if h > 0 then
  86. stamina_update_level(player, h - 1)
  87. end
  88. end
  89. stamina.players[name].exhaustion = exhaustion
  90. end
  91. -- Sprint settings and function
  92. local enable_sprint = minetest.settings:get_bool("sprint") ~= false
  93. local enable_sprint_particles = minetest.settings:get_bool("sprint_particles") ~= false
  94. local monoids = minetest.get_modpath("player_monoids")
  95. local pova_mod = minetest.get_modpath("pova")
  96. local function set_sprinting(name, sprinting)
  97. local player = minetest.get_player_by_name(name) ; if not player then return end
  98. -- get player physics
  99. local def = player:get_physics_override()
  100. --print ("---", def.speed, def.jump)
  101. if sprinting == true and not stamina.players[name].sprint then
  102. if monoids then
  103. stamina.players[name].sprint = player_monoids.speed:add_change(
  104. player, def.speed + SPRINT_SPEED)
  105. stamina.players[name].jump = player_monoids.jump:add_change(
  106. player, def.jump + SPRINT_JUMP)
  107. elseif pova_mod then
  108. pova.add_override(name, "sprint",
  109. {speed = SPRINT_SPEED, jump = SPRINT_JUMP})
  110. pova.do_override(player)
  111. stamina.players[name].sprint = true
  112. else
  113. player:set_physics_override({
  114. speed = def.speed + SPRINT_SPEED,
  115. jump = def.jump + SPRINT_JUMP,
  116. })
  117. stamina.players[name].sprint = true
  118. end
  119. elseif sprinting == false
  120. and stamina.players[name]
  121. and stamina.players[name].sprint then
  122. if monoids then
  123. player_monoids.speed:del_change(player, stamina.players[name].sprint)
  124. player_monoids.jump:del_change(player, stamina.players[name].jump)
  125. stamina.players[name].sprint = nil
  126. stamina.players[name].jump = nil
  127. elseif pova_mod then
  128. pova.del_override(name, "sprint")
  129. pova.do_override(player)
  130. stamina.players[name].sprint = nil
  131. else
  132. player:set_physics_override({
  133. speed = def.speed - SPRINT_SPEED,
  134. jump = def.jump - SPRINT_JUMP,
  135. })
  136. stamina.players[name].sprint = nil
  137. end
  138. end
  139. end
  140. local function head_particle(player, texture)
  141. if not player then return end
  142. local prop = player:get_properties()
  143. local pos = player:get_pos() ; pos.y = pos.y + (prop.eye_height or 1.23) -- mouth level
  144. local dir = player:get_look_dir()
  145. minetest.add_particlespawner({
  146. amount = 5,
  147. time = 0.1,
  148. minpos = pos,
  149. maxpos = pos,
  150. minvel = {x = dir.x - 1, y = dir.y, z = dir.z - 1},
  151. maxvel = {x = dir.x + 1, y = dir.y, z = dir.z + 1},
  152. minacc = {x = 0, y = -5, z = 0},
  153. maxacc = {x = 0, y = -9, z = 0},
  154. minexptime = 1,
  155. maxexptime = 1,
  156. minsize = 1,
  157. maxsize = 2,
  158. texture = texture
  159. })
  160. end
  161. local function drunk_tick()
  162. for _,player in ipairs(minetest.get_connected_players()) do
  163. local name = player:get_player_name()
  164. if name
  165. and stamina.players[name]
  166. and stamina.players[name].drunk then
  167. -- play burp sound every 20 seconds when drunk
  168. local num = stamina.players[name].drunk
  169. if num and num > 0 and math.floor(num / 20) == num / 20 then
  170. head_particle(player, "bubble.png")
  171. minetest.sound_play("stamina_burp",
  172. {to_player = name, gain = 0.7}, true)
  173. end
  174. stamina.players[name].drunk = stamina.players[name].drunk - 1
  175. if stamina.players[name].drunk < 1 then
  176. stamina.players[name].drunk = nil
  177. stamina.players[name].units = 0
  178. if not stamina.players[name].poisoned then
  179. player:hud_change(stamina.players[name].hud_id,
  180. "text", "stamina_hud_fg.png")
  181. end
  182. end
  183. -- effect only works when not riding boat/cart/horse etc.
  184. if not player:get_attach() then
  185. local yaw = player:get_look_horizontal() + math.random(-0.5, 0.5)
  186. player:set_look_horizontal(yaw)
  187. end
  188. end
  189. end
  190. end
  191. local function health_tick()
  192. for _,player in ipairs(minetest.get_connected_players()) do
  193. local name = player:get_player_name()
  194. if name then
  195. local air = player:get_breath() or 0
  196. local hp = player:get_hp()
  197. local h = get_int_attribute(player)
  198. -- damage player by 1 hp if saturation is < 2
  199. if h and h < STAMINA_STARVE_LVL
  200. and hp > 0 then
  201. player:set_hp(hp - STAMINA_STARVE, {hunger = true})
  202. end
  203. -- don't heal if drowning or dead or poisoned
  204. if h and h >= STAMINA_HEAL_LVL
  205. and h >= hp
  206. and hp > 0
  207. and air > 0
  208. and not stamina.players[name].poisoned then
  209. player:set_hp(hp + STAMINA_HEAL)
  210. stamina_update_level(player, h - 1)
  211. end
  212. end
  213. end
  214. end
  215. local function action_tick()
  216. for _,player in ipairs(minetest.get_connected_players()) do
  217. local controls = player and player:get_player_control()
  218. -- Determine if the player is walking or jumping
  219. if controls then
  220. if controls.jump then
  221. exhaust_player(player, STAMINA_EXHAUST_JUMP)
  222. elseif controls.up
  223. or controls.down
  224. or controls.left
  225. or controls.right then
  226. exhaust_player(player, STAMINA_EXHAUST_MOVE)
  227. end
  228. end
  229. --- START sprint
  230. if enable_sprint then
  231. local name = player and player:get_player_name()
  232. -- check if player can sprint (stamina must be over 6 points)
  233. if name
  234. and stamina.players[name]
  235. and not stamina.players[name].poisoned
  236. and not stamina.players[name].drunk
  237. and controls and controls.aux1 and controls.up
  238. and not minetest.check_player_privs(player, {fast = true})
  239. and get_int_attribute(player) > 6 then
  240. set_sprinting(name, true)
  241. -- create particles behind player when sprinting
  242. if enable_sprint_particles then
  243. local pos = player:get_pos()
  244. local node = minetest.get_node({
  245. x = pos.x,
  246. y = pos.y - 1,
  247. z = pos.z
  248. })
  249. if node.name ~= "air" then
  250. minetest.add_particlespawner({
  251. amount = 5,
  252. time = 0.01,
  253. minpos = {x = pos.x - 0.25, y = pos.y + 0.1, z = pos.z - 0.25},
  254. maxpos = {x = pos.x + 0.25, y = pos.y + 0.1, z = pos.z + 0.25},
  255. minvel = {x = -0.5, y = 1, z = -0.5},
  256. maxvel = {x = 0.5, y = 2, z = 0.5},
  257. minacc = {x = 0, y = -5, z = 0},
  258. maxacc = {x = 0, y = -12, z = 0},
  259. minexptime = 0.25,
  260. maxexptime = 0.5,
  261. minsize = 0.5,
  262. maxsize = 1.0,
  263. vertical = false,
  264. collisiondetection = false,
  265. texture = "default_dirt.png"
  266. })
  267. end
  268. end
  269. -- Lower the player's stamina when sprinting
  270. local level = get_int_attribute(player)
  271. stamina_update_level(player,
  272. level - (SPRINT_DRAIN * STAMINA_MOVE_TICK))
  273. elseif name then
  274. set_sprinting(name, false)
  275. end
  276. end
  277. -- END sprint
  278. end
  279. end
  280. local function poison_tick()
  281. for _,player in ipairs(minetest.get_connected_players()) do
  282. local name = player and player:get_player_name()
  283. if name
  284. and stamina.players[name]
  285. and stamina.players[name].poisoned
  286. and stamina.players[name].poisoned > 0 then
  287. stamina.players[name].poisoned =
  288. stamina.players[name].poisoned - 1
  289. local hp = player:get_hp() - 1
  290. head_particle(player, "stamina_poison_particle.png")
  291. if hp > 0 then
  292. player:set_hp(hp, {poison = true})
  293. end
  294. elseif name
  295. and stamina.players[name]
  296. and stamina.players[name].poisoned then
  297. if not stamina.players[name].drunk then
  298. player:hud_change(stamina.players[name].hud_id,
  299. "text", "stamina_hud_fg.png")
  300. end
  301. stamina.players[name].poisoned = nil
  302. end
  303. end
  304. end
  305. local function stamina_tick()
  306. for _,player in ipairs(minetest.get_connected_players()) do
  307. local h = player and get_int_attribute(player)
  308. if h and h > STAMINA_TICK_MIN then
  309. stamina_update_level(player, h - 1)
  310. end
  311. end
  312. end
  313. -- Time based stamina functions
  314. local stamina_timer, health_timer, action_timer, poison_timer, drunk_timer = 0,0,0,0,0
  315. local function stamina_globaltimer(dtime)
  316. stamina_timer = stamina_timer + dtime
  317. health_timer = health_timer + dtime
  318. action_timer = action_timer + dtime
  319. poison_timer = poison_timer + dtime
  320. drunk_timer = drunk_timer + dtime
  321. -- simulate drunk walking (thanks LumberJ)
  322. if drunk_timer > 1.0 then
  323. drunk_tick() ; drunk_timer = 0
  324. end
  325. -- hurt player when poisoned
  326. if poison_timer > STAMINA_POISON_TICK then
  327. poison_tick() ; poison_timer = 0
  328. end
  329. -- sprint control and particle animation
  330. if action_timer > STAMINA_MOVE_TICK then
  331. action_tick() ; action_timer = 0
  332. end
  333. -- lower saturation by 1 point after STAMINA_TICK
  334. if stamina_timer > STAMINA_TICK then
  335. stamina_tick() ; stamina_timer = 0
  336. end
  337. -- heal or damage player, depending on saturation
  338. if health_timer > STAMINA_HEALTH_TICK then
  339. health_tick() ; health_timer = 0
  340. end
  341. end
  342. -- stamina and eating functions disabled if damage is disabled
  343. if minetest.settings:get_bool("enable_damage")
  344. and minetest.settings:get_bool("enable_stamina") ~= false then
  345. -- override core.do_item_eat() so we can redirect hp_change to stamina
  346. core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
  347. if user.is_fake_player then
  348. return -- abort if called by fake player (eg. pipeworks-wielder)
  349. end
  350. local old_itemstack = itemstack
  351. itemstack = stamina.eat(
  352. hp_change, replace_with_item, itemstack, user, pointed_thing)
  353. for _, callback in pairs(core.registered_on_item_eats) do
  354. local result = callback(hp_change, replace_with_item, itemstack, user,
  355. pointed_thing, old_itemstack)
  356. if result then
  357. return result
  358. end
  359. end
  360. return itemstack
  361. end
  362. -- not local since it's called from within core context
  363. function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  364. if not itemstack or not user then
  365. return itemstack
  366. end
  367. local level = get_int_attribute(user) or 0
  368. if level >= STAMINA_VISUAL_MAX then
  369. return itemstack
  370. end
  371. local name = user:get_player_name()
  372. if hp_change > 0 then
  373. stamina_update_level(user, level + hp_change)
  374. elseif hp_change < 0 then
  375. -- assume hp_change < 0
  376. user:hud_change(stamina.players[name].hud_id, "text", "stamina_hud_poison.png")
  377. stamina.players[name].poisoned = -hp_change
  378. end
  379. -- if {drink=1} group set then use sip sound instead of default eat
  380. local snd = "stamina_eat"
  381. local itemname = itemstack:get_name()
  382. local def = minetest.registered_items[itemname]
  383. if def and def.groups and def.groups.drink then
  384. snd = "stamina_sip"
  385. end
  386. minetest.sound_play(snd, {to_player = name, gain = 0.7}, true)
  387. -- particle effect when eating
  388. local texture = minetest.registered_items[itemname].inventory_image
  389. head_particle(user, texture)
  390. -- if player drinks milk then stop poison and being drunk
  391. local item_name = itemstack:get_name() or ""
  392. if item_name == "mobs:bucket_milk"
  393. or item_name == "mobs:glass_milk"
  394. or item_name == "farming:soy_milk" then
  395. stamina.players[name].poisoned = 0
  396. stamina.players[name].drunk = 0
  397. end
  398. itemstack:take_item()
  399. if replace_with_item then
  400. if itemstack:is_empty() then
  401. itemstack:add_item(replace_with_item)
  402. else
  403. local inv = user:get_inventory()
  404. if inv:room_for_item("main", {name = replace_with_item}) then
  405. inv:add_item("main", replace_with_item)
  406. else
  407. local pos = user:get_pos()
  408. if pos then core.add_item(pos, replace_with_item) end
  409. end
  410. end
  411. end
  412. -- check for alcohol
  413. local units = minetest.registered_items[itemname].groups
  414. and minetest.registered_items[itemname].groups.alcohol or 0
  415. if units > 0 then
  416. stamina.players[name].units = (stamina.players[name].units or 0) + 1
  417. if stamina.players[name].units > 3 then
  418. stamina.players[name].drunk = 60
  419. stamina.players[name].units = 0
  420. user:hud_change(stamina.players[name].hud_id, "text",
  421. "stamina_hud_poison.png")
  422. minetest.chat_send_player(name,
  423. minetest.get_color_escape_sequence("#1eff00")
  424. .. "You suddenly feel tipsy!")
  425. end
  426. end
  427. return itemstack
  428. end
  429. minetest.register_on_joinplayer(function(player)
  430. if not player then return end
  431. local level = STAMINA_VISUAL_MAX -- TODO
  432. if get_int_attribute(player) then
  433. level = math.min(get_int_attribute(player), STAMINA_VISUAL_MAX)
  434. else
  435. local meta = player:get_meta()
  436. meta:set_string("stamina:level", level)
  437. end
  438. local name = player:get_player_name()
  439. local id = player:hud_add({
  440. name = "stamina",
  441. hud_elem_type = "statbar",
  442. position = {x = 0.5, y = 1},
  443. size = {x = 24, y = 24},
  444. text = "stamina_hud_fg.png",
  445. number = level,
  446. alignment = {x = -1, y = -1},
  447. offset = {x = -266, y = -110},
  448. max = 0
  449. })
  450. stamina.players[name] = {
  451. hud_id = id,
  452. exhaustion = 0,
  453. poisoned = nil,
  454. drunk = nil,
  455. sprint = nil
  456. }
  457. end)
  458. minetest.register_on_respawnplayer(function(player)
  459. local name = player:get_player_name() ; if not name then return end
  460. if stamina.players[name].poisoned
  461. or stamina.players[name].drunk then
  462. player:hud_change(stamina.players[name].hud_id, "text", "stamina_hud_fg.png")
  463. end
  464. stamina.players[name].exhaustion = 0
  465. stamina.players[name].poisoned = nil
  466. stamina.players[name].drunk = nil
  467. stamina.players[name].sprint = nil
  468. stamina_update_level(player, STAMINA_VISUAL_MAX)
  469. end)
  470. minetest.register_globalstep(stamina_globaltimer)
  471. minetest.register_on_placenode(function(pos, oldnode, player, ext)
  472. exhaust_player(player, STAMINA_EXHAUST_PLACE)
  473. end)
  474. minetest.register_on_dignode(function(pos, oldnode, player, ext)
  475. exhaust_player(player, STAMINA_EXHAUST_DIG)
  476. end)
  477. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  478. exhaust_player(player, STAMINA_EXHAUST_CRAFT)
  479. end)
  480. minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch,
  481. tool_capabilities, dir, damage)
  482. exhaust_player(hitter, STAMINA_EXHAUST_PUNCH)
  483. end)
  484. else
  485. -- create player table on join
  486. minetest.register_on_joinplayer(function(player)
  487. if player then
  488. stamina.players[player:get_player_name()] = {
  489. poisoned = nil, sprint = nil, drunk = nil, exhaustion = 0}
  490. end
  491. end)
  492. end
  493. -- clear when player leaves
  494. minetest.register_on_leaveplayer(function(player)
  495. if player then
  496. stamina.players[player:get_player_name()] = nil
  497. end
  498. end)
  499. -- add lucky blocks (if damage and stamina active)
  500. if minetest.get_modpath("lucky_block")
  501. and minetest.settings:get_bool("enable_damage")
  502. and minetest.settings:get_bool("enable_stamina") ~= false then
  503. local MP = minetest.get_modpath(minetest.get_current_modname())
  504. dofile(MP .. "/lucky_block.lua")
  505. end