Symphony Of Empires
action.cpp
Go to the documentation of this file.
1 // Symphony of Empires
2 // Copyright (C) 2021, Symphony of Empires contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // action.cpp
20 //
21 // Abstract:
22 // Does some important stuff.
23 // ----------------------------------------------------------------------------
24 
25 #include "eng3d/serializer.hpp"
26 #include "eng3d/network.hpp"
27 
28 #include "action.hpp"
31 #include "product.hpp"
32 #include "building.hpp"
33 #include "unit.hpp"
34 #include "nation.hpp"
35 #include "province.hpp"
36 #include "world.hpp"
37 
38 namespace Action {
39 template<ActionType type, typename Pred, typename ...Targs>
43  Eng3D::Deser::serialize<ActionType>(ar, type);
44  p(ar);
45  packet.data(ar.get_buffer(), ar.size());
46  return packet;
47 }
48 
50  return action_handler_sr<ActionType::DIPLO_DECLARE_WAR>([&nation](auto& ar) {
51  Eng3D::Deser::serialize(ar, nation.get_id());
52  });
53 }
54 
55 Eng3D::Networking::Packet ProvinceUpdate::form_packet(const std::vector<Province>& list) {
56  return action_handler_sr<ActionType::PROVINCE_UPDATE>([&list](auto& ar) {
57  for(const auto& province : list) {
58  Eng3D::Deser::serialize(ar, province.get_id()); // ProvinceRef
59  Eng3D::Deser::serialize(ar, province); // ProvinceObj
60  }
61  });
62 }
63 
64 Eng3D::Networking::Packet NationUpdate::form_packet(const std::vector<Nation>& list) {
65  return action_handler_sr<ActionType::NATION_UPDATE>([&list](auto& ar) {
66  for(const auto& nation : list) {
67  Eng3D::Deser::serialize(ar, nation.get_id()); // NationRef
68  Eng3D::Deser::serialize(ar, nation); // NationObj
69  }
70  });
71 }
72 
74  return action_handler_sr<ActionType::SELECT_NATION>([&nation](auto& ar) {
75  Eng3D::Deser::serialize(ar, nation.get_id());
78  });
79 }
80 
81 Eng3D::Networking::Packet BuildingStartProducingUnit::form_packet(const Province& province, const BuildingType& building_type, const Nation& nation, const UnitType& unit_type) {
82  return action_handler_sr<ActionType::BUILDING_START_BUILDING_UNIT>([&](auto& ar) {
83  Eng3D::Deser::serialize(ar, province.get_id());
84  Eng3D::Deser::serialize(ar, building_type.get_id());
85  Eng3D::Deser::serialize(ar, nation.get_id());
86  Eng3D::Deser::serialize(ar, unit_type.get_id());
87  });
88 }
89 
91  return action_handler_sr<ActionType::BUILDING_ADD>([&province, &building_type](auto& ar) {
92  Eng3D::Deser::serialize(ar, province.get_id());
93  Eng3D::Deser::serialize(ar, building_type.get_id());
94  });
95 }
96 
98  return action_handler_sr<ActionType::FOCUS_TECH>([&](auto& ar) {
99  Eng3D::Deser::serialize(ar, technology.get_id());
100  });
101 }
102 
104  return action_handler_sr<ActionType::NATION_TAKE_DECISION>([&](auto& ar) {
105  Eng3D::Deser::serialize(ar, event);
106  Eng3D::Deser::serialize(ar, decision.ref_name);
107  });
108 }
109 
111  return action_handler_sr<ActionType::UNIT_ADD>([&](auto& ar) {
112  Eng3D::Deser::serialize(ar, unit);
113  Eng3D::Deser::serialize<ProvinceId>(ar, unit.province_id());
114  });
115 }
116 
118  return action_handler_sr<ActionType::UNIT_UPDATE>([&](auto& ar) {
119  size_t size = 0;
120  units.for_each([&](const auto& unit) {
121  size++;
122  });
123  Eng3D::Deser::serialize<UnitId>(ar, UnitId(size));
124  units.for_each([&](const auto& unit) {
125  Eng3D::Deser::serialize(ar, unit);
126  });
127  });
128 }
129 
131  return action_handler_sr<ActionType::UNIT_REMOVE>([&](auto& ar) {
132  Eng3D::Deser::serialize<UnitId>(ar, unit.get_id());
133  });
134 }
135 
137  return action_handler_sr<ActionType::UNIT_CHANGE_TARGET>([&](auto& ar) {
138  Eng3D::Deser::serialize<UnitId>(ar, unit.get_id());
139  Eng3D::Deser::serialize<ProvinceId>(ar, province.get_id());
140  });
141 }
142 } // namespace Action
ActionType
Definition: action.hpp:32
bool ai_controlled
Definition: nation.hpp:131
bool ai_do_cmd_troops
Definition: nation.hpp:132
A single province, which is used to simulate economy in a "bulk-tiles" way instead of doing economica...
Definition: province.hpp:48
Roughly a batallion, consisting of approximately 500 soldiers each.
Definition: unit.hpp:80
ProvinceId province_id() const
Definition: unit.cpp:83
Eng3D::Networking::Packet action_handler_sr(Pred p)
Definition: action.cpp:40
void serialize(Eng3D::Deser::Archive &ar, const T &obj)
Definition: serializer.hpp:144
static Eng3D::Networking::Packet form_packet(const Province &province, const BuildingType &building_type)
Definition: action.cpp:90
static Eng3D::Networking::Packet form_packet(const Province &province, const BuildingType &building_type, const Nation &nation, const UnitType &unit_type)
Definition: action.cpp:81
static Eng3D::Networking::Packet form_packet(const Nation &nation)
Definition: action.cpp:49
static Eng3D::Networking::Packet form_packet(const Technology &technology)
Definition: action.cpp:97
static Eng3D::Networking::Packet form_packet(const Event &event, const Decision &decision)
Definition: action.cpp:103
static Eng3D::Networking::Packet form_packet(const std::vector< Nation > &list)
Definition: action.cpp:64
static Eng3D::Networking::Packet form_packet(const std::vector< Province > &list)
Definition: action.cpp:55
static Eng3D::Networking::Packet form_packet(const Nation &nation)
Definition: action.cpp:73
static Eng3D::Networking::Packet form_packet(const Unit &unit)
Definition: action.cpp:110
static Eng3D::Networking::Packet form_packet(const Unit &unit, const Province &province)
Definition: action.cpp:136
static Eng3D::Networking::Packet form_packet(const Unit &unit)
Definition: action.cpp:130
static Eng3D::Networking::Packet form_packet(const Eng3D::Freelist< Unit > &units)
Definition: action.cpp:117
Type for military outposts.
Definition: building.hpp:38
Base class that serves as archiver, stores (in memory) the data required for serialization/deserializ...
Definition: serializer.hpp:64
void for_each(const F &lambda) const
Definition: freelist.hpp:75
constexpr Id get_id() const
Definition: entity.hpp:152
Definition: event.hpp:54
Eng3D::StringRef ref_name
Definition: entity.hpp:161
Defines a type of unit, it can be a tank, garrison, infantry, etc this is moddable via a lua script a...
Definition: unit.hpp:44