]> git.lizzy.rs Git - crafter_client.git/blob - weather_handling.lua
Add in double tap running and bunnyhopping
[crafter_client.git] / weather_handling.lua
1 local all_nodes = {}
2 local do_effects = false
3 local snow = false
4 local rain = false
5 local weather_update_timer = 0
6 local id_table = {}
7
8 local spawn_snow = function(player)
9         local pos = player:get_pos()
10         local radius = 10
11         local particle_table = {}
12         
13         local area = vector.new(10,10,10)
14         
15         local min = vector.subtract(pos, area)
16         local max = vector.add(pos, area)
17         
18         
19         local area_index = minetest.find_nodes_in_area_under_air(min, max, all_nodes)
20         
21         local spawn_table = {}
22         --find the highest y value
23         for _,index in pairs(area_index) do
24                 if not spawn_table[index.x] then spawn_table[index.x] = {} end
25                 if not spawn_table[index.x][index.z] then
26                         spawn_table[index.x][index.z] = index.y
27                 elseif spawn_table[index.x][index.z] < index.y then
28                         spawn_table[index.x][index.z] = index.y
29                 end
30         end
31         
32         for x,x_index in pairs(spawn_table) do
33                 for z,y in pairs(x_index) do
34                         if minetest.get_node_or_nil(vector.new(x,y+1,z)) ~= nil then
35                                 --print("got to this spointa")
36                                 local pos = vector.new(x,y+1,z)
37                                 local lightlevel = 14
38                                 --local lightlevel = minetest.get_node_light(pos, 0.5)
39                                 --print("but not here")
40                                 if lightlevel >= 14 then
41                                         minetest.add_particlespawner({
42                                                 amount = 1,
43                                                 time = 0.5,
44                                                 minpos = vector.new(x-0.5,y,z-0.5),
45                                                 maxpos = vector.new(x+0.5,y+20,z+0.5),
46                                                 minvel = {x=-0.2, y=-0.2, z=-0.2},
47                                                 maxvel = {x=0.2, y=-0.5, z=0.2},
48                                                 minacc = {x=0, y=0, z=0},
49                                                 maxacc = {x=0, y=0, z=0},
50                                                 minexptime = 1,
51                                                 maxexptime = 1,
52                                                 minsize = 1,
53                                                 maxsize = 1,
54                                                 collisiondetection = true,
55                                                 collision_removal = true,
56                                                 object_collision = false,
57                                                 texture = "snowflake_"..math.random(1,2)..".png",
58                                                 playername = player:get_name(),
59                                         })
60                                 end
61                         end
62                 end
63         end
64 end
65
66 local spawn_rain = function(player)
67         local pos = player:get_pos()
68         local radius = 10
69         local particle_table = {}
70         
71         local area = vector.new(10,10,10)
72         
73         local min = vector.subtract(pos, area)
74         local max = vector.add(pos, area)
75         
76         
77         local area_index = minetest.find_nodes_in_area_under_air(min, max, all_nodes)
78         
79         local spawn_table = {}
80         --find the highest y value
81         for _,index in pairs(area_index) do
82                 if not spawn_table[index.x] then spawn_table[index.x] = {} end
83                 if not spawn_table[index.x][index.z] then
84                         spawn_table[index.x][index.z] = index.y
85                 elseif spawn_table[index.x][index.z] < index.y then
86                         spawn_table[index.x][index.z] = index.y
87                 end
88         end
89         
90         for x,x_index in pairs(spawn_table) do
91                 for z,y in pairs(x_index) do
92                         if minetest.get_node_or_nil(vector.new(x,y+1,z)) ~= nil then
93                                 --print("got to this spointa")
94                                 local pos = vector.new(x,y+1,z)
95                                 local lightlevel = 14
96                                 --local lightlevel = minetest.get_node_light(pos, 0.5)
97                                 --print("but not here")
98                                 if lightlevel >= 14 then
99                                         minetest.add_particlespawner({
100                                                 amount = 1,
101                                                 time = 0.5,
102                                                 minpos = vector.new(x-0.5,y,z-0.5),
103                                                 maxpos = vector.new(x+0.5,y+20,z+0.5),
104                                                 minvel = {x=0, y=-9.81, z=0},
105                                                 maxvel = {x=0, y=-9.81, z=0},
106                                                 minacc = {x=0, y=0, z=0},
107                                                 maxacc = {x=0, y=0, z=0},
108                                                 minexptime = 1,
109                                                 maxexptime = 2,
110                                                 minsize = 1,
111                                                 maxsize = 1,
112                                                 collisiondetection = true,
113                                                 collision_removal = true,
114                                                 object_collision = false,
115                                                 vertical = true,
116                                                 texture = "raindrop.png",
117                                                 playername = player:get_name(),
118                                         })
119                                 end
120                         end
121                 end
122         end
123 end
124
125 minetest.register_globalstep(function(dtime)
126         if do_effects then
127                 if snow or rain then
128                         weather_update_timer = weather_update_timer + dtime
129                         if weather_update_timer >= 0.5 then
130                                 weather_update_timer = 0
131                                 local player = minetest.localplayer
132                                 if snow == true then
133                                         spawn_snow(minetest.localplayer)
134                                 elseif rain == true then
135                                         spawn_rain(minetest.localplayer)
136                                 end
137                         end
138                 end
139         end
140 end)
141
142
143
144 minetest.register_on_modchannel_message(function(channel_name, sender, message)
145         if channel_name == "weather_nodes" then
146                 all_nodes = minetest.deserialize(message)
147                 do_effects = true
148         end
149         if channel_name == "weather_type" then
150                 if message == "1" then
151                         rain = false
152                         snow = true
153                 elseif message == "2" then
154                         rain = true
155                         snow = false
156                 else
157                         rain = false
158                         snow = false
159                 end
160         end
161 end)