Round logic

This commit is contained in:
Fesiug 2023-12-21 19:04:34 -05:00
parent c6cd283755
commit faac326333
5 changed files with 252 additions and 94 deletions

View File

@ -9,45 +9,45 @@
"settings"
{
1
{
"name" "cnr_roundlength"
"text" "Round length (minutes)"
"help" ""
"type" "Numeric"
"default" "3"
}
2
{
"name" "cnr_roundlength"
"text" "Haste time (minutes)"
"help" ""
"type" "Numeric"
"default" "1"
}
3
{
"name" "cnr_maxrounds"
"text" "Game ends after X rounds"
"help" ""
"type" "Numeric"
"default" "6"
}
4
{
"name" "cnr_teamswap"
"text" "Swap teams after X rounds"
"help" ""
"type" "Numeric"
"default" "2"
}
5
{
"name" "cnr_friendlyfire"
"text" "Friendly fire"
"help" "Enable friendly fire"
"type" "CheckBox"
"default" "0"
}
//1
//{
// "name" "cnr_roundlength"
// "text" "Round length (minutes)"
// "help" ""
// "type" "Numeric"
// "default" "3"
//}
//2
//{
// "name" "cnr_hastetime"
// "text" "Haste time (minutes)"
// "help" ""
// "type" "Numeric"
// "default" "1"
//}
//3
//{
// "name" "cnr_maxrounds"
// "text" "Game ends after X rounds"
// "help" ""
// "type" "Numeric"
// "default" "6"
//}
//4
//{
// "name" "cnr_teamswap"
// "text" "Swap teams after X rounds"
// "help" ""
// "type" "Numeric"
// "default" "2"
//}
//5
//{
// "name" "cnr_friendlyfire"
// "text" "Friendly fire"
// "help" "Enable friendly fire"
// "type" "CheckBox"
// "default" "0"
//}
}
}

View File

@ -0,0 +1,22 @@
AddCSLuaFile()
ENT.Type = "point"
function ENT:SetupDataTables()
self:NetworkVar( "Float", 0, "RoundStartedAt" )
self:NetworkVar( "Float", 1, "PregameStartedAt" )
self:NetworkVar( "Float", 2, "RoundFinishedAt" )
self:NetworkVar( "Int", 0, "State" )
self:NetworkVar( "Int", 1, "RoundNumber" )
self:NetworkVar( "Int", 2, "Money" )
end
function ENT:Initialize()
self:AddEFlags( EFL_FORCE_CHECK_TRANSMIT )
end
function ENT:UpdateTransmitState()
return TRANSMIT_ALWAYS
end

View File

