z_mirror/Krastorio2
git clone https://git.raiguard.me/z_mirror/Krastorio2
c358df2e
santichu95/patch-1
1local flib_direction =require ( "__flib__.direction" ) 2local flib_position =require ( "__flib__.position" ) 3 4--- @type table<defines.direction, Vector> 5local offsets ={ 6[ defines . direction . north ] ={ 0 , - 1 } , 7[ defines . direction . east ] ={ 1 , 0 } , 8[ defines . direction . south ] ={ 0 , 1 } , 9[ defines . direction . west ] ={ - 1 , 0 } , 10} 11 12local belt_types ={ 13"linked-belt" , 14"loader" , 15"loader-1x1" , 16"splitter" , 17"transport-belt" , 18"underground-belt" , 19} 20 21local container_types ={ 22"assembling-machine" , 23"beacon" , 24"boiler" , 25"burner-generator" , 26"container" , 27"curved-rail" , 28"furnace" , 29"infinity-container" , 30"lab" , 31"logistic-container" , 32"mining-drill" , 33"rail" , 34"reactor" , 35"rocket-silo" , 36"straight-rail" , 37} 38 39--- @param entity LuaEntity 40local function flip_loader ( entity ) 41entity . direction =flib_direction . opposite ( entity . direction ) 42entity . loader_type =entity . loader_type == "output" and "input" or "output" 43end 44 45--- Find entities and entity ghosts of the corresponding types. 46--- @param surface LuaSurface 47--- @param position MapPosition 48--- @param force ForceID 49--- @param types string[] 50--- @return LuaEntity[] 51local function find_entities ( surface , position , force , types ) 52local entities =surface . find_entities_filtered ( { 53force =force , 54position =position , 55type =types , 56} ) 57local ghosts =surface . find_entities_filtered ( { 58force =force , 59ghost_type =types , 60position =position , 61} ) 62for i =1 , # ghosts do 63entities [ # entities + 1 ] =ghosts [ i ] 64end 65return entities 66end 67 68--- Ensure that the loader has the belt on the correct side. 69--- @param entity LuaEntity 70local function snap_direction ( entity ) 71local offset_direction =entity . direction 72if entity . loader_type == "input" then 73offset_direction =flib_direction . opposite ( offset_direction ) 74end 75local container_position =flib_position . add ( entity . position , offsets [ offset_direction ]) 76 77-- Case 1: If a container is in front, then flip the loader 78local containers =find_entities ( entity . surface , container_position , entity . force , container_types ) 79if # containers > 0 then 80-- Check that it's not a composite loader entity 81local is_composite =entity . surface . count_entities_filtered ( { 82force =entity . force , 83position =container_position , 84type ={ "loader" , "loader-1x1" } , 85} ) > 0 86if not is_composite then 87flip_loader ( entity ) 88return 89end 90end 91 92-- Case 2: If a belt is behind, then flip the loader 93offset_direction =flib_direction . opposite ( offset_direction ) 94local belt_position =flib_position . add ( entity . position , offsets [ offset_direction ]) 95local belt =find_entities ( entity . surface , belt_position , entity . force , belt_types )[ 1 ] 96if not belt then 97return 98end 99 100local belt_type =belt . type 101if belt_type == "entity-ghost" then 102belt_type =belt . ghost_type 103end 104if 105belt_type == "transport-belt" 106or belt_type == "underground-belt" 107or math . abs ( offset_direction - belt . direction ) % 4 == 0 108then 109flip_loader ( entity ) 110end 111end 112 113--- @param entity LuaEntity 114local function snap_to_belt ( entity ) 115local offset_direction =entity . direction 116if entity . loader_type == "input" then 117offset_direction =flib_direction . opposite ( offset_direction ) 118end 119local belt_position =flib_position . add ( entity . position , offsets [ offset_direction ]) 120 121local belt =find_entities ( entity . surface , belt_position , entity . force , belt_types )[ 1 ] 122if not belt then 123return 124end 125 126local belt_direction =belt . direction 127local belt_type =belt . type 128if belt_type == "entity-ghost" then 129belt_type =belt . ghost_type 130end 131if entity . direction == belt_direction then 132-- Pass 133elseif entity . direction == flib_direction . opposite ( belt_direction ) then 134entity . loader_type ="input" 135elseif belt_type == "transport-belt" or belt_type == "underground-belt" then 136-- Sideloading 137entity . loader_type ="output" 138end 139end 140 141--- @param e EntityBuiltEvent 142local function on_entity_built ( e ) 143local entity =e . entity or e . destination 144if not entity . valid or not string . match ( entity . name , "^kr.*%-loader$" ) then 145return 146end 147 148entity . update_connections () 149if not entity . loader_container then 150snap_direction ( entity ) 151end 152 153snap_to_belt ( entity ) 154end 155 156local snap_loader ={} 157 158snap_loader . events ={ 159[ defines . events . on_built_entity ] =on_entity_built , 160[ defines . events . on_entity_cloned ] =on_entity_built , 161[ defines . events . on_robot_built_entity ] =on_entity_built , 162[ defines . events . script_raised_built ] =on_entity_built , 163[ defines . events . script_raised_revive ] =on_entity_built , 164[ defines . events . on_space_platform_built_entity ] =on_entity_built , 165} 166 167return snap_loader