Zefram
2014-07-25 d59055dd2b1a03449ff11318a2f210d37dd7e76a
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.
30adc3 3 -- Very efficient but also expensive to run as it needs uranium. (10000EU 86400 ticks (one week))
ee0765 4 -- Provides HV EUs that can be down converted as needed.
S 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
ec008d 15 if not vector.length_square then
Z 16     vector.length_square = function (v)
17         return v.x*v.x + v.y*v.y + v.z*v.z
17c5b6 18     end
Z 19 end
20
ee0765 21 -- FIXME: recipe must make more sense like a rod recepticle, steam chamber, HV generator?
S 22 minetest.register_craft({
23     output = 'technic:hv_nuclear_reactor_core',
24     recipe = {
5e4a87 25         {'technic:carbon_plate',          'default:obsidian_glass', 'technic:carbon_plate'},
Z 26         {'technic:composite_plate',       'technic:machine_casing', 'technic:composite_plate'},
27         {'technic:stainless_steel_ingot',      'technic:hv_cable0', 'technic:stainless_steel_ingot'},
ee0765 28     }
S 29 })
30
31 local generator_formspec =
32     "invsize[8,9;]"..
be2f30 33     "label[0,0;"..S("Nuclear Reactor Rod Compartment").."]"..
ee0765 34     "list[current_name;src;2,1;3,2;]"..
S 35     "list[current_player;main;0,5;8,4;]"
36
37 -- "Boxy sphere"
38 local nodebox = {
39     { -0.353, -0.353, -0.353, 0.353, 0.353, 0.353 }, -- Box
40     { -0.495, -0.064, -0.064, 0.495, 0.064, 0.064 }, -- Circle +-x
41     { -0.483, -0.128, -0.128, 0.483, 0.128, 0.128 },
42     { -0.462, -0.191, -0.191, 0.462, 0.191, 0.191 },
43     { -0.433, -0.249, -0.249, 0.433, 0.249, 0.249 },
44     { -0.397, -0.303, -0.303, 0.397, 0.303, 0.303 },
45     { -0.305, -0.396, -0.305, 0.305, 0.396, 0.305 }, -- Circle +-y
46     { -0.250, -0.432, -0.250, 0.250, 0.432, 0.250 },
47     { -0.191, -0.461, -0.191, 0.191, 0.461, 0.191 },
48     { -0.130, -0.482, -0.130, 0.130, 0.482, 0.130 },
49     { -0.066, -0.495, -0.066, 0.066, 0.495, 0.066 },
50     { -0.064, -0.064, -0.495, 0.064, 0.064, 0.495 }, -- Circle +-z
51     { -0.128, -0.128, -0.483, 0.128, 0.128, 0.483 },
52     { -0.191, -0.191, -0.462, 0.191, 0.191, 0.462 },
53     { -0.249, -0.249, -0.433, 0.249, 0.249, 0.433 },
54     { -0.303, -0.303, -0.397, 0.303, 0.303, 0.397 },
55 }
56
d59055 57 local reactor_siren = {}
Z 58 local function siren_set_state(pos, newstate)
59     local hpos = minetest.hash_node_position(pos)
60     local siren = reactor_siren[hpos]
61     if not siren then
62         if newstate == "off" then return end
63         siren = {state="off"}
64         reactor_siren[hpos] = siren
65     end
66     if newstate == "danger" and siren.state ~= "danger" then
67         if siren.handle then minetest.sound_stop(siren.handle) end
68         siren.handle = minetest.sound_play("technic_hv_nuclear_reactor_siren_danger_loop", {pos=pos, gain=1.5, loop=true, max_hear_distance=48})
69         siren.state = "danger"
70     elseif newstate == "clear" then
71         if siren.handle then minetest.sound_stop(siren.handle) end
72         local clear_handle = minetest.sound_play("technic_hv_nuclear_reactor_siren_clear", {pos=pos, gain=1.5, loop=false, max_hear_distance=48})
73         siren.handle = clear_handle
74         siren.state = "clear"
75         minetest.after(10, function ()
76             if siren.handle == clear_handle then
77                 minetest.sound_stop(clear_handle)
78                 if reactor_siren[hpos] == siren then
79                     reactor_siren[hpos] = nil
80                 end
81             end
82         end)
83     elseif newstate == "off" and siren.state ~= "off" then
84         if siren.handle then minetest.sound_stop(siren.handle) end
85         siren.handle = nil
86         reactor_siren[hpos] = nil
87     end
88 end
89 local function siren_danger(pos, meta)
90     meta:set_int("siren", 1)
91     siren_set_state(pos, "danger")
92 end
93 local function siren_clear(pos, meta)
94     if meta:get_int("siren") ~= 0 then
95         siren_set_state(pos, "clear")
96         meta:set_int("siren", 0)
97     end
98 end
99
67b90f 100 local reactor_structure_badness = function(pos)
ee0765 101     -- The reactor consists of a 9x9x9 cube structure
S 102     -- A cross section through the middle:
103     --  CCCC CCCC
104     --  CBBB BBBC
105     --  CBSS SSBC
106     --  CBSWWWSBC
107     --  CBSW#WSBC
108     --  CBSW|WSBC
109     --  CBSS|SSBC
110     --  CBBB|BBBC
111     --  CCCC|CCCC
112     --  C = Concrete, B = Blast resistant concrete, S = Stainless Steel,
113     --  W = water node, # = reactor core, | = HV cable
114     --  The man-hole and the HV cable is only in the middle
115     --  The man-hole is optional
116
117     local vm = VoxelManip()
118     local pos1 = vector.subtract(pos, 4)
119     local pos2 = vector.add(pos, 4)
120     local MinEdge, MaxEdge = vm:read_from_map(pos1, pos2)
121     local data = vm:get_data()
122     local area = VoxelArea:new({MinEdge=MinEdge, MaxEdge=MaxEdge})
123
124     local c_concrete = minetest.get_content_id("technic:concrete")
125     local c_blast_concrete = minetest.get_content_id("technic:blast_resistant_concrete")
126     local c_stainless_steel = minetest.get_content_id("technic:stainless_steel_block")
127     local c_water_source = minetest.get_content_id("default:water_source")
128     local c_water_flowing = minetest.get_content_id("default:water_flowing")
129
130     local concretelayer, blastlayer, steellayer, waterlayer = 0, 0, 0, 0
131
132     for z = pos1.z, pos2.z do
133     for y = pos1.y, pos2.y do
134     for x = pos1.x, pos2.x do
135         -- If the position is in the outer layer
136         if x == pos1.x or x == pos2.x or
137            y == pos1.y or y == pos2.y or
138            z == pos1.z or z == pos2.z then
139             if data[area:index(x, y, z)] == c_concrete then
140                 concretelayer = concretelayer + 1
141             end
142         elseif x == pos1.x+1 or x == pos2.x-1 or
143            y == pos1.y+1 or y == pos2.y-1 or
144            z == pos1.z+1 or z == pos2.z-1 then
145             if data[area:index(x, y, z)] == c_blast_concrete then
146                 blastlayer = blastlayer + 1
147             end
148         elseif x == pos1.x+2 or x == pos2.x-2 or
149            y == pos1.y+2 or y == pos2.y-2 or
150            z == pos1.z+2 or z == pos2.z-2 then
151             if data[area:index(x, y, z)] == c_stainless_steel then
152                 steellayer = steellayer + 1
153             end
154         elseif x == pos1.x+3 or x == pos2.x-3 or
155            y == pos1.y+3 or y == pos2.y-3 or
156            z == pos1.z+3 or z == pos2.z-3 then
157                local cid = data[area:index(x, y, z)]
158             if cid == c_water_source or cid == c_water_flowing then
159                 waterlayer = waterlayer + 1
160             end
161         end
162     end
163     end
164     end
67b90f 165     if waterlayer > 25 then waterlayer = 25 end
Z 166     if steellayer > 96 then steellayer = 96 end
167     if blastlayer > 216 then blastlayer = 216 end
168     if concretelayer > 384 then concretelayer = 384 end
169     return (25 - waterlayer) + (96 - steellayer) + (216 - blastlayer) + (384 - concretelayer)
ee0765 170 end
S 171
67b90f 172 local function meltdown_reactor(pos)
Z 173     print("A reactor melted down at "..minetest.pos_to_string(pos))
366fc3 174     minetest.set_node(pos, {name="technic:corium_source"})
ee0765 175 end
67b90f 176
Z 177 minetest.register_abm({
178     nodenames = {"technic:hv_nuclear_reactor_core_active"},
179     interval = 1,
180     chance = 1,
181     action = function (pos, node)
182         local meta = minetest.get_meta(pos)
183         local badness = reactor_structure_badness(pos)
184         local accum_badness = meta:get_int("structure_accumulated_badness")
185         if badness == 0 then
186             if accum_badness ~= 0 then
187                 meta:set_int("structure_accumulated_badness", accum_badness - 1)
d59055 188                 siren_clear(pos, meta)
67b90f 189             end
Z 190         else
d59055 191             siren_danger(pos, meta)
67b90f 192             accum_badness = accum_badness + badness
Z 193             if accum_badness >= 100 then
194                 meltdown_reactor(pos)
195             else
196                 meta:set_int("structure_accumulated_badness", accum_badness)
197             end
198         end
199     end,
200 })
ee0765 201
563a4c 202 local run = function(pos, node)
N 203     local meta = minetest.get_meta(pos)
204     local machine_name = S("Nuclear %s Generator Core"):format("HV")
205     local burn_time = meta:get_int("burn_time") or 0
ee0765 206
563a4c 207     if burn_time >= burn_ticks or burn_time == 0 then
N 208         local inv = meta:get_inventory()
209         if not inv:is_empty("src") then 
210             local srclist = inv:get_list("src")
211             local correct_fuel_count = 0
212             for _, srcstack in pairs(srclist) do
213                 if srcstack then
214                     if  srcstack:get_name() == fuel_type then
215                         correct_fuel_count = correct_fuel_count + 1
ee0765 216                     end
S 217                 end
563a4c 218             end
N 219             -- Check that the reactor is complete as well
220             -- as the correct number of correct fuel
221             if correct_fuel_count == 6 and
67b90f 222                reactor_structure_badness(pos) == 0 then
563a4c 223                 meta:set_int("burn_time", 1)
N 224                 technic.swap_node(pos, "technic:hv_nuclear_reactor_core_active") 
225                 meta:set_int("HV_EU_supply", power_supply)
226                 for idx, srcstack in pairs(srclist) do
227                     srcstack:take_item()
228                     inv:set_stack("src", idx, srcstack)
ee0765 229                 end
563a4c 230                 return
ee0765 231             end
S 232         end
563a4c 233         meta:set_int("HV_EU_supply", 0)
N 234         meta:set_int("burn_time", 0)
235         meta:set_string("infotext", S("%s Idle"):format(machine_name))
236         technic.swap_node(pos, "technic:hv_nuclear_reactor_core")
67b90f 237         meta:set_int("structure_accumulated_badness", 0)
d59055 238         siren_clear(pos, meta)
563a4c 239     elseif burn_time > 0 then
N 240         burn_time = burn_time + 1
241         meta:set_int("burn_time", burn_time)
242         local percent = math.floor(burn_time / burn_ticks * 100)
243         meta:set_string("infotext", machine_name.." ("..percent.."%)")
244         meta:set_int("HV_EU_supply", power_supply)
ee0765 245     end
563a4c 246 end
N 247
248 minetest.register_node("technic:hv_nuclear_reactor_core", {
249     description = S("Nuclear %s Generator Core"):format("HV"),
250     tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
251              "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
252              "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"},
253     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_machine=1},
254     legacy_facedir_simple = true,
255     sounds = default.node_sound_wood_defaults(),
256     drawtype="nodebox",
257     paramtype = "light",
258     stack_max = 1,
259     node_box = {
260         type = "fixed",
261         fixed = nodebox
262     },
263     on_construct = function(pos)
264         local meta = minetest.get_meta(pos)
265         meta:set_string("infotext", S("Nuclear %s Generator Core"):format("HV"))
266         meta:set_int("HV_EU_supply", 0)
267         -- Signal to the switching station that this device burns some
268         -- sort of fuel and needs special handling
269         meta:set_int("HV_EU_from_fuel", 1)
270         meta:set_int("burn_time", 0)
271         meta:set_string("formspec", generator_formspec)
272         local inv = meta:get_inventory()
273         inv:set_size("src", 6)
274     end,    
275     can_dig = technic.machine_can_dig,
d59055 276     on_destruct = function(pos) siren_set_state(pos, "off") end,
563a4c 277     allow_metadata_inventory_put = technic.machine_inventory_put,
N 278     allow_metadata_inventory_take = technic.machine_inventory_take,
279     allow_metadata_inventory_move = technic.machine_inventory_move,
280     technic_run = run,
281 })
282
283 minetest.register_node("technic:hv_nuclear_reactor_core_active", {
284     tiles = {"technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
285              "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png",
286          "technic_hv_nuclear_reactor_core.png", "technic_hv_nuclear_reactor_core.png"},
ec008d 287     groups = {snappy=2, choppy=2, oddly_breakable_by_hand=2, technic_machine=1, radioactive=7, not_in_creative_inventory=1},
563a4c 288     legacy_facedir_simple = true,
N 289     sounds = default.node_sound_wood_defaults(),
290     drop="technic:hv_nuclear_reactor_core",
291     drawtype="nodebox",
292     light_source = 15,
293     paramtype = "light",
294     node_box = {
295         type = "fixed",
296         fixed = nodebox
297     },
298     can_dig = technic.machine_can_dig,
67b90f 299     after_dig_node = meltdown_reactor,
d59055 300     on_destruct = function(pos) siren_set_state(pos, "off") end,
563a4c 301     allow_metadata_inventory_put = technic.machine_inventory_put,
N 302     allow_metadata_inventory_take = technic.machine_inventory_take,
303     allow_metadata_inventory_move = technic.machine_inventory_move,
304     technic_run = run,
1c617f 305     technic_on_disable = function(pos, node)
N 306         local timer = minetest.get_node_timer(pos)
307             timer:start(1)
308         end,
309     on_timer = function(pos, node)
310         local meta = minetest.get_meta(pos)
311         
312         -- Connected back?
313         if meta:get_int("HV_EU_timeout") > 0 then return end    
314         
315         local burn_time = meta:get_int("burn_time") or 0
316
317         if burn_time >= burn_ticks or burn_time == 0 then
318             meta:set_int("HV_EU_supply", 0)
319             meta:set_int("burn_time", 0)
320             technic.swap_node(pos, "technic:hv_nuclear_reactor_core")
67b90f 321             meta:set_int("structure_accumulated_badness", 0)
d59055 322             siren_clear(pos, meta)
1c617f 323             return
N 324         end
325         
326         meta:set_int("burn_time", burn_time + 1)
327         local timer = minetest.get_node_timer(pos)
328             timer:start(1)
329     end,
ee0765 330 })
S 331
332 technic.register_machine("HV", "technic:hv_nuclear_reactor_core",        technic.producer)
333 technic.register_machine("HV", "technic:hv_nuclear_reactor_core_active", technic.producer)
334
ec008d 335 -- radioactivity
366fc3 336
ec008d 337 -- Radiation resistance represents the extent to which a material
Z 338 -- attenuates radiation passing through it; i.e., how good a radiation
339 -- shield it is.  This is identified per node type.  For materials that
340 -- exist in real life, the radiation resistance value that this system
341 -- uses for a node type consisting of a solid cube of that material is the
342 -- (approximate) number of halvings of ionising radiation that is achieved
343 -- by a metre of the material in real life.  This is approximately
344 -- proportional to density, which provides a good way to estimate it.
345 -- Homogeneous mixtures of materials have radiation resistance computed
346 -- by a simple weighted mean.  Note that the amount of attenuation that
347 -- a material achieves in-game is not required to be (and is not) the
348 -- same as the attenuation achieved in real life.
349 --
350 -- Radiation resistance for a node type may be specified in the node
351 -- definition, under the key "radiation_resistance".  As an interim
352 -- measure, until node definitions widely include this, this code
353 -- knows a bunch of values for particular node types in several mods,
354 -- and values for groups of node types.  The node definition takes
355 -- precedence if it specifies a value.  Nodes for which no value at
356 -- all is known are taken to provide no radiation resistance at all;
357 -- this is appropriate for the majority of node types.  Only node types
358 -- consisting of a fairly homogeneous mass of material should report
359 -- non-zero radiation resistance; anything with non-uniform geometry
360 -- or complex internal structure should show no radiation resistance.
361 -- Fractional resistance values are permitted; two significant figures
362 -- is the recommended precision.
363 local default_radiation_resistance_per_node = {
364     ["default:brick"] = 13,
365     ["default:bronzeblock"] = 45,
366     ["default:clay"] = 15,
367     ["default:coalblock"] = 9.6,
368     ["default:cobble"] = 15,
369     ["default:copperblock"] = 46,
370     ["default:desert_cobble"] = 15,
371     ["default:desert_sand"] = 10,
372     ["default:desert_stone"] = 17,
373     ["default:desert_stonebrick"] = 17,
374     ["default:diamondblock"] = 24,
375     ["default:dirt"] = 8.2,
376     ["default:dirt_with_grass"] = 8.2,
377     ["default:dirt_with_grass_footsteps"] = 8.2,
378     ["default:dirt_with_snow"] = 8.2,
379     ["default:glass"] = 17,
380     ["default:goldblock"] = 170,
381     ["default:gravel"] = 10,
382     ["default:ice"] = 5.6,
383     ["default:lava_flowing"] = 8.5,
384     ["default:lava_source"] = 17,
385     ["default:mese"] = 21,
386     ["default:mossycobble"] = 15,
387     ["default:nyancat"] = 1000,
388     ["default:nyancat_rainbow"] = 1000,
389     ["default:obsidian"] = 18,
390     ["default:obsidian_glass"] = 18,
391     ["default:sand"] = 10,
392     ["default:sandstone"] = 15,
393     ["default:sandstonebrick"] = 15,
394     ["default:snowblock"] = 1.7,
395     ["default:steelblock"] = 40,
396     ["default:stone"] = 17,
397     ["default:stone_with_coal"] = 16,
398     ["default:stone_with_copper"] = 20,
399     ["default:stone_with_diamond"] = 18,
400     ["default:stone_with_gold"] = 34,
401     ["default:stone_with_iron"] = 20,
402     ["default:stone_with_mese"] = 17,
403     ["default:stonebrick"] = 17,
404     ["default:water_flowing"] = 2.8,
405     ["default:water_source"] = 5.6,
406     ["farming:desert_sand_soil"] = 10,
407     ["farming:desert_sand_soil_wet"] = 10,
408     ["farming:soil"] = 8.2,
409     ["farming:soil_wet"] = 8.2,
410     ["glooptest:akalin_crystal_glass"] = 21,
411     ["glooptest:akalinblock"] = 40,
412     ["glooptest:alatro_crystal_glass"] = 21,
413     ["glooptest:alatroblock"] = 40,
414     ["glooptest:amethystblock"] = 18,
415     ["glooptest:arol_crystal_glass"] = 21,
416     ["glooptest:crystal_glass"] = 21,
417     ["glooptest:emeraldblock"] = 19,
418     ["glooptest:heavy_crystal_glass"] = 21,
419     ["glooptest:mineral_akalin"] = 20,
420     ["glooptest:mineral_alatro"] = 20,
421     ["glooptest:mineral_amethyst"] = 17,
422     ["glooptest:mineral_arol"] = 20,
423     ["glooptest:mineral_desert_coal"] = 16,
424     ["glooptest:mineral_desert_iron"] = 20,
425     ["glooptest:mineral_emerald"] = 17,
426     ["glooptest:mineral_kalite"] = 20,
427     ["glooptest:mineral_ruby"] = 18,
428     ["glooptest:mineral_sapphire"] = 18,
429     ["glooptest:mineral_talinite"] = 20,
430     ["glooptest:mineral_topaz"] = 18,
431     ["glooptest:reinforced_crystal_glass"] = 21,
432     ["glooptest:rubyblock"] = 27,
433     ["glooptest:sapphireblock"] = 27,
434     ["glooptest:talinite_crystal_glass"] = 21,
435     ["glooptest:taliniteblock"] = 40,
436     ["glooptest:topazblock"] = 24,
437     ["mesecons_extrawires:mese_powered"] = 21,
438     ["moreblocks:cactus_brick"] = 13,
439     ["moreblocks:cactus_checker"] = 8.5,
440     ["moreblocks:circle_stone_bricks"] = 17,
441     ["moreblocks:clean_glass"] = 17,
442     ["moreblocks:coal_checker"] = 9.0,
443     ["moreblocks:coal_glass"] = 17,
444     ["moreblocks:coal_stone"] = 17,
445     ["moreblocks:coal_stone_bricks"] = 17,
446     ["moreblocks:glow_glass"] = 17,
447     ["moreblocks:grey_bricks"] = 15,
448     ["moreblocks:iron_checker"] = 11,
449     ["moreblocks:iron_glass"] = 17,
450     ["moreblocks:iron_stone"] = 17,
451     ["moreblocks:iron_stone_bricks"] = 17,
452     ["moreblocks:plankstone"] = 9.3,
453     ["moreblocks:split_stone_tile"] = 15,
454     ["moreblocks:split_stone_tile_alt"] = 15,
455     ["moreblocks:stone_tile"] = 15,
456     ["moreblocks:super_glow_glass"] = 17,
457     ["moreblocks:tar"] = 7.0,
458     ["moreblocks:wood_tile"] = 1.7,
459     ["moreblocks:wood_tile_center"] = 1.7,
460     ["moreblocks:wood_tile_down"] = 1.7,
461     ["moreblocks:wood_tile_flipped"] = 1.7,
462     ["moreblocks:wood_tile_full"] = 1.7,
463     ["moreblocks:wood_tile_left"] = 1.7,
464     ["moreblocks:wood_tile_right"] = 1.7,
465     ["moreblocks:wood_tile_up"] = 1.7,
466     ["moreores:mineral_mithril"] = 18,
467     ["moreores:mineral_silver"] = 21,
468     ["moreores:mineral_tin"] = 19,
469     ["moreores:mithril_block"] = 26,
470     ["moreores:silver_block"] = 53,
471     ["moreores:tin_block"] = 37,
472     ["snow:snow_brick"] = 2.8,
473     ["technic:brass_block"] = 43,
474     ["technic:carbon_steel_block"] = 40,
475     ["technic:cast_iron_block"] = 40,
476     ["technic:chernobylite_block"] = 40,
477     ["technic:chromium_block"] = 37,
478     ["technic:corium_flowing"] = 40,
479     ["technic:corium_source"] = 80,
480     ["technic:granite"] = 18,
481     ["technic:marble"] = 18,
482     ["technic:marble_bricks"] = 18,
483     ["technic:mineral_chromium"] = 19,
484     ["technic:mineral_uranium"] = 71,
485     ["technic:mineral_zinc"] = 19,
486     ["technic:stainless_steel_block"] = 40,
487     ["technic:uranium_block"] = 500,
488     ["technic:zinc_block"] = 36,
489     ["tnt:tnt"] = 11,
490     ["tnt:tnt_burning"] = 11,
491 }
492 local default_radiation_resistance_per_group = {
493     concrete = 16,
494     tree = 3.4,
495     wood = 1.7,
496 }
497 local cache_radiation_resistance = {}
498 local function node_radiation_resistance(nodename)
499     local eff = cache_radiation_resistance[nodename]
500     if eff then return eff end
501     local def = minetest.registered_nodes[nodename] or {groups={}}
502     eff = def.radiation_resistance or default_radiation_resistance_per_node[nodename]
503     if not eff then
504         for g, v in pairs(def.groups) do
505             if v > 0 and default_radiation_resistance_per_group[g] then
506                 eff = default_radiation_resistance_per_group[g]
507                 break
508             end
509         end
510     end
511     if not eff then eff = 0 end
512     cache_radiation_resistance[nodename] = eff
513     return eff
514 end
515
516 -- Radioactive nodes cause damage to nearby players.  The damage
517 -- effect depends on the intrinsic strength of the radiation source,
518 -- the distance between the source and the player, and the shielding
519 -- effect of the intervening material.  These determine a rate of damage;
520 -- total damage caused is the integral of this over time.
521 --
522 -- In the absence of effective shielding, for a specific source the
523 -- damage rate varies realistically in inverse proportion to the square
524 -- of the distance.  (Distance is measured to the player's abdomen,
525 -- not to the nominal player position which corresponds to the foot.)
526 -- However, if the player is inside a non-walkable (liquid or gaseous)
527 -- radioactive node, the nominal distance could go to zero, yielding
528 -- infinite damage.  In that case, the player's body is displacing the
529 -- radioactive material, so the effective distance should remain non-zero.
530 -- We therefore apply a lower distance bound of sqrt(0.75) m, which is
531 -- the maximum distance one can get from the node centre within the node.
532 --
533 -- A radioactive node is identified by being in the "radioactive" group,
534 -- and the group value signifies the strength of the radiation source.
535 -- The group value is the distance in metres from a node at which an
536 -- unshielded player will be damaged by 0.25 HP/s.  Or, equivalently, it
537 -- is half the square root of the damage rate in HP/s that an unshielded
538 -- player 1 m away will take.
539 --
540 -- Shielding is assessed by sampling every 0.25 m along the path
541 -- from the source to the player, ignoring the source node itself.
542 -- The summed radiation resistance values from the sampled nodes yield
543 -- a measure of the total amount of radiation resistance on the path.
544 -- As in reality, shielding causes exponential attenuation of radiation.
545 -- However, the effect is scaled down relative to real life: each
546 -- metre-point of shielding, corresponding to a real-life halving of
547 -- radiation, reduces radiation by 0.01 nepers (a factor of about 1.01).
548 -- This scales down the difference between shielded and unshielded safe
549 -- distances, avoiding the latter becoming impractically large.
550 --
551 -- Damage is processed at rates down to 0.25 HP/s, which in the absence of
552 -- shielding is attained at the distance specified by the "radioactive"
553 -- group value.  Computed damage rates below 0.25 HP/s result in no
554 -- damage at all to the player.  This gives the player an opportunity
555 -- to be safe, and limits the range at which source/player interactions
556 -- need to be considered.
17c5b6 557 local assumed_abdomen_offset = vector.new(0, 1, 0)
Z 558 local assumed_abdomen_offset_length = vector.length(assumed_abdomen_offset)
366fc3 559 minetest.register_abm({
Z 560     nodenames = {"group:radioactive"},
561     interval = 1,
562     chance = 1,
563     action = function (pos, node)
17c5b6 564         local strength = minetest.registered_nodes[node.name].groups.radioactive
ec008d 565         for _, o in ipairs(minetest.get_objects_inside_radius(pos, strength + assumed_abdomen_offset_length)) do
17c5b6 566             if o:is_player() then
ec008d 567                 local rel = vector.subtract(vector.add(o:getpos(), assumed_abdomen_offset), pos)
Z 568                 local dist_sq = vector.length_square(rel)
569                 local dist = math.sqrt(dist_sq)
570                 local dirstep = dist == 0 and vector.new(0,0,0) or vector.divide(rel, dist*4)
571                 local intpos = pos
572                 local resistance = 0
573                 for intdist = 0.25, dist, 0.25 do
574                     intpos = vector.add(intpos, dirstep)
575                     local intnodepos = vector.round(intpos)
576                     if not vector.equals(intnodepos, pos) then
577                         resistance = resistance + node_radiation_resistance(minetest.get_node(intnodepos).name)
578                     end
579                 end
580                 local dmg_rate = 0.25 * strength*strength * math.exp(-0.0025*resistance) / math.max(0.75, dist_sq)
17c5b6 581                 if dmg_rate >= 0.25 then
Z 582                     local dmg_int = math.floor(dmg_rate)
583                     if math.random() < dmg_rate-dmg_int then
584                         dmg_int = dmg_int + 1
585                     end
586                     if dmg_int > 0 then
587                         o:set_hp(math.max(o:get_hp() - dmg_int, 0))
588                     end
366fc3 589                 end
Z 590             end
591         end
592     end,
593 })
ec008d 594
Z 595 -- radioactive materials that can result from destroying a reactor
366fc3 596
Z 597 for _, state in ipairs({ "flowing", "source" }) do
598     minetest.register_node("technic:corium_"..state, {
599         description = S(state == "source" and "Corium Source" or "Flowing Corium"),
600         drawtype = (state == "source" and "liquid" or "flowingliquid"),
601         [state == "source" and "tiles" or "special_tiles"] = {{
602             name = "technic_corium_"..state.."_animated.png",
603             animation = {
604                 type = "vertical_frames",
605                 aspect_w = 16,
606                 aspect_h = 16,
607                 length = 3.0,
608             },
609         }},
610         paramtype = "light",
611         paramtype2 = (state == "flowing" and "flowingliquid" or nil),
e11f0f 612         light_source = (state == "source" and 8 or 5),
366fc3 613         walkable = false,
Z 614         pointable = false,
615         diggable = false,
616         buildable_to = true,
617         drop = "",
618         drowning = 1,
619         liquidtype = state,
620         liquid_alternative_flowing = "technic:corium_flowing",
621         liquid_alternative_source = "technic:corium_source",
622         liquid_viscosity = LAVA_VISC,
623         liquid_renewable = false,
624         damage_per_second = 6,
625         post_effect_color = { a=192, r=80, g=160, b=80 },
626         groups = {
627             liquid = 2,
628             hot = 3,
629             igniter = 1,
ec008d 630             radioactive = (state == "source" and 32 or 16),
366fc3 631             not_in_creative_inventory = (state == "flowing" and 1 or nil),
Z 632         },
633     })
634 end
635
636 if bucket and bucket.register_liquid then
637     bucket.register_liquid(
638         "technic:corium_source",
639         "technic:corium_flowing",
640         "technic:bucket_corium",
641         "technic_bucket_corium.png",
642         "Corium Bucket"
643     )
644 end
645
646 minetest.register_node("technic:chernobylite_block", {
647         description = S("Chernobylite Block"),
648     tiles = { "technic_chernobylite_block.png" },
649     is_ground_content = true,
ec008d 650     groups = { cracky=1, radioactive=5, level=2 },
366fc3 651     sounds = default.node_sound_stone_defaults(),
Z 652     light_source = 2,
653
654 })
655
656 minetest.register_abm({
657     nodenames = {"group:water"},
658     neighbors = {"technic:corium_source"},
659     interval = 1,
660     chance = 1,
661     action = function (pos, node)
662         minetest.remove_node(pos)
663     end,
664 })
665
666 minetest.register_abm({
667     nodenames = {"technic:corium_flowing"},
668     neighbors = {"group:water"},
669     interval = 1,
670     chance = 1,
671     action = function (pos, node)
672         minetest.set_node(pos, {name="technic:chernobylite_block"})
673     end,
674 })
675
c5e948 676 local griefing = technic.config:get_bool("enable_corium_griefing")
Z 677
366fc3 678 minetest.register_abm({
Z 679     nodenames = {"technic:corium_flowing"},
680     interval = 5,
c5e948 681     chance = (griefing and 10 or 1),
366fc3 682     action = function (pos, node)
Z 683         minetest.set_node(pos, {name="technic:chernobylite_block"})
684     end,
685 })
686
c5e948 687 if griefing then
Z 688     minetest.register_abm({
689         nodenames = { "technic:corium_source", "technic:corium_flowing" },
690         interval = 4,
691         chance = 4,
692         action = function (pos, node)
693             for _, offset in ipairs({
694                 vector.new(1,0,0),
695                 vector.new(-1,0,0),
696                 vector.new(0,0,1),
697                 vector.new(0,0,-1),
698                 vector.new(0,-1,0),
699             }) do
700                 if math.random(8) == 1 then
701                     minetest.dig_node(vector.add(pos, offset))
702                 end
366fc3 703             end
c5e948 704         end,
Z 705     })
706 end