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