From 6b7f18d58a69abee0d74c72ec393c60d0a3ea8ba Mon Sep 17 00:00:00 2001 From: Oshisaure Date: Tue, 6 Oct 2020 18:14:00 +0100 Subject: [PATCH] Refactored funcs.lua - Renamed st and sp to strTrueValues and frameTime respectively - Modified files calling these to use the new names - Tidying like formatTime now using a single string.format --- funcs.lua | 27 ++++++++++++++++++--------- tetris/modes/big_a2.lua | 6 +++--- tetris/modes/demon_mode.lua | 4 ++-- tetris/modes/gamemode.lua | 2 +- tetris/modes/interval_training.lua | 4 ++-- tetris/modes/konoha.lua | 4 ++-- tetris/modes/marathon_2020.lua | 20 ++++++++++---------- tetris/modes/marathon_a1.lua | 8 ++++---- tetris/modes/marathon_a2.lua | 6 +++--- tetris/modes/marathon_a3.lua | 10 +++++----- tetris/modes/marathon_ax4.lua | 4 ++-- tetris/modes/marathon_c89.lua | 2 +- tetris/modes/phantom_mania.lua | 6 +++--- tetris/modes/phantom_mania2.lua | 20 ++++++++++---------- tetris/modes/survival_2020.lua | 8 ++++---- tetris/modes/survival_a1.lua | 8 ++++---- tetris/modes/survival_a2.lua | 4 ++-- tetris/modes/survival_a3.lua | 6 +++--- 18 files changed, 79 insertions(+), 70 deletions(-) diff --git a/funcs.lua b/funcs.lua index c837fc2..a61758d 100644 --- a/funcs.lua +++ b/funcs.lua @@ -1,4 +1,5 @@ function copy(t) + -- returns deep copy of t (as opposed to the shallow copy you get from var = t) if type(t) ~= "table" then return t end local meta = getmetatable(t) local target = {} @@ -7,7 +8,8 @@ function copy(t) return target end -function st(tbl) +function strTrueValues(tbl) + -- returns a concatenation of all the keys in tbl with value true, separated with spaces str = "" for k, v in pairs(tbl) do if v == true then @@ -17,14 +19,16 @@ function st(tbl) return str end -function sp(m, s, f) - if m == nil then m = 0 end - if s == nil then s = 0 end - if f == nil then f = 0 end - return m*3600 + s*60 + math.ceil(f * 0.6) +function frameTime(min, sec, hth) + -- returns a time in frames from a time in minutes-seconds-hundredths format + if min == nil then min = 0 end + if sec == nil then sec = 0 end + if hth == nil then hth = 0 end + return min*3600 + sec*60 + math.ceil(hth * 0.6) end function vAdd(v1, v2) + -- returns the sum of vectors v1 and v2 return { x = v1.x + v2.x, y = v1.y + v2.y @@ -32,6 +36,7 @@ function vAdd(v1, v2) end function vNeg(v) + -- returns the opposite of vector v return { x = -v.x, y = -v.y @@ -39,14 +44,18 @@ function vNeg(v) end function formatTime(frames) + -- returns a mm:ss:hh (h=hundredths) representation of the time in frames given if frames < 0 then return formatTime(0) end - str = string.format("%02d", math.floor(frames / 3600)) .. ":" - .. string.format("%02d", math.floor(frames / 60) % 60) .. "." - .. string.format("%02d", math.floor(frames / 0.6) % 100) + local min, sec, hund + min = math.floor(frames/3600) + sec = math.floor(frames/60) % 60 + hund = math.floor(frames/.6) % 100 + str = string.format("%02d:%02d.%02d", min, sec, hund) return str end function formatBigNum(number) + -- returns a string representing a number with commas as thousands separator (e.g. 12,345,678) local s = string.format("%d", number) local pos = string.len(s) % 3 if pos == 0 then pos = 3 end diff --git a/tetris/modes/big_a2.lua b/tetris/modes/big_a2.lua index 51d5f3a..193ceb7 100755 --- a/tetris/modes/big_a2.lua +++ b/tetris/modes/big_a2.lua @@ -262,7 +262,7 @@ function MarathonA2Game:qualifiesForMRoll() local section_average = 0 for section = 0, 4 do section_average = section_average + self.section_times[section] - if self.section_times[section] > sp(1,05) then + if self.section_times[section] > frameTime(1,05) then return false end end @@ -275,7 +275,7 @@ function MarathonA2Game:qualifiesForMRoll() return false end end - if self.grade < 17 or self.frames > sp(8,45) then + if self.grade < 17 or self.frames > frameTime(8,45) then return false end return true @@ -327,7 +327,7 @@ function MarathonA2Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("GRADE", 240, 120, 40, "left") diff --git a/tetris/modes/demon_mode.lua b/tetris/modes/demon_mode.lua index d65c2dd..d2d888d 100644 --- a/tetris/modes/demon_mode.lua +++ b/tetris/modes/demon_mode.lua @@ -159,7 +159,7 @@ function DemonModeGame:updateSectionTimes(old_level, new_level) end elseif old_level < 100 then -- If section time is under cutoff, skip to level 500. - if self.frames < sp(1,00) then + if self.frames < frameTime(1,00) then self.level = 500 self.grade = 5 self.section_tries = 0 @@ -226,7 +226,7 @@ function DemonModeGame:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("GRADE", 240, 120, 40, "left") diff --git a/tetris/modes/gamemode.lua b/tetris/modes/gamemode.lua index c5d5a15..e305efe 100644 --- a/tetris/modes/gamemode.lua +++ b/tetris/modes/gamemode.lua @@ -392,7 +392,7 @@ function GameMode:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) .. + strTrueValues(self.prev_inputs) .. self.drop_bonus ) diff --git a/tetris/modes/interval_training.lua b/tetris/modes/interval_training.lua index b11fe5e..9d5c4ab 100644 --- a/tetris/modes/interval_training.lua +++ b/tetris/modes/interval_training.lua @@ -120,7 +120,7 @@ function IntervalTrainingGame:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("TIME LEFT", 240, 250, 80, "left") @@ -134,7 +134,7 @@ function IntervalTrainingGame:drawScoringInfo() -- draw time left, flash red if necessary local time_left = self.section_time_limit - math.max(self:getSectionTime(), 0) - if not self.game_over and not self.clear and time_left < sp(0,10) and time_left % 4 < 2 then + if not self.game_over and not self.clear and time_left < frameTime(0,10) and time_left % 4 < 2 then love.graphics.setColor(1, 0.3, 0.3, 1) end love.graphics.printf(formatTime(time_left), 240, 270, 160, "left") diff --git a/tetris/modes/konoha.lua b/tetris/modes/konoha.lua index 6d7f210..707ada4 100755 --- a/tetris/modes/konoha.lua +++ b/tetris/modes/konoha.lua @@ -150,7 +150,7 @@ function KonohaGame:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("TIME LIMIT", 240, 120, 120, "left") @@ -158,7 +158,7 @@ function KonohaGame:drawScoringInfo() love.graphics.printf("LEVEL", 240, 320, 40, "left") love.graphics.setFont(font_3x5_3) - if not self.game_over and self.time_limit < sp(0,10) and self.time_limit % 4 < 2 then + if not self.game_over and self.time_limit < frameTime(0,10) and self.time_limit % 4 < 2 then love.graphics.setColor(1, 0.3, 0.3, 1) end love.graphics.printf(formatTime(self.time_limit), 240, 140, 120, "left") diff --git a/tetris/modes/marathon_2020.lua b/tetris/modes/marathon_2020.lua index 5d49793..4b61c14 100644 --- a/tetris/modes/marathon_2020.lua +++ b/tetris/modes/marathon_2020.lua @@ -151,11 +151,11 @@ function Marathon2020Game:advanceOneFrame() end local cool_cutoffs = { - sp(0,45,00), sp(0,41,50), sp(0,38,50), sp(0,35,00), sp(0,32,50), - sp(0,29,20), sp(0,27,20), sp(0,24,80), sp(0,22,80), sp(0,20,60), - sp(0,19,60), sp(0,19,40), sp(0,19,40), sp(0,18,40), sp(0,18,20), - sp(0,16,20), sp(0,16,20), sp(0,16,20), sp(0,16,20), sp(0,16,20), - sp(0,15,20) + frameTime(0,45,00), frameTime(0,41,50), frameTime(0,38,50), frameTime(0,35,00), frameTime(0,32,50), + frameTime(0,29,20), frameTime(0,27,20), frameTime(0,24,80), frameTime(0,22,80), frameTime(0,20,60), + frameTime(0,19,60), frameTime(0,19,40), frameTime(0,19,40), frameTime(0,18,40), frameTime(0,18,20), + frameTime(0,16,20), frameTime(0,16,20), frameTime(0,16,20), frameTime(0,16,20), frameTime(0,16,20), + frameTime(0,15,20) } local levels_for_cleared_rows = { 1, 2, 4, 6 } @@ -284,11 +284,11 @@ function Marathon2020Game:sectionPassed(old_level, new_level) end function Marathon2020Game:checkTorikan(section) - if section == 5 and self.frames < sp(6,00,00) then self.torikan_passed[500] = true end - if section == 9 and self.frames < sp(8,30,00) then self.torikan_passed[900] = true end - if section == 10 and self.frames < sp(8,45,00) then self.torikan_passed[1000] = true end - if section == 15 and self.frames < sp(11,30,00) then self.torikan_passed[1500] = true end - if section == 19 and self.frames < sp(13,15,00) then self.torikan_passed[1900] = true end + if section == 5 and self.frames < frameTime(6,00,00) then self.torikan_passed[500] = true end + if section == 9 and self.frames < frameTime(8,30,00) then self.torikan_passed[900] = true end + if section == 10 and self.frames < frameTime(8,45,00) then self.torikan_passed[1000] = true end + if section == 15 and self.frames < frameTime(11,30,00) then self.torikan_passed[1500] = true end + if section == 19 and self.frames < frameTime(13,15,00) then self.torikan_passed[1900] = true end end function Marathon2020Game:checkClear(level) diff --git a/tetris/modes/marathon_a1.lua b/tetris/modes/marathon_a1.lua index 7b520ab..52a0966 100644 --- a/tetris/modes/marathon_a1.lua +++ b/tetris/modes/marathon_a1.lua @@ -155,15 +155,15 @@ end function MarathonA1Game:checkGMRequirements(old_level, new_level) if old_level < 300 and new_level >= 300 then - if self.score > 12000 and self.frames <= sp(4,15) then + if self.score > 12000 and self.frames <= frameTime(4,15) then self.gm_conditions["level300"] = true end elseif old_level < 500 and new_level >= 500 then - if self.score > 40000 and self.frames <= sp(7,30) then + if self.score > 40000 and self.frames <= frameTime(7,30) then self.gm_conditions["level500"] = true end elseif old_level < 999 and new_level >= 999 then - if self.score > 126000 and self.frames <= sp(13,30) then + if self.score > 126000 and self.frames <= frameTime(13,30) then self.gm_conditions["level900"] = true end end @@ -184,7 +184,7 @@ function MarathonA1Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("GRADE", 240, 120, 40, "left") diff --git a/tetris/modes/marathon_a2.lua b/tetris/modes/marathon_a2.lua index 1730df0..d1f8d62 100644 --- a/tetris/modes/marathon_a2.lua +++ b/tetris/modes/marathon_a2.lua @@ -260,7 +260,7 @@ function MarathonA2Game:qualifiesForMRoll() local section_average = 0 for section = 0, 4 do section_average = section_average + self.section_times[section] - if self.section_times[section] > sp(1,05) then + if self.section_times[section] > frameTime(1,05) then return false end end @@ -273,7 +273,7 @@ function MarathonA2Game:qualifiesForMRoll() return false end end - if self.grade < 17 or self.frames > sp(8,45) then + if self.grade < 17 or self.frames > frameTime(8,45) then return false end return true @@ -325,7 +325,7 @@ function MarathonA2Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("GRADE", 240, 120, 40, "left") diff --git a/tetris/modes/marathon_a3.lua b/tetris/modes/marathon_a3.lua index 6e2bb1c..a39cb0e 100644 --- a/tetris/modes/marathon_a3.lua +++ b/tetris/modes/marathon_a3.lua @@ -160,13 +160,13 @@ function MarathonA3Game:onLineClear(cleared_row_count) end local cool_cutoffs = { - sp(0,52), sp(0,52), sp(0,49), sp(0,45), sp(0,45), - sp(0,42), sp(0,42), sp(0,38), sp(0,38), + frameTime(0,52), frameTime(0,52), frameTime(0,49), frameTime(0,45), frameTime(0,45), + frameTime(0,42), frameTime(0,42), frameTime(0,38), frameTime(0,38), } local regret_cutoffs = { - sp(0,90), sp(0,75), sp(0,75), sp(0,68), sp(0,60), - sp(0,60), sp(0,50), sp(0,50), sp(0,50), sp(0,50), + frameTime(0,90), frameTime(0,75), frameTime(0,75), frameTime(0,68), frameTime(0,60), + frameTime(0,60), frameTime(0,50), frameTime(0,50), frameTime(0,50), frameTime(0,50), } function MarathonA3Game:updateSectionTimes(old_level, new_level) @@ -372,7 +372,7 @@ function MarathonA3Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("GRADE", 240, 120, 40, "left") diff --git a/tetris/modes/marathon_ax4.lua b/tetris/modes/marathon_ax4.lua index 4ae54d3..3fcda94 100644 --- a/tetris/modes/marathon_ax4.lua +++ b/tetris/modes/marathon_ax4.lua @@ -141,7 +141,7 @@ function MarathonAX4Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("TIME LEFT", 240, 250, 80, "left") @@ -156,7 +156,7 @@ function MarathonAX4Game:drawScoringInfo() -- draw time left, flash red if necessary local time_left = self.section_time_limit - math.max(self:getSectionTime(), 0) - if not self.game_over and not self.clear and time_left < sp(0,10) and time_left % 4 < 2 then + if not self.game_over and not self.clear and time_left < frameTime(0,10) and time_left % 4 < 2 then love.graphics.setColor(1, 0.3, 0.3, 1) end love.graphics.printf(formatTime(time_left), 240, 270, 160, "left") diff --git a/tetris/modes/marathon_c89.lua b/tetris/modes/marathon_c89.lua index 7b451cc..bc4b75d 100644 --- a/tetris/modes/marathon_c89.lua +++ b/tetris/modes/marathon_c89.lua @@ -156,7 +156,7 @@ function MarathonC89Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("LINES", 240, 120, 40, "left") diff --git a/tetris/modes/phantom_mania.lua b/tetris/modes/phantom_mania.lua index 54cd39b..ff064dd 100644 --- a/tetris/modes/phantom_mania.lua +++ b/tetris/modes/phantom_mania.lua @@ -61,15 +61,15 @@ function PhantomManiaGame:getGravity() end function PhantomManiaGame:hitTorikan(old_level, new_level) - if old_level < 300 and new_level >= 300 and self.frames > sp(2,28) then + if old_level < 300 and new_level >= 300 and self.frames > frameTime(2,28) then self.level = 300 return true end - if old_level < 500 and new_level >= 500 and self.frames > sp(3,38) then + if old_level < 500 and new_level >= 500 and self.frames > frameTime(3,38) then self.level = 500 return true end - if old_level < 800 and new_level >= 800 and self.frames > sp(5,23) then + if old_level < 800 and new_level >= 800 and self.frames > frameTime(5,23) then self.level = 800 return true end diff --git a/tetris/modes/phantom_mania2.lua b/tetris/modes/phantom_mania2.lua index 2626250..5b30451 100644 --- a/tetris/modes/phantom_mania2.lua +++ b/tetris/modes/phantom_mania2.lua @@ -86,19 +86,19 @@ function PhantomMania2Game:getNextPiece(ruleset) end function PhantomMania2Game:hitTorikan(old_level, new_level) - if old_level < 300 and new_level >= 300 and self.frames > sp(2,02) then + if old_level < 300 and new_level >= 300 and self.frames > frameTime(2,02) then self.level = 300 return true end - if old_level < 500 and new_level >= 500 and self.frames > sp(3,03) then + if old_level < 500 and new_level >= 500 and self.frames > frameTime(3,03) then self.level = 500 return true end - if old_level < 800 and new_level >= 800 and self.frames > sp(4,45) then + if old_level < 800 and new_level >= 800 and self.frames > frameTime(4,45) then self.level = 800 return true end - if old_level < 1000 and new_level >= 1000 and self.frames > sp(5,38) then + if old_level < 1000 and new_level >= 1000 and self.frames > frameTime(5,38) then self.level = 1000 return true end @@ -188,15 +188,15 @@ end local cool_cutoffs = { - sp(0,36), sp(0,36), sp(0,36), sp(0,36), sp(0,36), - sp(0,30), sp(0,30), sp(0,30), sp(0,30), sp(0,30), - sp(0,27), sp(0,27), sp(0,27), + frameTime(0,36), frameTime(0,36), frameTime(0,36), frameTime(0,36), frameTime(0,36), + frameTime(0,30), frameTime(0,30), frameTime(0,30), frameTime(0,30), frameTime(0,30), + frameTime(0,27), frameTime(0,27), frameTime(0,27), } local regret_cutoffs = { - sp(0,50), sp(0,50), sp(0,50), sp(0,50), sp(0,50), - sp(0,40), sp(0,40), sp(0,40), sp(0,40), sp(0,40), - sp(0,35), sp(0,35), sp(0,35), + frameTime(0,50), frameTime(0,50), frameTime(0,50), frameTime(0,50), frameTime(0,50), + frameTime(0,40), frameTime(0,40), frameTime(0,40), frameTime(0,40), frameTime(0,40), + frameTime(0,35), frameTime(0,35), frameTime(0,35), } function PhantomMania2Game:updateSectionTimes(old_level, new_level) diff --git a/tetris/modes/survival_2020.lua b/tetris/modes/survival_2020.lua index 42c4098..d15860a 100644 --- a/tetris/modes/survival_2020.lua +++ b/tetris/modes/survival_2020.lua @@ -102,15 +102,15 @@ function Survival2020Game:getNextPiece(ruleset) end function Survival2020Game:hitTorikan(old_level, new_level) - if old_level < 500 and new_level >= 500 and self.frames > sp(3,00) then + if old_level < 500 and new_level >= 500 and self.frames > frameTime(3,00) then self.level = 500 return true end - if old_level < 1000 and new_level >= 1000 and self.frames > sp(5,00) then + if old_level < 1000 and new_level >= 1000 and self.frames > frameTime(5,00) then self.level = 1000 return true end - if old_level < 1500 and new_level >= 1500 and self.frames > sp(7,00) then + if old_level < 1500 and new_level >= 1500 and self.frames > frameTime(7,00) then self.level = 1500 return true end @@ -193,7 +193,7 @@ function Survival2020Game:updateSectionTimes(old_level, new_level) section_time = self.frames - self.section_start_time table.insert(self.section_times, section_time) self.section_start_time = self.frames - if section_time <= sp(0,30) then + if section_time <= frameTime(0,30) then self.grade = self.grade + 2 else self.grade = self.grade + 1 diff --git a/tetris/modes/survival_a1.lua b/tetris/modes/survival_a1.lua index 49edde5..78a81cc 100644 --- a/tetris/modes/survival_a1.lua +++ b/tetris/modes/survival_a1.lua @@ -125,15 +125,15 @@ end function SurvivalA1Game:checkGMRequirements(old_level, new_level) if old_level < 300 and new_level >= 300 then - if self.score > 12000 and self.frames <= sp(4,15) then + if self.score > 12000 and self.frames <= frameTime(4,15) then self.gm_conditions["level300"] = true end elseif old_level < 500 and new_level >= 500 then - if self.score > 40000 and self.frames <= sp(7,30) then + if self.score > 40000 and self.frames <= frameTime(7,30) then self.gm_conditions["level500"] = true end elseif old_level < 999 and new_level >= 999 then - if self.score > 126000 and self.frames <= sp(13,30) then + if self.score > 126000 and self.frames <= frameTime(13,30) then self.gm_conditions["level900"] = true end end @@ -151,7 +151,7 @@ function SurvivalA1Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("GRADE", 240, 120, 40, "left") diff --git a/tetris/modes/survival_a2.lua b/tetris/modes/survival_a2.lua index 9a21003..6b82e9c 100644 --- a/tetris/modes/survival_a2.lua +++ b/tetris/modes/survival_a2.lua @@ -62,7 +62,7 @@ function SurvivalA2Game:getGravity() end function SurvivalA2Game:hitTorikan(old_level, new_level) - if old_level < 500 and new_level >= 500 and self.frames > sp(3,25) then + if old_level < 500 and new_level >= 500 and self.frames > frameTime(3,25) then self.level = 500 return true end @@ -132,7 +132,7 @@ function SurvivalA2Game:drawScoringInfo() love.graphics.print( self.das.direction .. " " .. self.das.frames .. " " .. - st(self.prev_inputs) + strTrueValues(self.prev_inputs) ) love.graphics.printf("NEXT", 64, 40, 40, "left") love.graphics.printf("GRADE", text_x, 120, 40, "left") diff --git a/tetris/modes/survival_a3.lua b/tetris/modes/survival_a3.lua index 4a980a0..30ce28a 100644 --- a/tetris/modes/survival_a3.lua +++ b/tetris/modes/survival_a3.lua @@ -86,11 +86,11 @@ function SurvivalA3Game:getNextPiece(ruleset) end function SurvivalA3Game:hitTorikan(old_level, new_level) - if old_level < 500 and new_level >= 500 and self.frames > sp(2,28) then + if old_level < 500 and new_level >= 500 and self.frames > frameTime(2,28) then self.level = 500 return true end - if old_level < 1000 and new_level >= 1000 and self.frames > sp(4,56) then + if old_level < 1000 and new_level >= 1000 and self.frames > frameTime(4,56) then self.level = 1000 return true end @@ -168,7 +168,7 @@ function SurvivalA3Game:updateSectionTimes(old_level, new_level) section_time = self.frames - self.section_start_time table.insert(self.section_times, section_time) self.section_start_time = self.frames - if section_time <= sp(1,00) then + if section_time <= frameTime(1,00) then self.grade = self.grade + 1 end end