Debian Codegraph @

common/main/multiinternal.h

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 #pragma once
8
9 #include <span>
10 #include <type_traits>
11 #include "dxxerror.h"
12 #include "net_udp.h"
13 #include "object.h"
14 #include "powerup.h"
15 #include "serial.h"
16 #include "fwd-player.h"
17
18 #define _UNPACK_MULTIPLAYER_SERIAL_MESSAGE(A,...) A, ## __VA_ARGS__
19 #define DEFINE_MULTIPLAYER_SERIAL_MESSAGE(C,T,V,A) \
20 DEFINE_SERIAL_UDT_TO_MESSAGE(T,V, (multiplayer_command<C>(), _UNPACK_MULTIPLAYER_SERIAL_MESSAGE A)); \
21 ASSERT_SERIAL_UDT_MESSAGE_SIZE(T, command_length<C>)
22
23 #define define_multiplayer_command(NAME,SIZE) NAME,
24
25 #ifdef DXX_BUILD_DESCENT
26 #define for_each_multiplayer_command(VALUE) \
27 VALUE(MULTI_POSITION , 1 + quaternionpos::packed_size::value) \
28 VALUE(MULTI_REAPPEAR , 4) \
29 VALUE(MULTI_FIRE , 17) \
30 VALUE(MULTI_FIRE_TRACK , 20) \
31 VALUE(MULTI_FIRE_BOMB , 19) \
32 VALUE(MULTI_REMOVE_OBJECT , 4) \
33 VALUE(MULTI_MESSAGE , 37) /* (MAX_MESSAGE_LENGTH = 40) */ \
34 VALUE(MULTI_QUIT , 2) \
35 VALUE(MULTI_PLAY_SOUND , 8) \
36 VALUE(MULTI_CONTROLCEN , 4) \
37 VALUE(MULTI_ROBOT_CLAIM , 5) \
38 VALUE(MULTI_CLOAK , 2) \
39 VALUE(MULTI_ENDLEVEL_START , DXX_MP_SIZE_ENDLEVEL_START) \
40 VALUE(MULTI_CREATE_EXPLOSION , 2) \
41 VALUE(MULTI_CONTROLCEN_FIRE , 16) \
42 VALUE(MULTI_CREATE_POWERUP , 19) \
43 VALUE(MULTI_DECLOAK , 2) \
44 VALUE(MULTI_ROBOT_POSITION , 5 + quaternionpos::packed_size::value) \
45 VALUE(MULTI_PLAYER_DERES , DXX_MP_SIZE_PLAYER_RELATED) \
46 VALUE(MULTI_DOOR_OPEN , DXX_MP_SIZE_DOOR_OPEN) \
47 VALUE(MULTI_ROBOT_EXPLODE , 7) \
48 VALUE(MULTI_ROBOT_RELEASE , 5) \
49 VALUE(MULTI_ROBOT_FIRE , 18) \
50 VALUE(MULTI_SCORE , 6) \
51 VALUE(MULTI_CREATE_ROBOT , 6) \
52 VALUE(MULTI_TRIGGER , 3) \
53 VALUE(MULTI_BOSS_TELEPORT , 5) \
54 VALUE(MULTI_BOSS_CLOAK , 3) \
55 VALUE(MULTI_BOSS_START_GATE , 3) \
56 VALUE(MULTI_BOSS_STOP_GATE , 3) \
57 VALUE(MULTI_BOSS_CREATE_ROBOT , 8) \
58 VALUE(MULTI_CREATE_ROBOT_POWERUPS, 27) \
59 VALUE(MULTI_HOSTAGE_DOOR , 7) \
60 VALUE(MULTI_SAVE_GAME , 2+24) /* (ubyte slot, uint id, char name[20]) */ \
61 VALUE(MULTI_RESTORE_GAME , 2+4) /* (ubyte slot, uint id) */ \
62 VALUE(MULTI_HEARTBEAT , 5) \
63 VALUE(MULTI_KILLGOALS , 1 + MAX_PLAYERS) \
64 VALUE(MULTI_DO_BOUNTY , 2) \
65 VALUE(MULTI_TYPING_STATE , 3) \
66 VALUE(MULTI_GMODE_UPDATE , 3) \
67 VALUE(MULTI_KILL_HOST , 7) \
68 VALUE(MULTI_KILL_CLIENT , 5) \
69 VALUE(MULTI_RANK , 3) \
70 VALUE(MULTI_DROP_WEAPON , 10) \
71 VALUE(MULTI_VULWPN_AMMO_ADJ , 6) \
72 VALUE(MULTI_PLAYER_INV , DXX_MP_SIZE_PLAYER_INVENTORY) \
73 D2X_MP_COMMANDS(VALUE) \
74
75 #if DXX_BUILD_DESCENT == 1
76 #define DXX_MP_SIZE_ENDLEVEL_START 3
77 #define DXX_MP_SIZE_PLAYER_RELATED 58
78 #define DXX_MP_SIZE_PLAYER_INVENTORY 15
79 #define DXX_MP_SIZE_BEGIN_SYNC 37
80 #define DXX_MP_SIZE_DOOR_OPEN 4
81 #define D2X_MP_COMMANDS(VALUE)
82 #elif DXX_BUILD_DESCENT == 2
83 #define DXX_MP_SIZE_ENDLEVEL_START 2
84 #define DXX_MP_SIZE_PLAYER_RELATED (98+10)
85 #define DXX_MP_SIZE_PLAYER_INVENTORY 21
86 #define DXX_MP_SIZE_BEGIN_SYNC 41
87 #define DXX_MP_SIZE_DOOR_OPEN 5
88 #define D2X_MP_COMMANDS(VALUE) \
89 VALUE(MULTI_MARKER , 55) \
90 VALUE(MULTI_GUIDED , 26) \
91 VALUE(MULTI_STOLEN_ITEMS , 1 + std::tuple_size<decltype(d_thief_unique_state::Stolen_items)>::value) \
92 VALUE(MULTI_WALL_STATUS , 6) /* send to new players */ \
93 VALUE(MULTI_SEISMIC , 5) \
94 VALUE(MULTI_LIGHT , 16) \
95 VALUE(MULTI_START_TRIGGER , 2) \
96 VALUE(MULTI_FLAGS , 6) \
97 VALUE(MULTI_DROP_BLOB , 2) \
98 VALUE(MULTI_SOUND_FUNCTION , 4) \
99 VALUE(MULTI_CAPTURE_BONUS , 2) \
100 VALUE(MULTI_GOT_FLAG , 2) \
101 VALUE(MULTI_DROP_FLAG , 8) \
102 VALUE(MULTI_FINISH_GAME , 2) \
103 VALUE(MULTI_ORB_BONUS , 3) \
104 VALUE(MULTI_GOT_ORB , 2) \
105 VALUE(MULTI_EFFECT_BLOWUP , 17) \
106 VALUE(MULTI_UPDATE_BUDDY_STATE, 7) \
107
108 #endif
109
110 #include "dxxsconf.h"
111
112 namespace dcx {
113 enum class multiplayer_data_priority : uint8_t;
114 }
115
116 enum class multiplayer_command_t : uint8_t
117 {
118 for_each_multiplayer_command(define_multiplayer_command)
119 };
120
121 template <multiplayer_command_t>
122 [[deprecated("only explicit specializations can be used")]]
123 inline constexpr std::size_t command_length
124 #ifdef __clang__
125 /* clang raises an error if the variable is not initialized, even if the
126 * generic case is never referenced.
127 *
128 * gcc permits the generic case to be uninitialized if it is never referenced.
129 * Therefore, leave it uninitialized when possible, so that any erroneous use
130 * triggers an error.
131 */
132 = std::dynamic_extent
133 #endif
134 ;
135
136 #define define_command_length(NAME,SIZE) \
137 template <> \
138 inline constexpr std::size_t command_length<multiplayer_command_t::NAME>{SIZE};
139 for_each_multiplayer_command(define_command_length);
140
141 namespace dcx {
142
143 template <multiplayer_command_t C>
144 struct multi_command : public std::array<uint8_t, command_length<C>>
145 {
146 constexpr multi_command()
147 {
148 this->front() = static_cast<uint8_t>(C);
149 }
150 };
151
152 template <multiplayer_command_t C>
153 static inline void multi_send_data(const multi_command<C> &buf, const multiplayer_data_priority priority)
154 {
155 multi::dispatch->send_data(buf, priority);
156 }
157
158 static inline void multi_serialize_read(const uint8_t *const buf, auto &t)
159 {
160 serial::reader::le_bytebuffer b{buf};
161 serial::process_buffer(b, t);
162 }
163
164 template <std::size_t Extent>
165 static inline void multi_serialize_read(const std::span<const uint8_t, Extent> buf, auto &t)
166 {
167 multi_serialize_read(buf.data(), t);
168 }
169
170 template <typename T>
171 static inline void multi_serialize_write(const multiplayer_data_priority priority, const T &t)
172 {
173 constexpr size_t maximum_size = serial::message_type<T>::maximum_size;
174 uint8_t buf[maximum_size];
175 serial::writer::le_bytebuffer b{buf};
176 serial::process_buffer(b, t);
177 multi::dispatch->send_data(buf, priority);
178 }
179
180 template <multiplayer_command_t C>
181 using multiplayer_command = serial::pad<1, static_cast<uint8_t>(C)>;
182
183 template <multiplayer_command_t C>
184 using multiplayer_rspan = std::span<const uint8_t, command_length<C>>;
185
186 }
187
188 namespace dsx {
189
190 void multi_do_robot_explode(const d_robot_info_array &Robot_info, multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_EXPLODE> buf);
191 void multi_do_create_robot(const d_robot_info_array &Robot_info, const d_vclip_array &Vclip, playernum_t pnum, multiplayer_rspan<multiplayer_command_t::MULTI_CREATE_ROBOT> buf);
192 void multi_do_claim_robot(playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_CLAIM> buf);
193 void multi_do_robot_position(playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_POSITION> buf);
194 void multi_do_release_robot(playernum_t pnum, const multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_RELEASE> buf);
195 void multi_do_robot_fire(multiplayer_rspan<multiplayer_command_t::MULTI_ROBOT_FIRE> buf);
196 #if DXX_BUILD_DESCENT == 2
197 void multi_recv_escort_goal(d_unique_buddy_state &, multiplayer_rspan<multiplayer_command_t::MULTI_UPDATE_BUDDY_STATE> buf);
198 #endif
199 void multi_do_boss_teleport(const d_robot_info_array &Robot_info, const d_vclip_array &Vclip, playernum_t pnum, multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_TELEPORT> buf);
200 void multi_do_boss_cloak(multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_CLOAK> buf);
201 void multi_do_boss_start_gate(multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_START_GATE> buf);
202 void multi_do_boss_stop_gate(multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_STOP_GATE> buf);
203 void multi_do_boss_create_robot(playernum_t pnum, multiplayer_rspan<multiplayer_command_t::MULTI_BOSS_CREATE_ROBOT> buf);
204 void multi_do_create_robot_powerups(playernum_t pnum, multiplayer_rspan<multiplayer_command_t::MULTI_CREATE_ROBOT_POWERUPS> buf);
205
206 }
207
208 #endif