SmallJoker
2013-12-01 7a3cd495972f2dc399d7af7f596a6d9a4a5c728b
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,    
0809dd 78     can_dig = technic.machine_can_dig,
S 79     allow_metadata_inventory_put = technic.machine_inventory_put,
80     allow_metadata_inventory_take = technic.machine_inventory_take,
81     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 82 })
S 83
84 minetest.register_node("technic:hv_nuclear_reactor_core_active", {
85     tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
86              "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
87          "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"},
88     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, not_in_creative_inventory=1},
89     legacy_facedir_simple = true,
90     sounds = default.node_sound_wood_defaults(),
91     drop="technic:hv_nuclear_reactor_core",
92     drawtype="nodebox",
93     light_source = 15,
94     paramtype = "light",
95     node_box = {
96         type = "fixed",
97         fixed = nodebox
98     },
0809dd 99     can_dig = technic.machine_can_dig,
S 100     allow_metadata_inventory_put = technic.machine_inventory_put,
101     allow_metadata_inventory_take = technic.machine_inventory_take,
102     allow_metadata_inventory_move = technic.machine_inventory_move,
ee0765 103 })
S 104
105 local check_reactor_structure = function(pos)
106     -- The reactor consists of a 9x9x9 cube structure
107     -- A cross section through the middle:
108     --  CCCC CCCC
109     --  CBBB BBBC
110     --  CBSS SSBC
111     --  CBSWWWSBC
112     --  CBSW#WSBC
113     --  CBSW|WSBC
114     --  CBSS|SSBC
115     --  CBBB|BBBC
116     --  CCCC|CCCC
117     --  C = Concrete, B = Blast resistant concrete, S = Stainless Steel,
118     --  W = water node, # = reactor core, | = HV cable
119     --  The man-hole and the HV cable is only in the middle
120     --  The man-hole is optional
121
122     local vm = VoxelManip()
123     local pos1 = vector.subtract(pos, 4)
124     local pos2 = vector.add(pos, 4)
125     local MinEdge, MaxEdge = vm:read_from_map(pos1, pos2)
126     local data = vm:get_data()
127     local area = VoxelArea:new({MinEdge=MinEdge, MaxEdge=MaxEdge})
128
129     local c_concrete = minetest.get_content_id("technic:concrete")
130     local c_blast_concrete = minetest.get_content_id("technic:blast_resistant_concrete")
131     local c_stainless_steel = minetest.get_content_id("technic:stainless_steel_block")
132     local c_water_source = minetest.get_content_id("default:water_source")
133     local c_water_flowing = minetest.get_content_id("default:water_flowing")
134
135     local concretelayer, blastlayer, steellayer, waterlayer = 0, 0, 0, 0
136
137     for z = pos1.z, pos2.z do
138     for y = pos1.y, pos2.y do
139     for x = pos1.x, pos2.x do
140         -- If the position is in the outer layer
141         if x == pos1.x or x == pos2.x or
142            y == pos1.y or y == pos2.y or
143            z == pos1.z or z == pos2.z then
144             if data[area:index(x, y, z)] == c_concrete then
145                 concretelayer = concretelayer + 1
146             end
147         elseif x == pos1.x+1 or x == pos2.x-1 or
148            y == pos1.y+1 or y == pos2.y-1 or
149            z == pos1.z+1 or z == pos2.z-1 then
150             if data[area:index(x, y, z)] == c_blast_concrete then
151                 blastlayer = blastlayer + 1
152             end
153         elseif x == pos1.x+2 or x == pos2.x-2 or
154            y == pos1.y+2 or y == pos2.y-2 or
155            z == pos1.z+2 or z == pos2.z-2 then
156             if data[area:index(x, y, z)] == c_stainless_steel then
157                 steellayer = steellayer + 1
158             end
159         elseif x == pos1.x+3 or x == pos2.x-3 or
160            y == pos1.y+3 or y == pos2.y-3 or
161            z == pos1.z+3 or z == pos2.z-3 then
162                local cid = data[area:index(x, y, z)]
163             if cid == c_water_source or cid == c_water_flowing then
164                 waterlayer = waterlayer + 1
165             end
166         end
167     end
168     end
169     end
170     if waterlayer >= 25 and
171        steellayer >= 96 and
172        blastlayer >= 216 and
173        concretelayer >= 384 then
174         return true
175     end
176 end
177
178 local explode_reactor = function(pos)
179     print("A reactor exploded at "..minetest.pos_to_string(pos))
180 end
181
182 local function damage_nearby_players(pos)
183     local objs = minetest.get_objects_inside_radius(pos, 4)
184     for _, o in pairs(objs) do
185         if o:is_player() then
186             o:set_hp(math.max(o:get_hp() - 2, 0))
187         end
188     end
189 end
190
191 minetest.register_abm({
192     nodenames = {"technic:hv_nuclear_reactor_core", "technic:hv_nuclear_reactor_core_active"},
193     interval = 1,
194     chance   = 1,
195     action = function(pos, node, active_object_count, active_object_count_wider)
196         local meta = minetest.get_meta(pos)
be2f30 197         local machine_name = S("Nuclear Reactor Core")
ee0765 198         local burn_time = meta:get_int("burn_time") or 0
S 199
200         if burn_time >= burn_ticks or burn_time == 0 then
201             local inv = meta:get_inventory()
202             if not inv:is_empty("src") then 
203                 local srclist = inv:get_list("src")
204                 local correct_fuel_count = 0
205                 for _, srcstack in pairs(srclist) do
206                     if srcstack then
207                         if  srcstack:get_name() == fuel_type then
208                             correct_fuel_count = correct_fuel_count + 1
209                         end
210                     end
211                 end
212                 -- Check that the reactor is complete as well
213                 -- as the correct number of correct fuel
214                 if correct_fuel_count == 6 and
215                    check_reactor_structure(pos) then
216                     meta:set_int("burn_time", 1)
217                     hacky_swap_node(pos, "technic:hv_nuclear_reactor_core_active") 
218                     meta:set_int("HV_EU_supply", power_supply)
219                     for idx, srcstack in pairs(srclist) do
220                         srcstack:take_item()
221                         inv:set_stack("src", idx, srcstack)
222                     end
223                     return
224                 end
225             end
226             meta:set_int("HV_EU_supply", 0)
227             meta:set_int("burn_time", 0)
be2f30 228             meta:set_string("infotext", S("%s Idle"):format(machine_name))
ee0765 229             hacky_swap_node(pos, "technic:hv_nuclear_reactor_core")
S 230         elseif burn_time > 0 then
231             damage_nearby_players(pos)
232             if not check_reactor_structure(pos) then
233                 explode_reactor(pos)
234             end
235             burn_time = burn_time + 1
236             meta:set_int("burn_time", burn_time)
237             local percent = math.floor(burn_time / burn_ticks * 100)
be2f30 238             meta:set_string("infotext", machine_name.." ("..percent.."%)")
ee0765 239             meta:set_int("HV_EU_supply", power_supply)
S 240         end
241     end
242 })
243
244 technic.register_machine("HV", "technic:hv_nuclear_reactor_core",        technic.producer)
245 technic.register_machine("HV", "technic:hv_nuclear_reactor_core_active", technic.producer)
246