@ -34,6 +34,61 @@ surface.CreateFont( "CNR_HUD_4", {
weight = 0,
})
local special1 = {
normal = 12,
colon = 5,
clean1 = 2,
}
local special2 = {
normal = 16+4,
colon = 0,
clean1 = 6,
}
function MonoDraw( text, font, x, y, color, rightalign, special )
local s = ScreenScaleH
local bump = 0
text = tostring(text)
if rightalign then
local ogbump = 0
for i=1, #text do
local td = text[i]
if td == ":" or td == "," or td == "." then
ogbump = ogbump + special.colon
else
ogbump = ogbump + special.normal
end
end
x = x - s(ogbump)
end
for i=1, #text do
local td = text[i]
local clean = 0
if td == "1" then
clean = special.clean1
end
draw.SimpleText( td, font, x + s(bump) + s(clean), y, color )
if td == ":" or td == "," or td == "." then
bump = bump + special.colon
else
bump = bump + special.normal
end
end
end
-- State Look-up Table
local slut = {
[0] = "UNINIT",
[1] = "WAITING",
[2] = "PRE-GAME",
[3] = "TIME",
[4] = "POST-GAME",
}
local gamelogic = NULL
hook.Add("HUDPaint", "CNR_HUD", function()
local p, sw, sh = LocalPlayer(), ScrW(), ScrH()
local c = sw/2
@ -45,13 +100,21 @@ hook.Add("HUDPaint", "CNR_HUD", function()
local w = p:GetActiveWeapon()
w = IsValid(w) and w or false
if !gamelogic:IsValid() then
for i, ent in ents.Iterator() do
if ( ent:GetClass() == "cnr_logic" ) then gamelogic = ent print("Located CNR game logic entity") break end
end
end
do
local b_w, b_h = s(64+8), s(64)
local b_x, b_y = c1, sh - b_h - s(16)
surface.SetDrawColor( color_white )
surface.DrawRect( b_x, b_y, b_w, b_h )
draw.SimpleText( "HP", "CNR_HUD_1", c1 + b, sh - s(64+12), color_black )
draw.SimpleText( p:Health(), "CNR_HUD_2", c1 + b, sh - s(64), color_black )
local dumbfuck = tostring(p:Health()):Left(1) == "1" and s(4) or 0
MonoDraw( p:Health(), "CNR_HUD_2", c1 + b - dumbfuck, sh - s(64), color_black, false, special2 )
end
if w then
@ -60,76 +123,36 @@ hook.Add("HUDPaint", "CNR_HUD", function()
surface.SetDrawColor( color_white )
surface.DrawRect( b_x, b_y, b_w, b_h )
draw.SimpleText( "AMMO", "CNR_HUD_1", c2 - b, sh - s(64+12), color_black, TEXT_ALIGN_RIGHT )
draw.SimpleText( w:Clip1(), "CNR_HUD_2", c2 - b, sh - s(64), color_black, TEXT_ALIGN_RIGHT )
MonoDraw( w:Clip1(), "CNR_HUD_2", c2 - b, sh - s(64), color_black, true, special2 )
end
local state = gamelogic:GetState()
do
local b_w, b_h = s(64+8), s(42)
local b_x, b_y = c1, s(16)
surface.SetDrawColor( color_white )
surface.DrawRect( b_x, b_y, b_w, b_h )
draw.SimpleText( "TIME", "CNR_HUD_3", b_x + b, b_y + s(4), color_black, TEXT_ALIGN_LEFT )
draw.SimpleText( slut[ gamelogic:GetState() ], "CNR_HUD_3", b_x + b, b_y + s(4), color_black, TEXT_ALIGN_LEFT )
local fuckhead = ""
fuckhead = string.FormattedTime( -CurTime()*100, "%02i:%02i")
local bump = 0
for i=1, #fuckhead do
local damnit = b_x + b
local clean = 0
if fuckhead[i] == "1" then
clean = 2
end
draw.SimpleText( fuckhead[i], "CNR_HUD_4", damnit + s(bump) + s(clean), b_y + s(12), color_black, TEXT_ALIGN_LEFT )
if fuckhead[i] == ":" then
bump = bump + 5
local ltime = LOGIC:GetTimeLeft()
if ltime > 60 then
fuckhead = string.FormattedTime( LOGIC:GetTimeLeft(), "%02i:%02i")
else
bump = bump + 12
end
fuckhead = string.FormattedTime( ltime )
fuckhead = string.format( "%02i.%02i", fuckhead.s, fuckhead.ms )
end
MonoDraw( fuckhead, "CNR_HUD_4", b_x + b, b_y + s(12), color_black, false, special1 )
end
do
if state == STATE_INGAME or state == STATE_POSTGAME then
local b_w, b_h = s(172), s(30)
local b_x, b_y = c1, s(16) + b + s(42)
surface.SetDrawColor( color_white )
surface.DrawRect( b_x, b_y, b_w, b_h )
draw.SimpleText( "$", "CNR_HUD_3", b_x + b, b_y + s(8), color_black, TEXT_ALIGN_LEFT )
local fuckhead = ""
fuckhead = math.Round(CurTime()*100)
local fuckhead = gamelogic:GetMoney()
fuckhead = string.Comma( fuckhead )
local originalbump = 0
for i=1, #fuckhead do
if fuckhead[i] == "," then
originalbump = originalbump + s(6)
else
originalbump = originalbump + s(12)
end
end
local whatever = b_x + b_w - b - originalbump
local bump = 0
for i=1, #fuckhead do
local damnit = whatever + s(bump)
local clean = 0
if fuckhead[i] == "1" then
clean = 4
elseif fuckhead[i] == "2" then
clean = 0
end
draw.SimpleText( fuckhead[i], "CNR_HUD_4", damnit + s(clean), b_y, color_black, TEXT_ALIGN_LEFT )
if fuckhead[i] == "," then
bump = bump + 6
else
bump = bump + 12
end
end
MonoDraw( fuckhead, "CNR_HUD_4", b_x + b_w - b, b_y, color_black, true, special1 )
end
end)

View File

@ -0,0 +1,109 @@
CONVARS = {}
CONVARS["friendlyfire"] = CreateConVar( "cnr_friendlyfire", 0, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Whether shooting friendlies hurts them", 0, 1 )
CONVARS["time_round"] = CreateConVar( "cnr_time_round", 3*60, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Length of a round in seconds" )
CONVARS["time_haste"] = CreateConVar( "cnr_time_haste", 1*60, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Time before the round ends to enable haste mode in seconds" )
CONVARS["time_pregame"] = CreateConVar( "cnr_time_pregame", 5, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Time before the round starts in seconds" )
CONVARS["time_postgame"] = CreateConVar( "cnr_time_postgame", 5, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Time before the round restarts in seconds" )
CONVARS["rounds_swap"] = CreateConVar( "cnr_rounds_swap", 2, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Rounds before teams swap" )
CONVARS["rounds_max"] = CreateConVar( "cnr_rounds_max", 6, FCVAR_ARCHIVE + FCVAR_REPLICATED, "Maximum rounds before match finishes", 1 )
STATE_UNINITIALIZED = 0
STATE_WAITINGFORPLAYERS = 1
STATE_PREGAME = 2
STATE_INGAME = 3
STATE_POSTGAME = 4
if SERVER then
gamelogic = NULL
hook.Add( "Think", "CNR_GameLogic", function()
if !gamelogic:IsValid() then
for i, ent in ents.Iterator() do
if ( ent:GetClass() == "cnr_logic" ) then gamelogic = ent print("Located CNR game logic entity") break end
end
if !gamelogic:IsValid() then
gamelogic = ents.Create( "cnr_logic" )
assert( gamelogic:IsValid(), "Failed to create CNR game logic entity." )
gamelogic:Spawn()
print("Created CNR game logic entity")
end
end
local state = gamelogic:GetState()
if state == STATE_UNINITIALIZED then
-- Initialize
state = STATE_WAITINGFORPLAYERS
end
local willingplayers = #player.GetAll()
if state == STATE_WAITINGFORPLAYERS then
if willingplayers >= 2 then
-- Begin pregame
state = STATE_PREGAME
gamelogic:SetPregameStartedAt( RealTime() )
end
end
if state == STATE_PREGAME then
if willingplayers < 2 then
-- Cancel pregame
state = STATE_WAITINGFORPLAYERS
gamelogic:SetPregameStartedAt( 0 )
elseif (gamelogic:GetPregameStartedAt() + CONVARS["time_pregame"]:GetInt()) <= RealTime() then
-- Begin round
state = STATE_INGAME
gamelogic:SetRoundStartedAt( RealTime() )
gamelogic:SetMoney( 0 )
end
end
if state == STATE_INGAME then
gamelogic:SetMoney( gamelogic:GetMoney() + 1000 )
if (gamelogic:GetRoundStartedAt() + CONVARS["time_round"]:GetInt()) <= RealTime() then
state = STATE_POSTGAME
gamelogic:SetRoundFinishedAt( RealTime() )
end
end
if state == STATE_POSTGAME then
if (gamelogic:GetRoundFinishedAt() + CONVARS["time_postgame"]:GetInt()) <= RealTime() then
state = STATE_PREGAME
gamelogic:SetPregameStartedAt( RealTime() )
end
end
gamelogic:SetState( state )
end )
end
LOGIC = {}
function LOGIC:GetLogic()
for i, ent in ents.Iterator() do
if ( ent:GetClass() == "cnr_logic" ) then return ent end
end
if SERVER then
gamelogic = ents.Create( "cnr_logic" )
assert( gamelogic:IsValid(), "Failed to create CNR game logic entity." )
gamelogic:Spawn()
print("Created CNR game logic entity")
end
end
function LOGIC:GetTimeLeft()
local gamelogic = LOGIC:GetLogic()
if gamelogic:GetState() == STATE_PREGAME then
local time = CONVARS["time_pregame"]:GetInt()
return ( gamelogic:GetPregameStartedAt() + time ) - RealTime(), time
elseif gamelogic:GetState() == STATE_INGAME then
local time = CONVARS["time_round"]:GetInt()
return ( gamelogic:GetRoundStartedAt() + time ) - RealTime(), time
elseif gamelogic:GetState() == STATE_POSTGAME then
local time = CONVARS["time_postgame"]:GetInt()
return ( gamelogic:GetRoundFinishedAt() + time ) - RealTime(), time
else
return 0, 0
end
end

View File

@ -13,6 +13,9 @@ end
AddCSLuaFile("player_class_cnr.lua")
include ("player_class_cnr.lua")
AddCSLuaFile("logic.lua")
include ("logic.lua")
function GM:Initialize()
-- Do stuff
end
@ -132,3 +135,4 @@ concommand.Add( "cnr_cheat_weapons", function( p )
end)
-- Include module loader here