Skip to content

Commit

Permalink
Merge pull request #22 from Viltzu/feature-spectator
Browse files Browse the repository at this point in the history
Feature spectator
  • Loading branch information
valscion committed Mar 12, 2012
2 parents b7793ea + 5d5d638 commit 9f50164
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 30 deletions.
31 changes: 21 additions & 10 deletions cb_source/Client.cb
Expand Up @@ -18,6 +18,9 @@ gPacketWaitTimer = 0
Global gReconnectToServer
gReconnectToServer = False

Global gChangeTeam
gChangeTeam = -1

//================================================================================
// Liityt��n peliin
//================================================================================
Expand Down Expand Up @@ -285,11 +288,13 @@ Function ClientLoop()
If gSessionComplete = False And player\loggedIn = True And player\health > 0 And volAdjust = False Then

obj = player\obj
If MouseMoveX() <> 0 Or MouseMoveY() <> 0 Then
If MouseMoveX() <> 0 Or MouseMoveY() <> 0 And player\team <> 0 Then
// Jos hiiri liikkuu niin k��nnet��n sit� kohti
RotateObject obj, -GetAngle(ObjectX(obj), ObjectY(obj), MouseWX(), MouseWY())
// T�ht�in n�kyviin
ShowMouse gImages(IMG_SIGHT)
ElseIf player\team = 0 Then
ShowMouse Off
EndIf

// hided = False
Expand All @@ -303,13 +308,12 @@ Function ClientLoop()
// EndIf

If LeftKey() Or RightKey() Then ShowMouse OFF
If gControlMode = 1 Then
If gControlMode = 1 And player\team <> 0 Then
If KeyDown(cbKeyW) Or UpKey() Then MoveObject obj, PxPerSec(FORWARD_SPEED) * 100.0 / aWeapon( player\weapon, WPNF_WEIGHT )
If KeyDown(cbKeyS) Or DownKey() Then MoveObject obj, -PxPerSec(BACKWARD_SPEED) * 100.0 / aWeapon( player\weapon, WPNF_WEIGHT )
If KeyDown(cbKeyA) Then MoveObject obj, 0, -PxPerSec(SIDESTEP_SPEED) * 100.0 / aWeapon( player\weapon, WPNF_WEIGHT )
If KeyDown(cbKeyD) Then MoveObject obj, 0, PxPerSec(SIDESTEP_SPEED) * 100.0 / aWeapon( player\weapon, WPNF_WEIGHT )
EndIf
If gControlMode = 2 Then
ElseIf gControlMode = 2 Then
moveX = 0
moveY = 0
If KeyDown(cbKeyW) Then moveY = PxPerSec(FORWARD_SPEED) * 100.0 / aWeapon( player\weapon, WPNF_WEIGHT )
Expand Down Expand Up @@ -404,10 +408,12 @@ Function ClientLoop()
// GameUpdate()
If gSessionComplete = False Then
CloneCameraPosition player\obj
If gCameraMode =< 1 Then
CloneCameraPosition player\obj
ElseIf gCameraMode => 2 Then
PositionCamera ObjectX(player\obj)+(MouseX()-ScreenWidth()/2)/1.2, ObjectY(player\obj)-(MouseY()-ScreenHeight()/2)/1.2
If player\team <> 0 Then
If gCameraMode =< 1 Then
CloneCameraPosition player\obj
ElseIf gCameraMode => 2 Then
PositionCamera ObjectX(player\obj)+(MouseX()-ScreenWidth()/2)/1.2, ObjectY(player\obj)-(MouseY()-ScreenHeight()/2)/1.2
EndIf
EndIf
EndIf

Expand Down Expand Up @@ -498,7 +504,7 @@ Function ClientLoop()
Exit
EndIf
If gKickedOut Then Exit

