-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinit.lua
202 lines (183 loc) · 6.36 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
local THIRST_TIME = survival.conf_getnum("thirst.start_time", 720);
local PASS_OUT_TIME = survival.conf_getnum("thirst.pass_out_time", 300);
local DTIME = survival.conf_getnum("thirst.check_interval", 0.5);
-- Boilerplate to support localized strings if intllib mod is installed.
local S;
if (minetest.get_modpath("intllib")) then
dofile(minetest.get_modpath("intllib").."/intllib.lua");
S = intllib.Getter(minetest.get_current_modname());
else
S = function ( s ) return s; end
end
local timer = 0;
minetest.register_craftitem("survival_thirst:water_glass", {
description = S("Glass of Water");
inventory_image = "survival_thirst_water_glass.png";
groups = { drink=1; survival_no_override=1; };
stack_max = 10;
on_use = function ( itemstack, user, pointed_thing )
local state = survival.get_player_state(user:get_player_name(), "thirst");
state.count = 0;
state.thirsty = false;
minetest.sound_play({ name="survival_thirst_drink" }, {
to_player = user:getpos();
gain = 1.0;
});
local inv = user:get_inventory();
local stack = ItemStack("vessels:drinking_glass");
if (inv:room_for_item("main", stack)) then
inv:add_item("main", stack);
end
itemstack:take_item(1);
return itemstack;
end;
});
local alt_water_sources = {
["3dforniture:sink"] = true;
["homedecor:kitchen_cabinet_with_sink"] = true;
["default:water_source"] = true;
["default:water_flowing"] = true;
};
minetest.register_craftitem(":vessels:drinking_glass", {
--Or use minetest.registered_items[vessels:drinking_glass] for all parametre.
description = S("Drinking Glass (empty)"),
drawtype = "plantlike",
tiles = {"vessels_drinking_glass.png"},
inventory_image = "vessels_drinking_glass_inv.png",
wield_image = "vessels_drinking_glass.png",
paramtype = "light",
walkable = false,
selection_box = {
type = "fixed",
fixed = {-0.25, -0.5, -0.25, 0.25, 0.4, 0.25}
},
groups = {vessel=1,dig_immediate=3,attached_node=1},
sounds = default.node_sound_glass_defaults(),
liquids_pointable = true,
on_use = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then
return
end
local node = minetest.get_node(pointed_thing.under)
if alt_water_sources[node.name] then
local newitem = ItemStack("survival_thirst:water_glass 1");
local inv = user:get_inventory();
if (inv:room_for_item("main", newitem)) then
inv:add_item("main", newitem);
if not(minetest.registered_items[node.name].liquidtype=="none") then
minetest.remove_node(pointed_thing.under);
end
itemstack:take_item();
return itemstack
end
end
end,
})
minetest.register_craft({
output = "survival_thirst:water_glass";
type = "shapeless";
recipe = {
"vessels:drinking_glass",
"bucket:bucket_water",
};
replacements = {
{ "bucket:bucket_water", "bucket:bucket_empty" },
};
});
-- Known drink items (more suggestions are welcome)
local known_drinks = {
-- This very mod --
--"survival_thirst:water_glass",
-- rubenwardy's food --
"food:apple_juice", "food:cactus_juice",
-- Farming Redo by TenPlus1
"farming:coffee_cup", "farming:coffee_cup_hot",
"farming:smoothie_raspberry",
};
local function override_on_use ( def )
local on_use = def.on_use;
def.on_use = function ( itemstack, user, pointed_thing )
local state = survival.get_player_state(user:get_player_name(), "thirst");
minetest.sound_play({ name="survival_thirst_drink" }, {
to_player = user:getpos();
gain = 1.0;
});
if (on_use) then
return on_use(itemstack, user, pointed_thing);
else
itemstack:take_item(1);
return itemstack;
end
end
end
-- Try to override the on_use callback of as many food items as possible.
minetest.after(1, function ( )
for _,name in ipairs(known_drinks) do
local def = minetest.registered_items[name] or minetest.registered_nodes[name];
if (def) then
if ((not def.groups.survival_no_override) or (def.groups.survival_no_override == 0)) then
override_on_use(def);
end
end
end
for name, def in pairs(minetest.registered_items) do
if (def.groups and def.groups.drink and (def.groups.drink > 0)) then
if ((not def.groups.survival_no_override) or (def.groups.survival_no_override == 0)) then
override_on_use(def);
end
end
end
end);
survival.register_state("thirst", {
label = S("Thirst");
hud = {
pos = {x=0.5, y=0.9};
offset = {x=-10, y=-15};
image = "survival_thirst_hud_water_glass.png";
};
get_default = function ( hudidn )
return {
hudid = hudidn;
count = 0;
thirsty = false;
};
end;
get_scaled_value = function ( state )
if (state.thirsty) then
return 0;
else
return 100 * (THIRST_TIME - state.count) / THIRST_TIME;
end
end;
on_update = function ( dtime, player, state )
if (player:get_hp() > 0) then
state.count = state.count + dtime;
local name = player:get_player_name();
if (state.thirsty and (state.count >= PASS_OUT_TIME)) then
state.count = 0;
state.thirsty = false;
if (player:get_hp() > 0) then
minetest.chat_send_player(name, S("You died from dehydration."));
end
player:set_hp(0);
minetest.sound_play({ name="survival_thirst_pass_out" }, {
pos = player:getpos();
gain = 1.0;
max_hear_distance = 16;
});
elseif ((not state.thirsty) and (state.count >= THIRST_TIME)) then
state.count = 0;
state.thirsty = true;
minetest.sound_play({ name="survival_thirst_thirst" }, {
pos = player:getpos();
gain = 1.0;
max_hear_distance = 16;
});
minetest.chat_send_player(name, S("You are thirsty."));
end
end
end;
});
minetest.register_on_dieplayer(function ( player )
survival.reset_player_state(player:get_player_name(), "thirst");
end);