Classes

Concepts

Libraries

advertisement »

ambientSounds »

bitser »

contentPoints »

eventBoxText »

factValidity »

frameBuffer »

officeBuildingInserter »

priorityRenderer »

randomEvents »

scaling »

spritesheetParser »

statusIcons »

test3 »

util »

Objects

registerNew

Description

Registers a new random event.

Arguments

1 table data

the random event data.

Example

local parentDeath = {}
parentDeath.id = "parent_death"
parentDeath.eventRequirement = timeline.EVENTS.NEW_DAY
parentDeath.rollMin = 1
parentDeath.rollMax = 10000
parentDeath.occurChance = 1 -- 1 in 10000

parentDeath.awayDuration = 2 -- 2 days

parentDeath.validEmployees = {}

parentDeath.fact_baselineAge = 20 -- the additional chance per year is only added if the person's age is equal to or greater than this value

parentDeath.fact_deathChance = 20 -- the base chance

parentDeath.fact_chancePerYear = 1.75 -- additional success chance per each yeah past the fact_baselineAge value


parentDeath.formatTable = {}
parentDeath.popupText = _T("RELATIVE_DEATH_DESC", "A relative of NAME SURNAME has died. They have taken leave for TIMETEXT.")
parentDeath.popupTitle = _T("RELATIVE_DEATH", "Death of relative")

function parentDeath:occur()
	for key, employee in ipairs(studio:getEmployees()) do
		if employee:isAvailable() and not employee:getFact(self.id) and not employee:isPlayerCharacter() then
			table.insert(self.validEmployees, employee)
		end
	end
	
	if #self.validEmployees > 0 then
		local randomIndex = math.random(1, #self.validEmployees)
		local randomEmployee = self.validEmployees[randomIndex]
		
		randomEmployee:setFact(self.id, true)
		randomEmployee:setAwayUntil(timeline.curTime + self.awayDuration)
		
		self.formatTable.NAME = randomEmployee:getName()
		self.formatTable.SURNAME = randomEmployee:getSurname()
		self.formatTable.TIMETEXT = timeline:getTimePeriodText(self.awayDuration) --string.timegroup(self.awayDuration * 86400))

		
		local popup = gui.create("Popup")
		popup:setWidth(400)
		popup:centerX()
		popup:setTextFont(fonts.get("pix20"))
		popup:setFont(fonts.get("pix24"))
		popup:setTitle(self.popupTitle)
		popup:setText(string.formatbykeys(self.popupText, self.formatTable))
		popup:setDepth(105)
		popup:addButton(fonts.get("pix20"), "OK")
		popup:performLayout()
		popup:centerY()
		
		frameController:push(popup)
	end
	
	table.clear(self.validEmployees) -- it has to be clean after we've done everything

end

function parentDeath:validateFact(employee)
	local yearAffector = math.max(employee:getAge() - self.fact_baselineAge, 0) * self.fact_chancePerYear
	local factChance = self.fact_deathChance + yearAffector
	
	if math.random(1, 100) <= factChance then -- looks like this guy's parents are dead already

		employee:setFact(self.id, true)
	end
end

randomEvents:registerNew(parentDeath)