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