Refactor war state points counting for later additions

This commit is contained in:
Luke Hubmayer-Werner 2020-06-05 19:47:02 +09:30
parent 9ceaa67267
commit 5d7d2606d5
1 changed files with 65 additions and 62 deletions

View File

@ -55,7 +55,11 @@ local LOOP_BUFFER = 0.015 -- if looping track is this close to the end, go ahead
local UPDATE_PERIOD = 1
local musicType = 'peace'
local dethklok = {} -- keeps track of the number of doods killed in each time frame
local warPointsIter = 1 -- Position in circular buffer. 1-indexed because L[ew]a
local warPointsSize = 30 -- Size of circular buffer. Sampling is currently hardcoded but might change later.
local warPointsFriendly = {} -- keeps track of the number of doods killed in each time frame
local warPointsHostile = {}
local warPointsRollover = 4000000000 -- Roll back to zero after this many have accumulated
local timeframetimer = 0
local timeframetimer_short = 0
local loopTrack = ''
@ -64,8 +68,8 @@ local previousTrackType = ''
local newTrackWait = 1000
local numVisibleEnemy = 0
local fadeVol
local curTrack = "no name"
local songText = "no name"
local curTrac = "no name"
local songText = "no name"
local haltMusic = false
local looping = false
local paused = false
@ -227,11 +231,12 @@ function widget:Update(dt)
timeframetimer_short = 0
end
-- (Spring.GetGameRulesParam("recentNukeLaunch") == 1) -- Might need this for superweapon music later
timeframetimer = timeframetimer + dt
if (timeframetimer > UPDATE_PERIOD) then -- every second
timeframetimer = 0
newTrackWait = newTrackWait + 1
local PlayerTeam = Spring.GetMyTeamID()
numVisibleEnemy = 0
local doods = Spring.GetVisibleUnits(-1, nil, true)
for i=1,#doods do
@ -240,19 +245,21 @@ function widget:Update(dt)
end
end
local totalKilled = 0
for i = 1, 10, 1 do --calculate the first half of the table (1-15)
totalKilled = totalKilled + (dethklok[i] * 2)
end
local totalKilled, friendliesKilled, hostilesKilled = 0, 0, 0
local iLast = ((warPointsIter-11) % warPointsSize) + 1 -- Look back 10 periods.
local iLast2 = ((warPointsIter-21) % warPointsSize) + 1 -- Look back 20 periods.
-- Last 10 seconds count for double
friendliesKilled = friendliesKilled + ((warPointsFriendly[warPointsIter] - warPointsFriendly[iLast]) % warPointsRollover)
friendliesKilled = friendliesKilled + ((warPointsFriendly[warPointsIter] - warPointsFriendly[iLast2]) % warPointsRollover)
hostilesKilled = hostilesKilled + ((warPointsHostile[warPointsIter] - warPointsHostile[iLast]) % warPointsRollover)
hostilesKilled = hostilesKilled + ((warPointsHostile[warPointsIter] - warPointsHostile[iLast2]) % warPointsRollover)
totalKilled = (friendliesKilled * 1.5) + hostilesKilled
for i = 11, 20, 1 do -- calculate the second half of the table (16-45)
totalKilled = totalKilled + dethklok[i]
end
for i = 20, 1, -1 do -- shift value(s) to the end of table
dethklok[i+1] = dethklok[i]
end
dethklok[1] = 0 -- empty the first row
-- Roll to next index in the circular buffers, continue cumulative sum
local iNext = (warPointsIter % warPointsSize) + 1
warPointsFriendly[iNext] = warPointsFriendly[warPointsIter] % warPointsRollover
warPointsHostile[iNext] = warPointsHostile[warPointsIter] % warPointsRollover
warPointsIter = iNext
if (musicType == 'war' or musicType == 'peace') then
if (totalKilled >= warThreshold) then
@ -264,7 +271,7 @@ function widget:Update(dt)
if (not firstTime) then
StartTrack()
firstTime = true -- pop this cherry
firstTime = true
end
local playedTime, totalTime = Spring.GetSoundStreamTime()
@ -324,33 +331,25 @@ function widget:GameFrame()
end
function widget:UnitDamaged(unitID, unitDefID, unitTeam, damage, paralyzer)
if unitExceptions[unitDefID] then
return
end
if unitExceptions[unitDefID] then return end
if (damage < 1.5) then return end
local PlayerTeam = Spring.GetMyTeamID()
if (UnitDefs[unitDefID] == nil) then return end
if paralyzer then return end
if paralyzer then
return
local multifactor = 1
if (numVisibleEnemy > 3) then
multifactor = math.log(numVisibleEnemy)
end
if (teamID == myTeam) then
warPointsFriendly[warPointsIter] = warPointsFriendly[warPointsIter] + (damage * multifactor);
else
if (teamID == PlayerTeam) then
damage = damage * 1.5
end
local multifactor = 1
if (numVisibleEnemy > 3) then
multifactor = math.log(numVisibleEnemy)
end
dethklok[1] = dethklok[1] + (damage * multifactor);
warPointsHostile[warPointsIter] = warPointsHostile[warPointsIter] + (damage * multifactor);
end
end
function widget:UnitDestroyed(unitID, unitDefID, teamID)
if unitExceptions[unitDefID] then
return
end
if unitExceptions[unitDefID] then return end
local unitWorth = 50
if (UnitDefs[unitDefID].metalCost > 500) then
unitWorth = 200
@ -364,14 +363,16 @@ function widget:UnitDestroyed(unitID, unitDefID, teamID)
if (UnitDefs[unitDefID].metalCost > 8000) then
unitWorth = 700
end
if (teamID == PlayerTeam) then
unitWorth = unitWorth * 1.5
end
local multifactor = 1
if (numVisibleEnemy > 3) then
multifactor = math.log(numVisibleEnemy)
end
dethklok[1] = dethklok[1] + (unitWorth*multifactor);
if (teamID == myTeam) then
warPointsFriendly[warPointsIter] = warPointsFriendly[warPointsIter] + (unitWorth*multifactor);
else
warPointsHostile[warPointsIter] = warPointsHostile[warPointsIter] + (unitWorth*multifactor);
end
end
function widget:TeamDied(team)
@ -420,8 +421,10 @@ function widget:Initialize()
--math.randomseed(os.clock()* 101.01)--lurker wants you to burn in hell rgn
-- for i=1,20 do Spring.Echo(math.random()) end
for i = 1, 30, 1 do
dethklok[i]=0
for i = 1, warPointsSize do
warPoints[i] = 0
warPointsFriendly[i] = 0
warPointsHostile[i] = 0
end
end