| 1 |
/* |
| 2 |
C-Dogs SDL |
| 3 |
A port of the legendary (and fun) action/arcade cdogs. |
| 4 |
|
| 5 |
Copyright (c) 2014-2016, 2020-2021, 2023 Cong Xu |
| 6 |
All rights reserved. |
| 7 |
|
| 8 |
Redistribution and use in source and binary forms, with or without |
| 9 |
modification, are permitted provided that the following conditions are met: |
| 10 |
|
| 11 |
Redistributions of source code must retain the above copyright notice, this |
| 12 |
list of conditions and the following disclaimer. |
| 13 |
Redistributions in binary form must reproduce the above copyright notice, |
| 14 |
this list of conditions and the following disclaimer in the documentation |
| 15 |
and/or other materials provided with the distribution. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 21 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 |
POSSIBILITY OF SUCH DAMAGE. |
| 28 |
*/ |
| 29 |
#include "net_util.h" |
| 30 |
|
| 31 |
#include "proto/nanopb/pb_decode.h" |
| 32 |
#include "proto/nanopb/pb_encode.h" |
| 33 |
|
| 34 |
ENetPacket *NetEncode(const GameEventType e, const void *data) |
| 35 |
{ |
| 36 |
uint8_t buffer[1024]; |
| 37 |
pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof buffer); |
| 38 |
const pb_msgdesc_t *fields = GameEventGetEntry(e).Fields; |
| 39 |
const bool status = |
| 40 |
(data && fields) ? pb_encode(&stream, fields, data) : true; |
| 41 |
CASSERT(status, "Failed to encode pb"); |
| 42 |
int msgId = (int)e; |
| 43 |
ENetPacket *packet = enet_packet_create( |
| 44 |
&msgId, NET_MSG_SIZE + stream.bytes_written, |
| 45 |
ENET_PACKET_FLAG_RELIABLE); |
| 46 |
memcpy(packet->data + NET_MSG_SIZE, buffer, stream.bytes_written); |
| 47 |
return packet; |
| 48 |
} |
| 49 |
|
| 50 |
bool NetDecode(ENetPacket *packet, void *dest, const pb_msgdesc_t *fields) |
| 51 |
{ |
| 52 |
pb_istream_t stream = pb_istream_from_buffer( |
| 53 |
packet->data + NET_MSG_SIZE, packet->dataLength - NET_MSG_SIZE); |
| 54 |
bool status = pb_decode(&stream, fields, dest); |
| 55 |
CASSERT(status, "Failed to decode pb"); |
| 56 |
return status; |
| 57 |
} |
| 58 |
|
| 59 |
typedef struct |
| 60 |
{ |
| 61 |
map_t src; |
| 62 |
NPlayerData *d; |
| 63 |
} AddWeaponUsageData; |
| 64 |
static int AddWeaponUsage(any_t data, any_t key) |
| 65 |
{ |
| 66 |
AddWeaponUsageData *aData = (AddWeaponUsageData *)data; |
| 67 |
// TODO: copy weapon usage |
| 68 |
NWeaponUsage *w; |
| 69 |
int error = hashmap_get(aData->src, (char *)key, (any_t *)&w); |
| 70 |
if (error != MAP_OK) |
| 71 |
{ |
| 72 |
return error; |
| 73 |
} |
| 74 |
NWeaponUsage *n = &aData->d->WeaponUsages[aData->d->WeaponUsages_count]; |
| 75 |
strcpy(n->Weapon, (char *)key); |
| 76 |
n->Shots = w->Shots; |
| 77 |
n->Hits = w->Hits; |
| 78 |
aData->d->WeaponUsages_count++; |
| 79 |
return MAP_OK; |
| 80 |
} |
| 81 |
NPlayerData NMakePlayerData(const PlayerData *p) |
| 82 |
{ |
| 83 |
NPlayerData d = NPlayerData_init_default; |
| 84 |
d.has_Colors = d.has_Stats = d.has_Totals = true; |
| 85 |
const Character *c = &p->Char; |
| 86 |
strcpy(d.Name, p->name); |
| 87 |
strcpy(d.CharacterClass, p->Char.Class->Name); |
| 88 |
if (p->Char.HeadParts[HEAD_PART_HAIR]) |
| 89 |
{ |
| 90 |
strcpy(d.Hair, p->Char.HeadParts[HEAD_PART_HAIR]); |
| 91 |
} |
| 92 |
if (p->Char.HeadParts[HEAD_PART_FACEHAIR]) |
| 93 |
{ |
| 94 |
strcpy(d.Facehair, p->Char.HeadParts[HEAD_PART_FACEHAIR]); |
| 95 |
} |
| 96 |
if (p->Char.HeadParts[HEAD_PART_HAT]) |
| 97 |
{ |
| 98 |
strcpy(d.Hat, p->Char.HeadParts[HEAD_PART_HAT]); |
| 99 |
} |
| 100 |
if (p->Char.HeadParts[HEAD_PART_GLASSES]) |
| 101 |
{ |
| 102 |
strcpy(d.Glasses, p->Char.HeadParts[HEAD_PART_GLASSES]); |
| 103 |
} |
| 104 |
d.Colors = CharColors2Net(c->Colors); |
| 105 |
d.Weapons_count = MAX_WEAPONS; |
| 106 |
for (int i = 0; i < (int)d.Weapons_count; i++) |
| 107 |
{ |
| 108 |
strcpy(d.Weapons[i], p->guns[i] != NULL ? p->guns[i]->name : ""); |
| 109 |
} |
| 110 |
d.WeaponUsages_count = 0; |
| 111 |
AddWeaponUsageData aData; |
| 112 |
aData.src = p->WeaponUsages; |
| 113 |
aData.d = &d; |
| 114 |
hashmap_iterate_keys(p->WeaponUsages, AddWeaponUsage, &aData); |
| 115 |
|
| 116 |
d.Lives = p->Lives; |
| 117 |
d.Stats = p->Stats; |
| 118 |
d.Totals = p->Totals; |
| 119 |
d.MaxHealth = p->Char.maxHealth; |
| 120 |
d.ExcessHealth = p->Char.excessHealth; |
| 121 |
d.HP = p->HP; |
| 122 |
d.LastMission = p->lastMission; |
| 123 |
d.UID = p->UID; |
| 124 |
Ammo2Net(&d.Ammo_count, d.Ammo, &p->ammo); |
| 125 |
return d; |
| 126 |
} |
| 127 |
NCampaignDef NMakeCampaignDef(const Campaign *co) |
| 128 |
{ |
| 129 |
NCampaignDef def; |
| 130 |
memset(&def, 0, sizeof def); |
| 131 |
if (co->Entry.Path) |
| 132 |
{ |
| 133 |
strcpy((char *)def.Path, co->Entry.Path); |
| 134 |
} |
| 135 |
def.GameMode = co->Entry.Mode; |
| 136 |
def.Mission = co->MissionIndex; |
| 137 |
return def; |
| 138 |
} |
| 139 |
NMissionComplete NMakeMissionComplete(const struct MissionOptions *mo) |
| 140 |
{ |
| 141 |
NMissionComplete mc; |
| 142 |
mc.ShowMsg = MissionHasRequiredObjectives(mo); |
| 143 |
return mc; |
| 144 |
} |
| 145 |
|
| 146 |
struct vec2i Net2Vec2i(const NVec2i v) |
| 147 |
{ |
| 148 |
return svec2i(v.x, v.y); |
| 149 |
} |
| 150 |
NVec2i Vec2i2Net(const struct vec2i v) |
| 151 |
{ |
| 152 |
NVec2i nv; |
| 153 |
nv.x = v.x; |
| 154 |
nv.y = v.y; |
| 155 |
return nv; |
| 156 |
} |
| 157 |
struct vec2 NetToVec2(const NVec2 v) |
| 158 |
{ |
| 159 |
return svec2(v.x, v.y); |
| 160 |
} |
| 161 |
NVec2 Vec2ToNet(const struct vec2 v) |
| 162 |
{ |
| 163 |
NVec2 nv; |
| 164 |
nv.x = v.x; |
| 165 |
nv.y = v.y; |
| 166 |
return nv; |
| 167 |
} |
| 168 |
color_t Net2Color(const NColor c) |
| 169 |
{ |
| 170 |
color_t co; |
| 171 |
co.r = (uint8_t)((c.RGBA & 0xff000000) >> 24); |
| 172 |
co.g = (uint8_t)((c.RGBA & 0x00ff0000) >> 16); |
| 173 |
co.b = (uint8_t)((c.RGBA & 0x0000ff00) >> 8); |
| 174 |
co.a = (uint8_t)(c.RGBA & 0x000000ff); |
| 175 |
return co; |
| 176 |
} |
| 177 |
NColor Color2Net(const color_t c) |
| 178 |
{ |
| 179 |
NColor co; |
| 180 |
co.RGBA = (c.r << 24) | (c.g << 16) | (c.b << 8) | c.a; |
| 181 |
return co; |
| 182 |
} |
| 183 |
NCharColors CharColors2Net(const CharColors c) |
| 184 |
{ |
| 185 |
NCharColors co; |
| 186 |
co.has_Skin = co.has_Arms = co.has_Body = co.has_Legs = co.has_Hair = |
| 187 |
co.has_Feet = co.has_Facehair = co.has_Hat = co.has_Glasses = true; |
| 188 |
co.Skin = Color2Net(c.Skin); |
| 189 |
co.Arms = Color2Net(c.Arms); |
| 190 |
co.Body = Color2Net(c.Body); |
| 191 |
co.Legs = Color2Net(c.Legs); |
| 192 |
co.Hair = Color2Net(c.Hair); |
| 193 |
co.Feet = Color2Net(c.Feet); |
| 194 |
co.Facehair = Color2Net(c.Facehair); |
| 195 |
co.Hat = Color2Net(c.Hat); |
| 196 |
co.Glasses = Color2Net(c.Glasses); |
| 197 |
return co; |
| 198 |
} |
| 199 |
CharColors Net2CharColors(const NCharColors c) |
| 200 |
{ |
| 201 |
CharColors co; |
| 202 |
co.Skin = Net2Color(c.Skin); |
| 203 |
co.Arms = Net2Color(c.Arms); |
| 204 |
co.Body = Net2Color(c.Body); |
| 205 |
co.Legs = Net2Color(c.Legs); |
| 206 |
co.Hair = Net2Color(c.Hair); |
| 207 |
co.Feet = Net2Color(c.Feet); |
| 208 |
co.Facehair = Net2Color(c.Facehair); |
| 209 |
co.Hat = Net2Color(c.Hat); |
| 210 |
co.Glasses = Net2Color(c.Glasses); |
| 211 |
return co; |
| 212 |
} |
| 213 |
void Ammo2Net(pb_size_t *ammoCount, NAmmo *ammo, const CArray *a) |
| 214 |
{ |
| 215 |
*ammoCount = 0; |
| 216 |
CA_FOREACH(int, amount, *a) |
| 217 |
if (*amount > 0) |
| 218 |
{ |
| 219 |
ammo[*ammoCount].Id = _ca_index; |
| 220 |
ammo[*ammoCount].Amount = *amount; |
| 221 |
(*ammoCount)++; |
| 222 |
} |
| 223 |
CA_FOREACH_END() |
| 224 |
} |