From 366e1759832ce8ba74e904a60f50bc22962c7ffe Mon Sep 17 00:00:00 2001 From: btd Date: Sun, 29 Aug 2010 00:53:42 +0400 Subject: [PATCH] Logic and scores seems done. Next will be collision --- src/game/server/entities/character.cpp | 166 +++++++++++++------------ src/game/server/entities/character.h | 4 +- src/game/server/gamecontext.cpp | 20 +++ src/game/server/gamecontroller.cpp | 6 +- src/game/server/gamecontroller.h | 4 +- src/game/server/gamemodes/DDRace.cpp | 2 +- src/game/server/gamemodes/DDRace.h | 5 +- src/game/server/teams.cpp | 60 ++++++--- src/game/server/teams.h | 20 +-- 9 files changed, 182 insertions(+), 105 deletions(-) diff --git a/src/game/server/entities/character.cpp b/src/game/server/entities/character.cpp index a27f451d7..d38475055 100644 --- a/src/game/server/entities/character.cpp +++ b/src/game/server/entities/character.cpp @@ -551,8 +551,89 @@ void CCharacter::OnDirectInput(CNetObj_PlayerInput *pNewInput) mem_copy(&m_LatestPrevInput, &m_LatestInput, sizeof(m_LatestInput)); } +void CCharacter::OnFinish() { + //TODO: this ugly + float time = (float)(Server()->Tick() - m_StartTime) / ((float)Server()->TickSpeed()); + CPlayerData *pData = GameServer()->Score()->PlayerData(m_pPlayer->GetCID()); + char aBuf[128]; + m_CpActive=-2; + str_format(aBuf, sizeof(aBuf), "%s finished in: %d minute(s) %5.2f second(s)", Server()->ClientName(m_pPlayer->GetCID()), (int)time/60, time-((int)time/60*60)); + if(!g_Config.m_SvHideScore) + GameServer()->SendChatTarget(m_pPlayer->GetCID(), aBuf); + else + GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf); + + if(time - pData->m_BestTime < 0) + { + // new record \o/ + str_format(aBuf, sizeof(aBuf), "New record: %5.2f second(s) better", time - pData->m_BestTime); + if(!g_Config.m_SvHideScore) + GameServer()->SendChatTarget(m_pPlayer->GetCID(), aBuf); + else + GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf); + } + + if(!pData->m_BestTime || time < pData->m_BestTime) + { + // update the score + pData->Set(time, m_CpCurrent); + + if(str_comp_num(Server()->ClientName(m_pPlayer->GetCID()), "nameless tee", 12) != 0) + GameServer()->Score()->SaveScore(m_pPlayer->GetCID(), time, this); + } + + // update server best time + if(GameServer()->m_pController->m_CurrentRecord == 0 || time < GameServer()->m_pController->m_CurrentRecord) + { + // check for nameless + if(str_comp_num(Server()->ClientName(m_pPlayer->GetCID()), "nameless tee", 12) != 0) { + GameServer()->m_pController->m_CurrentRecord = time; + //dbg_msg("character", "Finish"); +// GetPlayer()->SendServerRecord(); + } + + } + + m_RaceState = RACE_NONE; + // set player score + if(!GameServer()->Score()->PlayerData(m_pPlayer->GetCID())->m_CurrentTime || GameServer()->Score()->PlayerData(m_pPlayer->GetCID())->m_CurrentTime > time) + { + GameServer()->Score()->PlayerData(m_pPlayer->GetCID())->m_CurrentTime = time; + + // send it to all players + for(int i = 0; i < MAX_CLIENTS; i++) + { + if(GameServer()->m_apPlayers[i] && GameServer()->m_apPlayers[i]->m_IsUsingRaceClient) + { + if(g_Config.m_SvHideScore || i == m_pPlayer->GetCID()) + { + CNetMsg_Sv_PlayerTime Msg; + char aBuf[16]; + str_format(aBuf, sizeof(aBuf), "%.0f", time*100.0f); // damn ugly but the only way i know to do it + int TimeToSend; + sscanf(aBuf, "%d", &TimeToSend); + Msg.m_Time = TimeToSend; + Msg.m_Cid = m_pPlayer->GetCID(); + Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, i); + } + } + } + } + + int TTime = 0-(int)time; + if(m_pPlayer->m_Score < TTime) + m_pPlayer->m_Score = TTime; + +} + +int CCharacter::Team() { + CGameControllerDDRace* Controller = (CGameControllerDDRace*)GameServer()->m_pController; + return Controller->m_Teams.GetTeam(m_pPlayer->GetCID()); +} + void CCharacter::Tick() { + CGameControllerDDRace* Controller = (CGameControllerDDRace*)GameServer()->m_pController; int MapIndex = GameServer()->Collision()->GetMapIndex(m_PrevPos, m_Pos); int TileIndex1 = GameServer()->Collision()->GetCollisionDDRace(MapIndex); int TileIndex2 = GameServer()->Collision()->GetFCollisionDDRace(MapIndex); @@ -560,7 +641,7 @@ void CCharacter::Tick() if(m_pPlayer->m_ForceBalanced) { char Buf[128]; - str_format(Buf, sizeof(Buf), "You were moved to %s due to team balancing", GameServer()->m_pController->GetTeamName(m_pPlayer->GetTeam())); + str_format(Buf, sizeof(Buf), "You were moved to %s due to team balancing", Controller->GetTeamName(m_pPlayer->GetTeam())); GameServer()->SendBroadcast(Buf, m_pPlayer->GetCID()); m_pPlayer->m_ForceBalanced = false; @@ -673,87 +754,14 @@ void CCharacter::Tick() m_CpCurrent[cp] = time; m_CpTick = Server()->Tick() + Server()->TickSpeed()*2; } - if(((TileIndex1 == TILE_BEGIN) || (TileIndex2 == TILE_BEGIN)) && (m_RaceState == RACE_NONE || m_RaceState == RACE_STARTED)) + if(((TileIndex1 == TILE_BEGIN) || (TileIndex2 == TILE_BEGIN)) && m_RaceState == RACE_NONE) { - //TODO: CTeams::OnCharacterStart(); - m_StartTime = Server()->Tick(); - m_RefreshTime = Server()->Tick(); - m_RaceState = RACE_STARTED; + Controller->m_Teams.OnCharacterStart(m_pPlayer->GetCID()); } if(((TileIndex1 == TILE_END) || (TileIndex2 == TILE_END)) && m_RaceState == RACE_STARTED) { - //TODO: CTeams::OnCharacterFinish() - //TODO2: make method for finishing - char aBuf[128]; - m_CpActive=-2; - str_format(aBuf, sizeof(aBuf), "%s finished in: %d minute(s) %5.2f second(s)", Server()->ClientName(m_pPlayer->GetCID()), (int)time/60, time-((int)time/60*60)); - if(!g_Config.m_SvHideScore) - GameServer()->SendChatTarget(m_pPlayer->GetCID(), aBuf); - else - GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf); - - if(time - pData->m_BestTime < 0) - { - // new record \o/ - str_format(aBuf, sizeof(aBuf), "New record: %5.2f second(s) better", time - pData->m_BestTime); - if(!g_Config.m_SvHideScore) - GameServer()->SendChatTarget(m_pPlayer->GetCID(), aBuf); - else - GameServer()->SendChat(-1, CGameContext::CHAT_ALL, aBuf); - } - - if(!pData->m_BestTime || time < pData->m_BestTime) - { - // update the score - pData->Set(time, m_CpCurrent); - - if(str_comp_num(Server()->ClientName(m_pPlayer->GetCID()), "nameless tee", 12) != 0) - GameServer()->Score()->SaveScore(m_pPlayer->GetCID(), time, this); - } - - // update server best time - if(GameServer()->m_pController->m_CurrentRecord == 0 || time < GameServer()->m_pController->m_CurrentRecord) - { - // check for nameless - if(str_comp_num(Server()->ClientName(m_pPlayer->GetCID()), "nameless tee", 12) != 0) { - GameServer()->m_pController->m_CurrentRecord = time; - //dbg_msg("character", "Finish"); -// GetPlayer()->SendServerRecord(); - } - - } - - m_RaceState = RACE_NONE; - // set player score - if(!GameServer()->Score()->PlayerData(m_pPlayer->GetCID())->m_CurrentTime || GameServer()->Score()->PlayerData(m_pPlayer->GetCID())->m_CurrentTime > time) - { - GameServer()->Score()->PlayerData(m_pPlayer->GetCID())->m_CurrentTime = time; - - // send it to all players - for(int i = 0; i < MAX_CLIENTS; i++) - { - if(GameServer()->m_apPlayers[i] && GameServer()->m_apPlayers[i]->m_IsUsingRaceClient) - { - if(g_Config.m_SvHideScore || i == m_pPlayer->GetCID()) - { - CNetMsg_Sv_PlayerTime Msg; - char aBuf[16]; - str_format(aBuf, sizeof(aBuf), "%.0f", time*100.0f); // damn ugly but the only way i know to do it - int TimeToSend; - sscanf(aBuf, "%d", &TimeToSend); - Msg.m_Time = TimeToSend; - Msg.m_Cid = m_pPlayer->GetCID(); - Server()->SendPackMsg(&Msg, MSGFLAG_VITAL, i); - } - } - } - } - - int TTime = 0-(int)time; - if(m_pPlayer->m_Score < TTime) - m_pPlayer->m_Score = TTime; - + Controller->m_Teams.OnCharacterFinish(m_pPlayer->GetCID()); } if(((TileIndex1 == TILE_FREEZE) || (TileIndex2 == TILE_FREEZE)) && !m_Super) { @@ -1156,13 +1164,15 @@ bool CCharacter::IncreaseArmor(int Amount) void CCharacter::Die(int Killer, int Weapon) { int ModeSpecial = GameServer()->m_pController->OnCharacterDeath(this, GameServer()->m_apPlayers[Killer], Weapon); - + CGameControllerDDRace* Controller = (CGameControllerDDRace*)GameServer()->m_pController; char aBuf[256]; str_format(aBuf, sizeof(aBuf), "kill killer='%d:%s' victim='%d:%s' weapon=%d special=%d", Killer, Server()->ClientName(Killer), m_pPlayer->GetCID(), Server()->ClientName(m_pPlayer->GetCID()), Weapon, ModeSpecial); GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf); + Controller->m_Teams.SetCharacterTeam(m_pPlayer->GetCID(), 0); + // send the kill message CNetMsg_Sv_KillMsg Msg; Msg.m_Killer = Killer; diff --git a/src/game/server/entities/character.h b/src/game/server/entities/character.h index 120405b7e..f6d379e9e 100644 --- a/src/game/server/entities/character.h +++ b/src/game/server/entities/character.h @@ -4,7 +4,6 @@ #include #include #include - #include enum @@ -92,6 +91,9 @@ public: int m_RaceState; + void OnFinish(); + int Team(); + struct { vec2 m_ActivationDir; diff --git a/src/game/server/gamecontext.cpp b/src/game/server/gamecontext.cpp index 3910f25b1..8aa81072e 100644 --- a/src/game/server/gamecontext.cpp +++ b/src/game/server/gamecontext.cpp @@ -748,6 +748,25 @@ void CGameContext::OnMessage(int MsgId, CUnpacker *pUnpacker, int ClientId) pChr->m_BroadTime=true; } } + else if(!str_comp_num(pMsg->m_pMessage, "/team", 5)) + { + int Num; + + if(sscanf(pMsg->m_pMessage, "/team %d", &Num) == 1) { + if(((CGameControllerDDRace*)m_pController)->m_Teams.SetCharacterTeam(p->GetCID(), Num)) { + char aBuf[512]; + str_format(aBuf, sizeof(aBuf), "%s joined to Team %d", Server()->ClientName(p->GetCID()), Num); + SendChat(-1, CGameContext::CHAT_ALL, aBuf); + } else { + SendChatTarget(ClientId, "You cannot join to this team"); + } + } else { + char aBuf[512]; + str_format(aBuf, sizeof(aBuf), "You are in team %d", p->GetCharacter()->Team()); + SendChatTarget(ClientId, aBuf); + } + + } else if (!str_comp_nocase(pMsg->m_pMessage, "/broadcast")&&g_Config.m_SvEmotionalTees) { CCharacter* pChr = p->GetCharacter(); @@ -1915,6 +1934,7 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/) //world = new GAMEWORLD; //players = new CPlayer[MAX_CLIENTS]; + //TODO: No need any more? char buf[512]; str_format(buf, sizeof(buf), "data/maps/%s.cfg", g_Config.m_SvMap); // Console()->ExecuteFile(buf); diff --git a/src/game/server/gamecontroller.cpp b/src/game/server/gamecontroller.cpp index c7a47ee68..772057c8c 100644 --- a/src/game/server/gamecontroller.cpp +++ b/src/game/server/gamecontroller.cpp @@ -2,12 +2,14 @@ #include #include + #include -#include "entities/pickup.h" + #include "gamecontroller.h" #include "gamecontext.h" +#include "entities/pickup.h" #include "entities/light.h" #include "entities/dragger.h" #include "entities/gun.h" @@ -44,10 +46,12 @@ IGameController::IGameController(class CGameContext *pGameServer) m_aNumSpawnPoints[2] = 0; m_CurrentRecord = 0; + } IGameController::~IGameController() { + } float IGameController::EvaluateSpawnPos(CSpawnEval *pEval, vec2 Pos) diff --git a/src/game/server/gamecontroller.h b/src/game/server/gamecontroller.h index 4d77794d2..40915d1ac 100644 --- a/src/game/server/gamecontroller.h +++ b/src/game/server/gamecontroller.h @@ -2,9 +2,9 @@ #define GAME_SERVER_GAMECONTROLLER_H #include +//#include class CDoor; - /* Class: Game Controller Controls the main game logic. Keeping track of team and player score, @@ -71,6 +71,8 @@ public: IGameController(class CGameContext *pGameServer); virtual ~IGameController(); + + //void DoTeamScoreWincheck(); //void DoPlayerScoreWincheck(); void DoWarmup(int Seconds); diff --git a/src/game/server/gamemodes/DDRace.cpp b/src/game/server/gamemodes/DDRace.cpp index 654af5aa9..02e38f69f 100644 --- a/src/game/server/gamemodes/DDRace.cpp +++ b/src/game/server/gamemodes/DDRace.cpp @@ -6,7 +6,7 @@ #include #include "DDRace.h" -CGameControllerDDRace::CGameControllerDDRace(class CGameContext *pGameServer) : IGameController(pGameServer) +CGameControllerDDRace::CGameControllerDDRace(class CGameContext *pGameServer) : IGameController(pGameServer), m_Teams(pGameServer) { m_pGameType = "DDRace"; diff --git a/src/game/server/gamemodes/DDRace.h b/src/game/server/gamemodes/DDRace.h index 28e7fcd7e..ba772aade 100644 --- a/src/game/server/gamemodes/DDRace.h +++ b/src/game/server/gamemodes/DDRace.h @@ -3,7 +3,8 @@ #ifndef DDRACE_H #define DDRACE_H #include -#include +#include + class CGameControllerDDRace : public IGameController { @@ -12,6 +13,8 @@ public: CGameControllerDDRace(class CGameContext *pGameServer); ~CGameControllerDDRace(); + CTeams m_Teams; + vec2 *m_pTeleporter; void InitTeleporter(); diff --git a/src/game/server/teams.cpp b/src/game/server/teams.cpp index 289735548..ebdf21d57 100644 --- a/src/game/server/teams.cpp +++ b/src/game/server/teams.cpp @@ -1,7 +1,7 @@ #include "teams.h" -CTeams::CTeams(CGameContext* gameContext) : m_GameServer(gameContext) { +CTeams::CTeams(CGameContext *pGameContext) : m_pGameContext(pGameContext) { for(int i = 0; i < MAX_CLIENTS; ++i) { m_Team[i] = 0; m_TeamState[i] = EMPTY; @@ -10,16 +10,24 @@ CTeams::CTeams(CGameContext* gameContext) : m_GameServer(gameContext) { } void CTeams::OnCharacterStart(int id) { - if(m_TeamState[m_Team[id]] <= CLOSED) { - ChangeTeamState(m_Team[id], STARTED); - int Tick = Server()->Tick(); - for(int i = 0; i < MAX_CLIENTS; ++i) { - if(m_Team[i] == m_Team[id]) { - CCharacter* Char = getCharacter(i); + int Tick = Server()->Tick(); + if(m_Team[id] == 0) { + CCharacter* Char = Character(id); + Char->m_RaceState = RACE_STARTED; + Char->m_StartTime = Tick; + Char->m_RefreshTime = Tick; + } else { + if(m_TeamState[m_Team[id]] <= CLOSED) { + ChangeTeamState(m_Team[id], STARTED); + + for(int i = 0; i < MAX_CLIENTS; ++i) { + if(m_Team[i] == m_Team[id]) { + CCharacter* Char = Character(i); - Char->m_RaceState = RACE_STARTED; - Char->m_StartTime = Tick; - Char->m_RefreshTime = Tick; + Char->m_RaceState = RACE_STARTED; + Char->m_StartTime = Tick; + Char->m_RefreshTime = Tick; + } } } } @@ -27,11 +35,23 @@ void CTeams::OnCharacterStart(int id) { void CTeams::OnCharacterFinish(int id) { if(m_Team[id] == 0) { - //TODO: as simple DDRace results + Character(id)->OnFinish(); } else { m_TeeFinished[id] = true; if(TeamFinished(m_Team[id])) { - //TODO: as simple DDRace results + ChangeTeamState(m_Team[id], FINISHED);//TODO: Make it better + for(int i = 0; i < MAX_CLIENTS; ++i) { + if(SameTeam(i, id)) { + CCharacter * Char = Character(i); + if(Char != 0) { + Char->OnFinish(); + m_TeeFinished[i] = false; + } //else { + // m_Team[id] = 0; //i saw zomby =) + //} + } + } + } } } @@ -44,8 +64,17 @@ bool CTeams::SetCharacterTeam(int id, int Team) { if(m_TeamState[Team] >= CLOSED) { return false; } - if(getCharacter(id)->m_RaceState != RACE_NONE) { - return false; + if(m_Team[id] != 0 && m_TeamState[m_Team[id]] != EMPTY) { + bool NoOneInOldTeam = true; + for(int i = 0; i < MAX_CLIENTS; ++i) { + if(SameTeam(i, id)) { + NoOneInOldTeam = false;//all good exists someone in old team + break; + } + } + if(NoOneInOldTeam) { + m_TeamState[m_Team[id]] = EMPTY; + } } m_Team[id] = Team; if(m_TeamState[Team] == EMPTY) { @@ -69,3 +98,6 @@ bool CTeams::TeamFinished(int Team) { return true; } +bool CTeams::SameTeam(int Cid1, int Cid2) { + return m_Team[Cid1] = m_Team[Cid2]; +} diff --git a/src/game/server/teams.h b/src/game/server/teams.h index 92df50fa4..e3d0f86dd 100644 --- a/src/game/server/teams.h +++ b/src/game/server/teams.h @@ -1,17 +1,15 @@ #ifndef GAME_SERVER_TEAMS_H #define GAME_SERVER_TEAMS_H - #include -#include #include - class CTeams { int m_Team[MAX_CLIENTS]; int m_TeamState[MAX_CLIENTS]; bool m_TeeFinished[MAX_CLIENTS]; - CGameContext* m_GameServer; + + class CGameContext * m_pGameContext; public: enum { @@ -22,12 +20,13 @@ public: FINISHED }; - CTeams(CGameContext* gameContext); + CTeams(CGameContext *pGameContext); //helper methods - CCharacter* getCharacter(int id) { return GameServer()->GetPlayerChar(id); } - CGameContext* GameServer() { return m_GameServer; } - IServer* Server() { return m_GameServer->Server(); } + CCharacter* Character(int id) { return GameServer()->GetPlayerChar(id); } + + class CGameContext *GameServer() { return m_pGameContext; } + class IServer *Server() { return m_pGameContext->Server(); } void OnCharacterStart(int id); void OnCharacterFinish(int id); @@ -38,6 +37,11 @@ public: bool TeamFinished(int Team); + bool SameTeam(int Cid1, int Cid2); + + int GetTeam(int Cid) { + return m_Team[Cid]; + } }; #endif \ No newline at end of file