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