init.lua 17 KB

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