From 29a9c927f22942724852b93277f84d66a2154f5c Mon Sep 17 00:00:00 2001
From: Rtch90 <ritchie.cunningham@protonmail.com>
Date: Wed, 31 Jan 2018 22:26:05 +0000
Subject: [PATCH] [Add] Some serialization work on Body/Star systems. Still a
 fair bit to do.

---
 src/serializer.cpp | 52 +++++++++++++++++++++++++++++++++++++++++++---
 src/serializer.h   | 16 +++++++++++++-
 2 files changed, 64 insertions(+), 4 deletions(-)

diff --git a/src/serializer.cpp b/src/serializer.cpp
index fb77585..4ae6976 100644
--- a/src/serializer.cpp
+++ b/src/serializer.cpp
@@ -1,3 +1,4 @@
+#include "star_system.h"
 #include "serializer.h"
 #include "l3d.h"
 #include "frame.h"
@@ -6,6 +7,8 @@
 namespace Serializer {
 
 static std::vector<Frame*> g_frames;
+static std::vector<Body*>g_bodies;
+static std::vector<StarSystem::SBody*>g_sbodies;
 
 Frame* LookupFrame(int index) {
   return g_frames[index];
@@ -29,7 +32,43 @@ static void AddFrame(Frame* f) {
 void IndexFrames(void) {
   g_frames.clear();
   AddFrame(Space::rootFrame);
-  printf("%d frames indexed\n", g_frames.size());
+}
+
+StarSystem::SBody* LookupSystemBody(int index) { return (index == -1 ? 0 : g_sbodies[index]); }
+int LookupSystemBody(StarSystem::SBody* b) {
+  if(!b) return -1;
+  for(unsigned int i = 0; i < g_sbodies.size(); i++) {
+    if(g_sbodies[i] == b) return i;
+  }
+  assert(0);
+}
+
+static void add_sbody(StarSystem::SBody* b) {
+  g_sbodies.push_back(b);
+  for(unsigned int i = 0; i < b->children.size(); i++) {
+    add_sbody(b->children[i]);
+  }
+}
+
+void IndexSystemBodies(StarSystem* s) {
+  g_sbodies.clear();
+  add_sbody(s->rootBody);
+}
+
+Body* LookupBody(int index) { return (index == -1 ? 0 : g_bodies[index]); }
+int LookupBody(Body* b) {
+  if(!b) return -1;
+  for(unsigned int i = 0; i < g_bodies.size(); i++) {
+    if(g_bodies[i] == b) return 1;
+  }
+  assert(0);
+}
+
+void IndexBodies(void) {
+  g_bodies.clear();
+  for(Space::bodiesIter_t i = Space::bodies.begin(); i != Space::bodies.end(); ++i) {
+    g_bodies.push_back(*i);
+  }
 }
 
 namespace Write {
@@ -37,7 +76,6 @@ namespace Write {
   static FILE* sfptr;
 
   bool Game(const char* filename) {
-    struct Object* o;
     sfptr = fopen(filename, "wb");
 
     if(sfptr == NULL) {
@@ -68,6 +106,10 @@ namespace Write {
     checksum += x;
   }
 
+  void wr_bool(bool x) {
+    wr_byte((unsigned char)x);
+  }
+
   void wr_short(short x) {
     wr_byte((unsigned char)(x & 0xff));
     wr_byte((unsigned char)((x>>8) & 0xff));
@@ -120,7 +162,7 @@ namespace Write {
     wr_string(s.c_str());
   }
 
-  void wr_vector3d(vector3d& vec) {
+  void wr_vector3d(vector3d vec) {
     wr_double(vec.x);
     wr_double(vec.y);
     wr_double(vec.z);
@@ -177,6 +219,10 @@ namespace Read {
     return x;
   }
 
+  bool rd_bool(void) {
+    return(bool)rd_byte();
+  }
+
   short rd_short(void) {
     int t1, t2;
     t2 = rd_byte();
diff --git a/src/serializer.h b/src/serializer.h
index b2f7efc..687ab65 100644
--- a/src/serializer.h
+++ b/src/serializer.h
@@ -5,13 +5,26 @@
 #define SAVEFILE_VERSION 1
 
 class Frame;
+class Body;
+class StarSystem;
+class StarSystem::SBody;
 
 namespace Serializer {
   void IndexFrames(void);
   Frame* LookupFrame(Frame* f);
 
+  void IndexBodies(void);
+  Body* LookupBody(int index);
+  int LookupSystemBody(StarSystem::SBody*);
+  int LookupBody(Body*);
+
+  void IndexSystemBodies(StarSystem*);
+  StarSystem::SBody* LookupSystemBody(int index);
+  int LookupSystemBody(StarSystem::SBody*);
+
   namespace Write {
     bool Game(const char* filename);
+    void wr_bool(bool x);
     void wr_byte(unsigned char x);
     void wr_short(short x);
     void wr_int(int x);
@@ -19,12 +32,13 @@ namespace Serializer {
     void wr_double(double f);
     void wr_cstring(const char* s);
     void wr_string(std::string& s);
-    void wr_vector3d(vector3d& vec);
+    void wr_vector3d(vector3d vec);
   }
 
   namespace Read {
     bool Game(const char* filename);
     bool is_olderthan(int version);
+    bool rd_bool(void);
     unsigned char rd_byte(void);
     short rd_short(void);
     int rd_int(void);