-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
449 lines (381 loc) · 12.6 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
local mod_storage = minetest.get_mod_storage()
local snow = {} --the class
snow.playertable = {} --the player table
local is_snowing = false
local is_raining = false
local heavy_percip = false
--a note :
local changeupdate = 2 --checks if player position changed every x seconds
local snowrange = 40 --the visual of the snow and deposit spawn radius
local snowrange_height = 8 --the height of snow -y +y
local deposit_chance = 0.95 --uses math.random(deposit_chance) to deposit snow on each loop
-----------------------------------------------------------------------------------------
--texture tile style (strange)
minetest.register_node("snow:snowfall", {
description = "H@CK3R",
drawtype = "plantlike",
visual_scale = 1.4,
waving = true,
--tiles = {"default_papyrus.png"},
tiles = {
{
name = "snowfallhd.png",
animation = {
type = "vertical_frames",
aspect_w = 64,
aspect_h = 64,
length = 3,
},
},
},
inventory_image = "default_papyrus.png",
wield_image = "default_papyrus.png",
paramtype = "light",
pointable = false,
diggable = false,
buildable_to = true,
floodable = true,
sunlight_propagates = true,
walkable = false,
selection_box = {
type = "fixed",
fixed = {0, 0, 0, 0, 0, 0},
},
groups = {snow=1},
})
-----------------------------------------------------------------------------------------------
minetest.register_node("snow:rainfall", {
description = "H@CK3R",
drawtype = "plantlike",
visual_scale = 1.4,
waving = true,
--tiles = {"default_papyrus.png"},
tiles = {
{
name = "rainfall.png",
animation = {
type = "vertical_frames",
aspect_w = 256,
aspect_h = 256,
length = 1,
},
},
},
inventory_image = "default_papyrus.png",
wield_image = "default_papyrus.png",
paramtype = "light",
pointable = false,
diggable = false,
buildable_to = true,
floodable = true,
sunlight_propagates = true,
walkable = false,
selection_box = {
type = "fixed",
fixed = {0, 0, 0, 0, 0, 0},
},
groups = {snow=1},
})
--------------------------------------------------------------------
--check if player has moved nodes
local changetimer = 0
local old_weather = 0
minetest.register_globalstep(function(dtime)
--add to timer to prevent extreme lag
changetimer = changetimer + dtime
if changetimer >= changeupdate then
for _,player in pairs(minetest.get_connected_players()) do
if player:get_player_name() then
--print("checking "..player:get_player_name())
changetimer = 0
--only snow if snowing
--get required info
--snow or rain
local oldpos = snow.playertable[player:get_player_name()]
if is_snowing == true or is_raining == true then
local playerpos = player:getpos()
local exactplayerpos = {x=math.floor(playerpos.x+0.5),y=math.floor(playerpos.y+0.5),z=math.floor(playerpos.z+0.5)}
--if position moved clear old weather
if oldpos.x ~= exactplayerpos.x or oldpos.y ~= exactplayerpos.y or oldpos.z ~= exactplayerpos.z then
if oldpos.x then --stop crashes
snow.clear_old_snow(oldpos)
snow.set_ms_pos(exactplayerpos,player)
end
--print("clearing old weather")
end
--then make new weather
snow.make_snow_around_player(exactplayerpos)
--make weather form in world (snow on ground, puddles)
snow.make_snow_fall(exactplayerpos)
--this acts as old position
snow.playertable[player:get_player_name()] = exactplayerpos
old_weather = 1 --do this so that it doesn't try to clear weather every loop
--clear up snow
elseif old_weather == 1 then
local playerpos = player:getpos()
local exactplayerpos = {x=math.floor(playerpos.x+0.5),y=math.floor(playerpos.y+0.5),z=math.floor(playerpos.z+0.5)}
snow.clear_old_snow(oldpos)
--this acts as old position
snow.playertable[player:get_player_name()] = exactplayerpos
old_weather = 0
end
end
end
end
end)
---------------------------------------------------------------------------
--this checks and makes snow fall nodes
snow.make_snow_around_player = function(pos)
print("making snow")
local range = snowrange
local height = snowrange_height
local air = minetest.get_content_id("air")
local snowy = minetest.get_content_id("snow:snowfall")
local rain = minetest.get_content_id("snow:rainfall")
local water = minetest.get_content_id("default:water_source")
local min = {x=pos.x-range,y=pos.y-range,z=pos.z-range}
local max = {x=pos.x+range,y=pos.y+range,z=pos.z+range}
local vm = minetest.get_voxel_manip()
local emin, emax = vm:read_from_map(min,max)
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
local lightdata = vm:get_light_data()
local content_id = minetest.get_name_from_content_id
--this sets if snow or rain
local percip
if is_snowing == true then
percip = snowy
elseif is_raining == true then
percip = rain
end
--this reconverts back to namestring for quick snow removal
local ctester = minetest.get_name_from_content_id(percip)
for x=-range, range do
for y=-height, height do
for z=-range, range do
if vector.distance(pos, vector.add(pos, {x=x, y=y, z=z})) <= range then
local p_pos = area:index(pos.x+x,pos.y+y,pos.z+z)
local n = content_id(data[p_pos])
local l = lightdata[p_pos]
--sets the data
--also autoremove old weather
if (n == "air" or (ctester == "snow:snowfall" and n == "snow:rainfall") or (ctester == "snow:rainfall" and n == "snow:snowfall")) and l >= 15 then
local lightleveltest = minetest.get_node_light({x=pos.x+x,y=pos.y+y,z=pos.z+z}, 0.5)
--print(lightleveltest)
if lightleveltest and lightleveltest >= 15 then
data[p_pos] = percip
else
data[p_pos] = air
end
elseif (n == "snow:snowfall" and l < 15) or (n == "snow:rainfall" and l < 15) then
data[p_pos] = air --this makes the snow adapt to the environment
end
end
end
end
end
vm:set_data(data)
vm:write_to_map()
end
--this function actually puts snow and rain on the ground
snow.make_snow_fall = function(pos)
local range = snowrange
local height = snowrange_height
local air = minetest.get_content_id("air")
local snowy = minetest.get_content_id("snow:snowfall")
local rain = minetest.get_content_id("snow:rainfall")
local water = minetest.get_content_id("default:water_source")
local snow_node = minetest.get_content_id("default:snow")
local water_node = minetest.get_content_id("default:water_flowing")
local min = {x=pos.x-range,y=pos.y-range,z=pos.z-range}
local max = {x=pos.x+range,y=pos.y+range,z=pos.z+range}
local vm = minetest.get_voxel_manip()
local emin, emax = vm:read_from_map(min,max)
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
local content_id = minetest.get_name_from_content_id
--this sets if snow or rain
local percip
local deposit_block
local param2er
if is_snowing == true then
percip = snowy
deposit_block = snow_node
elseif is_raining == true then
percip = rain
deposit_block = water_node
param2er = 3
end
--save resources
local p2data
if param2er then
p2data = vm:get_param2_data()
end
----[[ minetest.set_node(pos,{name="default:water_flowing",param2=3}) THE PARAM2 OF WATER ]]-------------------------------HEY
--this reconverts back to namestring for quick snow removal
local ctester = minetest.get_name_from_content_id(percip)
for x=-range, range do
for y=-height, height do
for z=-range, range do
if vector.distance(pos, vector.add(pos, {x=x, y=y, z=z})) <= range then
--deposit snow randomly
local test_deposit_chance = deposit_chance
if heavy_percip == true then
test_deposit_chance = test_deposit_chance - 0.25 -- heavy percip
end
if math.random() > test_deposit_chance then
--the actual node being indexed
local p_pos = area:index(pos.x+x,pos.y+y,pos.z+z)
local n = content_id(data[p_pos])
--the node above it (testing for place for snow and water)
local p_pos_above = area:index(pos.x+x,pos.y+y+1,pos.z+z)
local n_above = content_id(data[p_pos_above])
--also autoremove old weather
if n ~= ctester and n_above == ctester then
--print(lightleveltest)
--data[p_pos] = percip
if n ~= "air" and n ~= "snow:snowfall" and minetest.get_item_group(n, "liquid") == 0 and n ~= "default:snow" and minetest.registered_nodes[n]["buildable_to"] == false then
data[p_pos_above] = deposit_block
if param2er then
p2data[p_pos_above] = param2er
end
--replace node if buildable to
elseif n ~= "air" and n ~= "snow:snowfall" and minetest.get_item_group(n, "liquid") == 0 and n ~= "default:snow" and minetest.registered_nodes[n]["buildable_to"] == true then
data[p_pos] = deposit_block
if param2er then
p2data[p_pos] = param2er
end
end
end
end
end
end
end
end
vm:set_data(data)
if param2er then
vm:set_param2_data(p2data)
end
vm:write_to_map()
end
--this checks and removes old nodes
snow.clear_old_snow = function(pos)
local range = snowrange
local height = snowrange_height
local air = minetest.get_content_id("air")
local snowy = minetest.get_content_id("snow:snowfall")
local rain = minetest.get_content_id("snow:rainfall")
local min = {x=pos.x-range,y=pos.y-range,z=pos.z-range}
local max = {x=pos.x+range,y=pos.y+range,z=pos.z+range}
local vm = minetest.get_voxel_manip()
local emin, emax = vm:read_from_map(min,max)
local area = VoxelArea:new{MinEdge=emin, MaxEdge=emax}
local data = vm:get_data()
local content_id = minetest.get_name_from_content_id
for x=-range, range do
for y=-height, height do
for z=-range, range do
if vector.distance(pos, vector.add(pos, {x=x, y=y, z=z})) <= range then
local p_pos = area:index(pos.x+x,pos.y+y,pos.z+z)
local n = content_id(data[p_pos])
--sets the data
if n == "snow:snowfall" or n == "snow:rainfall" then
data[p_pos] = air --this clears old snow
end
end
end
end
end
vm:set_data(data)
vm:write_to_map()
end
--commands
minetest.register_chatcommand("rain", {
params = "<text>",
description = "make it rain",
privs = {server = true},
func = function( _ , text)
is_raining = true
is_snowing = false
heavy_percip = false
end,
})
minetest.register_chatcommand("snow", {
params = "<text>",
description = "make it rain",
privs = {server = true},
func = function( _ , text)
is_raining = false
is_snowing = true
heavy_percip = false
end,
})
minetest.register_chatcommand("clear", {
params = "<text>",
description = "make it rain",
privs = {server = true},
func = function( _ , text)
is_raining = false
is_snowing = false
heavy_percip = false
end,
})
minetest.register_chatcommand("snowstorm", {
params = "<text>",
description = "make it rain",
privs = {server = true},
func = function( _ , text)
is_raining = false
is_snowing = true
heavy_percip = true
end,
})
minetest.register_chatcommand("rainstorm", {
params = "<text>",
description = "make it rain",
privs = {server = true},
func = function( _ , text)
is_raining = true
is_snowing = false
heavy_percip = true
end,
})
------------------------------------- additional helpers
--remove snow when player leaves!
minetest.register_on_leaveplayer(function(player)
print("clearing "..player:get_player_name().."'s weather")
--clear old snow in current and last position(if there)
local playerpos = player:getpos()
local exactplayerpos = {x=math.floor(playerpos.x+0.5),y=math.floor(playerpos.y+0.5),z=math.floor(playerpos.z+0.5)}
snow.clear_old_snow(exactplayerpos)
if snow.playertable[player:get_player_name()] then
snow.clear_old_snow(snow.playertable[player:get_player_name()])
end
snow.set_ms_pos(exactplayerpos,player)
end)
--remove snow when player leaves!
--get player node position on joining
minetest.register_on_joinplayer(function(player)
print("clearing "..player:get_player_name().."'s weather")
--clear old snow in current pos
local playerpos = player:getpos()
local exactplayerpos = {x=math.floor(playerpos.x+0.5),y=math.floor(playerpos.y+0.5),z=math.floor(playerpos.z+0.5)}
snow.clear_old_snow(exactplayerpos)
--remove old snow in last position
if snow.get_ms_pos(player) then
snow.clear_old_snow(snow.get_ms_pos(player))
end
snow.set_ms_pos(exactplayerpos,player)
snow.playertable[player:get_player_name()] = exactplayerpos
end)
--helpers for position in mod storage
snow.set_ms_pos = function(pos,player)
mod_storage:set_string(player:get_player_name().."_pos", minetest.pos_to_string(pos, 0))
end
snow.get_ms_pos = function(player)
return(minetest.string_to_pos(mod_storage:get_string(player:get_player_name().."_pos")))
end
--new snow (allows snow to pile up)
for i = 1,7 do
end
--