Skip to content

Commit

Permalink
Sync mission time over network (fixes #405)
Browse files Browse the repository at this point in the history
Version 0.6.4
  • Loading branch information
cxong committed Mar 4, 2017
1 parent 83c103b commit 0931cb7
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ project(cdogs-sdl C)

SET(VERSION_MAJOR "0")
SET(VERSION_MINOR "6")
SET(VERSION_PATCH "3")
SET(VERSION_PATCH "4")
SET(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
# this must be 4 numbers
SET(VERSION_RC "${VERSION_MAJOR},${VERSION_MINOR},${VERSION_PATCH},0")
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/game_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2016, Cong Xu
Copyright (c) 2013-2017, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -72,7 +72,7 @@ static GameEventEntry sGameEventEntries[] =
{ GAME_EVENT_SET_MESSAGE, false, false, true, true, NULL },

{ GAME_EVENT_GAME_START, true, false, true, true, NULL },
{ GAME_EVENT_GAME_BEGIN, true, false, true, true, NULL },
{ GAME_EVENT_GAME_BEGIN, true, false, true, true, NGameBegin_fields },

{ GAME_EVENT_ACTOR_ADD, true, false, true, true, NActorAdd_fields },
{ GAME_EVENT_ACTOR_MOVE, true, true, true, true, NActorMove_fields },
Expand Down
5 changes: 4 additions & 1 deletion src/cdogs/game_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2013-2016, Cong Xu
Copyright (c) 2013-2017, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -112,7 +112,9 @@ typedef enum
typedef struct
{
GameEventType Type;
// Whether this message is sent by server to clients
bool Broadcast;
// Whether clients send this message to the server
bool Submit;
// Whether to simply enqueue as game event; otherwise processed by handler
bool Enqueue;
Expand Down Expand Up @@ -143,6 +145,7 @@ typedef struct
char Message[256];
int Ticks;
} SetMessage;
NGameBegin GameBegin;
NActorAdd ActorAdd;
NActorMove ActorMove;
NActorState ActorState;
Expand Down
4 changes: 2 additions & 2 deletions src/cdogs/handle_game_events.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2014-2016, Cong Xu
Copyright (c) 2014-2017, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -179,7 +179,7 @@ static void HandleGameEvent(
gMission.HasBegun = false;
break;
case GAME_EVENT_GAME_BEGIN:
MissionBegin(&gMission);
MissionBegin(&gMission, e.u.GameBegin);
break;
case GAME_EVENT_ACTOR_ADD:
ActorAdd(e.u.ActorAdd);
Expand Down
6 changes: 3 additions & 3 deletions src/cdogs/mission.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2016, Cong Xu
Copyright (c) 2013-2017, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -363,7 +363,7 @@ bool MissionCanBegin(void)
return GetNumPlayers(PLAYER_ALIVE_OR_DYING, false, false) > 0;
}

void MissionBegin(struct MissionOptions *m)
void MissionBegin(struct MissionOptions *m, const NGameBegin gb)
{
m->HasBegun = true;
m->state = MISSION_STATE_PLAY;
Expand All @@ -378,7 +378,7 @@ void MissionBegin(struct MissionOptions *m)
e.u.SetMessage.Ticks = FPS_FRAMELIMIT * 2;
GameEventsEnqueue(&gGameEvents, e);
}
m->time = 0;
m->time = gb.MissionTime;
m->pickupTime = 0;
}

Expand Down
5 changes: 3 additions & 2 deletions src/cdogs/mission.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2016, Cong Xu
Copyright (c) 2013-2017, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -52,6 +52,7 @@

#include "config.h"
#include "objective.h"
#include "proto/msg.pb.h"
#include "sys_config.h"

#define ObjectiveFromTileItem(f) ((((f) & TILEITEM_OBJECTIVE) >> OBJECTIVE_SHIFT)-1)
Expand Down Expand Up @@ -239,7 +240,7 @@ void UpdateMissionObjective(
const struct MissionOptions *options,
const int flags, const ObjectiveType type, const int count);
bool MissionCanBegin(void);
void MissionBegin(struct MissionOptions *m);
void MissionBegin(struct MissionOptions *m, const NGameBegin gb);
bool CanCompleteMission(const struct MissionOptions *options);
bool MissionAllObjectivesComplete(const struct MissionOptions *mo);
bool IsMissionComplete(const struct MissionOptions *options);
Expand Down
6 changes: 4 additions & 2 deletions src/cdogs/net_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
C-Dogs SDL
A port of the legendary (and fun) action/arcade cdogs.
Copyright (c) 2014-2016, Cong Xu
Copyright (c) 2014-2017, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -331,7 +331,9 @@ static void OnReceive(NetServer *n, ENetEvent event)
}
if (gMission.HasBegun)
{
NetServerSendMsg(n, peerId, GAME_EVENT_GAME_BEGIN, NULL);
NGameBegin gb = NGameBegin_init_default;
gb.MissionTime = gMission.time;
NetServerSendMsg(n, peerId, GAME_EVENT_GAME_BEGIN, &gb);
}

