init.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 drunk_tick()
  140. for _,player in ipairs(minetest.get_connected_players()) do
  141. local name = player:get_player_name()
  142. if name
  143. and stamina.players[name]
  144. and stamina.players[name].drunk then
  145. -- play burp sound every 20 seconds when drunk
  146. local num = stamina.players[name].drunk
  147. if num and num > 0 and math.floor(num / 20) == num / 20 then
  148. minetest.sound_play("stamina_burp",
  149. {to_player = name, gain = 0.7}, true)
  150. end
  151. stamina.players[name].drunk = stamina.players[name].drunk - 1
  152. if stamina.players[name].drunk < 1 then
  153. stamina.players[name].drunk = nil
  154. stamina.players[name].units = 0
  155. if not stamina.players[name].poisoned then
  156. player:hud_change(stamina.players[name].hud_id,
  157. "text", "stamina_hud_fg.png")
  158. end
  159. end
  160. -- effect only works when not riding boat/cart/horse etc.
  161. if not player:get_attach() then
  162. local yaw = player:get_look_horizontal() + math.random(-0.5, 0.5)
  163. player:set_look_horizontal(yaw)
  164. end
  165. end
  166. end
  167. end
  168. local function health_tick()
  169. for _,player in ipairs(minetest.get_connected_players()) do
  170. local air = player:get_breath() or 0
  171. local hp = player:get_hp()
  172. local h = get_int_attribute(player)
  173. local name = player:get_player_name()
  174. if name then
  175. -- damage player by 1 hp if saturation is < 2
  176. if h and h < STAMINA_STARVE_LVL
  177. and hp > 0 then
  178. player:set_hp(hp - STAMINA_STARVE, {hunger = true})
  179. end
  180. -- don't heal if drowning or dead or poisoned
  181. if h and h >= STAMINA_HEAL_LVL
  182. and h >= hp
  183. and hp > 0
  184. and air > 0
  185. and not stamina.players[name].poisoned then
  186. player:set_hp(hp + STAMINA_HEAL)
  187. stamina_update_level(player, h - 1)
  188. end
  189. end
  190. end
  191. end
  192. local function action_tick()
  193. for _,player in ipairs(minetest.get_connected_players()) do
  194. local controls = player and player:get_player_control()
  195. -- Determine if the player is walking or jumping
  196. if controls then
  197. if controls.jump then
  198. exhaust_player(player, STAMINA_EXHAUST_JUMP)
  199. elseif controls.up
  200. or controls.down
  201. or controls.left
  202. or controls.right then
  203. exhaust_player(player, STAMINA_EXHAUST_MOVE)
  204. end
  205. end
  206. --- START sprint
  207. if enable_sprint then
  208. local name = player and player:get_player_name()
  209. -- check if player can sprint (stamina must be over 6 points)
  210. if name
  211. and stamina.players[name]
  212. and not stamina.players[name].poisoned
  213. and not stamina.players[name].drunk
  214. and controls and controls.aux1 and controls.up
  215. and not minetest.check_player_privs(player, {fast = true})
  216. and get_int_attribute(player) > 6 then
  217. set_sprinting(name, true)
  218. -- create particles behind player when sprinting
  219. if enable_sprint_particles then
  220. local pos = player:get_pos()
  221. local node = minetest.get_node({
  222. x = pos.x,
  223. y = pos.y - 1,
  224. z = pos.z
  225. })
  226. if node.name ~= "air" then
  227. minetest.add_particlespawner({
  228. amount = 5,
  229. time = 0.01,
  230. minpos = {x = pos.x - 0.25, y = pos.y + 0.1, z = pos.z - 0.25},
  231. maxpos = {x = pos.x + 0.25, y = pos.y + 0.1, z = pos.z + 0.25},
  232. minvel = {x = -0.5, y = 1, z = -0.5},
  233. maxvel = {x = 0.5, y = 2, z = 0.5},
  234. minacc = {x = 0, y = -5, z = 0},
  235. maxacc = {x = 0, y = -12, z = 0},
  236. minexptime = 0.25,
  237. maxexptime = 0.5,
  238. minsize = 0.5,
  239. maxsize = 1.0,
  240. vertical = false,
  241. collisiondetection = false,
  242. texture = "default_dirt.png"
  243. })
  244. end
  245. end
  246. -- Lower the player's stamina when sprinting
  247. local level = get_int_attribute(player)
  248. stamina_update_level(player,
  249. level - (SPRINT_DRAIN * STAMINA_MOVE_TICK))
  250. elseif name then
  251. set_sprinting(name, false)
  252. end
  253. end
  254. -- END sprint
  255. end
  256. end
  257. local function poison_tick()
  258. for _,player in ipairs(minetest.get_connected_players()) do
  259. local name = player and player:get_player_name()
  260. if name
  261. and stamina.players[name]
  262. and stamina.players[name].poisoned
  263. and stamina.players[name].poisoned > 0 then
  264. stamina.players[name].poisoned =
  265. stamina.players[name].poisoned - 1
  266. local hp = player:get_hp() - 1
  267. if hp > 0 then
  268. player:set_hp(hp, {poison = true})
  269. end
  270. elseif name
  271. and stamina.players[name]
  272. and stamina.players[name].poisoned then
  273. if not stamina.players[name].drunk then
  274. player:hud_change(stamina.players[name].hud_id,
  275. "text", "stamina_hud_fg.png")
  276. end
  277. stamina.players[name].poisoned = nil
  278. end
  279. end
  280. end
  281. local function stamina_tick()
  282. for _,player in ipairs(minetest.get_connected_players()) do
  283. local h = get_int_attribute(player)
  284. if h and h > STAMINA_TICK_MIN then
  285. stamina_update_level(player, h - 1)
  286. end
  287. end
  288. end
  289. -- Time based stamina functions
  290. local stamina_timer, health_timer, action_timer, poison_timer, drunk_timer = 0,0,0,0,0
  291. local function stamina_globaltimer(dtime)
  292. stamina_timer = stamina_timer + dtime
  293. health_timer = health_timer + dtime
  294. action_timer = action_timer + dtime
  295. poison_timer = poison_timer + dtime
  296. drunk_timer = drunk_timer + dtime
  297. -- simulate drunk walking (thanks LumberJ)
  298. if drunk_timer > 1.0 then
  299. drunk_tick() ; drunk_timer = 0
  300. end
  301. -- hurt player when poisoned
  302. if poison_timer > STAMINA_POISON_TICK then
  303. poison_tick() ; poison_timer = 0
  304. end
  305. -- sprint control and particle animation
  306. if action_timer > STAMINA_MOVE_TICK then
  307. action_tick() ; action_timer = 0
  308. end
  309. -- lower saturation by 1 point after STAMINA_TICK
  310. if stamina_timer > STAMINA_TICK then
  311. stamina_tick() ; stamina_timer = 0
  312. end
  313. -- heal or damage player, depending on saturation
  314. if health_timer > STAMINA_HEALTH_TICK then
  315. health_tick() ; health_timer = 0
  316. end
  317. end
  318. -- override core.do_item_eat() so we can redirect hp_change to stamina
  319. core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
  320. if user.is_fake_player then
  321. return -- abort if called by fake player (eg. pipeworks-wielder)
  322. end
  323. local old_itemstack = itemstack
  324. itemstack = stamina.eat(
  325. hp_change, replace_with_item, itemstack, user, pointed_thing)
  326. for _, callback in pairs(core.registered_on_item_eats) do
  327. local result = callback(hp_change, replace_with_item, itemstack, user,
  328. pointed_thing, old_itemstack)
  329. if result then
  330. return result
  331. end
  332. end
  333. return itemstack
  334. end
  335. -- not local since it's called from within core context
  336. function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
  337. if not itemstack or not user then
  338. return itemstack
  339. end
  340. local level = get_int_attribute(user) or 0
  341. if level >= STAMINA_VISUAL_MAX then
  342. return itemstack
  343. end
  344. local name = user:get_player_name()
  345. if hp_change > 0 then
  346. stamina_update_level(user, level + hp_change)
  347. elseif hp_change < 0 then
  348. -- assume hp_change < 0
  349. user:hud_change(stamina.players[name].hud_id, "text", "stamina_hud_poison.png")
  350. stamina.players[name].poisoned = -hp_change
  351. end
  352. -- if {drink=1} group set then use sip sound instead of default eat
  353. local snd = "stamina_eat"
  354. local itemname = itemstack:get_name()
  355. local def = minetest.registered_items[itemname]
  356. if def and def.groups and def.groups.drink then
  357. snd = "stamina_sip"
  358. end
  359. minetest.sound_play(snd, {to_player = name, gain = 0.7}, true)
  360. -- particle effect when eating
  361. local prop = user:get_properties()
  362. local pos = user:get_pos() ; pos.y = pos.y + prop.eye_height -- mouth level
  363. local texture = minetest.registered_items[itemname].inventory_image
  364. local dir = user:get_look_dir()
  365. minetest.add_particlespawner({
  366. amount = 5,
  367. time = 0.1,
  368. minpos = pos,
  369. maxpos = pos,
  370. minvel = {x = dir.x - 1, y = dir.y, z = dir.z - 1},
  371. maxvel = {x = dir.x + 1, y = dir.y, z = dir.z + 1},
  372. minacc = {x = 0, y = -5, z = 0},
  373. maxacc = {x = 0, y = -9, z = 0},
  374. minexptime = 1,
  375. maxexptime = 1,
  376. minsize = 1,
  377. maxsize = 2,
  378. texture = texture
  379. })
  380. -- if player drinks milk then stop poison and being drunk
  381. local item_name = itemstack:get_name() or ""
  382. if item_name == "mobs:bucket_milk"
  383. or item_name == "mobs:glass_milk"
  384. or item_name == "farming:soy_milk" then
  385. stamina.players[name].poisoned = 0
  386. stamina.players[name].drunk = 0
  387. end
  388. itemstack:take_item()
  389. if replace_with_item then
  390. if itemstack:is_empty() then
  391. itemstack:add_item(replace_with_item)
  392. else
  393. local inv = user:get_inventory()
  394. if inv:room_for_item("main", {name = replace_with_item}) then
  395. inv:add_item("main", replace_with_item)
  396. else
  397. pos.y = math.floor(pos.y - 1.0)
  398. core.add_item(pos, replace_with_item)
  399. end
  400. end
  401. end
  402. -- check for alcohol
  403. local units = minetest.registered_items[itemname].groups
  404. and minetest.registered_items[itemname].groups.alcohol or 0
  405. if units > 0 then
  406. stamina.players[name].units = (stamina.players[name].units or 0) + 1
  407. if stamina.players[name].units > 3 then
  408. stamina.players[name].drunk = 60
  409. stamina.players[name].units = 0
  410. user:hud_change(stamina.players[name].hud_id, "text",
  411. "stamina_hud_poison.png")
  412. minetest.chat_send_player(name,
  413. minetest.get_color_escape_sequence("#1eff00")
  414. .. "You suddenly feel tipsy!")
  415. end
  416. end
  417. return itemstack
  418. end
  419. -- stamina is disabled if damage is disabled
  420. if minetest.settings:get_bool("enable_damage")
  421. and minetest.settings:get_bool("enable_stamina") ~= false then
  422. minetest.register_on_joinplayer(function(player)
  423. local level = STAMINA_VISUAL_MAX -- TODO
  424. if get_int_attribute(player) then
  425. level = math.min(get_int_attribute(player), STAMINA_VISUAL_MAX)
  426. else
  427. local meta = player:get_meta()
  428. meta:set_string("stamina:level", level)
  429. end
  430. local name = player:get_player_name()
  431. local id = player:hud_add({
  432. name = "stamina",
  433. hud_elem_type = "statbar",
  434. position = {x = 0.5, y = 1},
  435. size = {x = 24, y = 24},
  436. text = "stamina_hud_fg.png",
  437. number = level,
  438. alignment = {x = -1, y = -1},
  439. offset = {x = -266, y = -110},
  440. max = 0
  441. })
  442. stamina.players[name] = {
  443. hud_id = id,
  444. exhaustion = 0,
  445. poisoned = nil,
  446. drunk = nil,
  447. sprint = nil
  448. }
  449. end)
  450. minetest.register_on_respawnplayer(function(player)
  451. local name = player:get_player_name()
  452. if stamina.players[name].poisoned
  453. or stamina.players[name].drunk then
  454. player:hud_change(stamina.players[name].hud_id, "text", "stamina_hud_fg.png")
  455. end
  456. stamina.players[name].exhaustion = 0
  457. stamina.players[name].poisoned = nil
  458. stamina.players[name].drunk = nil
  459. stamina.players[name].sprint = nil
  460. stamina_update_level(player, STAMINA_VISUAL_MAX)
  461. end)
  462. minetest.register_globalstep(stamina_globaltimer)
  463. minetest.register_on_placenode(function(pos, oldnode, player, ext)
  464. exhaust_player(player, STAMINA_EXHAUST_PLACE)
  465. end)
  466. minetest.register_on_dignode(function(pos, oldnode, player, ext)
  467. exhaust_player(player, STAMINA_EXHAUST_DIG)
  468. end)
  469. minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
  470. exhaust_player(player, STAMINA_EXHAUST_CRAFT)
  471. end)
  472. minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch,
  473. tool_capabilities, dir, damage)
  474. exhaust_player(hitter, STAMINA_EXHAUST_PUNCH)
  475. end)
  476. else
  477. -- create player table on join
  478. minetest.register_on_joinplayer(function(player)
  479. stamina.players[player:get_player_name()] = {
  480. poisoned = nil, sprint = nil, drunk = nil, exhaustion = 0}
  481. end)
  482. end
  483. -- clear when player leaves
  484. minetest.register_on_leaveplayer(function(player)
  485. stamina.players[player:get_player_name()] = nil
  486. end)
  487. --lucky blocks (if damage and stamina active)
  488. if minetest.get_modpath("lucky_block")
  489. and minetest.settings:get_bool("enable_damage")
  490. and minetest.settings:get_bool("enable_stamina") ~= false then
  491. local effect_me = function(pos, player, def)
  492. local green = minetest.get_color_escape_sequence("#bada55")
  493. local name = player:get_player_name()
  494. if def.poison or def.drunk then
  495. player:hud_change(stamina.players[name].hud_id,
  496. "text", "stamina_hud_poison.png")
  497. end
  498. if def.poison and def.poison > 0 then
  499. stamina.players[name].poisoned = def.poison
  500. minetest.chat_send_player(name,
  501. green .. "Seems you have been poisoned!")
  502. elseif def.drunk and def.drunk > 0 then
  503. stamina.players[name].drunk = def.drunk
  504. minetest.chat_send_player(name,
  505. green .. "You seem a little tipsy!")
  506. end
  507. end
  508. lucky_block:add_blocks({
  509. {"cus", effect_me, {poison = 5} },
  510. {"cus", effect_me, {poison = 10} },
  511. {"cus", effect_me, {drunk = 30} },
  512. })
  513. end