<commit_before>--------------------------------------------------------------------------------
-- EVENT BALANCING LIB ---------------------------------------------------------
--------------------------------------------------------------------------------
-- darkconsole <<EMAIL>> ---------------------------------------

--[[ SEE README.MD FOR DETAILS YO ]]--

--------------------------------------------------------------------------------
-- STOCK FILE MODIFICATIONS ----------------------------------------------------
--------------------------------------------------------------------------------

-- player\eventscheduler.lua
-- + require event balancer
-- + increase event delay
-- + disable multiple players speeding up events
-- + determine if event should be skipped

-- events\pirateattack.lua
-- + require event balancer
-- + determine if event should be skipped

--------------------------------------------------------------------------------
-- MOD CONFIG ------------------------------------------------------------------
--------------------------------------------------------------------------------

EventBalance = {
	PauseMultiplier = 8,     -- mutiplier for delay between events
	SkipWindowCap   = 10,    -- max ships before we stop curving chance
	SkipWindowFlex  = 0.75,  -- multiplier for ship count impact
	SkipWindowFloat = 750,   -- artificially float the sector's actual average
	SkipWindow      = 33.0,  -- percentage of sector volume ripe for attack
	SkipChance      = 20.0,  -- percentage flat chance to skip for no reason
	Debug           = true   -- if we should print stupid things to console.
}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- vscode-fold=1

require("galaxy")

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function EventBalance.GetSectorShipInfo(Coord)
-- @argv vec2 SectorCoords
-- @return [ int Count, float Volume, float VolumeAverage ]
-- find out some information about the ships in this sector.
-- Float Volume of Ships

	local Key
	local Value

	local Count = 0
	local Volume = 0
	local VolumeAverage = 0

	--------
	-- find all the ships, counting how many we have and how much total volume
	-- they take up in this sector.
	--------

	for Key,Value in pairs({Sector(Coord):getEntitiesByType(EntityType.Ship)})
	do
		Volume = Volume + Value.volume
		Count = Count + 1
	end

	--------
	-- determine the mean volume of the ships in this sector.
	--------

	VolumeAverage = Volume / Count

	--------

	return Count, Volume, VolumeAverage
end

function EventBalance.GetFactoredSkipWindow(ShipCount)
-- @argv Float SkipWindow, Int ShipCount
-- @argv Float
-- modify the skip window based on how many ships are in this sector. this is to
-- attempt to simulate the pirates needing to be sure they want to attack the
-- sector by narrowing the window the more ships there are.

	if(ShipCount > EventBalance.SkipWindowCap) then
		ShipCount = EventBalance.SkipWindowCap
	elseif(ShipCount < 1) then
		ShipCount = 0
	end

	return EventBalance.SkipWindow - (ShipCount * EventBalance.SkipWindowFlex)
end

function EventBalance.ShouldSkipEvent(Event)
-- the purpose of this method is to determine if we should skip the event that
-- tried to happen. this is based on the "power" of a sector. the more empty a
-- sector is the less likely pirates will think it worth attacking. on the flip
-- side once the sector becomes too strong pirates should think twice about
-- attempting to pillage it.

	--------
	-- passive events that won't fuck up your day are always allowed.
	--------

	if(Event.script == "spawntravellingmerchant")
	then
		return false
	end

	if(Event.script == "convoidistresssignal")
	then
		return false
	end

	--------
	-- chances that an event may be skipped
	--------

	if(EventBalance.ShouldSkipEvent_BySectorVolume(Event) == true) then
		return true
	end

	if(EventBalance.ShouldSkipEvent_ByFlatChance(Event) == true) then
		return true
	end

	--------
	--------

	return false
end

