Sign in
mod

Hunger

by Potluck · version 1 · Checked for all ages

A food bar beside your health in survival. When you get hungry you eat what you carry, each food as filling as it is: pies, stews, meat, bread, fish, baked potatoes, fried eggs, carrots, apples, berries, milk and honey. On an empty belly you stop healing, and it slowly hurts you down to half health.

1--!strict
2-- Hunger, for survival. A food bar sits beside your health bar and goes
3-- down slowly. When you get hungry you eat food you carry, a bite at a
4-- time: the most filling that fits (FOODS below says what's food and how
5-- filling each is, from a pie or stew down to raw wheat, and foods other
6-- creations make count too). On an empty belly you stop healing by
7-- yourself (world.healing) and it slowly hurts, but never below half your
8-- health (world.hurt's down_to), and armor doesn't help (guarded = false).
9-- When you're well fed you heal faster.
10
11local world = require("@potluck/world@0.25")
12local tweak = require("@potluck/tweak")
13local ui = require("@potluck/ui@0.2")
14local sound = require("@potluck/sound@0.1")
15
16local knobs = tweak {
17 minutes = { 20, min = 5, max = 60, label = "Minutes from full to empty", order = 1 },
18 color = { "#e0a040", label = "Food bar", order = 2 },
19 well_fed = { true, label = "Heal faster when well fed", order = 3 },
20 hurts = { true, label = "An empty belly hurts", order = 4 },
21 down_to = { 10, min = 1, max = 19, label = "It hurts you down to (of 20)", order = 5 },
22}
23
24local FULL = 20
25local HUNGRY = 12 -- eat when the bar is this low or lower
26local WELL_FED = 16 -- heal faster at this or more
27local HURT_EVERY = 5 -- seconds between hurts on an empty belly
28
29-- What's food and how filling each is, most filling first (but honey and
30-- milk last, kept for the cooking pot): a remix can add its own. `back` is
31-- what's left when it's eaten (the bucket milk was in).
32type Food = { name: string, fills: number, says: string, back: string? }
33local FOODS: { Food } = {
34 { name = "Honey cake", fills = 12, says = "You ate some honey cake." },
35 { name = "Pumpkin pie", fills = 11, says = "You ate some pumpkin pie." },
36 { name = "Fish stew", fills = 10, says = "You ate some fish stew." },
37 { name = "Berry pie", fills = 10, says = "You ate some berry pie." },
38 { name = "Bean stew", fills = 9, says = "You ate some bean stew." },
39 { name = "Cooked meat", fills = 9, says = "You ate some cooked meat." },
40 { name = "Cooked fish", fills = 8, says = "You ate some cooked fish." },
41 { name = "Bread", fills = 8, says = "You ate some bread." },
42 { name = "Porridge", fills = 8, says = "You ate some porridge." },
43 { name = "Baked potato", fills = 7, says = "You ate a baked potato." },
44 { name = "Roasted carrots", fills = 7, says = "You ate some roasted carrots." },
45 { name = "Fried egg", fills = 6, says = "You ate a fried egg." },
46 { name = "Carrots", fills = 6, says = "You ate some carrots." },
47 { name = "Apple", fills = 4, says = "You ate an apple." },
48 { name = "Berries", fills = 3, says = "You ate some berries." },
49 { name = "Beets", fills = 3, says = "You ate a raw beet." },
50 { name = "Beans", fills = 3, says = "You ate some beans." },
51 { name = "Raw fish", fills = 3, says = "You ate some raw fish." },
52 { name = "Raw meat", fills = 2, says = "You ate some raw meat. Cooked would be better." },
53 { name = "Potatoes", fills = 2, says = "You ate a raw potato." },
54 { name = "Wheat", fills = 2, says = "You chewed some wheat." },
55 -- last of all, since they're for cooking
56 { name = "Honey", fills = 4, says = "You ate some honey." },
57 { name = "Milk bucket", fills = 4, says = "You drank some milk.", back = "Bucket" },
58}
59
60local food: { [number]: number } = {}
61local told: { [number]: number } = {} -- when they were last told they're hungry
62local empty: { [number]: number } = {} -- seconds on an empty belly
63
64-- Whether a food is in this world: potatoes, beans and eggs come from
65-- other creations (Farming, Hens), so a world without them has none.
66local function exists(name: string): boolean
67 return (pcall(world.kind, name))
68end
69
70-- FOODS and the foods other creations made ([items.*] with kind = "food",
71-- world.item), each made one by how filling it is, before honey and milk;
72-- only those in this world. Worked out again every MENU_EVERY seconds, as
73-- creations come and go, rather than for every bite.
74local MENU_EVERY = 10
75local menu_list: { Food }? = nil
76local menu_at = -math.huge
77local function menu(): { Food }
78 local now = world.time()
79 if menu_list and now - menu_at < MENU_EVERY then
80 return menu_list
81 end
82 local list = table.clone(FOODS)
83 for _, name in world.items() do
84 local it = world.item(name)
85 if it and it.kind == "food" then
86 local fills = it.food or 4
87 local at = #list + 1
88 for i, kind in list do
89 if kind.fills < fills or kind.name == "Honey" then
90 at = i
91 break
92 end
93 end
94 table.insert(list, at, { name = name, fills = fills, says = it.says or ("You ate some " .. string.lower(name) .. ".") })
95 end
96 end
97 local here: { Food } = {}
98 for _, kind in list do
99 if exists(kind.name) then
100 table.insert(here, kind)
101 end
102 end
103 menu_list, menu_at = here, now
104 return here
105end
106
107-- has a bite of something that fits, if they carry any. What they carry
108-- is read once (`pockets`): asking world.has about every food for each of
109-- fifty hungry players was most of a tick on a busy server.
110local function eat(p, f: number, pockets: { [string]: number }): number
111 for _, kind in menu() do
112 if f + kind.fills <= FULL and (pockets[kind.name] or 0) >= 1 and world.take(p, kind.name, 1) then
113 if kind.back then
114 world.give(p, kind.back, 1)
115 end
116 ui.show(p, "ate", kind.says, { at = "bottom" })
117 world.after(3, function()
118 ui.hide(p, "ate")
119 end)
120 sound.play_for(p, "crack", { volume = 0.4, caption = "Crunch" })
121 return f + kind.fills
122 end
123 end
124 return f
125end
126
127world.every(1, function()
128 local now = world.time()
129 for _, p in world.players() do
130 local pockets = world.pockets(p)
131 if pockets == nil then
132 -- creative: no hunger
133 if food[p.id] then
134 food[p.id] = nil
135 ui.meter(p, "food", nil)
136 world.healing(p, 1)
137 end
138 continue
139 end
140 local f = (food[p.id] or FULL) - FULL / (knobs.minutes * 60)
141 f = math.max(f, 0)
142 if f <= HUNGRY then
143 f = eat(p, f, pockets)
144 end
145 food[p.id] = f
146 -- the bar moves in whole steps, so it isn't sent every second
147 ui.meter(p, "food", math.ceil(f) / FULL, { color = knobs.color })
148 if f <= 0 then
149 world.healing(p, 0)
150 empty[p.id] = (empty[p.id] or 0) + 1
151 if knobs.hurts and empty[p.id] % HURT_EVERY == 0 then
152 world.hurt(p, 1, { down_to = knobs.down_to, guarded = false })
153 end
154 if now - (told[p.id] or -1000) > 120 then
155 told[p.id] = now
156 local says = if knobs.hurts
157 then "You're starving. Carry food and you'll eat: bread, carrots, berries, apples."
158 else "You're hungry, so you won't heal. Carry food: bread, carrots, berries, apples."
159 ui.show(p, "hungry", says, { at = "left" })
160 world.after(8, function()
161 ui.hide(p, "hungry")
162 end)
163 end
164 else
165 empty[p.id] = nil
166 if f >= WELL_FED and knobs.well_fed then
167 world.healing(p, 2)
168 else
169 world.healing(p, 1)
170 end
171 end
172 end
173end)
174
175world.on("player_left", function(p)
176 food[p.id] = nil
177 told[p.id] = nil
178 empty[p.id] = nil
179end)