If player\team = 0 Then ResetObjectCollision player\obj
If player\loggedIn Then DrawScreen
UpdateGame2()
PlayMusic()
Expand Down Expand Up @@ -1152,7 +1158,7 @@ Function SendDataToServer()
If aWeapon(player\weapon, WPNF_AMMO) > 0 Then ammoExists = True
// Tutkitaan voidaanko l�hett�� tieto ampumisesta
shootNow = 0
If (MouseDown(1) = True Or KeyDown(cbKeySpace) = True) And (player\weapon<>WPN_PISTOL Or gNotShotYet = True) And gConsoleMode = False And gDevConsole = False And gSessionComplete = False And player\spawnTime = 0 Then
If (MouseDown(1) = True Or KeyDown(cbKeySpace) = True) And (player\weapon<>WPN_PISTOL Or gNotShotYet = True) And gConsoleMode = False And gDevConsole = False And gSessionComplete = False And player\spawnTime = 0 And player\team <> 0 Then
// Onko ase latingissa
If player\lastShoot + aWeapon(player\weapon, WPNF_RELOADTIME) < Timer() And player\health > 0 Then
// Onko ammuksia
Expand Down Expand Up @@ -1190,6 +1196,11 @@ Function SendDataToServer()
_Net_PutString(gServerMap)
EndIf

If gChangeTeam <> -1 Then
_Net_PutByte(NET_TEAMINFO)
_Net_PutByte(gChangeTeam)
gChangeTeam = -1
EndIf

_Net_PutByte(NET_END)
gBbsOutCount = gBbsOutCount + MEMBlockSize(gNetMemBlock)-4
Expand Down
12 changes: 12 additions & 0 deletions cb_source/Config.cb
Expand Up @@ -83,6 +83,14 @@ gDisableSpeedhackTest = False
Global gCameraMode
gCameraMode = 1

// Maksimi aika sekunteina kauanko pelaaja voi olla katsojana
Global gMaxSpectatorTime
gMaxSpectatorTime = 300 // 5min

// Aika joka pelaajan pit�� odottaa ennenkuin p��see katsojaksi
Global gSpectatorDelay
gSpectatorDelay = 10

ReadConfig()
//--------------------------------------------------------------------------------
// Luetaan asetukset jos tiedosto on olemassa
Expand Down Expand Up @@ -115,6 +123,8 @@ Function ReadConfig()
If key = "MAPSERVER" Then gMapServerUrl = value
If key = "DISABLESPEEDHACKTEST" Then gDisableSpeedhackTest = value
If key = "CAMERAMODE" Then gCameraMode = value
If key = "MAXSPECTATORTIME" Then gMaxSpectatorTime = value
If key = "SPECTATORDELAY" Then gSpectatorDelay = value
Wend
CloseFile f
EndIf
Expand Down Expand Up @@ -145,5 +155,7 @@ Function SaveConfig()
WriteLine f, "MapServer=" + Replace( gMapServerUrl, "=", "&#61;" )
WriteLine f, "DisableSpeedhackTest=" + gDisableSpeedhackTest
WriteLine f, "CameraMode=" + gCameraMode
WriteLine f, "MaxSpectatorTime=" + gMaxSpectatorTime
WriteLine f, "SpectatorDelay=" + gSpectatorDelay
CloseFile f
EndFunction
31 changes: 28 additions & 3 deletions cb_source/Console.cb
Expand Up @@ -22,9 +22,11 @@ Const CONSOLE_ROWS = 25
// Komennot
// Komentojen toiminnallisuus tapahtuu RunCommand-funktiossa
//--------------------------------------------------------------------------------
Const COMMAND_HELP = 0
Const COMMAND_QUIT = 1
Const COMMAND_COUNT = 1
Const COMMAND_HELP = 0
Const COMMAND_QUIT = 1
Const COMMAND_JOIN = 2
Const COMMAND_SPECTATE = 3
Const COMMAND_COUNT = 3

Const CMD_NAME = 0 // Name
Const CMD_ARGS = 1 // Arguments
Expand All @@ -46,6 +48,14 @@ aCommands(COMMAND_QUIT, CMD_NAME) = "quit"
aCommands(COMMAND_QUIT, CMD_ARGS) = ""
aCommands(COMMAND_QUIT, CMD_DESC) = "Quits the game"

aCommands(COMMAND_JOIN, CMD_NAME) = "join"
aCommands(COMMAND_JOIN, CMD_ARGS) = "[team]"
aCommands(COMMAND_JOIN, CMD_DESC) = "Joins to team, team can be 0 for spectator, if left blank it will join to game" // My�hemmin my�s 1 ja 2

