Skip to content

Commit

Permalink
Merge pull request #535 from Wargus/clean-up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
Jarod42 authored Oct 10, 2023
2 parents dc4c915 + 1f14cbc commit b0a29be
Show file tree
Hide file tree
Showing 17 changed files with 2,312 additions and 2,415 deletions.
6 changes: 2 additions & 4 deletions src/game/replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class FullReplay
bool CommandLogDisabled; /// True if command log is off
ReplayType ReplayGameType; /// Replay game type
static bool DisabledLog; /// Disabled log for replay
static CFile *LogFile; /// Replay log file
static std::unique_ptr<CFile> LogFile; /// Replay log file
static fs::path LastLogFileName; /// Last log file name
static unsigned long NextLogCycle; /// Next log cycle number
static bool InitReplay; /// Initialize replay
Expand Down Expand Up @@ -349,11 +349,10 @@ void CommandLog(const char *action,
path /= "log_of_stratagus_" + std::to_string(ThisPlayer->Index) + "_"
+ std::to_string((intmax_t) now) + ".log";

LogFile = new CFile;
LogFile = std::make_unique<CFile>();
if (LogFile->open(path.string().c_str(), CL_OPEN_WRITE) == -1) {
// don't retry for each command
CommandLogDisabled = false;
delete LogFile;
LogFile = nullptr;
return;
}
Expand Down Expand Up @@ -611,7 +610,6 @@ void EndReplayLog()
{
if (LogFile) {
LogFile->close();
delete LogFile;
LogFile = nullptr;
}
CurrentReplay = nullptr;
Expand Down
26 changes: 13 additions & 13 deletions src/include/network/netsockets.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#ifndef NETSOCKETS_H
#define NETSOCKETS_H

#include <memory>
#include <string>

//@{
Expand Down Expand Up @@ -75,27 +76,26 @@ class CUDPSocket
{
friend class CUDPSocket;
public:
CStatistic();
void clear();
CStatistic() = default;
public:
unsigned int sentPacketsCount;
unsigned int receivedPacketsCount;
unsigned int sentBytesCount;
unsigned int receivedBytesCount;
unsigned int receivedErrorCount;
unsigned int receivedBytesExpectedCount;
unsigned int biggestSentPacketSize;
unsigned int biggestReceivedPacketSize;
unsigned int sentPacketsCount = 0;
unsigned int receivedPacketsCount = 0;
unsigned int sentBytesCount = 0;
unsigned int receivedBytesCount = 0;
unsigned int receivedErrorCount = 0;
unsigned int receivedBytesExpectedCount = 0;
unsigned int biggestSentPacketSize = 0;
unsigned int biggestReceivedPacketSize = 0;
};

void clearStatistic() { m_statistic.clear(); }
void clearStatistic() { m_statistic = {}; }
const CStatistic &getStatistic() const { return m_statistic; }
private:
CStatistic m_statistic;
#endif

private:
CUDPSocket_Impl *m_impl;
std::unique_ptr<CUDPSocket_Impl> m_impl;
};

// Class representing TCP socket used in communication
Expand All @@ -114,7 +114,7 @@ class CTCPSocket
int HasDataToRead(int timeout);
bool IsValid() const;
private:
CTCPSocket_Impl *m_impl;
std::unique_ptr<CTCPSocket_Impl> m_impl;
};

//@}
Expand Down
35 changes: 18 additions & 17 deletions src/include/online_service.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
#ifndef __ONLINE_SERVICE_H__
#define __ONLINE_SERVICE_H__

#include <string>

#include "network/netsockets.h"

