z_mirror/Krastorio2
git clone https://git.raiguard.me/z_mirror/Krastorio2
21723abc
real-fusion-reactor
1local util =require ( "scripts.util" ) 2 3--- @class ShelterData 4--- @field container LuaEntity 5--- @field electric LuaEntity 6--- @field light LuaEntity 7--- @field former_spawn_point MapPosition 8 9--- @param force LuaForce 10local function init_force ( force ) 11if not storage . shelter then 12return 13end 14storage . shelter . forces [ force . index ] ={} 15end 16 17local function spawn_flying_texts () 18for unit_number , entity in pairs ( storage . shelter . inactive ) do 19if entity . valid then 20util . entity_flying_text ( entity , { "message.kr-shelter-is-inactive" } , { r =1 } ) 21else 22storage . shelter . inactive [ unit_number ] =nil 23end 24end 25end 26 27--- @param e EntityBuiltEvent 28local function on_entity_built ( e ) 29local entity =e . entity or e . destination 30if not entity . valid then 31return 32end 33if entity . name ~= "kr-shelter-container" and entity . name ~= "kr-shelter-plus-container" then 34return 35end 36--- @cast entity LuaEntity 37 38local force =entity . force 39local surface =entity . surface 40local force_shelters =storage . shelter . forces [ force . index ] 41 42if force_shelters [ surface . index ] then 43local name =entity . name : gsub ( "kr%-" , "kr-inactive-" ): gsub ( "%-container" , "" ) 44local position =entity . position 45local force =entity . force 46local player =entity . last_user 47local surface =entity . surface 48entity . destroy () 49 50local new_entity =surface . create_entity ( { 51name =name , 52position =position , 53force =force , 54player =player , 55create_build_effect_smoke =false , 56raise_built =true , 57} ) 58if new_entity and new_entity . valid then 59storage . shelter . inactive [ new_entity . unit_number ] =new_entity 60end 61return 62end 63 64-- Build entities 65local _ , _ , base_name =string . find ( entity . name , "^(.*)%-container" ) 66--- @type ShelterData 67local shelter_data ={ container =entity } 68 69for key , suffix in pairs ( { electric ="" , light ="-light" } ) do 70local name =base_name .. suffix 71shelter_data [ key ] =surface . create_entity ( { 72name =name , 73position =entity . position , 74force =force , 75player =entity . last_user , 76create_build_effect_smoke =false , 77raise_built =true , 78} ) 79end 80 81-- Set spawn point 82shelter_data . former_spawn_point =force . get_spawn_position ( surface ) 83force . set_spawn_position ( { x =entity . position . x , y =entity . position . y + 3.5 } , surface ) 84 85force_shelters [ surface . index ] =shelter_data 86end 87 88--- @param e EntityDestroyedEvent 89local function on_entity_destroyed ( e ) 90local entity =e . entity 91if not entity or not entity . valid then 92return 93end 94if entity . name ~= "kr-shelter-container" and entity . name ~= "kr-shelter-plus-container" then 95return 96end 97 98local inactive =storage . shelter . inactive [ entity . unit_number ] 99if inactive then 100storage . shelter . inactive [ entity . unit_number ] =nil 101else 102local force =entity . force 103 104local force_shelters =storage . shelter . forces [ force . index ] 105if not force_shelters then 106return 107end 108 109local surface_index =entity . surface . index 110local shelter_data =force_shelters [ surface_index ] 111if not shelter_data then 112return 113end 114 115-- Destroy entities 116shelter_data . electric . destroy () 117shelter_data . light . destroy () 118force_shelters [ surface_index ] =nil 119 120-- Restore former spawn point 121force . set_spawn_position ( shelter_data . former_spawn_point , surface_index ) 122end 123end 124 125--- @param e EventData.on_force_created 126local function on_force_created ( e ) 127if e . force . valid then 128init_force ( e . force ) 129end 130end 131 132local shelter ={} 133 134function shelter . on_init () 135storage . shelter ={ 136--- @type table<uint, table<uint, ShelterData>> 137forces ={} , 138--- @type table<uint, LuaEntity> 139inactive ={} , 140} 141 142for _ , force in pairs ( game . forces ) do 143init_force ( force ) 144end 145end 146 147shelter . events ={ 148[ defines . events . on_built_entity ] =on_entity_built , 149[ defines . events . on_entity_cloned ] =on_entity_built , 150[ defines . events . on_entity_died ] =on_entity_destroyed , 151[ defines . events . on_force_created ] =on_force_created , 152[ defines . events . on_player_mined_entity ] =on_entity_destroyed , 153[ defines . events . on_robot_built_entity ] =on_entity_built , 154[ defines . events . on_robot_mined_entity ] =on_entity_destroyed , 155[ defines . events . script_raised_built ] =on_entity_built , 156[ defines . events . script_raised_destroy ] =on_entity_destroyed , 157[ defines . events . script_raised_revive ] =on_entity_built , 158[ defines . events . on_space_platform_built_entity ] =on_entity_built , 159[ defines . events . on_space_platform_mined_entity ] =on_entity_destroyed , 160} 161 162shelter . on_nth_tick ={ 163[ 180 ] =spawn_flying_texts , 164} 165 166return shelter