2022-02-03 05:28:24 +01:00
-- name: Hide and Seek
-- incompatible: gamemode
2022-02-05 21:37:13 +01:00
-- description: A simple hide-and-seek gamemode for\nCo-op.\n\nThe game is split into two teams:\n\nHiders and Seekers. The goal is for all\n\Hiders to be converted into a Seeker within a certain timeframe.\n\nAll Seekers appear as a metal character, and are given boosted speed\n\and jump height.\n\nHiders are given no enhancements, and\n\become a Seeker upon dying.\n\nEnjoy! :D\n\nConcept by: Super Keeberghrh
2022-02-03 05:28:24 +01:00
2022-02-04 04:50:27 +01:00
-- globally sync enabled state
gGlobalSyncTable.hideAndSeek = true
2022-02-17 03:27:52 +01:00
-- keep track of round info
ROUND_STATE_WAIT = 0
ROUND_STATE_ACTIVE = 1
ROUND_STATE_SEEKERS_WIN = 2
ROUND_STATE_HIDERS_WIN = 3
ROUND_STATE_UNKNOWN_END = 4
2022-03-26 07:16:56 +01:00
gGlobalSyncTable.campingTimer = true -- enable/disable camping timer
2022-02-17 03:27:52 +01:00
gGlobalSyncTable.roundState = ROUND_STATE_WAIT -- current round state
gGlobalSyncTable.displayTimer = 0 -- the displayed timer
sRoundTimer = 0 -- the server's round timer
sRoundStartTimeout = 15 * 30 -- fifteen seconds
sRoundEndTimeout = 3 * 60 * 30 -- three minutes
2022-02-03 05:28:24 +01:00
2022-02-04 04:06:21 +01:00
-- server keeps track of last player turned seeker
sLastSeekerIndex = 0
2022-02-03 05:28:24 +01:00
2022-02-04 09:15:14 +01:00
-- keep track of distance moved recently (camping detection)
2022-02-04 04:06:21 +01:00
sLastPos = { }
sLastPos.x = 0
sLastPos.y = 0
sLastPos.z = 0
sDistanceMoved = 0
sDistanceTimer = 0
2022-02-17 03:27:52 +01:00
sDistanceTimeout = 10 * 30 -- ten seconds
-- flashing 'keep moving' index
sFlashingIndex = 0
2022-02-04 04:06:21 +01:00
function server_update ( m )
2022-02-17 03:27:52 +01:00
-- increment timer
sRoundTimer = sRoundTimer + 1
gGlobalSyncTable.displayTimer = math.floor ( sRoundTimer / 30 )
2022-02-03 05:28:24 +01:00
-- figure out state of the game
local hasSeeker = false
local hasHider = false
local activePlayers = { }
2022-02-04 04:50:27 +01:00
local connectedCount = 0
2022-02-03 05:28:24 +01:00
for i = 0 , ( MAX_PLAYERS - 1 ) do
if gNetworkPlayers [ i ] . connected then
2022-02-04 04:50:27 +01:00
connectedCount = connectedCount + 1
2022-02-03 05:28:24 +01:00
table.insert ( activePlayers , gPlayerSyncTable [ i ] )
if gPlayerSyncTable [ i ] . seeking then
hasSeeker = true
else
hasHider = true
end
end
end
2022-02-04 04:50:27 +01:00
-- only change state if there are 2+ players
if connectedCount < 2 then
2022-02-17 03:27:52 +01:00
gGlobalSyncTable.roundState = ROUND_STATE_WAIT
2022-02-04 04:50:27 +01:00
return
2022-02-17 03:27:52 +01:00
elseif gGlobalSyncTable.roundState == ROUND_STATE_WAIT then
gGlobalSyncTable.roundState = ROUND_STATE_UNKNOWN_END
sRoundTimer = 0
gGlobalSyncTable.displayTimer = 0
2022-02-04 04:50:27 +01:00
end
2022-02-04 09:15:14 +01:00
-- check to see if the round should end
2022-02-17 03:27:52 +01:00
if gGlobalSyncTable.roundState == ROUND_STATE_ACTIVE then
if not hasHider or not hasSeeker or sRoundTimer > sRoundEndTimeout then
2022-02-04 09:15:28 +01:00
if not hasHider then
2022-02-17 03:27:52 +01:00
gGlobalSyncTable.roundState = ROUND_STATE_SEEKERS_WIN
elseif sRoundTimer > sRoundEndTimeout then
gGlobalSyncTable.roundState = ROUND_STATE_HIDERS_WIN
2022-02-04 09:15:28 +01:00
else
2022-02-17 03:27:52 +01:00
gGlobalSyncTable.roundState = ROUND_STATE_UNKNOWN_END
2022-02-04 09:15:28 +01:00
end
2022-02-17 03:27:52 +01:00
sRoundTimer = 0
gGlobalSyncTable.displayTimer = 0
2022-02-04 09:14:38 +01:00
else
return
end
2022-02-03 05:28:24 +01:00
end
2022-02-17 03:27:52 +01:00
-- start round
if sRoundTimer >= sRoundStartTimeout then
2022-02-03 05:28:24 +01:00
-- reset seekers
2022-02-06 00:05:18 +01:00
for i = 0 , ( MAX_PLAYERS - 1 ) do
gPlayerSyncTable [ i ] . seeking = false
2022-02-03 05:28:24 +01:00
end
2022-02-06 00:05:18 +01:00
hasSeeker = false
2022-02-03 05:28:24 +01:00
2022-02-04 04:06:21 +01:00
-- set seeker to last one turned into seeker
local np = gNetworkPlayers [ sLastSeekerIndex ]
if np.connected then
local s = gPlayerSyncTable [ sLastSeekerIndex ]
s.seeking = true
hasSeeker = true
end
-- pick random seeker if last turned to seeker is invalid
2022-02-03 05:28:24 +01:00
if not hasSeeker then
2022-02-04 04:06:21 +01:00
local s = activePlayers [ math.random ( # activePlayers ) ]
2022-02-03 05:28:24 +01:00
s.seeking = true
end
2022-02-04 04:06:21 +01:00
2022-02-17 03:27:52 +01:00
-- set round state
gGlobalSyncTable.roundState = ROUND_STATE_ACTIVE
sRoundTimer = 0
gGlobalSyncTable.displayTimer = 0
2022-02-03 05:28:24 +01:00
end
end
2022-02-04 04:06:21 +01:00
function camping_detection ( m )
2022-02-03 05:28:24 +01:00
-- this code only runs for the local player
local s = gPlayerSyncTable [ m.playerIndex ]
2022-02-04 09:15:14 +01:00
-- track how far the local player has moved recently
2022-02-04 04:06:21 +01:00
sDistanceMoved = sDistanceMoved - 0.25 + vec3f_dist ( sLastPos , m.pos ) * 0.02
vec3f_copy ( sLastPos , m.pos )
2022-02-04 09:15:14 +01:00
-- clamp between 0 to 100
if sDistanceMoved < 0 then sDistanceMoved = 0 end
if sDistanceMoved > 100 then sDistanceMoved = 100 end
-- if player hasn't moved enough, start a timer
2022-02-04 04:06:21 +01:00
if sDistanceMoved < 10 and not s.seeking then
sDistanceTimer = sDistanceTimer + 1
2022-02-03 05:28:24 +01:00
end
2022-02-04 09:15:14 +01:00
-- if the player has moved enough, reset the timer
2022-02-17 03:10:06 +01:00
if sDistanceMoved > 25 then
2022-02-04 04:06:21 +01:00
sDistanceTimer = 0
end
2022-02-04 09:15:14 +01:00
-- inform the player that they need to move, or make them a seeker
2022-02-17 03:10:06 +01:00
if sDistanceTimer > sDistanceTimeout then
2022-02-04 09:15:14 +01:00
s.seeking = true
end
2022-02-17 03:10:06 +01:00
-- make sound
if sDistanceTimer > 0 and sDistanceTimer % 30 == 1 then
play_sound ( SOUND_MENU_CAMERA_BUZZ , m.marioObj . header.gfx . cameraToObject )
end
2022-02-04 04:06:21 +01:00
end
2022-02-03 05:28:24 +01:00
2022-02-04 09:15:14 +01:00
function update ( )
-- check gamemode enabled state
if not gGlobalSyncTable.hideAndSeek then
return
end
2022-02-03 05:28:24 +01:00
2022-02-04 09:15:14 +01:00
-- only allow the server to figure out the seeker
if network_is_server ( ) then
server_update ( gMarioStates [ 0 ] )
2022-02-04 04:06:21 +01:00
end
2022-02-04 09:15:14 +01:00
-- check if local player is camping
2022-02-17 03:27:52 +01:00
if gGlobalSyncTable.roundState == ROUND_STATE_ACTIVE then
2022-03-26 07:16:56 +01:00
if gGlobalSyncTable.campingTimer then
camping_detection ( gMarioStates [ 0 ] )
else
sDistanceTimer = 0
end
2022-02-05 21:22:41 +01:00
else
sDistanceTimer = 0
end
2022-02-03 05:28:24 +01:00
end
function mario_update ( m )
2022-02-04 04:50:27 +01:00
-- check gamemode enabled state
if not gGlobalSyncTable.hideAndSeek then
return
end
2022-02-03 05:28:24 +01:00
-- this code runs for all players
local s = gPlayerSyncTable [ m.playerIndex ]
2022-02-04 09:15:14 +01:00
-- if the local player died, make them a seeker
if m.playerIndex == 0 and m.health <= 0x110 then
s.seeking = true
2022-02-04 04:06:21 +01:00
end
2022-02-03 05:28:24 +01:00
-- display all seekers as metal
if s.seeking then
m.marioBodyState . modelState = MODEL_STATE_METAL
m.health = 0x880
end
end
function mario_before_phys_step ( m )
2022-02-06 00:05:18 +01:00
-- prevent physics from being altered when bubbled
if m.action == ACT_BUBBLED then
return
end
2022-02-04 04:50:27 +01:00
-- check gamemode enabled state
if not gGlobalSyncTable.hideAndSeek then
return
end
2022-02-03 05:28:24 +01:00
local s = gPlayerSyncTable [ m.playerIndex ]
-- only make seekers faster
if not s.seeking then
return
end
local hScale = 1.0
local vScale = 1.0
-- make swimming seekers 5% faster
if ( m.action & ACT_FLAG_SWIMMING ) ~= 0 then
hScale = hScale * 1.05
if m.action ~= ACT_WATER_PLUNGE then
vScale = vScale * 1.05
end
end
-- faster ground movement
if ( m.action & ACT_FLAG_MOVING ) ~= 0 then
hScale = hScale * 1.19
end
m.vel . x = m.vel . x * hScale
m.vel . y = m.vel . y * vScale
m.vel . z = m.vel . z * hScale
end
function on_pvp_attack ( attacker , victim )
2022-02-04 04:50:27 +01:00
-- check gamemode enabled state
if not gGlobalSyncTable.hideAndSeek then
return
end
2022-02-03 05:28:24 +01:00
-- this code runs when a player attacks another player
local sAttacker = gPlayerSyncTable [ attacker.playerIndex ]
local sVictim = gPlayerSyncTable [ victim.playerIndex ]
-- only consider local player
if victim.playerIndex ~= 0 then
return
end
-- make victim a seeker
if sAttacker.seeking and not sVictim.seeking then
sVictim.seeking = true
end
end
function on_player_connected ( m )
2022-02-04 04:06:21 +01:00
-- start out as a non-seeker
2022-02-03 05:28:24 +01:00
local s = gPlayerSyncTable [ m.playerIndex ]
2022-02-17 03:27:52 +01:00
s.seeking = true
2022-02-17 07:30:17 +01:00
network_player_set_description ( gNetworkPlayers [ m.playerIndex ] , " seeker " , 255 , 64 , 64 , 255 )
2022-02-03 05:28:24 +01:00
end
2022-02-17 03:10:06 +01:00
function hud_top_render ( )
-- check gamemode enabled state
if not gGlobalSyncTable.hideAndSeek then
return
end
local seconds = 0
local text = ' '
2022-02-17 03:27:52 +01:00
if gGlobalSyncTable.roundState == ROUND_STATE_WAIT then
2022-02-17 03:10:06 +01:00
seconds = 60
text = ' waiting for players '
2022-02-17 03:27:52 +01:00
elseif gGlobalSyncTable.roundState == ROUND_STATE_ACTIVE then
seconds = math.floor ( sRoundEndTimeout / 30 - gGlobalSyncTable.displayTimer )
if seconds < 0 then seconds = 0 end
2022-02-17 03:10:06 +01:00
text = ' seekers have ' .. seconds .. ' seconds '
else
2022-02-17 03:27:52 +01:00
seconds = math.floor ( sRoundStartTimeout / 30 - gGlobalSyncTable.displayTimer )
if seconds < 0 then seconds = 0 end
2022-02-17 03:10:06 +01:00
text = ' next round in ' .. seconds .. ' seconds '
end
local scale = 0.50
-- get width of screen and text
local screenWidth = djui_hud_get_screen_width ( )
local width = djui_hud_measure_text ( text ) * scale
local x = ( screenWidth - width ) / 2.0
local y = 0
local background = 0.0
2022-02-17 03:27:52 +01:00
if seconds < 60 and gGlobalSyncTable.roundState == ROUND_STATE_ACTIVE then
2022-02-17 03:10:06 +01:00
background = ( math.sin ( sFlashingIndex / 10.0 ) * 0.5 + 0.5 ) * 1.0
background = background * background
background = background * background
end
-- render top
djui_hud_set_color ( 255 * background , 0 , 0 , 128 ) ;
djui_hud_render_rect ( x - 6 , y , width + 12 , 16 ) ;
djui_hud_set_color ( 255 , 255 , 255 , 255 ) ;
djui_hud_print_text ( text , x , y , scale ) ;
end
function hud_bottom_render ( )
local seconds = math.floor ( ( sDistanceTimeout - sDistanceTimer ) / 30 )
if seconds < 0 then seconds = 0 end
if sDistanceTimer < 1 then return end
local text = ' Keep moving! ( ' .. seconds .. ' ) '
local scale = 0.50
-- get width of screen and text
local screenWidth = djui_hud_get_screen_width ( )
local screenHeight = djui_hud_get_screen_height ( )
local width = djui_hud_measure_text ( text ) * scale
local x = ( screenWidth - width ) / 2.0
local y = screenHeight - 16
local background = ( math.sin ( sFlashingIndex / 10.0 ) * 0.5 + 0.5 ) * 1.0
background = background * background
background = background * background
-- render top
djui_hud_set_color ( 255 * background , 0 , 0 , 128 ) ;
djui_hud_render_rect ( x - 6 , y , width + 12 , 16 ) ;
djui_hud_set_color ( 255 , 255 , 255 , 255 ) ;
djui_hud_print_text ( text , x , y , scale ) ;
end
function hud_center_render ( )
2022-02-17 03:27:52 +01:00
if gGlobalSyncTable.displayTimer > 3 then return end
2022-02-17 03:10:06 +01:00
-- set text
local text = ' '
2022-02-17 03:27:52 +01:00
if gGlobalSyncTable.roundState == ROUND_STATE_SEEKERS_WIN then
2022-02-17 03:10:06 +01:00
text = ' Seekers Win! '
2022-02-17 03:27:52 +01:00
elseif gGlobalSyncTable.roundState == ROUND_STATE_HIDERS_WIN then
2022-02-17 03:10:06 +01:00
text = ' Hiders Win! '
2022-02-17 03:27:52 +01:00
elseif gGlobalSyncTable.roundState == ROUND_STATE_ACTIVE then
2022-02-17 03:10:06 +01:00
text = ' Go! '
else
return
end
-- set scale
2022-02-17 03:27:52 +01:00
local scale = 1
2022-02-17 03:10:06 +01:00
-- get width of screen and text
local screenWidth = djui_hud_get_screen_width ( )
local screenHeight = djui_hud_get_screen_height ( )
local width = djui_hud_measure_text ( text ) * scale
local height = 32 * scale
local x = ( screenWidth - width ) / 2.0
local y = ( screenHeight - height ) / 2.0
-- render
djui_hud_set_color ( 0 , 0 , 0 , 128 ) ;
djui_hud_render_rect ( x - 6 * scale , y , width + 12 * scale , height ) ;
djui_hud_set_color ( 255 , 255 , 255 , 255 ) ;
djui_hud_print_text ( text , x , y , scale ) ;
end
function on_hud_render ( )
-- render to N64 screen space, with the HUD font
djui_hud_set_resolution ( RESOLUTION_N64 )
djui_hud_set_font ( FONT_NORMAL )
hud_top_render ( )
hud_bottom_render ( )
hud_center_render ( )
sFlashingIndex = sFlashingIndex + 1
end
2022-02-04 04:50:27 +01:00
function on_hide_and_seek_command ( msg )
2022-02-04 09:14:38 +01:00
if not network_is_server ( ) then
djui_chat_message_create ( ' Only the server can change this setting! ' )
return true
end
2022-02-04 04:50:27 +01:00
if msg == ' on ' then
djui_chat_message_create ( ' Hide-and-seek mod: enabled ' )
gGlobalSyncTable.hideAndSeek = true
return true
elseif msg == ' off ' then
djui_chat_message_create ( ' Hide-and-seek mod: disabled ' )
gGlobalSyncTable.hideAndSeek = false
return true
end
return false
end
2022-03-26 07:16:56 +01:00
function on_anti_camp_command ( msg )
if not network_is_server ( ) then
djui_chat_message_create ( ' Only the server can change this setting! ' )
return true
end
if msg == ' on ' then
djui_chat_message_create ( ' Anti-camping timer: enabled ' )
gGlobalSyncTable.campingTimer = true
return true
elseif msg == ' off ' then
djui_chat_message_create ( ' Anti-camping timer: disabled ' )
gGlobalSyncTable.campingTimer = false
return true
end
return false
end
2022-03-26 07:10:43 +01:00
function on_pause_exit ( exitToCastle )
local s = gPlayerSyncTable [ 0 ]
if not s.seeking then
s.seeking = true
network_player_set_description ( gNetworkPlayers [ 0 ] , " seeker " , 255 , 64 , 64 , 255 )
end
end
2022-03-26 07:18:48 +01:00
function allow_pvp_attack ( m1 , m2 )
local s1 = gPlayerSyncTable [ m1.playerIndex ]
local s2 = gPlayerSyncTable [ m2.playerIndex ]
if s1.seeking == s2.seeking then
return false
end
return true
end
2022-02-04 09:15:14 +01:00
-----------------------
-- network callbacks --
-----------------------
2022-02-17 03:27:52 +01:00
function on_round_state_changed ( tag , oldVal , newVal )
local rs = gGlobalSyncTable.roundState
2022-02-04 09:15:14 +01:00
2022-02-17 03:27:52 +01:00
if rs == ROUND_STATE_WAIT then
-- nothing
elseif rs == ROUND_STATE_ACTIVE then
play_character_sound ( gMarioStates [ 0 ] , CHAR_SOUND_HERE_WE_GO )
elseif rs == ROUND_STATE_SEEKERS_WIN then
2022-02-17 03:10:06 +01:00
play_sound ( SOUND_MENU_CLICK_CHANGE_VIEW , gMarioStates [ 0 ] . marioObj.header . gfx.cameraToObject )
2022-02-17 03:27:52 +01:00
elseif rs == ROUND_STATE_HIDERS_WIN then
play_sound ( SOUND_MENU_CLICK_CHANGE_VIEW , gMarioStates [ 0 ] . marioObj.header . gfx.cameraToObject )
elseif rs == ROUND_STATE_UNKNOWN_END then
-- nothing
2022-02-04 09:15:14 +01:00
end
end
2022-02-17 03:27:52 +01:00
2022-02-04 09:15:14 +01:00
function on_seeking_changed ( tag , oldVal , newVal )
local m = gMarioStates [ tag ]
local np = gNetworkPlayers [ tag ]
-- play sound and create popup if became a seeker
if newVal and not oldVal then
play_sound ( SOUND_OBJ_BOWSER_LAUGH , m.marioObj . header.gfx . cameraToObject )
playerColor = network_get_player_text_color_string ( m.playerIndex )
djui_popup_create ( playerColor .. np.name .. ' \\ #ffa0a0 \\ is now a seeker ' , 2 )
sLastSeekerIndex = m.playerIndex
2022-02-17 03:27:52 +01:00
sRoundTimer = 0
2022-02-04 09:15:14 +01:00
end
2022-02-17 07:30:17 +01:00
if newVal then
network_player_set_description ( np , " seeker " , 255 , 64 , 64 , 255 )
else
network_player_set_description ( np , " hider " , 128 , 128 , 128 , 255 )
end
2022-02-04 09:15:14 +01:00
end
2022-02-03 05:28:24 +01:00
-----------
-- hooks --
-----------
hook_event ( HOOK_UPDATE , update )
hook_event ( HOOK_MARIO_UPDATE , mario_update )
hook_event ( HOOK_BEFORE_PHYS_STEP , mario_before_phys_step )
hook_event ( HOOK_ON_PVP_ATTACK , on_pvp_attack )
hook_event ( HOOK_ON_PLAYER_CONNECTED , on_player_connected )
2022-02-17 03:10:06 +01:00
hook_event ( HOOK_ON_HUD_RENDER , on_hud_render )
2022-03-26 07:10:43 +01:00
hook_event ( HOOK_ON_PAUSE_EXIT , on_pause_exit )
2022-03-26 07:18:48 +01:00
hook_event ( HOOK_ALLOW_PVP_ATTACK , allow_pvp_attack )
2022-02-04 04:50:27 +01:00
2022-02-04 09:14:38 +01:00
hook_chat_command ( ' hide-and-seek ' , " [on|off] turn hide-and-seek on or off " , on_hide_and_seek_command )
2022-03-26 07:16:56 +01:00
hook_chat_command ( ' anti-camp ' , " [on|off] turn the anti-camp timer on or off " , on_anti_camp_command )
2022-02-04 09:15:14 +01:00
-- call functions when certain sync table values change
2022-02-17 03:27:52 +01:00
hook_on_sync_table_change ( gGlobalSyncTable , ' roundState ' , 0 , on_round_state_changed )
2022-02-04 09:15:14 +01:00
for i = 0 , ( MAX_PLAYERS - 1 ) do
2022-02-17 03:10:06 +01:00
gPlayerSyncTable [ i ] . seeking = true
2022-02-04 09:15:14 +01:00
hook_on_sync_table_change ( gPlayerSyncTable [ i ] , ' seeking ' , i , on_seeking_changed )
2022-02-17 07:30:17 +01:00
network_player_set_description ( gNetworkPlayers [ i ] , " seeker " , 255 , 64 , 64 , 255 )
2022-02-04 09:15:14 +01:00
end
2022-03-26 07:10:43 +01:00