function EventBalance.ShouldSkipEvent_BySectorVolume(Event)
-- analyze the sector and decide if we should skip this event based on the
-- volume of ships within it.

	local
		ShipTotalCount,
		ShipTotalVolume,
		ShipAverageVolume
	= EventBalance.GetSectorShipInfo(Sector():getCoordinates())

	local SectorAverageVolume = Balancing_GetSectorShipVolume(Sector():getCoordinates())
	local SectorAllowedDiff = (EventBalance.GetFactoredSkipWindow(ShipTotalCount) * SectorAverageVolume) / 100

	if(EventBalance.Debug)
	then
		print("---- Event Balancer: Sector Analysis (" .. Event.script .. ")")
		print("-- Ships In Sector: Total(" .. ShipTotalCount .. "), Volume(" .. ShipTotalVolume .. "), AverageVolume(" .. ShipAverageVolume .. ")")
		print("-- Sector Values: AverageVolume(" .. SectorAverageVolume .. ") AllowedDiff(" .. SectorAllowedDiff .. " (" .. EventBalance.SkipWindow .. "%))")
	end

	if(ShipAverageVolume <= (SectorAverageVolume + EventBalance.SkipWindowFloat) - SectorAllowedDiff) then
		if(EventBalance.Debug)
		then
			print(">> " .. ShipAverageVolume .. " <= " .. (SectorAverageVolume - SectorAllowedDiff) .. " + " .. EventBalance.SkipWindowFloat)
			print(">> sector too boring for attack")
			print(" ")
		end

		return EventBalance.ShouldSkipEvent_ByFlatChance(Event) and EventBalance.ShouldSkipEvent_ByFlatChance(Event)
	elseif(ShipAverageVolume >= (SectorAverageVolume + EventBalance.SkipWindowFloat) + SectorAllowedDiff) then
		if(EventBalance.Debug)
		then
			print(">> " .. ShipAverageVolume .. " >= " .. (SectorAverageVolume + SectorAllowedDiff) .. " + " .. EventBalance.SkipWindowFloat)
			print(">> sector too strong for attack")
			print(" ")
		end

		return EventBalance.ShouldSkipEvent_ByFlatChance(Event) and EventBalance.ShouldSkipEvent_ByFlatChance(Event)
	end

	return false
end

function EventBalance.ShouldSkipEvent_ByFlatChance(Event)
-- decide if we should skip the event based on a stupid flat chance.

	if(EventBalance.SkipChance > 0) then
		return random():getInt(1,100) < EventBalance.SkipChance
	end

	return false
end<commit_msg>more balance updates, adds flat chance for volume rejects<commit_after>--------------------------------------------------------------------------------
-- EVENT BALANCING LIB ---------------------------------------------------------
--------------------------------------------------------------------------------
-- darkconsole <<EMAIL>> ---------------------------------------

--[[ SEE README.MD FOR DETAILS YO ]]--

--------------------------------------------------------------------------------
-- STOCK FILE MODIFICATIONS ----------------------------------------------------
--------------------------------------------------------------------------------

-- player\eventscheduler.lua
-- + require event balancer
-- + increase event delay
-- + disable multiple players speeding up events
-- + determine if event should be skipped

-- events\pirateattack.lua
-- + require event balancer
-- + determine if event should be skipped

--------------------------------------------------------------------------------
-- MOD CONFIG ------------------------------------------------------------------
--------------------------------------------------------------------------------

EventBalance = {
	PauseMultiplier   = 8,     -- mutiplier for delay between events
	SkipWindowCap     = 10,    -- max ships before we stop curving chance
	SkipWindowFlex    = 0.75,  -- multiplier for ship count impact
	SkipWindowFloat   = 750,   -- artificially float the sector's actual average
	SkipWindow        = 33.0,  -- percentage of sector volume ripe for attack
	SkipChance        = 20.0,  -- percentage flat chance to allow event
	SkipChanceVolume  = 10.0,  -- percentage flat chance, when for volume
	Debug             = true   -- if we should print stupid things to console.
}

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

-- vscode-fold=1

require("galaxy")

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function EventBalance.GetSectorShipInfo(Coord)
-- @argv vec2 SectorCoords
-- @return [ int Count, float Volume, float VolumeAverage ]
-- find out some information about the ships in this sector.
-- Float Volume of Ships

	local Key
	local Value

	local Count = 0
	local Volume = 0
	local VolumeAverage = 0

	--------
	-- find all the ships, counting how many we have and how much total volume
	-- they take up in this sector.
	--------

	for Key,Value in pairs({Sector(Coord):getEntitiesByType(EntityType.Ship)})
	do
		Volume = Volume + Value.volume
		Count = Count + 1
	end

	--------
	-- determine the mean volume of the ships in this sector.
	--------

	VolumeAverage = Volume / Count

	--------

	return Count, Volume, VolumeAverage
end