NetServerFlush(n);
Expand Down
7 changes: 5 additions & 2 deletions src/cdogs/pic_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,11 @@ void PicManagerLoadDir(
tinydir_dir dir;
if (tinydir_open(&dir, path) == -1)
{
LOG(LM_MAIN, LL_ERROR, "Error opening image dir '%s': %s",
path, strerror(errno));
if (errno != ENOENT)
{
LOG(LM_MAIN, LL_ERROR, "Error opening image dir '%s': %s",
path, strerror(errno));
}
goto bail;
}

Expand Down
9 changes: 7 additions & 2 deletions src/cdogs/proto/msg.pb.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Automatically generated nanopb constant definitions */
/* Generated by nanopb-0.3.0 at Thu May 19 11:29:41 2016. */
/* Generated by nanopb-0.3.0 at Sat Mar 04 22:48:22 2017. */

#include "msg.pb.h"

Expand Down Expand Up @@ -149,6 +149,11 @@ const pb_field_t NVec2i_fields[3] = {
PB_LAST_FIELD
};

const pb_field_t NGameBegin_fields[2] = {
PB_FIELD( 1, INT32 , REQUIRED, STATIC , FIRST, NGameBegin, MissionTime, MissionTime, 0),
PB_LAST_FIELD
};

const pb_field_t NActorAdd_fields[8] = {
PB_FIELD( 1, UINT32 , REQUIRED, STATIC , FIRST, NActorAdd, UID, UID, 0),
PB_FIELD( 2, UINT32 , REQUIRED, STATIC , OTHER, NActorAdd, CharId, UID, 0),
Expand Down Expand Up @@ -388,7 +393,7 @@ const pb_field_t NMissionEnd_fields[4] = {
* numbers or field sizes that are larger than what can fit in 8 or 16 bit
* field descriptors.
*/
PB_STATIC_ASSERT((pb_membersize(NCharColors, Skin) < 65536 && pb_membersize(NCharColors, Arms) < 65536 && pb_membersize(NCharColors, Body) < 65536 && pb_membersize(NCharColors, Legs) < 65536 && pb_membersize(NCharColors, Hair) < 65536 && pb_membersize(NPlayerData, Colors) < 65536 && pb_membersize(NPlayerData, Stats) < 65536 && pb_membersize(NPlayerData, Totals) < 65536 && pb_membersize(NTileSet, Pos) < 65536 && pb_membersize(NMapObjectAdd, Pos) < 65536 && pb_membersize(NSound, Pos) < 65536 && pb_membersize(NActorAdd, FullPos) < 65536 && pb_membersize(NActorMove, Pos) < 65536 && pb_membersize(NActorMove, MoveVel) < 65536 && pb_membersize(NActorSlide, Vel) < 65536 && pb_membersize(NActorImpulse, Vel) < 65536 && pb_membersize(NActorImpulse, Pos) < 65536 && pb_membersize(NActorHit, Vel) < 65536 && pb_membersize(NAddPickup, Pos) < 65536 && pb_membersize(NBulletBounce, BouncePos) < 65536 && pb_membersize(NBulletBounce, BounceVel) < 65536 && pb_membersize(NGunReload, FullPos) < 65536 && pb_membersize(NGunFire, MuzzleFullPos) < 65536 && pb_membersize(NAddBullet, MuzzlePos) < 65536 && pb_membersize(NTrigger, Tile) < 65536 && pb_membersize(NExploreTiles, Runs[0]) < 65536 && pb_membersize(NExploreTiles_Run, Tile) < 65536 && pb_membersize(NAddKeys, Pos) < 65536 && pb_membersize(NMissionComplete, ExitStart) < 65536 && pb_membersize(NMissionComplete, ExitEnd) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_NServerInfo_NClientId_NCampaignDef_NColor_NCharColors_NPlayerStats_NPlayerData_NPlayerRemove_NConfig_NTileSet_NMapObjectAdd_NMapObjectDamage_NMapObjectRemove_NScore_NSound_NVec2i_NActorAdd_NActorMove_NActorState_NActorDir_NActorSlide_NActorImpulse_NActorSwitchGun_NActorPickupAll_NActorReplaceGun_NActorHeal_NActorHit_NActorAddAmmo_NActorUseAmmo_NActorDie_NActorMelee_NAddPickup_NRemovePickup_NBulletBounce_NRemoveBullet_NGunReload_NGunFire_NGunState_NAddBullet_NTrigger_NExploreTiles_NExploreTiles_Run_NRescueCharacter_NObjectiveUpdate_NAddKeys_NMissionComplete_NMissionEnd)
PB_STATIC_ASSERT((pb_membersize(NCharColors, Skin) < 65536 && pb_membersize(NCharColors, Arms) < 65536 && pb_membersize(NCharColors, Body) < 65536 && pb_membersize(NCharColors, Legs) < 65536 && pb_membersize(NCharColors, Hair) < 65536 && pb_membersize(NPlayerData, Colors) < 65536 && pb_membersize(NPlayerData, Stats) < 65536 && pb_membersize(NPlayerData, Totals) < 65536 && pb_membersize(NTileSet, Pos) < 65536 && pb_membersize(NMapObjectAdd, Pos) < 65536 && pb_membersize(NSound, Pos) < 65536 && pb_membersize(NActorAdd, FullPos) < 65536 && pb_membersize(NActorMove, Pos) < 65536 && pb_membersize(NActorMove, MoveVel) < 65536 && pb_membersize(NActorSlide, Vel) < 65536 && pb_membersize(NActorImpulse, Vel) < 65536 && pb_membersize(NActorImpulse, Pos) < 65536 && pb_membersize(NActorHit, Vel) < 65536 && pb_membersize(NAddPickup, Pos) < 65536 && pb_membersize(NBulletBounce, BouncePos) < 65536 && pb_membersize(NBulletBounce, BounceVel) < 65536 && pb_membersize(NGunReload, FullPos) < 65536 && pb_membersize(NGunFire, MuzzleFullPos) < 65536 && pb_membersize(NAddBullet, MuzzlePos) < 65536 && pb_membersize(NTrigger, Tile) < 65536 && pb_membersize(NExploreTiles, Runs[0]) < 65536 && pb_membersize(NExploreTiles_Run, Tile) < 65536 && pb_membersize(NAddKeys, Pos) < 65536 && pb_membersize(NMissionComplete, ExitStart) < 65536 && pb_membersize(NMissionComplete, ExitEnd) < 65536), YOU_MUST_DEFINE_PB_FIELD_32BIT_FOR_MESSAGES_NServerInfo_NClientId_NCampaignDef_NColor_NCharColors_NPlayerStats_NPlayerData_NPlayerRemove_NConfig_NTileSet_NMapObjectAdd_NMapObjectDamage_NMapObjectRemove_NScore_NSound_NVec2i_NGameBegin_NActorAdd_NActorMove_NActorState_NActorDir_NActorSlide_NActorImpulse_NActorSwitchGun_NActorPickupAll_NActorReplaceGun_NActorHeal_NActorHit_NActorAddAmmo_NActorUseAmmo_NActorDie_NActorMelee_NAddPickup_NRemovePickup_NBulletBounce_NRemoveBullet_NGunReload_NGunFire_NGunState_NAddBullet_NTrigger_NExploreTiles_NExploreTiles_Run_NRescueCharacter_NObjectiveUpdate_NAddKeys_NMissionComplete_NMissionEnd)
#endif

#if !defined(PB_FIELD_16BIT) && !defined(PB_FIELD_32BIT)
Expand Down
11 changes: 10 additions & 1 deletion src/cdogs/proto/msg.pb.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Automatically generated nanopb header */
/* Generated by nanopb-0.3.0 at Thu May 19 11:29:41 2016. */
/* Generated by nanopb-0.3.0 at Sat Mar 04 22:48:22 2017. */

#ifndef PB_MSG_PB_H_INCLUDED
#define PB_MSG_PB_H_INCLUDED
Expand Down Expand Up @@ -95,6 +95,10 @@ typedef struct _NConfig {
char Value[128];
} NConfig;

typedef struct _NGameBegin {
int32_t MissionTime;
} NGameBegin;

typedef struct _NGunState {
uint32_t ActorUID;
int32_t State;
Expand Down Expand Up @@ -358,6 +362,7 @@ extern const int32_t NAddBullet_ActorUID_default;
#define NScore_init_default {0, 0}
#define NSound_init_default {"", NVec2i_init_default, 0}
#define NVec2i_init_default {0, 0}
#define NGameBegin_init_default {0}
#define NActorAdd_init_default {0, 0, 4, 0, -1, 0, NVec2i_init_default}
#define NActorMove_init_default {0, NVec2i_init_default, NVec2i_init_default}
#define NActorState_init_default {0, 0}
Expand Down Expand Up @@ -405,6 +410,7 @@ extern const int32_t NAddBullet_ActorUID_default;
#define NScore_init_zero {0, 0}
#define NSound_init_zero {"", NVec2i_init_zero, 0}
#define NVec2i_init_zero {0, 0}
#define NGameBegin_init_zero {0}
#define NActorAdd_init_zero {0, 0, 0, 0, 0, 0, NVec2i_init_zero}
#define NActorMove_init_zero {0, NVec2i_init_zero, NVec2i_init_zero}
#define NActorState_init_zero {0, 0}
Expand Down Expand Up @@ -476,6 +482,7 @@ extern const int32_t NAddBullet_ActorUID_default;
#define NColor_RGBA_tag 1
#define NConfig_Name_tag 1
#define NConfig_Value_tag 2
#define NGameBegin_MissionTime_tag 1
#define NGunState_ActorUID_tag 1
#define NGunState_State_tag 2
#define NMapObjectDamage_UID_tag 1
Expand Down Expand Up @@ -623,6 +630,7 @@ extern const pb_field_t NMapObjectRemove_fields[5];
extern const pb_field_t NScore_fields[3];
extern const pb_field_t NSound_fields[4];
extern const pb_field_t NVec2i_fields[3];
extern const pb_field_t NGameBegin_fields[2];
extern const pb_field_t NActorAdd_fields[8];
extern const pb_field_t NActorMove_fields[4];
extern const pb_field_t NActorState_fields[3];
Expand Down Expand Up @@ -672,6 +680,7 @@ extern const pb_field_t NMissionEnd_fields[4];
#define NScore_size 17
#define NSound_size 157
#define NVec2i_size 22
#define NGameBegin_size 11
#define NActorAdd_size 75
#define NActorMove_size 54
#define NActorState_size 17
Expand Down
4 changes: 4 additions & 0 deletions src/cdogs/proto/msg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ message NVec2i {
required int32 y = 2;
}

message NGameBegin {
required int32 MissionTime = 1;
}

message NActorAdd {
required uint32 UID = 1;
required uint32 CharId = 2;
Expand Down
6 changes: 5 additions & 1 deletion src/cdogs/sounds.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ void SoundLoadDir(map_t sounds, const char *path, const char *prefix)
tinydir_dir dir;
if (tinydir_open(&dir, path) == -1)
{
LOG(LM_MAIN, LL_ERROR, "Cannot open sound dir '%s'", path);
if (errno != ENOENT)
{
LOG(LM_MAIN, LL_ERROR, "Cannot open sound dir '%s': %s",
path, strerror(errno));
}
goto bail;
}
for (; dir.has_next; tinydir_next(&dir))
Expand Down
3 changes: 2 additions & 1 deletion src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
This file incorporates work covered by the following copyright and
permission notice:
Copyright (c) 2013-2016, Cong Xu
Copyright (c) 2013-2017, Cong Xu
All rights reserved.
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -441,6 +441,7 @@ static GameLoopResult RunGameUpdate(void *data)
if (!rData->m->HasBegun && MissionCanBegin())
{
GameEvent begin = GameEventNew(GAME_EVENT_GAME_BEGIN);
begin.u.GameBegin.MissionTime = gMission.time;
GameEventsEnqueue(&gGameEvents, begin);
}

Expand Down

0 comments on commit 0931cb7

Please sign in to comment.