aCommands(COMMAND_SPECTATE, CMD_NAME) = "spectate"
aCommands(COMMAND_SPECTATE, CMD_ARGS) = ""
aCommands(COMMAND_SPECTATE, CMD_DESC) = "Joins to spectators"

Global gDevConsoleText$, gDevConsoleInput, gDevConsoleACId
gDevConsoleACId = -1

Expand Down Expand Up @@ -139,6 +149,21 @@ Function RunCommand(_command$, _output = 1)
// Odotellaan viel� puoli sekuntia
Wait 500 - (Timer() - t)
End


// Syntax: join
ElseIf cmdId = COMMAND_JOIN Then
team = GetArgument("team", _command, cmdId, 1)
If team < 0 Or team > 2 Then
out = "Team must be 0 for spectator or 1 for joining to game" // My�hemmin my�s tiimit 1 ja 2
Else
gChangeTeam = team // Jos 0, liittyy katsojaksi, jos jotain muuta liittyy peliin ja serveri p��tt�� tiimin t�ll� hetkell�
EndIf

// Syntax: spectate
ElseIf cmdId = COMMAND_SPECTATE Then
gChangeTeam = 0

EndIf
Else
out = out + "Unknown command '" + cmd + "'."
Expand Down
9 changes: 5 additions & 4 deletions cb_source/Game.cb
Expand Up @@ -172,7 +172,7 @@ Function GameUpdate()

// Soitellaan tyhj�k�ynti��ni�
playIdle = False
If player\weapon = WPN_CHAINSAW And player\health > 0 Then
If player\weapon = WPN_CHAINSAW And player\health > 0 And player\team <> 0 Then
If player\playerId = gCurrentPlayerId Then
If aWeapon(WPN_CHAINSAW, WPNF_AMMO) > 0 Then
playIdle = True
Expand Down Expand Up @@ -220,7 +220,7 @@ Function GameUpdate()
UpdateRadar(player\obj)

// Varmistetaan ett� pelaaja pysyy kartalla
If InMap(ObjectX(player\obj), ObjectY(player\obj)) = True Then
If InMap(ObjectX(player\obj), ObjectY(player\obj)) = True Or player\team = 0 Then
// Jos pelaaja on kartalla niin talletetaan valid-position
player\lastValidX = ObjectX(player\obj)
player\lastValidY = ObjectY(player\obj)
Expand Down Expand Up @@ -274,7 +274,7 @@ Function GameUpdate()
If player\zombie = True Then player\hasAmmos = True

// Onko pelaaja kuollut
If player\health <= 0 Then
If player\health <= 0 And player\team <> 0 Then
// Jos kuolemasta on kulunut tarpeeksi aikaa niin her�tet��n
If player\timeToDeath + DEATH_DELAY < Timer() Then
SpawnObject(player\obj)
Expand Down Expand Up @@ -855,6 +855,7 @@ Function Login(_in, _playerId = 0)
//COM1 If player\loggedIn = True Then gPlayerCount - 1
player\loggedIn = False
player\admin = False
player\team = 1
WriteLog(player\name + " logged out")
// L�hetet��n viesti kaikille muille paitsi boteille
For plr.PLAYERS = Each PLAYERS
Expand Down Expand Up @@ -1130,7 +1131,7 @@ Function AnimatePlayers()
player\prevPosY = oy
lp# = player\legPos
lp = WrapAngle(lp + Distance(ox, oy, px, py) * 3.0)
If player\playerId = gCurrentPlayerId Then
If player\playerId = gCurrentPlayerId And player\team <> 0 Then
If (player\legPos <= 90 And lp > 90) Or (player\legPos <= 270 And lp > 270) Then
PlayGameSound(SND_FOOTSTEP, CameraX(), CameraY() + 1500)
EndIf
Expand Down
2 changes: 1 addition & 1 deletion cb_source/NetMatch_TheEnd.cb
Expand Up @@ -28,7 +28,7 @@ EndIf
SAFEEXIT OFF