class OnlineContext {
#include <string>

class OnlineContext
{
public:
virtual ~OnlineContext() = default;

// called in the sdl event loop
virtual bool handleUDP(const unsigned char *buffer, int len, CHost host) = 0;
// called in the sdl event loop
virtual bool handleUDP(const unsigned char *buffer, int len, CHost host) = 0;

// called in the sdl event loop
virtual void doOneStep() = 0;
// called in the sdl event loop
virtual void doOneStep() = 0;

// called when joining a network game
virtual void joinGame(std::string hostPlayerName, std::string pw) = 0;
// called when joining a network game
virtual void joinGame(std::string hostPlayerName, std::string pw) = 0;

// called when leaving a network game
virtual void leaveGame() = 0;
// called when leaving a network game
virtual void leaveGame() = 0;

// called when advertised game is starting (just reports the game as in-progress)
virtual void startAdvertising(bool isStarted = false) = 0;
// called when advertised game is starting (just reports the game as in-progress)
virtual void startAdvertising(bool isStarted = false) = 0;

// called when advertised game is left by the server
virtual void stopAdvertising() = 0;
// called when advertised game is left by the server
virtual void stopAdvertising() = 0;

// called when network game ends
virtual void reportGameResult() = 0;
// called when network game ends
virtual void reportGameResult() = 0;
};

extern OnlineContext *OnlineContextHandler;
Expand Down
75 changes: 40 additions & 35 deletions src/include/particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
//@{

#include <vector>
#include <memory>

class CGraphic;
class CViewport;
Expand Down Expand Up @@ -82,14 +83,13 @@ class CParticle
{}
virtual ~CParticle() {}

virtual bool isVisible(const CViewport &vp) const = 0;
virtual CParticle *clone() = 0;
virtual void draw() = 0;
virtual bool isVisible(const CViewport &vp) const = 0;
virtual void update(int) = 0;

inline void destroy() { destroyed = true; }
inline bool isDestroyed() { return destroyed; }

virtual CParticle *clone() = 0;
void destroy() { destroyed = true; }
bool isDestroyed() { return destroyed; }

int getDrawLevel() const { return drawLevel; }
void setDrawLevel(int value) { drawLevel = value; }
Expand All @@ -105,12 +105,12 @@ class StaticParticle : public CParticle
{
public:
StaticParticle(CPosition position, GraphicAnimation *flame, int drawlevel = 0);
virtual ~StaticParticle();
~StaticParticle() override;

virtual bool isVisible(const CViewport &vp) const;
virtual void draw();
virtual void update(int ticks);
virtual CParticle *clone();
CParticle *clone() override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;

protected:
GraphicAnimation *animation;
Expand All @@ -125,12 +125,13 @@ class CChunkParticle : public CParticle
GraphicAnimation *destroyAnimation,
int minVelocity = 0, int maxVelocity = 400,
int minTrajectoryAngle = 77, int maxTTL = 0, int drawlevel = 0);
virtual ~CChunkParticle();
~CChunkParticle() override;

CParticle *clone() override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;

virtual bool isVisible(const CViewport &vp) const;
virtual void draw();
virtual void update(int ticks);
virtual CParticle *clone();
int getSmokeDrawLevel() const { return smokeDrawLevel; }
int getDestroyDrawLevel() const { return destroyDrawLevel; }
void setSmokeDrawLevel(int value) { smokeDrawLevel = value; }
Expand Down Expand Up @@ -166,12 +167,12 @@ class CSmokeParticle : public CParticle
{
public:
CSmokeParticle(CPosition position, GraphicAnimation *animation, float speedx = 0, float speedy = -22.0f, int drawlevel = 0);
virtual ~CSmokeParticle();
~CSmokeParticle() override;

virtual bool isVisible(const CViewport &vp) const;
virtual void draw();
virtual void update(int ticks);
virtual CParticle *clone();
CParticle *clone() override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;

protected:
GraphicAnimation *puff;
Expand All @@ -185,12 +186,12 @@ class CRadialParticle : public CParticle
{
public:
CRadialParticle(CPosition position, GraphicAnimation *animation, int maxSpeed, int drawlevel = 0);
virtual ~CRadialParticle();
~CRadialParticle() override;

virtual bool isVisible(const CViewport &vp) const;
virtual void draw();
virtual void update(int ticks);
virtual CParticle *clone();
CParticle *clone() override;
void draw() override;
bool isVisible(const CViewport &vp) const override;
void update(int ticks) override;

protected:
GraphicAnimation *animation;
Expand All @@ -203,8 +204,10 @@ class CRadialParticle : public CParticle
class CParticleManager
{
public:
CParticleManager();
~CParticleManager();
CParticleManager() = default;
~CParticleManager() = default;

CParticleManager(const CParticleManager &) = delete;

static void init();
static void exit();
Expand All @@ -214,20 +217,22 @@ class CParticleManager

void update();

void add(CParticle *particle);
void add(CParticle* particle); // For tolua++

void add(std::unique_ptr<CParticle> particle);
void clear();

CPosition getScreenPos(const CPosition &pos) const;

inline void setLowDetail(bool detail) { lowDetail = detail; }
inline bool getLowDetail() const { return lowDetail; }
void setLowDetail(bool detail) { lowDetail = detail; }
bool getLowDetail() const { return lowDetail; }

private:
std::vector<CParticle *> particles;
std::vector<CParticle *> new_particles;
const CViewport *vp;
unsigned long lastTicks;
bool lowDetail;
std::vector<std::unique_ptr<CParticle>> particles;
std::vector<std::unique_ptr<CParticle>> new_particles;
const CViewport *vp = nullptr;
unsigned long lastTicks = 0;
bool lowDetail = false;
};

extern CParticleManager ParticleManager;
Expand Down
2 changes: 1 addition & 1 deletion src/include/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class CUnit
unsigned ByPlayer : PlayerMax; /// Track unit seen by player
} Seen;

CVariable *Variable = nullptr; /// array of User Defined variables.
std::vector<CVariable> Variable; /// array of User Defined variables.

unsigned long TTL = 0; /// time to live

Expand Down
6 changes: 6 additions & 0 deletions src/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ namespace ranges
v.erase(std::remove(std::begin(v), std::end(v), value), std::end(v));
}

template <typename T, typename A, typename Pred>
void erase_if(std::vector<T, A> &v, Pred pred)
{
v.erase(std::remove_if(std::begin(v), std::end(v), pred), std::end(v));
}

template <typename Range>
auto min_element(Range &range)
{
Expand Down
Loading

0 comments on commit b0a29be

Please sign in to comment.