pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r3640 - trunk/pingus/src


From: grumbel at BerliOS
Subject: [Pingus-CVS] r3640 - trunk/pingus/src
Date: Thu, 3 Jul 2008 06:10:28 +0200

Author: grumbel
Date: 2008-07-03 06:10:26 +0200 (Thu, 03 Jul 2008)
New Revision: 3640

Modified:
   trunk/pingus/src/server_event.cpp
Log:
Improved position tracking and demo verification

Modified: trunk/pingus/src/server_event.cpp
===================================================================
--- trunk/pingus/src/server_event.cpp   2008-07-03 03:46:29 UTC (rev 3639)
+++ trunk/pingus/src/server_event.cpp   2008-07-03 04:10:26 UTC (rev 3640)
@@ -26,24 +26,55 @@
 #include "pingu.hpp"
 #include "string_util.hpp"
 
-std::string float2string(float value)
+static char num2hex[] = "0123456789abcdef";
+
+/** Write out the raw bits of a float as hex */
+static std::string float2string(float value)
 {
-  std::string str = "0x";
-  for(size_t i = 0; i < sizeof(value); ++i)
+  std::string str(2*sizeof(float), '0');
+
+  for(size_t i = 0; i < sizeof(float); ++i)
     {
-      str += (boost::format("%02x") % 
int(reinterpret_cast<uint8_t*>(&value)[i])).str();
+      char v = reinterpret_cast<char*>(&value)[i];
+      str[2*i + 0] = num2hex[(v & 0xf0) >> 4];
+      str[2*i + 1] = num2hex[v & 0x0f];
     }
   return str;
 }
 
-ServerEvent::ServerEvent() :
-       type(PINGU_ACTION_EVENT),
-       time_stamp(0),
-       pingu_id(0),
-       pingu_action(Actions::Walker)
+static char hex2int(char c)
 {
+  if (c >= '0' && c <= '9')
+    return c - '0';
+  else if (c >= 'a' && c <= 'f')
+    return c - 'a' + 0xa;
+  else
+    return 0;    
 }
 
+/** Restore the raw bits of a float from a string */
+static float string2float(const std::string& str)
+{
+  assert(str.size() == 2*sizeof(float));
+
+  float value;
+  for(size_t i = 0; i < sizeof(float); ++i)
+    {
+      char& v = reinterpret_cast<char*>(&value)[i];
+      v = (hex2int(str[2*i+0]) << 4) | hex2int(str[2*i+1]);
+    }
+
+  return value;
+}
+
+ServerEvent::ServerEvent() 
+  : type(PINGU_ACTION_EVENT),
+    time_stamp(0),
+    pingu_id(0),
+    pingu_action(Actions::Walker)
+{
+}
+
 ServerEvent::ServerEvent(FileReader reader)
 {
   if (reader.get_name() == "armageddon")
@@ -63,11 +94,18 @@
     }
   else if (reader.get_name() == "pingu-action")
     {
+      std::string raw_x;
+      std::string raw_y;
+
       type = PINGU_ACTION_EVENT;
       reader.read_int ("time",   time_stamp);
       reader.read_int ("id",     pingu_id);
-      reader.read_vector("pos",    pos);
+      reader.read_string("raw-x", raw_x);
+      reader.read_string("raw-y", raw_y);
       reader.read_enum("action", pingu_action, Actions::action_from_string);
+
+      pos.x = string2float(raw_x);
+      pos.y = string2float(raw_y);
     }
   else
     {
@@ -81,25 +119,26 @@
 {
   switch(type)
     {
-    case ARMAGEDDON_EVENT:
-      out << "(armageddon (time " << time_stamp << "))" << std::endl;
-      break;
+      case ARMAGEDDON_EVENT:
+        out << "(armageddon (time " << time_stamp << "))" << std::endl;
+        break;
 
-    case FINISH_EVENT:
-      out << "(finish (time " << time_stamp << "))" << std::endl;
-      break;
+      case FINISH_EVENT:
+        out << "(finish (time " << time_stamp << "))" << std::endl;
+        break;
 
-    case PINGU_ACTION_EVENT:
-      out << "(pingu-action "
-          << "(time " << time_stamp << ") "
-          << "(id " << pingu_id << ") "
-          << "(pos " << pos.x << " " << pos.y << " " << pos.z << ") "
-          << "(action \"" << Actions::action_to_string(pingu_action) << "\"))"
-          << std::endl;
-      break;
+      case PINGU_ACTION_EVENT:
+        out << "(pingu-action "
+            << "(time " << time_stamp << ") "
+            << "(id " << pingu_id << ") "
+            << "(raw-x \"" << float2string(pos.x) << "\") "
+            << "(raw-y \"" << float2string(pos.y) << "\") "
+            << "(action \"" << Actions::action_to_string(pingu_action) << 
"\"))"
+            << std::endl;
+        break;
 
       default:
-      assert(!"Unknown type");
+        assert(!"Unknown type");
     }
 }
 
@@ -147,41 +186,40 @@
 {
   switch(type)
     {
-    case ARMAGEDDON_EVENT:
-      server->send_armageddon_event();
-      break;
+      case ARMAGEDDON_EVENT:
+        server->send_armageddon_event();
+        break;
 
-    case FINISH_EVENT:
-      server->send_finish_event();      
-      break;
+      case FINISH_EVENT:
+        server->send_finish_event();      
+        break;
 
-    case END_EVENT:
-      // do nothing
-      break;
+      case END_EVENT:
+        // do nothing
+        break;
 
-    case PINGU_ACTION_EVENT:
-      {
-       Pingu* pingu = server->get_world()->get_pingus()->get_pingu(pingu_id);
-       if (pingu)
-         {
-            std::cout << "Apply: "
-              //<< float2string(pos.x) << "," << float2string(pos.y) << " == "
-                      << float2string(pingu->get_pos().x) << "," << 
float2string(pingu->get_pos().y) << " "
-              //<< "(" << pos.x << ", " << pos.y << ") == "
-              //        << "(" << pingu->get_pos().x << ", " << 
pingu->get_pos().y << ")"
-                      << std::endl;
-           server->send_pingu_action_event(pingu,
-                                           pingu_action);
-         }
-       else
-         {
-           std::cout << "ServerEvent: DemoFile inconsistent with world, pingu 
" << pingu_id << " missing" << std::endl;
-         }
-      }
-      break;
+      case PINGU_ACTION_EVENT:
+        {
+          Pingu* pingu = 
server->get_world()->get_pingus()->get_pingu(pingu_id);
+          if (pingu)
+            {
+              if (pos.x != pingu->get_pos().x ||
+                  pos.y != pingu->get_pos().y)
+                {
+                  std::cout << "ServerEvent: DemoFile inconsistent with world, 
pingu " << pingu_id << " is at the wrong position" << std::endl;
+                }
 
-    default:
-      assert(!"Unknown type");
+              server->send_pingu_action_event(pingu, pingu_action);
+            }
+          else
+            {
+              std::cout << "ServerEvent: DemoFile inconsistent with world, 
pingu " << pingu_id << " missing" << std::endl;
+            }
+        }
+        break;
+
+      default:
+        assert(!"Unknown type");
     }
 }
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]