z_mirror/Krastorio2
git clone https://git.raiguard.me/z_mirror/Krastorio2
26acaf0d
santichu95/patch-1
1local flib_position =require ( "__flib__.position" ) 2 3local util =require ( "scripts.util" ) 4 5--- @class RadioactivityPlayerData 6--- @field entity boolean 7--- @field inventory boolean 8--- @field last_position MapPosition 9 10local default_entities ={ "uranium-ore" } 11local default_items ={ 12"nuclear-fuel" , 13"uranium-235" , 14"uranium-238" , 15"uranium-fuel-cell" , 16"uranium-ore" , 17"depleted-uranium-fuel-cell" , 18} 19local range =7 20 21--- @param player LuaPlayer 22local function check_inventory ( player ) 23if not storage . radioactivity . enabled or not next ( storage . radioactivity . entities ) then 24return 25end 26 27local player_data =storage . radioactivity . players [ player . index ] 28if not player_data then 29return 30end 31 32if not player . character or not player . character . valid then 33player_data . inventory =false 34return 35end 36 37for _ , item_name in pairs ( storage . radioactivity . items ) do 38-- FIXME: This does not check trash inventory 39if player . get_item_count ( item_name ) > 0 then 40player_data . inventory =true 41return 42end 43end 44 45player_data . inventory =false 46end 47 48--- @param player LuaPlayer 49local function check_around_player ( player ) 50if not storage . radioactivity . enabled or not next ( storage . radioactivity . items ) then 51return 52end 53 54local player_data =storage . radioactivity . players [ player . index ] 55if not player_data then 56return 57end 58 59if not player . character or not player . character . valid then 60player_data . entity =false 61return 62end 63 64local position =flib_position . floor ( player . physical_position ) 65local last_position =player_data . last_position 66if flib_position . eq ( position , last_position ) then 67return 68end 69player_data . last_position =position 70 71player_data . entity =player . surface . count_entities_filtered ( { 72name =storage . radioactivity . entities , 73radius =range , 74position =position , 75} ) > 0 76end 77 78--- @param player LuaPlayer 79local function add_player ( player ) 80storage . radioactivity . players [ player . index ] ={ 81entity =false , 82inventory =false , 83last_position ={ x =0 , y =0 } , 84} 85end 86 87--- @param player_index uint 88local function remove_player ( player_index ) 89storage . radioactivity . players [ player_index ] =nil 90end 91 92local function update_and_damage () 93if not storage . radioactivity . enabled then 94return 95end 96 97for player_index , player_data in pairs ( storage . radioactivity . players ) do 98if not player_data . entity and not player_data . inventory then 99goto continue 100end 101 102local player =game . get_player ( player_index ) 103if not player or not player . connected or not player . character then 104goto continue 105end 106 107player . add_custom_alert ( 108player . character , 109{ type ="virtual" , name ="kr-nuclear-2" } , 110{ "other.kr-taking-radioactive-damage" } , 111false 112) 113 114-- Damage the player 115-- TODO: Account for armor and energy shields 116local base_damage =7.25 117player . character . damage ( base_damage , "enemy" , "radioactive" ) 118 119:: continue :: 120end 121end 122 123local function update_sounds () 124if not storage . radioactivity . enabled then 125return 126end 127 128for player_index , player_data in pairs ( storage . radioactivity . players ) do 129if not player_data . entity and not player_data . inventory then 130goto continue 131end 132 133local player =game . get_player ( player_index ) 134if not player or not player . connected then 135goto continue 136end 137 138player . play_sound ( { 139path ="kr-radioactive" , 140position =player . physical_position , 141volume_modifier =0.5 , 142} ) 143 144:: continue :: 145end 146end 147 148--- @param e EventData.on_player_changed_position|EventData.on_player_changed_surface 149local function on_player_moved ( e ) 150local player =game . get_player ( e . player_index ) 151if player then 152check_around_player ( player ) 153end 154end 155 156--- @param e EventData.on_player_main_inventory_changed|EventData.on_player_trash_inventory_changed 157local function on_player_inventory_changed ( e ) 158local player =game . get_player ( e . player_index ) 159if player then 160check_inventory ( player ) 161end 162end 163 164--- @param e EventData.on_player_created 165local function on_player_created ( e ) 166local player =game . get_player ( e . player_index ) 167if not player then 168return 169end 170 171add_player ( player ) 172end 173 174--- @param e EventData.on_player_removed 175local function on_player_removed ( e ) 176remove_player ( e . player_index ) 177end 178 179local radioactivity ={} 180 181function radioactivity . on_init () 182storage . radioactivity ={ 183enabled =true , 184--- @type table<uint, RadioactivityPlayerData> 185players ={} , 186} 187radioactivity . on_configuration_changed () 188end 189 190function radioactivity . on_configuration_changed () 191-- TODO: Migration 192storage . radioactivity . entities ={} 193for _ , entity_name in pairs ( default_entities ) do 194if prototypes . entity [ entity_name ] then 195table . insert ( storage . radioactivity . entities , entity_name ) 196end 197end 198storage . radioactivity . items ={} 199for _ , item_name in pairs ( default_items ) do 200if prototypes . item [ item_name ] then 201table . insert ( storage . radioactivity . items , item_name ) 202end 203end 204end 205 206remote . add_interface ( "kr-radioactivity" , { 207add_entity =function ( name ) 208if not storage . radioactivity then 209return 210end 211if not name or type ( name ) ~= "string" then 212error ( "`name` must be a string." ) 213end 214 215if not prototypes . entity [ name ] then 216error ( "Entity `" .. name .. "` does not exist." ) 217end 218 219table . insert ( storage . radioactivity . entities , name ) 220end , 221add_item =function ( name ) 222if not storage . radioactivity then 223return 224end 225if not name or type ( name ) ~= "string" then 226error ( "`name` must be a string." ) 227end 228 229if not prototypes . item [ name ] then 230error ( "Item `" .. name .. "` does not exist." ) 231end 232 233table . insert ( storage . radioactivity . items , name ) 234end , 235set_enabled =function ( to_state ) 236if not storage . radioactivity then 237return 238end 239if to_state == nil or type ( to_state ) ~= "boolean" then 240error ( "`to_state` must be a boolean." ) 241end 242 243storage . radioactivity . enabled =to_state 244end , 245} ) 246 247util . add_commands ( { 248[ "kr-disable-radioactivity" ] =function () 249storage . radioactivity . enabled =false 250game . ( { "message.kr-radioactivity-disabled" } ) 251end , 252[ "kr-enable-radioactivity" ] =function () 253storage . radioactivity . enabled =true 254game . ( { "message.kr-radioactivity-enabled" } ) 255end , 256} ) 257 258radioactivity . events ={ 259[ defines . events . on_player_changed_position ] =on_player_moved , 260[ defines . events . on_player_changed_surface ] =on_player_moved , 261[ defines . events . on_player_created ] =on_player_created , 262[ defines . events . on_player_cursor_stack_changed ] =on_player_inventory_changed , 263[ defines . events . on_player_died ] =on_player_moved , 264[ defines . events . on_player_main_inventory_changed ] =on_player_inventory_changed , 265[ defines . events . on_player_removed ] =on_player_removed , 266[ defines . events . on_player_respawned ] =on_player_moved , 267[ defines . events . on_player_toggled_map_editor ] =on_player_moved , 268[ defines . events . on_player_trash_inventory_changed ] =on_player_inventory_changed , 269[ defines . events . on_tick ] =update_sounds , 270} 271 272radioactivity . on_nth_tick ={ 273[ 20 ] =update_and_damage , 274} 275 276return radioactivity