Skip to content

Commit 7ed3c03

Browse files
committed
Made headers cpp specific (hpp instead of h), modern include guard, fixed issue with json deserialisation and added hash to save to avoid manual editing
1 parent e1aef86 commit 7ed3c03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+650
-537
lines changed

game/CMakeLists.txt

+41-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,48 @@ FetchContent_Declare(
1414

1515
FetchContent_MakeAvailable(nlohmann_json)
1616

17-
set(SOURCE_FILES game.cpp spells.h spells.cpp game.h types.h offensiveattributes.h offensiveattributes.cpp defensiveattributes.h defensiveattributes.cpp shields.cpp shields.h armors.cpp armors.h weapons.cpp weapons.h specials.cpp specials.h itemattributes.cpp itemattributes.h itemofs.cpp itemofs.h boringitems.cpp boringitems.h monsters.cpp monsters.h races.cpp races.h classes.cpp classes.h titles.cpp titles.h newgame.cpp newgame.h character.h monstermods.cpp monstermods.h
17+
set(
18+
SOURCE_FILES
19+
game.cpp
20+
spells.hpp
21+
spells.cpp
22+
game.hpp
23+
types.hpp
24+
offensiveattributes.hpp
25+
offensiveattributes.cpp
26+
defensiveattributes.hpp
27+
defensiveattributes.cpp
28+
shields.cpp
29+
shields.hpp
30+
armors.cpp
31+
armors.hpp
32+
weapons.cpp
33+
weapons.hpp
34+
specials.cpp
35+
specials.hpp
36+
itemattributes.cpp
37+
itemattributes.hpp
38+
itemofs.cpp
39+
itemofs.hpp
40+
boringitems.cpp
41+
boringitems.hpp
42+
monsters.cpp
43+
monsters.hpp
44+
races.cpp
45+
races.hpp
46+
classes.cpp
47+
classes.hpp
48+
titles.cpp
49+
titles.hpp
50+
newgame.cpp
51+
newgame.hpp
52+
character.hpp
53+
monstermods.cpp
54+
monstermods.hpp
1855
jsonserialiser.cpp
19-
jsonserialiser.h)
56+
jsonserialiser.hpp
57+
character.cpp
58+
)
2059

2160
add_library(pq2game SHARED ${SOURCE_FILES})
2261

game/armors.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Created by nbollom on 1/06/16.
33
//
44

5-
#include "armors.h"
5+
#include "armors.hpp"
66
#include <vector>
77

88
using namespace std;

game/armors.h

-19
This file was deleted.

game/armors.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Created by nbollom on 1/06/16.
3+
//
4+
5+
#pragma once
6+
7+
#include <random>
8+
#include <memory>
9+
#include "types.hpp"
10+
11+
namespace data {
12+
13+
[[nodiscard]] Item get_random_armor(const std::shared_ptr<std::mt19937_64>& engine);
14+
15+
}

game/boringitems.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Created by nbollom on 1/06/16.
33
//
44

5-
#include "boringitems.h"
5+
#include "boringitems.hpp"
66
#include <vector>
77

88
using namespace std;

game/boringitems.h

-18
This file was deleted.

game/boringitems.hpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Created by nbollom on 1/06/16.
3+
//
4+
5+
#pragma once
6+
7+
#include <string>
8+
#include <random>
9+
#include <memory>
10+
11+
namespace data {
12+
13+
[[nodiscard]] std::string get_random_boring_item(const std::shared_ptr<std::mt19937_64>& engine);
14+
15+
}

game/character.cpp

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
//
2+
// Created by nbollom on 22/12/24.
3+
//
4+
5+
#include "character.hpp"
6+
7+
using namespace data;
8+
9+
template<>
10+
struct std::hash<Race>
11+
{
12+
std::size_t operator()(const Race& race) const noexcept
13+
{
14+
size_t hash = 17;
15+
hash = hash * 31 + std::hash<std::string>{}(race.name);
16+
for (const auto &attribute : race.attributes) {
17+
hash = hash * 31 + std::hash<std::string>{}(attribute);
18+
}
19+
return hash;
20+
}
21+
};
22+
23+
template<>
24+
struct std::hash<Class>
25+
{
26+
std::size_t operator()(const Class& cls) const noexcept
27+
{
28+
size_t hash = 17;
29+
hash = hash * 31 + std::hash<std::string>{}(cls.name);
30+
for (const auto &attribute : cls.attributes) {
31+
hash = hash * 31 + std::hash<std::string>{}(attribute);
32+
}
33+
return hash;
34+
}
35+
};
36+
37+
template<>
38+
struct std::hash<Stack>
39+
{
40+
std::size_t operator()(const Stack& stack) const noexcept
41+
{
42+
size_t hash = 17;
43+
hash = hash * 31 + std::hash<std::string>{}(stack.name);
44+
hash = hash * 31 + std::hash<uint64_t>{}(stack.count);
45+
return hash;
46+
}
47+
};
48+
49+
template<>
50+
struct std::hash<Monster>
51+
{
52+
std::size_t operator()(const Monster& monster) const noexcept
53+
{
54+
size_t hash = 17;
55+
hash = hash * 31 + std::hash<std::string>{}(monster.name);
56+
hash = hash * 31 + std::hash<uint64_t>{}(monster.level);
57+
hash = hash * 31 + std::hash<std::string>{}(monster.drop);
58+
return hash;
59+
}
60+
};
61+
62+
template<>
63+
struct std::hash<Quest>
64+
{
65+
std::size_t operator()(const Quest& quest) const noexcept
66+
{
67+
size_t hash = 17;
68+
hash = hash * 31 + std::hash<std::string>{}(quest.label);
69+
if (quest.monster.has_value()) {
70+
hash = hash * 31 + std::hash<Monster>{}(quest.monster.value());
71+
}
72+
return hash;
73+
}
74+
};
75+
76+
template<>
77+
struct std::hash<QueueItem>
78+
{
79+
std::size_t operator()(const QueueItem& queue_item) const noexcept
80+
{
81+
size_t hash = 17;
82+
hash = hash * 31 + std::hash<std::string>{}(queue_item.label);
83+
hash = hash * 31 + std::hash<uint64_t>{}(queue_item.ms);
84+
hash = hash * 31 + std::hash<QueueItemType>{}(queue_item.type);
85+
return hash;
86+
}
87+
};
88+
89+
90+
uint64_t Character::CalculateHash() const {
91+
size_t hash = 17;
92+
hash = hash * 31 + std::hash<std::string>{}(Name);
93+
hash = hash * 31 + std::hash<Race>{}(CharacterRace);
94+
hash = hash * 31 + std::hash<Class>{}(CharacterClass);
95+
hash = hash * 31 + std::hash<uint64_t>{}(Level);
96+
hash = hash * 31 + std::hash<uint64_t>{}(Experience);
97+
hash = hash * 31 + std::hash<uint64_t>{}(STR);
98+
hash = hash * 31 + std::hash<uint64_t>{}(CON);
99+
hash = hash * 31 + std::hash<uint64_t>{}(DEX);
100+
hash = hash * 31 + std::hash<uint64_t>{}(INT);
101+
hash = hash * 31 + std::hash<uint64_t>{}(WIS);
102+
hash = hash * 31 + std::hash<uint64_t>{}(CHA);
103+
hash = hash * 31 + std::hash<uint64_t>{}(MAX_HP);
104+
hash = hash * 31 + std::hash<uint64_t>{}(MAX_MP);
105+
hash = hash * 31 + std::hash<uint64_t>{}(Gold);
106+
107+
for (const auto &spell : Spells) {
108+
hash = hash * 31 + std::hash<Stack>{}(spell);
109+
}
110+
111+
for (const auto &equipment : Equipment) {
112+
hash = hash * 31 + std::hash<std::string>{}(equipment);
113+
}
114+
115+
for (const auto &item : Inventory) {
116+
hash = hash * 31 + std::hash<Stack>{}(item);
117+
}
118+
119+
for (const auto &plot_item : Plot) {
120+
hash = hash * 31 + std::hash<std::string>{}(plot_item);
121+
}
122+
123+
for (const auto &quest : Quests) {
124+
hash = hash * 31 + std::hash<Quest>{}(quest);
125+
}
126+
127+
for (const auto &queue_item : Queue) {
128+
hash = hash * 31 + std::hash<QueueItem>{}(queue_item);
129+
}
130+
131+
hash = hash * 31 + std::hash<CurrentActionType>{}(CurrentAction);
132+
hash = hash * 31 + std::hash<std::string>{}(CurrentActionLabel);
133+
hash = hash * 31 + std::hash<Monster>{}(CurrentMonster);
134+
hash = hash * 31 + std::hash<uint64_t>{}(CurrentProgress);
135+
hash = hash * 31 + std::hash<uint64_t>{}(MaxProgress);
136+
hash = hash * 31 + std::hash<uint64_t>{}(CurrentPlotProgress);
137+
hash = hash * 31 + std::hash<uint64_t>{}(CurrentQuestProgress);
138+
hash = hash * 31 + std::hash<uint64_t>{}(MaxQuestProgress);
139+
140+
return hash;
141+
}
142+

game/character.h game/character.hpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
// Created by nbollom on 3/06/16.
33
//
44

5-
#ifndef PQ2_CHARACTER_H
6-
#define PQ2_CHARACTER_H
5+
#pragma once
76

87
#include <string>
98
#include <array>
109
#include <vector>
1110
#include <deque>
1211

13-
#include "types.h"
12+
#include "types.hpp"
1413

1514
struct Character {
1615

@@ -51,6 +50,6 @@ struct Character {
5150
uint64_t CurrentQuestProgress;
5251
uint64_t MaxQuestProgress;
5352

54-
};
53+
uint64_t CalculateHash() const;
5554

56-
#endif //PQ2_CHARACTER_H
55+
};

game/classes.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Created by nbollom on 2/06/16.
33
//
44

5-
#include "classes.h"
5+
#include "classes.hpp"
66
#include <vector>
77

88
using namespace std;

game/classes.h

-21
This file was deleted.

game/classes.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Created by nbollom on 2/06/16.
3+
//
4+
5+
#pragma once
6+
7+
#include <random>
8+
#include <memory>
9+
#include "types.hpp"
10+
11+
namespace data {
12+
13+
[[nodiscard]] Class get_random_class(const std::shared_ptr<std::mt19937_64>& engine);
14+
15+
[[nodiscard]] std::vector<Class> get_class_list();
16+
17+
}

game/defensiveattributes.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Created by nbollom on 26/05/16.
33
//
44

5-
#include "defensiveattributes.h"
5+
#include "defensiveattributes.hpp"
66
#include <vector>
77

88
using namespace std;

game/defensiveattributes.h

-20
This file was deleted.

0 commit comments

Comments
 (0)