function EventBalance.GetFactoredSkipWindow(ShipCount)
-- @argv Float SkipWindow, Int ShipCount
-- @argv Float
-- modify the skip window based on how many ships are in this sector. this is to
-- attempt to simulate the pirates needing to be sure they want to attack the
-- sector by narrowing the window the more ships there are.

	if(ShipCount > EventBalance.SkipWindowCap) then
		ShipCount = EventBalance.SkipWindowCap
	elseif(ShipCount < 1) then
		ShipCount = 0
	end

	return EventBalance.SkipWindow - (ShipCount * EventBalance.SkipWindowFlex)
end

function EventBalance.ShouldSkipEvent(Event)
-- the purpose of this method is to determine if we should skip the event that
-- tried to happen. this is based on the "power" of a sector. the more empty a
-- sector is the less likely pirates will think it worth attacking. on the flip
-- side once the sector becomes too strong pirates should think twice about
-- attempting to pillage it.

	--------
	-- passive events that won't fuck up your day are always allowed.
	--------

	if(Event.script == "spawntravellingmerchant")
	then
		return false
	end

	if(Event.script == "convoidistresssignal")
	then
		return false
	end

	--------
	-- chances that an event may be skipped
	--------

	if(EventBalance.ShouldSkipEvent_BySectorVolume(Event) == true) then
		return true
	end

	if(EventBalance.ShouldSkipEvent_ByFlatChance(Event,false) == true) then
		return true
	end

	--------
	--------

	return false
end

function EventBalance.ShouldSkipEvent_BySectorVolume(Event)
-- analyze the sector and decide if we should skip this event based on the
-- volume of ships within it.

	local
		ShipTotalCount,
		ShipTotalVolume,
		ShipAverageVolume
	= EventBalance.GetSectorShipInfo(Sector():getCoordinates())

	local SectorAverageVolume = Balancing_GetSectorShipVolume(Sector():getCoordinates())
	local SectorAllowedDiff = (EventBalance.GetFactoredSkipWindow(ShipTotalCount) * SectorAverageVolume) / 100

	if(EventBalance.Debug)
	then
		print("---- Event Balancer: Sector Analysis (" .. Event.script .. ")")
		print("-- Ships In Sector: Total(" .. ShipTotalCount .. "), Volume(" .. ShipTotalVolume .. "), AverageVolume(" .. ShipAverageVolume .. ")")
		print("-- Sector Values: AverageVolume(" .. SectorAverageVolume .. ") AllowedDiff(" .. SectorAllowedDiff .. " (" .. EventBalance.SkipWindow .. "%))")
	end

	if(ShipAverageVolume <= (SectorAverageVolume + EventBalance.SkipWindowFloat) - SectorAllowedDiff) then
		if(EventBalance.Debug)
		then
			print(">> " .. ShipAverageVolume .. " <= " .. (SectorAverageVolume - SectorAllowedDiff) .. " + " .. EventBalance.SkipWindowFloat)
			print(">> sector too boring for attack")
			print(" ")
		end

		return EventBalance.ShouldSkipEvent_ByFlatChance(Event,true)
	elseif(ShipAverageVolume >= (SectorAverageVolume + EventBalance.SkipWindowFloat) + SectorAllowedDiff) then
		if(EventBalance.Debug)
		then
			print(">> " .. ShipAverageVolume .. " >= " .. (SectorAverageVolume + SectorAllowedDiff) .. " + " .. EventBalance.SkipWindowFloat)
			print(">> sector too strong for attack")
			print(" ")
		end

		return EventBalance.ShouldSkipEvent_ByFlatChance(Event,true)
	end

	return false
end

function EventBalance.ShouldSkipEvent_ByFlatChance(Event, FactorVolume)
-- decide if we should skip the event based on a stupid flat chance.

	local SkipChance

	if(FactorVolume) then
		SkipChance = EventBalance.SkipChanceVolume
	else
		SkipChance = EventBalance.SkipChance
	end

	if(EventBalance.SkipChance > 0) then
		return not random():getInt(1,100) < SkipChance
	end

	return false
end
{'fraction_non_alphanumeric': 0.2682945557859172, 'fraction_numerical': 0.004991169469400291, 'mean_word_length': 1.7407407407407407, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2755692', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3149, 'n_words': 1037}