| 1 | /* |
| 2 | * LegacyClonk |
| 3 | * |
| 4 | * Copyright (c) RedWolf Design |
| 5 | * Copyright (c) 2017-2020, The LegacyClonk Team and contributors |
| 6 | * |
| 7 | * Distributed under the terms of the ISC license; see accompanying file |
| 8 | * "COPYING" for details. |
| 9 | * |
| 10 | * "Clonk" is a registered trademark of Matthes Bender, used with permission. |
| 11 | * See accompanying file "TRADEMARK" for details. |
| 12 | * |
| 13 | * To redistribute this file separately, substitute the full license texts |
| 14 | * for the above references. |
| 15 | */ |
| 16 | |
| 17 | /* Game parameters - game data that is valid before the game is started */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "C4DelegatedIterable.h" |
| 22 | #include "C4IDList.h" |
| 23 | #include "C4PlayerInfo.h" |
| 24 | #include "C4LangStringTable.h" |
| 25 | #include "C4Teams.h" |
| 26 | |
| 27 | #include <memory> |
| 28 | #include <vector> |
| 29 | |
| 30 | class C4GameRes |
| 31 | { |
| 32 | friend class C4GameResList; |
| 33 | |
| 34 | public: |
| 35 | C4GameRes(); |
| 36 | C4GameRes(const C4GameRes &Res); |
| 37 | ~C4GameRes(); |
| 38 | |
| 39 | C4GameRes &operator=(const C4GameRes &Res); |
| 40 | |
| 41 | private: |
| 42 | C4Network2ResType eType; |
| 43 | StdStrBuf File; |
| 44 | const C4Network2ResCore *pResCore; |
| 45 | C4Network2Res::Ref pNetRes; |
| 46 | |
| 47 | public: |
| 48 | C4Network2ResType getType() const { return eType; } |
| 49 | const char *getFile() const { return File.getData(); } |
| 50 | bool isPresent() const { return !!File; } |
| 51 | const C4Network2ResCore *getResCore() const { return pResCore; } |
| 52 | C4Network2Res::Ref getNetRes() const { return pNetRes; } |
| 53 | |
| 54 | void SetFile(C4Network2ResType eType, const char *szFile); |
| 55 | void SetNetRes(C4Network2Res::Ref pRes); |
| 56 | |
| 57 | bool Publish(C4Network2ResList *pResList); |
| 58 | bool Load(C4Network2ResList *pResList); |
| 59 | bool InitNetwork(C4Network2ResList *pResList); |
| 60 | |
| 61 | void CalcHash(); |
| 62 | |
| 63 | void Clear(); |
| 64 | |
| 65 | void CompileFunc(StdCompiler *pComp); |
| 66 | }; |
| 67 | |
| 68 | class C4GameResList : public C4DelegatedIterable<C4GameResList> |
| 69 | { |
| 70 | private: |
| 71 | std::vector<std::unique_ptr<C4GameRes>> resList; |
| 72 | |
| 73 | public: |
| 74 | class ResTypeIterator |
| 75 | { |
| 76 | using InternalIterator = std::vector<std::unique_ptr<C4GameRes>>::const_iterator; |
| 77 | const std::vector<std::unique_ptr<C4GameRes>> &resList; |
| 78 | C4Network2ResType type; |
| 79 | InternalIterator it; |
| 80 | |
| 81 | public: |
| 82 | ResTypeIterator(C4Network2ResType type, const std::vector<std::unique_ptr<C4GameRes>> &resList); |
| 83 | |
| 84 | ResTypeIterator &operator++(); |
| 85 | |
| 86 | bool operator==(const ResTypeIterator &other) const; |
| 87 | |
| 88 | C4GameRes &operator*() const; |
| 89 | C4GameRes *operator->() const; |
| 90 | |
| 91 | ResTypeIterator begin() const { return *this; } |
| 92 | ResTypeIterator end() const; |
| 93 | |
| 94 | private: |
| 95 | void filter(); |
| 96 | }; |
| 97 | |
| 98 | using Iterable = ConstIterableMember<&C4GameResList::resList>; |
| 99 | |
| 100 | C4GameResList &operator=(const C4GameResList &List); |
| 101 | |
| 102 | ResTypeIterator iterRes(C4Network2ResType eType); |
| 103 | |
| 104 | void Clear(); |
| 105 | bool Load(const std::vector<std::string> &DefinitionFilenames); // host: create res cores by definition filenames |
| 106 | |
| 107 | C4GameRes *CreateByFile(C4Network2ResType eType, const char *szFile); |
| 108 | bool InitNetwork(C4Network2ResList *pNetResList); |
| 109 | |
| 110 | void CalcHashes(); |
| 111 | |
| 112 | bool RetrieveFiles(); // client: make sure all definition files are loaded |
| 113 | |
| 114 | void CompileFunc(StdCompiler *pComp); |
| 115 | |
| 116 | protected: |
| 117 | void Add(C4GameRes *pRes); |
| 118 | }; |
| 119 | |
| 120 | class C4GameParameters |
| 121 | { |
| 122 | public: |
| 123 | C4GameParameters(); |
| 124 | ~C4GameParameters(); |
| 125 | |
| 126 | // League (empty if it's not a league game) |
| 127 | StdStrBuf League; |
| 128 | StdStrBuf LeagueAddress; |
| 129 | StdStrBuf StreamAddress; |
| 130 | |
| 131 | // Random seed |
| 132 | int32_t RandomSeed; |
| 133 | |
| 134 | // Maximum player count allowed, count at game start |
| 135 | int32_t MaxPlayers, StartupPlayerCount; |
| 136 | |
| 137 | // Fair crew option |
| 138 | bool UseFairCrew; |
| 139 | bool FairCrewForced; // true for scenarios in which this setting may not be altered |
| 140 | int32_t FairCrewStrength; |
| 141 | |
| 142 | // Original network game? Also set in replays of network games for sync safety |
| 143 | bool IsNetworkGame; |
| 144 | |
| 145 | // Control rate |
| 146 | int32_t ControlRate; |
| 147 | |
| 148 | // Automatic frame skip enabled for this game? |
| 149 | bool AutoFrameSkip; |
| 150 | |
| 151 | // Allow debug mode? |
| 152 | bool AllowDebug; |
| 153 | |
| 154 | // Scenario title |
| 155 | ValidatedStdStrBuf<C4InVal::VAL_NameExNoEmpty> ScenarioTitle; |
| 156 | |
| 157 | // Active rules and goals |
| 158 | C4IDList Rules; |
| 159 | C4IDList Goals; |
| 160 | |
| 161 | // Game resources |
| 162 | C4GameRes Scenario; |
| 163 | C4GameResList GameRes; |
| 164 | |
| 165 | // Clients |
| 166 | C4ClientList Clients; |
| 167 | |
| 168 | // Players & Teams |
| 169 | C4PlayerInfoList PlayerInfos; |
| 170 | C4PlayerInfoList RestorePlayerInfos; |
| 171 | C4TeamList Teams; |
| 172 | |
| 173 | bool isLeague() const { return !!LeagueAddress.getLength(); } |
| 174 | bool doStreaming() const { return !!StreamAddress.getLength(); } |
| 175 | const char *getLeague() { return League.getData(); } |
| 176 | std::string GetGameGoalString(); |
| 177 | void EnforceLeagueRules(class C4Scenario *pScenario); |
| 178 | bool CheckLeagueRulesStart(bool fFixIt); |
| 179 | |
| 180 | void Clear(); |
| 181 | bool Load(C4Group &hGroup, C4Scenario *pDefault, const char *szGameText, C4LangStringTable *pLang, const std::vector<std::string> &DefinitionFilenames); |
| 182 | bool InitNetwork(C4Network2ResList *pResList); |
| 183 | bool Save(C4Group &hGroup, C4Scenario *pDefault); |
| 184 | |
| 185 | void CompileFunc(StdCompiler *pComp, C4Scenario *pScenario = nullptr); |
| 186 | }; |
| 187 | |