Const NM_VERSION$ = "2.5"
Const NM_REVISION$ = ""
Const NM_REVISION$ = "a"
// Jos palvelinta ei tarvitse p�ivitt�� muutosten yhteydess�, nostetaan vain NM_PATCH
// muuttujan arvoa. T�m� nollataan aina kun tulee uusi revision tai versio.
Const NM_PATCH = 0
Expand Down
2 changes: 2 additions & 0 deletions cb_source/Player.cb
Expand Up @@ -116,6 +116,8 @@ Type PLAYERS
Field lag As Integer // Pelaajan lagi millisekunneissa
Field spHackTimer As Integer // Viimeisimm�ss� nopeuden tarkistuksessa otettu Timer()
Field handShooted As Byte // Kummalla k�dell� on viimeksi ammuttu (pistooli) 0=vasen 1=oikea
Field toSpectator As Byte // Kertoo jos pelaaja on menossa katsojaksi
Field spectatorTime As Integer // Milloin pelaaja liittyi katsojaksi
EndType

//--------------------------------------------------------------------------------
Expand Down
107 changes: 95 additions & 12 deletions cb_source/Server.cb
Expand Up @@ -5,6 +5,7 @@
//********************************************************************************

Global gBotFreeze As Byte
Global gLastWarnTime As Integer // Aika milloin viimeksi pelaajaa varoitettiin potkuista

// cbNetworkin Gosub-toteutusta varten tarvittavat muuttujat.
Dim gs_valueInt As Integer
Expand Down Expand Up @@ -346,6 +347,36 @@ ServerLoop:
Goto returnToLoop
EndIf

// Pelaajan heitto katsojaksi
If player\toSpectator = True And player\spectatorTime + gSpectatorDelay*1000 < Timer() Then
SetTeam(player\playerId,0)
player\health = -10
player\death = True
player\timeToDeath = Timer()
player\toSpectator = False
player\spectatorTime = Timer()

newMsg.NET_MESSAGES = New( NET_MESSAGES )
newMsg\msgType = NET_SERVERMSG
newMsg\toPlayer = gCurrentPlayerId
newMsg\msgText = "You have been moved to spectators, AFK timeout is set to "+gMaxSpectatorTime+"s"
EndIf

// Varoitetaan pelaajaa 5s v�lein jos ollaan potkimassa ulos
If player\team = 0 And gMaxSpectatorTime > 0 And Timer() - player\spectatorTime > gMaxSpectatorTime*1000 - 31000 And Timer() - gLastWarnTime > 5000 Then
timeLeft = (gMaxSpectatorTime*1000 - (Timer() - player\spectatorTime))/1000
newMsg.NET_MESSAGES = New( NET_MESSAGES )
newMsg\msgType = NET_SERVERMSG
newMsg\toPlayer = gCurrentPlayerId
newMsg\msgText = "You have to join to the game in "+timeLeft+" seconds or you will be kicked out"
gLastWarnTime = Timer()
EndIf

// Liian kauan katsojana
If player\team = 0 And gMaxSpectatorTime > 0 And player\spectatorTime + gMaxSpectatorTime*1000 < Timer() Then
KickPlayer(player\playerId, 0, "Too long as a spectator")
EndIf

// Lasketaan pelaajan ja serverin v�linen lagi
player\lag = ( Timer() - player\lastActivity )

Expand Down Expand Up @@ -379,7 +410,7 @@ ServerLoop:
picked = gs_valueByte // Poimitun itemin id (0, jos ei poimittu)

// Arvot p�ivitet�n vain jos pelaaja on hengiss�
If player\death = False Then
If player\death = False And player\team <> 0 Then
player\x = x
player\y = y
player\angle = angle
Expand Down Expand Up @@ -425,7 +456,7 @@ ServerLoop:
EndIf
EndIf
EndIf
If player\health > 0 Then player\death = False
If player\health > 0 And player\team <> 0 Then player\death = False
If player\loggedIn = False Then
//COM1 gPlayerCount + 1
player\loggedIn = True
Expand Down Expand Up @@ -458,6 +489,55 @@ ServerLoop:
Gosub gs_GetString
player\mapName = Trim(gs_valueString)

