|
| 1 | +local one_minute = 60 * 60 |
| 2 | + |
| 3 | +local function tick_reactor(struct) |
| 4 | + local entity = struct.entity |
| 5 | + local entity_name = entity.name |
| 6 | + |
| 7 | + if entity.valid == false then |
| 8 | + global.structs_count = global.structs_count - 1 |
| 9 | + global.structs[struct.unit_number] = nil |
| 10 | + return |
| 11 | + end |
| 12 | + |
| 13 | + if game.tick > struct.tick + one_minute then |
| 14 | + -- game.print('reactor timed out.') |
| 15 | + -- entity.surface.create_entity{ |
| 16 | + -- name = 'flying-text', |
| 17 | + -- position = entity.position, |
| 18 | + -- text = 'reactor timed out.', |
| 19 | + -- } |
| 20 | + global.structs_count = global.structs_count - 1 |
| 21 | + global.structs[struct.unit_number] = nil |
| 22 | + return |
| 23 | + end |
| 24 | + |
| 25 | + if entity.temperature > global.max_temperature[entity_name] / 2 then |
| 26 | + -- game.print('reactor warmed up.') |
| 27 | + -- entity.surface.create_entity{ |
| 28 | + -- name = 'flying-text', |
| 29 | + -- position = entity.position, |
| 30 | + -- text = 'reactor warmed up.', |
| 31 | + -- } |
| 32 | + global.structs_count = global.structs_count - 1 |
| 33 | + global.structs[struct.unit_number] = nil |
| 34 | + return |
| 35 | + end |
| 36 | + |
| 37 | + local fuel = entity.burner.remaining_burning_fuel |
| 38 | + if fuel == 0 then return end -- not (yet) fueled |
| 39 | + |
| 40 | + local per_tick = global.max_energy_usage[entity_name] |
| 41 | + local multiplier = math.min(9, math.max(fuel / per_tick)) |
| 42 | + if multiplier == 0 then return end |
| 43 | + |
| 44 | + struct.tick = game.tick -- prevent the reactor from timing out |
| 45 | + local transfer = per_tick * multiplier * (1 + entity.neighbour_bonus) |
| 46 | + |
| 47 | + entity.burner.remaining_burning_fuel = entity.burner.remaining_burning_fuel - transfer |
| 48 | + entity.temperature = entity.temperature + (transfer / global.specific_heat[entity_name]) |
| 49 | +end |
| 50 | + |
| 51 | +local function on_tick(event) |
| 52 | + for unit_number, struct in pairs(global.structs) do |
| 53 | + tick_reactor(struct) |
| 54 | + end |
| 55 | + |
| 56 | + if global.structs_count == 0 then |
| 57 | + script.on_event(defines.events.on_tick, nil) |
| 58 | + end |
| 59 | +end |
| 60 | + |
| 61 | +script.on_load(function(event) |
| 62 | + if global.structs_count > 0 then |
| 63 | + script.on_event(defines.events.on_tick, on_tick) |
| 64 | + end |
| 65 | +end) |
| 66 | + |
| 67 | +local function on_created_entity(event) |
| 68 | + local entity = event.created_entity or event.entity or event.destination |
| 69 | + |
| 70 | + -- SE energy beam receiver & SE energy beam injector |
| 71 | + if entity.prototype.burner_prototype == nil then return end |
| 72 | + |
| 73 | + -- case not yet handled (is it * the transfer, or does it also do something to the buffered energy?) |
| 74 | + assert(entity.prototype.burner_prototype.effectivity == 1) |
| 75 | + |
| 76 | + global.structs_count = global.structs_count + 1 |
| 77 | + global.structs[entity.unit_number] = { |
| 78 | + unit_number = entity.unit_number, |
| 79 | + entity = entity, |
| 80 | + tick = event.tick, |
| 81 | + } |
| 82 | + |
| 83 | + script.on_event(defines.events.on_tick, on_tick) |
| 84 | +end |
| 85 | + |
| 86 | +script.on_event(defines.events.on_selected_entity_changed, function(event) |
| 87 | + local player = game.get_player(event.player_index) |
| 88 | + if player.selected and player.selected.type == 'reactor' and global.structs[player.selected.unit_number] == nil then |
| 89 | + on_created_entity({entity = player.selected, tick = event.tick}) |
| 90 | + end |
| 91 | +end) |
| 92 | + |
| 93 | +for _, event in ipairs({ |
| 94 | + defines.events.on_built_entity, |
| 95 | + defines.events.on_robot_built_entity, |
| 96 | + defines.events.script_raised_built, |
| 97 | + defines.events.script_raised_revive, |
| 98 | + defines.events.on_entity_cloned, |
| 99 | +}) do |
| 100 | + script.on_event(event, on_created_entity, { |
| 101 | + {filter = 'type', type = 'reactor'}, |
| 102 | + }) |
| 103 | +end |
| 104 | + |
| 105 | +local function on_configuration_changed(event) |
| 106 | + global.max_energy_usage = {} |
| 107 | + global.max_temperature = {} |
| 108 | + global.specific_heat = {} |
| 109 | + |
| 110 | + local prototypes = game.get_filtered_entity_prototypes{{filter='type', type='reactor'}} |
| 111 | + for _, prototype in pairs(prototypes) do |
| 112 | + global.max_energy_usage[prototype.name] = prototype.max_energy_usage |
| 113 | + global.max_temperature[prototype.name] = prototype.heat_buffer_prototype.max_temperature |
| 114 | + global.specific_heat[prototype.name] = prototype.heat_buffer_prototype.specific_heat |
| 115 | + end |
| 116 | +end |
| 117 | + |
| 118 | +script.on_init(function(event) |
| 119 | + global.structs = {} |
| 120 | + global.structs_count = 0 |
| 121 | + on_configuration_changed() |
| 122 | +end) |
| 123 | + |
| 124 | +script.on_configuration_changed(on_configuration_changed) |
| 125 | + |
| 126 | +-- commands.add_command("reactors-warm-up-faster-debug", nil, function(command) |
| 127 | +-- local player = game.get_player(command.player_index) |
| 128 | + |
| 129 | +-- player.print(serpent.block({ |
| 130 | +-- structs_count = global.structs_count, |
| 131 | +-- })) |
| 132 | +-- end) |
0 commit comments