PSC fixes (#543)

removed anti-cheat
    removed djui_hud_set_render_behind_hud, now uses both HOOK_ON_HUD_RENDER and HOOK_ON_HUD_RENDER_BEHIND
    fixed API
    tabs -> 4 spaces
This commit is contained in:
PeachyPeach 2023-11-26 22:38:09 +01:00 committed by MysterD
parent d7ed085817
commit 25f69605cf
1 changed files with 114 additions and 98 deletions

View File

@ -6,30 +6,29 @@
----- Localize functions ----- ----- Localize functions -----
------------------------------ ------------------------------
-- This looks strange but it makes the mod more performant -- This looks strange but it makes the mod more performant
local tonumber, mod_storage_load, mod_storage_save, tostring, djui_hud_is_pause_menu_created, djui_hud_set_color, hud_get_value, djui_hud_print_text, djui_hud_render_texture, obj_get_first_with_behavior_id, djui_hud_set_resolution, djui_hud_set_font, djui_hud_get_screen_height, djui_hud_get_screen_width, djui_hud_set_render_behind_hud, hud_set_value, network_player_set_description, djui_popup_create = local tonumber, mod_storage_load, mod_storage_save, tostring, djui_hud_is_pause_menu_created, djui_hud_set_color, hud_get_value, djui_hud_print_text, djui_hud_render_texture, obj_get_first_with_behavior_id, djui_hud_set_resolution, djui_hud_set_font, djui_hud_get_screen_width, hud_set_value, network_player_set_description =
tonumber, mod_storage_load, mod_storage_save, tostring, djui_hud_is_pause_menu_created, djui_hud_set_color, hud_get_value, djui_hud_print_text, djui_hud_render_texture, obj_get_first_with_behavior_id, djui_hud_set_resolution, djui_hud_set_font, djui_hud_get_screen_height, djui_hud_get_screen_width, djui_hud_set_render_behind_hud, hud_set_value, network_player_set_description, djui_popup_create tonumber, mod_storage_load, mod_storage_save, tostring, djui_hud_is_pause_menu_created, djui_hud_set_color, hud_get_value, djui_hud_print_text, djui_hud_render_texture, obj_get_first_with_behavior_id, djui_hud_set_resolution, djui_hud_set_font, djui_hud_get_screen_width, hud_set_value, network_player_set_description
------------------------------ ------------------------------
-- Initialize a value for the psc for everyone -- Initialize a value for the psc for everyone
for i = 0, MAX_PLAYERS - 1 do for i = 0, MAX_PLAYERS - 1 do
gPlayerSyncTable[i].psc = 0 gPlayerSyncTable[i].psc = 0
end end
local psc_toggle = true local psc_toggle = true
local has_cheats_enabled = gServerSettings.enableCheats ~= 0 local psc_hud_toggle = true
-- If there is no value in storage, default to 0 -- If there is no value in storage, default to 0
local TotalStarCounter = tonumber(mod_storage_load("StarCounter")) or 0 local TotalStarCounter = tonumber(mod_storage_load("StarCounter")) or 0
local ThisGameStarCounter = 0 local ThisGameStarCounter = 0
local prevNumStars = 0 local prevNumStars = 0
local introTimer = 0 local introTimer = 0
-- When in these cutscenes, do special things like hiding or moving the star counters -- When in these cutscenes, do special things like hiding or moving the star counters
local is_in_cutscenes = { local is_in_cutscenes = {
[ACT_END_PEACH_CUTSCENE] = true, [ACT_END_PEACH_CUTSCENE] = true,
[ACT_CREDITS_CUTSCENE] = true, [ACT_CREDITS_CUTSCENE] = true,
[ACT_END_WAVING_CUTSCENE] = true, [ACT_END_WAVING_CUTSCENE] = true,
} }
--- Whenever a star is collected, increment the other star counters --- Whenever a star is collected, increment the other star counters
@ -41,18 +40,15 @@ local is_in_cutscenes = {
local function star_counter_on_interact(m, obj, intType, interacted) local function star_counter_on_interact(m, obj, intType, interacted)
if not psc_toggle then return end if not psc_toggle then return end
if m.playerIndex == 0 and intType == INTERACT_STAR_OR_KEY then if m.playerIndex == 0 and intType == INTERACT_STAR_OR_KEY then
-- Interactions happen before hud rendering, which is why this check can work -- Interactions happen before hud rendering, which is why this check can work
-- despite prevNumStars being updated every frame -- despite prevNumStars being updated every frame
if m.numStars ~= prevNumStars then if m.numStars ~= prevNumStars then
ThisGameStarCounter = ThisGameStarCounter + 1 -- Red star counter ThisGameStarCounter = ThisGameStarCounter + 1 -- Red star counter
-- Disable total star counter if cheats are enabled TotalStarCounter = TotalStarCounter + 1 -- Green star counter
if not has_cheats_enabled then mod_storage_save("StarCounter", tostring(TotalStarCounter)) -- Save to mod storage
TotalStarCounter = TotalStarCounter + 1 -- Green star counter
mod_storage_save("StarCounter", tostring(TotalStarCounter)) -- Save to mod storage
end
end end
end end
end end
--- Hud alpha stuff from Agent X --- Hud alpha stuff from Agent X
@ -63,19 +59,20 @@ end
---@param a number Alpha (Transparency) ---@param a number Alpha (Transparency)
---@return nil ---@return nil
local function djui_hud_set_adjusted_color(r, g, b, a) local function djui_hud_set_adjusted_color(r, g, b, a)
local multiplier = 1 local multiplier = 1
if djui_hud_is_pause_menu_created() then multiplier = 0.57 end -- Star Display compatibility if djui_hud_is_pause_menu_created() then multiplier = 0.57 end -- Star Display compatibility
djui_hud_set_color(r * multiplier, g * multiplier, b * multiplier, a) djui_hud_set_color(r * multiplier, g * multiplier, b * multiplier, a)
end end
--- Sets the star counters onto the hud --- Sets the star counters onto the hud
---@param m MarioState Mario ---@param m MarioState Mario
---@param height number Screen height (Unused)
---@param width number Screen width ---@param width number Screen width
---@return nil ---@return nil
local function set_hud_star_positions(m, height, width) local function set_hud_star_positions(m, width)
if introTimer >= 1195 or m.action ~= ACT_INTRO_CUTSCENE then if introTimer >= 1195 or m.action ~= ACT_INTRO_CUTSCENE then
local star_texture = gTextures.star local star_texture = gTextures.star
local is_in_cutscene = is_in_cutscenes[m.action] ~= nil
-- Check if the timer is currently active -- Check if the timer is currently active
local timer_current_value = hud_get_value(HUD_DISPLAY_TIMER) local timer_current_value = hud_get_value(HUD_DISPLAY_TIMER)
@ -85,7 +82,6 @@ local function set_hud_star_positions(m, height, width)
-- Offset based on if each counter has less than 100 stars -- Offset based on if each counter has less than 100 stars
local low_stars_offset_X = 0 local low_stars_offset_X = 0
local low_stars_offset_Y = 0
-- Based off of star texture pos -- Based off of star texture pos
local star_counter_offset_X = 0 local star_counter_offset_X = 0
@ -98,12 +94,10 @@ local function set_hud_star_positions(m, height, width)
------------------------------- -------------------------------
-- Don't display while in a cutscene -- Don't display while in a cutscene
if not is_in_cutscenes[m.action] then if not is_in_cutscene then
star_counter_offset_X = 76 star_counter_offset_X = 76
top_screen_offset_Y = 15 top_screen_offset_Y = 15
low_stars_offset_X = 14 low_stars_offset_X = 14
offsetX = 14 offsetX = 14
if m.numStars < 100 then if m.numStars < 100 then
djui_hud_set_adjusted_color(246, 246, 246, 255) djui_hud_set_adjusted_color(246, 246, 246, 255)
@ -125,50 +119,54 @@ local function set_hud_star_positions(m, height, width)
timer_offset_Y = -17 timer_offset_Y = -17
end end
star_counter_offset_X = is_in_cutscenes[m.action] and 22 or 76 star_counter_offset_X = is_in_cutscene and 22 or 76
top_screen_offset_Y = is_in_cutscenes[m.action] and 15 or 15 + 17 top_screen_offset_Y = is_in_cutscene and 15 or 32
low_stars_offset_X = 14 low_stars_offset_X = 14
local left_align = is_in_cutscenes[m.action]
-- Render X if this counter has less than 100 stars -- Render X if this counter has less than 100 stars
if ThisGameStarCounter < 100 then if ThisGameStarCounter < 100 then
djui_hud_set_adjusted_color(246, 246, 246, 255) djui_hud_set_adjusted_color(246, 246, 246, 255)
djui_hud_print_text( djui_hud_print_text(
"@", -- Text "@", -- Text
(left_align and (star_counter_offset_X + 16) or -- X pos in cutscenes (is_in_cutscene and
(width - (star_counter_offset_X - 16) - timer_offset_X)), -- X pos outside cutscenes (star_counter_offset_X + 16) or -- X pos in cutscenes
(width - (star_counter_offset_X - 16) - timer_offset_X) -- X pos outside cutscenes
),
top_screen_offset_Y + timer_offset_Y, -- Y pos top_screen_offset_Y + timer_offset_Y, -- Y pos
1) -- Scale 1 -- Scale
)
low_stars_offset_X = 0 low_stars_offset_X = 0
end end
-- Render counter -- Render counter
djui_hud_set_adjusted_color(255, 255, 255, 255) djui_hud_set_adjusted_color(255, 255, 255, 255)
djui_hud_print_text( djui_hud_print_text(
tostring(ThisGameStarCounter), -- Text tostring(ThisGameStarCounter), -- Text
(left_align and (star_counter_offset_X + 30 - low_stars_offset_X) or -- X pos in cutscenes (is_in_cutscene and
(width - (star_counter_offset_X - 30) - timer_offset_X - low_stars_offset_X)), -- X pos outside cutscenes (star_counter_offset_X + 30 - low_stars_offset_X) or -- X pos in cutscenes
(width - (star_counter_offset_X - 30) - timer_offset_X - low_stars_offset_X) -- X pos outside cutscenes
),
top_screen_offset_Y + timer_offset_Y, -- Y pos top_screen_offset_Y + timer_offset_Y, -- Y pos
1) -- Scale 1 -- Scale
)
djui_hud_set_adjusted_color(232, 17, 35, 255) djui_hud_set_adjusted_color(232, 17, 35, 255)
djui_hud_render_texture( djui_hud_render_texture(
star_texture, -- Texture star_texture, -- Texture
(left_align and star_counter_offset_X or -- X pos in cutscenes (is_in_cutscene and
(width - star_counter_offset_X - timer_offset_X)), -- X pos outside cutscenes (star_counter_offset_X) or -- X pos in cutscenes
(width - star_counter_offset_X - timer_offset_X) -- X pos outside cutscenes
),
top_screen_offset_Y + timer_offset_Y, -- Y pos top_screen_offset_Y + timer_offset_Y, -- Y pos
1, 1) -- Scale 1, 1 -- Scale
)
------------------------------ ------------------------------
----- Green star counter ----- ----- Green star counter -----
------------------------------ ------------------------------
-- Increment a seperate counter if the total star count is over 10000 -- Increment a seperate counter if the total star count is over 10000
local perceived_total_counter = TotalStarCounter local perceived_total_counter = TotalStarCounter % 10000
local milestone_counter = 0 local milestone_counter = math.floor(TotalStarCounter / 10000)
while perceived_total_counter >= 10000 do
perceived_total_counter = perceived_total_counter - 10000
milestone_counter = milestone_counter + 1
end
-- Move HUD graphics away from the TIMER HUD -- Move HUD graphics away from the TIMER HUD
if timer_current_value ~= 0 then if timer_current_value ~= 0 then
@ -177,9 +175,9 @@ local function set_hud_star_positions(m, height, width)
end end
star_counter_offset_X = 76 star_counter_offset_X = 76
top_screen_offset_Y = is_in_cutscenes[m.action] and 15 or 15 + 17 * 2 top_screen_offset_Y = is_in_cutscene and 15 or 49
low_stars_offset_X = 14 low_stars_offset_X = 14
-- Render X if this counter has less than 100 stars -- Render X if this counter has less than 100 stars
if perceived_total_counter < 100 then if perceived_total_counter < 100 then
djui_hud_set_adjusted_color(246, 246, 246, 255) djui_hud_set_adjusted_color(246, 246, 246, 255)
@ -187,43 +185,47 @@ local function set_hud_star_positions(m, height, width)
"@", -- Text "@", -- Text
width - (star_counter_offset_X - 16) - timer_offset_X, -- X pos width - (star_counter_offset_X - 16) - timer_offset_X, -- X pos
top_screen_offset_Y + timer_offset_Y, -- Y pos top_screen_offset_Y + timer_offset_Y, -- Y pos
1) -- Scale 1 -- Scale
)
low_stars_offset_X = 0 low_stars_offset_X = 0
end end
-- Render counter -- Render counter
djui_hud_set_adjusted_color(255, 255, 255, 255) djui_hud_set_adjusted_color(255, 255, 255, 255)
djui_hud_print_text( djui_hud_print_text(
tostring(perceived_total_counter), -- Text tostring(perceived_total_counter), -- Text
width - (star_counter_offset_X - 30) - timer_offset_X - low_stars_offset_X, -- X pos width - (star_counter_offset_X - 30) - timer_offset_X - low_stars_offset_X, -- X pos
top_screen_offset_Y + timer_offset_Y, -- Y pos top_screen_offset_Y + timer_offset_Y, -- Y pos
1) -- Scale 1 -- Scale
)
djui_hud_set_adjusted_color(50, 176, 40, 255) djui_hud_set_adjusted_color(50, 176, 40, 255)
djui_hud_render_texture( djui_hud_render_texture(
star_texture, -- Texture star_texture, -- Texture
width - star_counter_offset_X - timer_offset_X, -- X pos width - star_counter_offset_X - timer_offset_X, -- X pos
top_screen_offset_Y + timer_offset_Y, -- Y pos top_screen_offset_Y + timer_offset_Y, -- Y pos
1, 1) -- Scale 1, 1 -- Scale
)
-- Render milestone counter
if milestone_counter ~= 0 then if milestone_counter ~= 0 then
djui_hud_set_adjusted_color(255, 255, 255, 255) djui_hud_set_adjusted_color(255, 255, 255, 255)
djui_hud_print_text( djui_hud_print_text(
"@" .. milestone_counter, -- Text "@" .. milestone_counter, -- Text
width - star_counter_offset_X - timer_offset_X, -- X pos width - star_counter_offset_X - timer_offset_X, -- X pos
(top_screen_offset_Y + 16) + timer_offset_Y, -- Y pos (top_screen_offset_Y + 16) + timer_offset_Y, -- Y pos
0.5) -- Scale 0.5 -- Scale
)
end end
end end
end end
--- Renders star counters onto the screen --- Renders star counters onto the screen
---@param m MarioState Mario
---@return nil ---@return nil
local function hud_render_psc() local function render_psc(m)
if not psc_toggle then return end
-- If the act selector is active
if obj_get_first_with_behavior_id(id_bhvActSelector) then return end
-- HOOK_ON_HUD_RENDER doesn't supply a MarioState, so one has to be gotten from this -- Update prevNumStars
---@type MarioState prevNumStars = m.numStars
local m = gMarioStates[0]
-- Make HUD appear when the real one does during the intro -- Make HUD appear when the real one does during the intro
if m.action == ACT_INTRO_CUTSCENE then if m.action == ACT_INTRO_CUTSCENE then
@ -232,44 +234,46 @@ local function hud_render_psc()
end end
end end
-- Don't render the counters if one of the toggle is disabled
if not psc_toggle or not psc_hud_toggle then return end
-- Don't render the counters if the act selector is active
if obj_get_first_with_behavior_id(id_bhvActSelector) then return end
-- Set resolution and font
djui_hud_set_resolution(RESOLUTION_N64) djui_hud_set_resolution(RESOLUTION_N64)
djui_hud_set_font(FONT_HUD) djui_hud_set_font(FONT_HUD)
local screen_height = djui_hud_get_screen_height()
local screen_width = djui_hud_get_screen_width()
-- Set prevNumStars
prevNumStars = m.numStars
-- Draw over credits fades
djui_hud_set_render_behind_hud(not is_in_cutscenes[m.action])
-- Force the normal star counter to be hidden -- Force the normal star counter to be hidden
hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) & ~HUD_DISPLAY_FLAG_STAR_COUNT) hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) & ~HUD_DISPLAY_FLAG_STAR_COUNT)
set_hud_star_positions(m, screen_height, screen_width) set_hud_star_positions(m, djui_hud_get_screen_width())
end
--- Renders star counters onto the screen, after the HUD is being rendered
---@return nil
local function hud_render_psc()
local m = gMarioStates[0]
if is_in_cutscenes[m.action] then
render_psc(m)
end
end
--- Renders star counters onto the screen, before the HUD is being rendered
---@return nil
local function behind_hud_render_psc()
local m = gMarioStates[0]
if not is_in_cutscenes[m.action] then
render_psc(m)
end
end end
--- Display each person's star counter in the player menu --- Display each person's star counter in the player menu
---@return nil ---@return nil
local function psc_update() local function psc_update()
gPlayerSyncTable[0].psc = ThisGameStarCounter gPlayerSyncTable[0].psc = ThisGameStarCounter
for i = 0, MAX_PLAYERS - 1 do for i = 0, MAX_PLAYERS - 1 do
network_player_set_description(gNetworkPlayers[i], "\\#FF0000\\Stars: " .. tostring(gPlayerSyncTable[i].psc), 255, 255, 255, 255) network_player_set_description(gNetworkPlayers[i], "\\#FF0000\\Stars: " .. tostring(gPlayerSyncTable[i].psc), 255, 255, 255, 255)
end
end
local timer = 0
--- Inform the player that they have joined a game with cheats enabled
--- and won't be able to get any green stars
---@param m MarioState
---@return nil
local function psc_mario_update_inform_cheats(m)
if m.playerIndex ~= 0 then return end
if has_cheats_enabled then
timer = timer + 1
if timer == 30 then
djui_popup_create("This game has cheats enabled. You cannot gain any \\#00FF00\\green stars\\#DCDCDC\\ from this game.", 3)
end
end end
end end
@ -277,12 +281,14 @@ end
---@param msg string ---@param msg string
---@return boolean ---@return boolean
local function toggle_psc(msg) local function toggle_psc(msg)
if msg:lower() == "on" then if msg:lower() == "on" then
psc_toggle = true psc_toggle = true
elseif msg:lower() == "off" then psc_hud_toggle = true
psc_toggle = false elseif msg:lower() == "off" then
psc_toggle = false
psc_hud_toggle = false
hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) | HUD_DISPLAY_FLAG_STAR_COUNT) hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) | HUD_DISPLAY_FLAG_STAR_COUNT)
end end
return true return true
end end
@ -292,30 +298,40 @@ end
hook_event(HOOK_ON_INTERACT, star_counter_on_interact) hook_event(HOOK_ON_INTERACT, star_counter_on_interact)
hook_event(HOOK_ON_HUD_RENDER, hud_render_psc) hook_event(HOOK_ON_HUD_RENDER, hud_render_psc)
hook_event(HOOK_ON_HUD_RENDER_BEHIND, behind_hud_render_psc)
hook_event(HOOK_UPDATE, psc_update) hook_event(HOOK_UPDATE, psc_update)
hook_event(HOOK_MARIO_UPDATE, psc_mario_update_inform_cheats)
hook_chat_command('psc', "On|Off - Displays stars you've collected. Default is On.", toggle_psc) hook_chat_command('psc', "On|Off - Displays stars you've collected. Default is On.", toggle_psc)
-- Globalize functions for other mods to use -- Globalize functions for other mods to use
-- Created by PeachyPeach -- Created by PeachyPeach
_G.PersonalStarCounter = { _G.PersonalStarCounter = {
--- Get the amount of red stars collected --- Get the amount of red stars collected
---@return integer ---@return integer
get_star_counter = function () get_star_counter = function ()
return ThisGameStarCounter return ThisGameStarCounter
end, end,
--- Get the amount of green stars collected --- Get the amount of green stars collected
---@return integer ---@return integer
get_total_star_counter = function () get_total_star_counter = function ()
return TotalStarCounter return TotalStarCounter
end, end,
--- Enable or disable the star counters
---@param disable boolean
disable_star_counters = function (disable)
psc_toggle = not disable
end,
--- Force show or hide the star counters --- Force show or hide the star counters
---@param hide boolean ---@param hide boolean
hide_star_counters = function (hide) hide_star_counters = function (hide)
psc_toggle = hide psc_hud_toggle = not hide
end, if hide then
hud_set_value(HUD_DISPLAY_FLAGS, hud_get_value(HUD_DISPLAY_FLAGS) | HUD_DISPLAY_FLAG_STAR_COUNT)
end
end,
--- Modify the value of the star counters --- Modify the value of the star counters
---@param red_change number How much to change the red (single game) star counter by ---@param red_change number How much to change the red (single game) star counter by