ElseIf netmsg = NET_TEAMINFO Then
Gosub gs_GetByte
team = gs_valueByte
If team = 0 And player\team <> 0 And gMaxSpectatorTime >= 0 Then // Pelaaja haluaa katsojaksi
player\toSpectator = True
player\spectatorTime = Timer()
newMsg.NET_MESSAGES = New( NET_MESSAGES )
newMsg\msgType = NET_SERVERMSG
newMsg\toPlayer = gCurrentPlayerId
newMsg\msgText = "You will be moved to spectators in "+gSpectatorDelay+" seconds"
ElseIf gMaxSpectatorTime < 0 Then
newMsg.NET_MESSAGES = New( NET_MESSAGES )
newMsg\msgType = NET_SERVERMSG
newMsg\toPlayer = gCurrentPlayerId
newMsg\msgText = "Spectator mode is disabled on this server"
ElseIf team <> 0 Then
team = 1
greenplayers = 0
redplayers = 0
If gPlayMode = TDM Then
For plr.PLAYERS = Each PLAYERS
If plr\active = True Then
If plr\team = 1 Then greenplayers + 1
If plr\team = 2 Then redplayers + 1
EndIf
Next plr
If greenplayers<redplayers Then
team = 1
ElseIf redplayers<greenplayers
team = 2
Else
team = Rand(1,2)
EndIf
EndIf

// Spawnataan pelaaja
SetTeam(player\playerId, team)
SpawnObject(player\obj)
player\x = ObjectX(player\obj)
player\y = ObjectY(player\obj)
player\angle = ObjectAngle(player\obj)
player\health = 100
player\death = False
player\lastValidX = player\x
player\lastValidY = player\y
player\hackTestX = player\x
player\hackTestY = player\y
player\spawnTime = Timer()
EndIf
Else
// Viestit loppui tai tuli tuntematon viesti
Exit
Expand Down Expand Up @@ -510,17 +590,19 @@ ServerLoop:
EndIf

// L�hetet��n niiden pelaajien tiedot jotka ovat hengiss� ja n�kyviss�
If plr\active = True Then
If plr\active = True And plr\team <> 0 Then
visible = True
x1 = ObjectX(player\obj)
y1 = ObjectY(player\obj)
x2 = ObjectX(plr\obj)
y2 = ObjectY(plr\obj)
If Abs(x1 - x2) > 750 Then visible = False
If Abs(y1 - y2) > 650 Then visible = False

If player\team <> 0 Then
x1 = ObjectX(player\obj)
y1 = ObjectY(player\obj)
x2 = ObjectX(plr\obj)
y2 = ObjectY(plr\obj)
If Abs(x1 - x2) > 750 Then visible = False
If Abs(y1 - y2) > 650 Then visible = False
EndIf

// Onko n�kyviss� tai voidaanko muuten l�hett��
If sendNames = True Or visible = True Or plr\health <= 0 Then
If sendNames = True Or visible = True Or plr\health <= 0 And plr\team <> 0 Then
// N�kyy
gs_valueByte = NET_PLAYER : Gosub gs_PutByte // Pelaajan tietoja
gs_valueByte = plr\playerId : Gosub gs_PutByte // Pelaajan tunnus
Expand All @@ -544,7 +626,7 @@ ServerLoop:
gs_valueByte = plr\health : Gosub gs_PutByte // Terveys
gs_valueShort = plr\kills : Gosub gs_PutShort // Tapot
gs_valueShort = plr\deaths : Gosub gs_PutShort // Kuolemat
ElseIf gRadarArrows = True Or gPlayMode=TDM Then
ElseIf gRadarArrows = True Or gPlayMode=TDM And plr\team <> 0 Then
// Ei n�y. L�hetet��n tutkatieto
If player\team = plr\team And gRadarArrows = False Then
gs_valueByte = NET_RADAR : Gosub gs_PutByte
Expand Down Expand Up @@ -864,6 +946,7 @@ Function KickPlayer( _playerId, _kickerId, _reason$ = "" )
player\loggedIn = False
player\active = False
player\admin = False
player\team = 1
// L�hetet��n viesti kaikille muille paitsi boteille
For plr.PLAYERS = Each PLAYERS
If plr\active = True And plr\zombie = False Then
Expand Down

0 comments on commit 9f50164

Please sign in to comment.