summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/NetPacket.cpp
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-04-01 21:23:39 +0200
committerAki <please@ignore.pl>2022-04-01 21:23:39 +0200
commit3c487c5cd69c53d6fea948643c0a76df03516605 (patch)
tree72730c7b8b26a5ef8fc9a987ec4c16129efd5aac /StarsEx/NetPacket.cpp
parent8f353abd0bfe18baddd8a8250ab7c4f2d1c83a6e (diff)
downloadstarshatter-3c487c5cd69c53d6fea948643c0a76df03516605.zip
starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.gz
starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.bz2
Moved Stars45 to StarsEx
Diffstat (limited to 'StarsEx/NetPacket.cpp')
-rw-r--r--StarsEx/NetPacket.cpp354
1 files changed, 354 insertions, 0 deletions
diff --git a/StarsEx/NetPacket.cpp b/StarsEx/NetPacket.cpp
new file mode 100644
index 0000000..1056e11
--- /dev/null
+++ b/StarsEx/NetPacket.cpp
@@ -0,0 +1,354 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+
+ AUTHOR: John DiCamillo
+
+
+ OVERVIEW
+ ========
+ Network Game Manager and Player classes
+*/
+
+#include "NetPacket.h"
+#include "NetLink.h"
+#include "NetMsg.h"
+#include "Ship.h"
+
+#include "Game.h"
+
+// +--------------------------------------------------------------------+
+
+const int PING_SIZE = 8;
+const int SHIP_LOC_SIZE = 36;
+const int JOIN_REQ_SIZE = 116;
+const int JOIN_ANN_SIZE = 116;
+const int QUIT_ANN_SIZE = 8;
+
+// +--------------------------------------------------------------------+
+
+NetPacket::NetPacket(NetMsg* g)
+: msg(g)
+{ }
+
+NetPacket::NetPacket(DWORD netid, BYTE type)
+{
+ int len = 0;
+ char buf[256];
+ ZeroMemory(buf, 256);
+
+ switch (type) {
+ case NET_PING: len = PING_SIZE; break;
+ case NET_PONG: len = PING_SIZE; break;
+ case NET_OBJ_LOC: len = SHIP_LOC_SIZE; break;
+
+ case NET_JOIN_REQUEST: len = JOIN_REQ_SIZE; break;
+ case NET_JOIN_ANNOUNCE: len = JOIN_ANN_SIZE; break;
+ case NET_QUIT_ANNOUNCE: len = QUIT_ANN_SIZE; break;
+
+ default: len = JOIN_REQ_SIZE; break;
+ }
+
+ msg = new NetMsg(netid, type, buf, len);
+}
+
+NetPacket::~NetPacket()
+{
+ delete msg;
+}
+
+bool
+NetPacket::Send(NetLink& link)
+{
+ bool sent = false;
+
+ if (msg)
+ sent = link.SendMessage(msg);
+
+ msg = 0;
+ return sent;
+}
+
+// +--------------------------------------------------------------------+
+
+DWORD
+NetPacket::NetID() const
+{
+ if (msg)
+ return msg->NetID();
+
+ return 0;
+}
+
+BYTE
+NetPacket::Type() const
+{
+ if (msg)
+ return msg->Type();
+
+ return 0;
+}
+
+// +--------------------------------------------------------------------+
+
+DWORD
+NetPacket::GetPingSequence()
+{
+ if (msg && msg->Length() >= PING_SIZE) {
+ DWORD* data = (DWORD*) (msg->Data()+4);
+ return *data;
+ }
+
+ return 0;
+}
+
+void
+NetPacket::SetPingSequence(DWORD seq)
+{
+ if (msg && msg->Length() >= PING_SIZE) {
+ DWORD* data = (DWORD*) (msg->Data()+4);
+ *data = seq;
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+DWORD
+NetPacket::GetNetID()
+{
+ if (msg && msg->Length() >= PING_SIZE) {
+ DWORD* data = (DWORD*) (msg->Data()+4);
+ return *data;
+ }
+
+ return 0;
+}
+
+void
+NetPacket::SetNetID(DWORD id)
+{
+ if (msg && msg->Length() >= PING_SIZE) {
+ DWORD* data = (DWORD*) (msg->Data()+4);
+ *data = id;
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+Point
+NetPacket::GetShipLocation()
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ long* data = (long*) (msg->Data()+8);
+ long x = *(data + 0);
+ long y = *(data + 1);
+ long z = *(data + 2);
+
+ return Point(x/100.0, y/100.0, z/100.0);
+ }
+
+ return Point();
+}
+
+void
+NetPacket::SetShipLocation(const Point& loc)
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ long x = (long) (loc.x * 100);
+ long y = (long) (loc.y * 100);
+ long z = (long) (loc.z * 100);
+
+ long* data = (long*) (msg->Data()+8);
+
+ *(data + 0) = x;
+ *(data + 1) = y;
+ *(data + 2) = z;
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+Point
+NetPacket::GetShipVelocity()
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ short* data = (short*) (msg->Data()+20);
+
+ short dx = *(data + 0);
+ short dy = *(data + 1);
+ short dz = *(data + 2);
+
+ return Point(dx, dy, dz);
+ }
+
+ return Point();
+}
+
+void
+NetPacket::SetShipVelocity(const Point& vel)
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ short* data = (short*) (msg->Data()+20);
+
+ *(data + 0) = (short) vel.x;
+ *(data + 1) = (short) vel.y;
+ *(data + 2) = (short) vel.z;
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+Point
+NetPacket::GetShipOrientation()
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ short* data = (short*) (msg->Data()+26);
+
+ short r = *(data + 0);
+ short p = *(data + 1);
+ short y = *(data + 2);
+
+ return Point(2*PI*r/32767, 2*PI*p/32767, 2*PI*y/32767);
+ }
+
+ return Point();
+}
+
+void
+NetPacket::SetShipOrientation(const Point& rpy)
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ short* data = (short*) (msg->Data()+26);
+
+ *(data + 0) = (short) (32767*rpy.x/(2*PI));
+ *(data + 1) = (short) (32767*rpy.y/(2*PI));
+ *(data + 2) = (short) (32767*rpy.z/(2*PI));
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+double
+NetPacket::GetThrottle()
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+32;
+
+ return (double) *data;
+ }
+
+ return 0;
+}
+
+void
+NetPacket::SetThrottle(double t)
+{
+ if (msg && msg->Length() >= SHIP_LOC_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+32;
+
+ *data = (BYTE) t;
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+bool
+NetPacket::GetTrigger(int i)
+{
+ if (i >= 0 && i < 8 && msg && msg->Length() >= SHIP_LOC_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+33;
+
+ BYTE select = 1 << i;
+ return (*data & select)?true:false;
+ }
+
+ return false;
+}
+
+void
+NetPacket::SetTrigger(int i, bool trigger)
+{
+ if (i >= 0 && i < 8 && msg && msg->Length() >= SHIP_LOC_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+33;
+
+ BYTE select = 1 << i;
+
+ if (trigger)
+ *data = *data | select;
+ else
+ *data = *data & ~select;
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+const char*
+NetPacket::GetName()
+{
+ if (msg && msg->Length() >= JOIN_REQ_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+20;
+
+ return (const char*) data;
+ }
+
+ return 0;
+}
+
+void
+NetPacket::SetName(const char* name)
+{
+ if (msg && msg->Length() >= JOIN_REQ_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+20;
+ strncpy((char*) data, name, 32);
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+const char*
+NetPacket::GetDesign()
+{
+ if (msg && msg->Length() >= JOIN_REQ_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+52;
+
+ return (const char*) data;
+ }
+
+ return 0;
+}
+
+void
+NetPacket::SetDesign(const char* design)
+{
+ if (msg && msg->Length() >= JOIN_REQ_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+52;
+ strncpy((char*) data, design, 32);
+ }
+}
+
+// +--------------------------------------------------------------------+
+
+const char*
+NetPacket::GetRegion()
+{
+ if (msg && msg->Length() >= JOIN_REQ_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+84;
+
+ return (const char*) data;
+ }
+
+ return 0;
+}
+
+void
+NetPacket::SetRegion(const char* rgn_name)
+{
+ if (msg && msg->Length() >= JOIN_REQ_SIZE) {
+ BYTE* data = (BYTE*) msg->Data()+84;
+ strncpy((char*) data, rgn_name, 32);
+ }
+}
+
+// +--------------------------------------------------------------------+
+