ShadowNinja
2013-10-30 be2f30a1a2f5b6c2aae7fd4cf8231aec2da0844d
commit | author | age
ee0765 1 -- The enriched uranium rod driven EU generator.
S 2 -- A very large and advanced machine providing vast amounts of power.
3 -- Very efficient but also expensive to run as it needs uranium. (10000EU 86400 ticks (24h))
4 -- Provides HV EUs that can be down converted as needed.
5 --
6 -- The nuclear reactor core needs water and a protective shield to work.
7 -- This is checked now and then and if the machine is tampered with... BOOM!
8
9 local burn_ticks   = 7 * 24 * 60 * 60       -- (seconds).
10 local power_supply = 100000                 -- EUs
11 local fuel_type    = "technic:uranium_fuel" -- The reactor burns this stuff
12
be2f30 13 local S = technic.getter
ee0765 14
S 15 -- FIXME: recipe must make more sense like a rod recepticle, steam chamber, HV generator?
16 minetest.register_craft({
17     output = 'technic:hv_nuclear_reactor_core',
18     recipe = {
19         {'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot', 'technic:stainless_steel_ingot'},
20         {'technic:stainless_steel_ingot',                              '', 'technic:stainless_steel_ingot'},
21         {'technic:stainless_steel_ingot',              'technic:hv_cable', 'technic:stainless_steel_ingot'},
22     }
23 })
24
25 local generator_formspec =
26     "invsize[8,9;]"..
be2f30 27     "label[0,0;"..S("Nuclear Reactor Rod Compartment").."]"..
ee0765 28     "list[current_name;src;2,1;3,2;]"..
S 29     "list[current_player;main;0,5;8,4;]"
30
31 -- "Boxy sphere"
32 local nodebox = {
33     { -0.353, -0.353, -0.353, 0.353, 0.353, 0.353 }, -- Box
34     { -0.495, -0.064, -0.064, 0.495, 0.064, 0.064 }, -- Circle +-x
35     { -0.483, -0.128, -0.128, 0.483, 0.128, 0.128 },
36     { -0.462, -0.191, -0.191, 0.462, 0.191, 0.191 },
37     { -0.433, -0.249, -0.249, 0.433, 0.249, 0.249 },
38     { -0.397, -0.303, -0.303, 0.397, 0.303, 0.303 },
39     { -0.305, -0.396, -0.305, 0.305, 0.396, 0.305 }, -- Circle +-y
40     { -0.250, -0.432, -0.250, 0.250, 0.432, 0.250 },
41     { -0.191, -0.461, -0.191, 0.191, 0.461, 0.191 },
42     { -0.130, -0.482, -0.130, 0.130, 0.482, 0.130 },
43     { -0.066, -0.495, -0.066, 0.066, 0.495, 0.066 },
44     { -0.064, -0.064, -0.495, 0.064, 0.064, 0.495 }, -- Circle +-z
45     { -0.128, -0.128, -0.483, 0.128, 0.128, 0.483 },
46     { -0.191, -0.191, -0.462, 0.191, 0.191, 0.462 },
47     { -0.249, -0.249, -0.433, 0.249, 0.249, 0.433 },
48     { -0.303, -0.303, -0.397, 0.303, 0.303, 0.397 },
49 }
50
51 minetest.register_node("technic:hv_nuclear_reactor_core", {
be2f30 52     description = S("Nuclear Reactor Core"),
ee0765 53     tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
S 54              "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
55              "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"},
56     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2},
57     legacy_facedir_simple = true,
58     sounds = default.node_sound_wood_defaults(),
59     drawtype="nodebox",
60     paramtype = "light",
61     stack_max = 1,
62     node_box = {
63         type = "fixed",
64         fixed = nodebox
65     },
66     on_construct = function(pos)
67         local meta = minetest.get_meta(pos)
be2f30 68         meta:set_string("infotext", S("Nuclear Reactor Core"))
ee0765 69         meta:set_int("HV_EU_supply", 0)
S 70         -- Signal to the switching station that this device burns some
71         -- sort of fuel and needs special handling
72         meta:set_int("HV_EU_from_fuel", 1)
73         meta:set_int("burn_time", 0)
74         meta:set_string("formspec", generator_formspec)
75         local inv = meta:get_inventory()
76         inv:set_size("src", 6)
77     end,    
be2f30 78     can_dig = function(pos, player)
ee0765 79         local meta = minetest.get_meta(pos);
S 80         local inv = meta:get_inventory()
81         if not inv:is_empty("src") then
82             minetest.chat_send_player(player:get_player_name(),
be2f30 83                 S("Machine cannot be removed because it is not empty"))
ee0765 84             return false
S 85         else
86             return true
87         end
88     end,
89 })
90
91 minetest.register_node("technic:hv_nuclear_reactor_core_active", {
92     tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
93              "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
94          "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"},
95     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, not_in_creative_inventory=1},
96     legacy_facedir_simple = true,
97     sounds = default.node_sound_wood_defaults(),
98     drop="technic:hv_nuclear_reactor_core",
99     drawtype="nodebox",
100     light_source = 15,
101     paramtype = "light",
102     node_box = {
103         type = "fixed",
104         fixed = nodebox
105     },
be2f30 106     can_dig = function(pos, player)
ee0765 107         local meta = minetest.get_meta(pos);
S 108         local inv = meta:get_inventory()
109         if not inv:is_empty("src") then
110             minetest.chat_send_player(player:get_player_name(),
be2f30 111                 S("Machine cannot be removed because it is not empty"))
ee0765 112             return false
S 113         else
114             return true
115         end
116     end,
117 })
118
119 local check_reactor_structure = function(pos)
120     -- The reactor consists of a 9x9x9 cube structure
121     -- A cross section through the middle:
122     --  CCCC CCCC
123     --  CBBB BBBC
124     --  CBSS SSBC
125     --  CBSWWWSBC
126     --  CBSW#WSBC
127     --  CBSW|WSBC
128     --  CBSS|SSBC
129     --  CBBB|BBBC
130     --  CCCC|CCCC
131     --  C = Concrete, B = Blast resistant concrete, S = Stainless Steel,
132     --  W = water node, # = reactor core, | = HV cable
133     --  The man-hole and the HV cable is only in the middle
134     --  The man-hole is optional
135
136     local vm = VoxelManip()
137     local pos1 = vector.subtract(pos, 4)
138     local pos2 = vector.add(pos, 4)
139     local MinEdge, MaxEdge = vm:read_from_map(pos1, pos2)
140     local data = vm:get_data()
141     local area = VoxelArea:new({MinEdge=MinEdge, MaxEdge=MaxEdge})
142
143     local c_concrete = minetest.get_content_id("technic:concrete")
144     local c_blast_concrete = minetest.get_content_id("technic:blast_resistant_concrete")
145     local c_stainless_steel = minetest.get_content_id("technic:stainless_steel_block")
146     local c_water_source = minetest.get_content_id("default:water_source")
147     local c_water_flowing = minetest.get_content_id("default:water_flowing")
148
149     local concretelayer, blastlayer, steellayer, waterlayer = 0, 0, 0, 0
150
151     for z = pos1.z, pos2.z do
152     for y = pos1.y, pos2.y do
153     for x = pos1.x, pos2.x do
154         -- If the position is in the outer layer
155         if x == pos1.x or x == pos2.x or
156            y == pos1.y or y == pos2.y or
157            z == pos1.z or z == pos2.z then
158             if data[area:index(x, y, z)] == c_concrete then
159                 concretelayer = concretelayer + 1
160             end
161         elseif x == pos1.x+1 or x == pos2.x-1 or
162            y == pos1.y+1 or y == pos2.y-1 or
163            z == pos1.z+1 or z == pos2.z-1 then
164             if data[area:index(x, y, z)] == c_blast_concrete then
165                 blastlayer = blastlayer + 1
166             end
167         elseif x == pos1.x+2 or x == pos2.x-2 or
168            y == pos1.y+2 or y == pos2.y-2 or
169            z == pos1.z+2 or z == pos2.z-2 then
170             if data[area:index(x, y, z)] == c_stainless_steel then
171                 steellayer = steellayer + 1
172             end
173         elseif x == pos1.x+3 or x == pos2.x-3 or
174            y == pos1.y+3 or y == pos2.y-3 or
175            z == pos1.z+3 or z == pos2.z-3 then
176                local cid = data[area:index(x, y, z)]
177             if cid == c_water_source or cid == c_water_flowing then
178                 waterlayer = waterlayer + 1
179             end
180         end
181     end
182     end
183     end
184     if waterlayer >= 25 and
185        steellayer >= 96 and
186        blastlayer >= 216 and
187        concretelayer >= 384 then
188         return true
189     end
190 end
191
192 local explode_reactor = function(pos)
193     print("A reactor exploded at "..minetest.pos_to_string(pos))
194 end
195
196 local function damage_nearby_players(pos)
197     local objs = minetest.get_objects_inside_radius(pos, 4)
198     for _, o in pairs(objs) do
199         if o:is_player() then
200             o:set_hp(math.max(o:get_hp() - 2, 0))
201         end
202     end
203 end
204
205 minetest.register_abm({
206     nodenames = {"technic:hv_nuclear_reactor_core", "technic:hv_nuclear_reactor_core_active"},
207     interval = 1,
208     chance   = 1,
209     action = function(pos, node, active_object_count, active_object_count_wider)
210         local meta = minetest.get_meta(pos)
be2f30 211         local machine_name = S("Nuclear Reactor Core")
ee0765 212         local burn_time = meta:get_int("burn_time") or 0
S 213
214         if burn_time >= burn_ticks or burn_time == 0 then
215             local inv = meta:get_inventory()
216             if not inv:is_empty("src") then 
217                 local srclist = inv:get_list("src")
218                 local correct_fuel_count = 0
219                 for _, srcstack in pairs(srclist) do
220                     if srcstack then
221                         if  srcstack:get_name() == fuel_type then
222                             correct_fuel_count = correct_fuel_count + 1
223                         end
224                     end
225                 end
226                 -- Check that the reactor is complete as well
227                 -- as the correct number of correct fuel
228                 if correct_fuel_count == 6 and
229                    check_reactor_structure(pos) then
230                     meta:set_int("burn_time", 1)
231                     hacky_swap_node(pos, "technic:hv_nuclear_reactor_core_active") 
232                     meta:set_int("HV_EU_supply", power_supply)
233                     for idx, srcstack in pairs(srclist) do
234                         srcstack:take_item()
235                         inv:set_stack("src", idx, srcstack)
236                     end
237                     return
238                 end
239             end
240             meta:set_int("HV_EU_supply", 0)
241             meta:set_int("burn_time", 0)
be2f30 242             meta:set_string("infotext", S("%s Idle"):format(machine_name))
ee0765 243             hacky_swap_node(pos, "technic:hv_nuclear_reactor_core")
S 244         elseif burn_time > 0 then
245             damage_nearby_players(pos)
246             if not check_reactor_structure(pos) then
247                 explode_reactor(pos)
248             end
249             burn_time = burn_time + 1
250             meta:set_int("burn_time", burn_time)
251             local percent = math.floor(burn_time / burn_ticks * 100)
be2f30 252             meta:set_string("infotext", machine_name.." ("..percent.."%)")
ee0765 253             meta:set_int("HV_EU_supply", power_supply)
S 254         end
255     end
256 })
257
258 technic.register_machine("HV", "technic:hv_nuclear_reactor_core",        technic.producer)
259 technic.register_machine("HV", "technic:hv_nuclear_reactor_core_active", technic.producer)
260