z_mirror/Krastorio2
git clone https://git.raiguard.me/z_mirror/Krastorio2
cb2d9fed
santichu95/patch-1
1local flib_math =require ( "__flib__.math" ) 2 3local biter_virus_evolution_multiplier =0.67 4 5local virus_iteration_counts ={ 6[ 1000 ] =20 , 7[ 5000 ] =40 , 8[ 10000 ] =80 , 9[ 20000 ] =160 , 10[ 30000 ] =280 , 11[ 40000 ] =300 , 12[ 50000 ] =400 , 13[ flib_math . max_int53 ] =800 , 14} 15 16--- Modifier to change the quantity of objects iterated for each round of the function, based on the total 17--- @param count number 18local function get_removal_count ( count ) 19for limit , per in pairs ( virus_iteration_counts ) do 20if count <= limit then 21return per 22end 23end 24end 25 26--- @param virus_data BiterVirusData 27local function iterate_biter_virus ( virus_data ) 28local surface =virus_data . surface 29if not surface or not surface . valid then 30storage . virus . biter [ surface . index ] =nil 31return 32end 33 34local entities =virus_data . entities 35local entities_killed =virus_data . entities_killed 36local entities_to_kill =virus_data . entities_to_kill 37local force =virus_data . force 38local len =virus_data . entities_len 39 40-- Kill some entities 41for _ =1 , virus_data . amount_per_iteration do 42if entities_killed >= entities_to_kill then 43storage . virus . biter [ surface . index ] =nil 44break 45end 46 47local i =math . random ( 1 , len ) 48local entity =entities [ i ] 49if entity and entity . valid then 50entity . die ( force ) 51 52-- Move the element at the end to the gap 53-- This removes the element at `i` while achieving O(1) performance 54entities [ i ] =entities [ len ] 55entities [ len ] =nil 56 57entities_killed =entities_killed + 1 58len =len - 1 59end 60end 61 62virus_data . entities_killed =entities_killed 63virus_data . tiles_len =len 64end 65 66--- @param player LuaPlayer 67--- @param surface LuaSurface 68local function init_biter_virus ( player , surface ) 69local biter_viruses =storage . virus . biter 70if not biter_viruses [ surface . index ] then 71-- Reduce evolution factor 72local enemy =game . forces . enemy 73enemy . evolution_factor =enemy . evolution_factor * biter_virus_evolution_multiplier 74 75-- Begin gradual enemy killoff 76local enemy_entities =surface . find_entities_filtered ( { force ="enemy" } ) 77local len =# enemy_entities 78if len > 0 then 79--- @class BiterVirusData 80biter_viruses [ surface . index ] ={ 81amount_per_iteration =math . ceil ( len / get_removal_count ( len )), 82entities =enemy_entities , 83entities_killed =0 , 84entities_len =len , 85entities_to_kill =len / 2.5 , 86force =player . force , 87surface =surface , 88} 89end 90end 91end 92 93--- @param e EventData.on_player_used_capsule 94local function on_player_used_capsule ( e ) 95local item =e . item 96if not item or not item . valid then 97return 98end 99 100local player =game . get_player ( e . player_index ) --[[@as LuaPlayer]] 101local surface =player . surface 102 103if item . name == "kr-biter-virus" then 104init_biter_virus ( player , surface ) 105end 106end 107 108local function on_tick () 109for _ , biter_virus in pairs ( storage . virus . biter ) do 110iterate_biter_virus ( biter_virus ) 111end 112end 113 114local virus ={} 115 116function virus . on_init () 117storage . virus ={ 118--- @type table<uint, BiterVirusData> 119biter ={} , 120} 121end 122 123virus . events ={ 124[ defines . events . on_player_used_capsule ] =on_player_used_capsule , 125[ defines . events . on_tick ] =on_tick , 126} 127 128return virus