Logic and scores seems done. Next will be collision

This commit is contained in:
btd 2010-08-29 00:53:42 +04:00
parent fa6a79b309
commit 366e175983
9 changed files with 182 additions and 105 deletions

View file

@ -551,8 +551,89 @@ void CCharacter::OnDirectInput(CNetObj_PlayerInput *pNewInput)
mem_copy(&m_LatestPrevInput, &m_LatestInput, sizeof(m_LatestInput)); 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() void CCharacter::Tick()
{ {
CGameControllerDDRace* Controller = (CGameControllerDDRace*)GameServer()->m_pController;
int MapIndex = GameServer()->Collision()->GetMapIndex(m_PrevPos, m_Pos); int MapIndex = GameServer()->Collision()->GetMapIndex(m_PrevPos, m_Pos);
int TileIndex1 = GameServer()->Collision()->GetCollisionDDRace(MapIndex); int TileIndex1 = GameServer()->Collision()->GetCollisionDDRace(MapIndex);
int TileIndex2 = GameServer()->Collision()->GetFCollisionDDRace(MapIndex); int TileIndex2 = GameServer()->Collision()->GetFCollisionDDRace(MapIndex);
@ -560,7 +641,7 @@ void CCharacter::Tick()
if(m_pPlayer->m_ForceBalanced) if(m_pPlayer->m_ForceBalanced)
{ {
char Buf[128]; 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()); GameServer()->SendBroadcast(Buf, m_pPlayer->GetCID());
m_pPlayer->m_ForceBalanced = false; m_pPlayer->m_ForceBalanced = false;
@ -673,87 +754,14 @@ void CCharacter::Tick()
m_CpCurrent[cp] = time; m_CpCurrent[cp] = time;
m_CpTick = Server()->Tick() + Server()->TickSpeed()*2; 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(); Controller->m_Teams.OnCharacterStart(m_pPlayer->GetCID());
m_StartTime = Server()->Tick();
m_RefreshTime = Server()->Tick();
m_RaceState = RACE_STARTED;
} }
if(((TileIndex1 == TILE_END) || (TileIndex2 == TILE_END)) && m_RaceState == RACE_STARTED) if(((TileIndex1 == TILE_END) || (TileIndex2 == TILE_END)) && m_RaceState == RACE_STARTED)
{ {
//TODO: CTeams::OnCharacterFinish() Controller->m_Teams.OnCharacterFinish(m_pPlayer->GetCID());
//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;
} }
if(((TileIndex1 == TILE_FREEZE) || (TileIndex2 == TILE_FREEZE)) && !m_Super) 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) void CCharacter::Die(int Killer, int Weapon)
{ {
int ModeSpecial = GameServer()->m_pController->OnCharacterDeath(this, GameServer()->m_apPlayers[Killer], Weapon); int ModeSpecial = GameServer()->m_pController->OnCharacterDeath(this, GameServer()->m_apPlayers[Killer], Weapon);
CGameControllerDDRace* Controller = (CGameControllerDDRace*)GameServer()->m_pController;
char aBuf[256]; char aBuf[256];
str_format(aBuf, sizeof(aBuf), "kill killer='%d:%s' victim='%d:%s' weapon=%d special=%d", str_format(aBuf, sizeof(aBuf), "kill killer='%d:%s' victim='%d:%s' weapon=%d special=%d",
Killer, Server()->ClientName(Killer), Killer, Server()->ClientName(Killer),
m_pPlayer->GetCID(), Server()->ClientName(m_pPlayer->GetCID()), Weapon, ModeSpecial); m_pPlayer->GetCID(), Server()->ClientName(m_pPlayer->GetCID()), Weapon, ModeSpecial);
GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf); GameServer()->Console()->Print(IConsole::OUTPUT_LEVEL_DEBUG, "game", aBuf);
Controller->m_Teams.SetCharacterTeam(m_pPlayer->GetCID(), 0);
// send the kill message // send the kill message
CNetMsg_Sv_KillMsg Msg; CNetMsg_Sv_KillMsg Msg;
Msg.m_Killer = Killer; Msg.m_Killer = Killer;

View file

@ -4,7 +4,6 @@
#include <game/server/entity.h> #include <game/server/entity.h>
#include <game/generated/server_data.h> #include <game/generated/server_data.h>
#include <game/generated/protocol.h> #include <game/generated/protocol.h>
#include <game/gamecore.h> #include <game/gamecore.h>
enum enum
@ -92,6 +91,9 @@ public:
int m_RaceState; int m_RaceState;
void OnFinish();
int Team();
struct struct
{ {
vec2 m_ActivationDir; vec2 m_ActivationDir;

View file

@ -748,6 +748,25 @@ void CGameContext::OnMessage(int MsgId, CUnpacker *pUnpacker, int ClientId)
pChr->m_BroadTime=true; 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) else if (!str_comp_nocase(pMsg->m_pMessage, "/broadcast")&&g_Config.m_SvEmotionalTees)
{ {
CCharacter* pChr = p->GetCharacter(); CCharacter* pChr = p->GetCharacter();
@ -1915,6 +1934,7 @@ void CGameContext::OnInit(/*class IKernel *pKernel*/)
//world = new GAMEWORLD; //world = new GAMEWORLD;
//players = new CPlayer[MAX_CLIENTS]; //players = new CPlayer[MAX_CLIENTS];
//TODO: No need any more?
char buf[512]; char buf[512];
str_format(buf, sizeof(buf), "data/maps/%s.cfg", g_Config.m_SvMap); // str_format(buf, sizeof(buf), "data/maps/%s.cfg", g_Config.m_SvMap); //
Console()->ExecuteFile(buf); Console()->ExecuteFile(buf);

View file

@ -2,12 +2,14 @@
#include <engine/shared/config.h> #include <engine/shared/config.h>
#include <game/mapitems.h> #include <game/mapitems.h>
#include <game/generated/protocol.h> #include <game/generated/protocol.h>
#include "entities/pickup.h"
#include "gamecontroller.h" #include "gamecontroller.h"
#include "gamecontext.h" #include "gamecontext.h"
#include "entities/pickup.h"
#include "entities/light.h" #include "entities/light.h"
#include "entities/dragger.h" #include "entities/dragger.h"
#include "entities/gun.h" #include "entities/gun.h"
@ -44,10 +46,12 @@ IGameController::IGameController(class CGameContext *pGameServer)
m_aNumSpawnPoints[2] = 0; m_aNumSpawnPoints[2] = 0;
m_CurrentRecord = 0; m_CurrentRecord = 0;
} }
IGameController::~IGameController() IGameController::~IGameController()
{ {
} }
float IGameController::EvaluateSpawnPos(CSpawnEval *pEval, vec2 Pos) float IGameController::EvaluateSpawnPos(CSpawnEval *pEval, vec2 Pos)

View file

@ -2,9 +2,9 @@
#define GAME_SERVER_GAMECONTROLLER_H #define GAME_SERVER_GAMECONTROLLER_H
#include <base/vmath.h> #include <base/vmath.h>
//#include <game/server/teams.h>
class CDoor; class CDoor;
/* /*
Class: Game Controller Class: Game Controller
Controls the main game logic. Keeping track of team and player score, Controls the main game logic. Keeping track of team and player score,
@ -71,6 +71,8 @@ public:
IGameController(class CGameContext *pGameServer); IGameController(class CGameContext *pGameServer);
virtual ~IGameController(); virtual ~IGameController();
//void DoTeamScoreWincheck(); //void DoTeamScoreWincheck();
//void DoPlayerScoreWincheck(); //void DoPlayerScoreWincheck();
void DoWarmup(int Seconds); void DoWarmup(int Seconds);

View file

@ -6,7 +6,7 @@
#include <game/server/gamecontext.h> #include <game/server/gamecontext.h>
#include "DDRace.h" #include "DDRace.h"
CGameControllerDDRace::CGameControllerDDRace(class CGameContext *pGameServer) : IGameController(pGameServer) CGameControllerDDRace::CGameControllerDDRace(class CGameContext *pGameServer) : IGameController(pGameServer), m_Teams(pGameServer)
{ {
m_pGameType = "DDRace"; m_pGameType = "DDRace";

View file

@ -3,7 +3,8 @@
#ifndef DDRACE_H #ifndef DDRACE_H
#define DDRACE_H #define DDRACE_H
#include <game/server/gamecontroller.h> #include <game/server/gamecontroller.h>
#include <game/server/score.h> #include <game/server/teams.h>
class CGameControllerDDRace : public IGameController class CGameControllerDDRace : public IGameController
{ {
@ -12,6 +13,8 @@ public:
CGameControllerDDRace(class CGameContext *pGameServer); CGameControllerDDRace(class CGameContext *pGameServer);
~CGameControllerDDRace(); ~CGameControllerDDRace();
CTeams m_Teams;
vec2 *m_pTeleporter; vec2 *m_pTeleporter;
void InitTeleporter(); void InitTeleporter();

View file

@ -1,7 +1,7 @@
#include "teams.h" #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) { for(int i = 0; i < MAX_CLIENTS; ++i) {
m_Team[i] = 0; m_Team[i] = 0;
m_TeamState[i] = EMPTY; m_TeamState[i] = EMPTY;
@ -10,16 +10,24 @@ CTeams::CTeams(CGameContext* gameContext) : m_GameServer(gameContext) {
} }
void CTeams::OnCharacterStart(int id) { void CTeams::OnCharacterStart(int id) {
if(m_TeamState[m_Team[id]] <= CLOSED) { int Tick = Server()->Tick();
ChangeTeamState(m_Team[id], STARTED); if(m_Team[id] == 0) {
int Tick = Server()->Tick(); CCharacter* Char = Character(id);
for(int i = 0; i < MAX_CLIENTS; ++i) { Char->m_RaceState = RACE_STARTED;
if(m_Team[i] == m_Team[id]) { Char->m_StartTime = Tick;
CCharacter* Char = getCharacter(i); Char->m_RefreshTime = Tick;
} else {
if(m_TeamState[m_Team[id]] <= CLOSED) {
ChangeTeamState(m_Team[id], STARTED);
Char->m_RaceState = RACE_STARTED; for(int i = 0; i < MAX_CLIENTS; ++i) {
Char->m_StartTime = Tick; if(m_Team[i] == m_Team[id]) {
Char->m_RefreshTime = Tick; CCharacter* Char = Character(i);
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) { void CTeams::OnCharacterFinish(int id) {
if(m_Team[id] == 0) { if(m_Team[id] == 0) {
//TODO: as simple DDRace results Character(id)->OnFinish();
} else { } else {
m_TeeFinished[id] = true; m_TeeFinished[id] = true;
if(TeamFinished(m_Team[id])) { 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) { if(m_TeamState[Team] >= CLOSED) {
return false; return false;
} }
if(getCharacter(id)->m_RaceState != RACE_NONE) { if(m_Team[id] != 0 && m_TeamState[m_Team[id]] != EMPTY) {
return false; 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; m_Team[id] = Team;
if(m_TeamState[Team] == EMPTY) { if(m_TeamState[Team] == EMPTY) {
@ -69,3 +98,6 @@ bool CTeams::TeamFinished(int Team) {
return true; return true;
} }
bool CTeams::SameTeam(int Cid1, int Cid2) {
return m_Team[Cid1] = m_Team[Cid2];
}

View file

@ -1,17 +1,15 @@
#ifndef GAME_SERVER_TEAMS_H #ifndef GAME_SERVER_TEAMS_H
#define GAME_SERVER_TEAMS_H #define GAME_SERVER_TEAMS_H
#include <game/server/entities/character.h> #include <game/server/entities/character.h>
#include <engine/server/server.h>
#include <game/server/gamecontext.h> #include <game/server/gamecontext.h>
class CTeams { class CTeams {
int m_Team[MAX_CLIENTS]; int m_Team[MAX_CLIENTS];
int m_TeamState[MAX_CLIENTS]; int m_TeamState[MAX_CLIENTS];
bool m_TeeFinished[MAX_CLIENTS]; bool m_TeeFinished[MAX_CLIENTS];
CGameContext* m_GameServer;
class CGameContext * m_pGameContext;
public: public:
enum { enum {
@ -22,12 +20,13 @@ public:
FINISHED FINISHED
}; };
CTeams(CGameContext* gameContext); CTeams(CGameContext *pGameContext);
//helper methods //helper methods
CCharacter* getCharacter(int id) { return GameServer()->GetPlayerChar(id); } CCharacter* Character(int id) { return GameServer()->GetPlayerChar(id); }
CGameContext* GameServer() { return m_GameServer; }
IServer* Server() { return m_GameServer->Server(); } class CGameContext *GameServer() { return m_pGameContext; }
class IServer *Server() { return m_pGameContext->Server(); }
void OnCharacterStart(int id); void OnCharacterStart(int id);
void OnCharacterFinish(int id); void OnCharacterFinish(int id);
@ -38,6 +37,11 @@ public:
bool TeamFinished(int Team); bool TeamFinished(int Team);
bool SameTeam(int Cid1, int Cid2);
int GetTeam(int Cid) {
return m_Team[Cid];
}
}; };
#endif #endif