]> git.mxchange.org Git - flightgear.git/blobdiff - src/MultiPlayer/mpplayer.cxx
Portability fix - use SimGear when available
[flightgear.git] / src / MultiPlayer / mpplayer.cxx
index 00374ae219b713bec01f69d991320acd927fcfdb..0ea86cad36710ac883fbbd67ca23db9d104d0036 100644 (file)
@@ -46,8 +46,9 @@
 *
 ******************************************************************/
 
-#include "mpplayer.hxx"
+#include <simgear/timing/timestamp.hxx>
 
+#include "mpplayer.hxx"
 
 #include <stdlib.h>
 #if !(defined(_MSC_VER) || defined(__MINGW32__))
@@ -193,15 +194,13 @@ MPPlayer::Close(void)
 bool MPPlayer::CheckTime(int time, int timeusec)
 {
     double curOffset;
-    
-    // set the offset
-    struct timeval tv;
     int toff, utoff;
-    gettimeofday(&tv, NULL);
+    
+    SGTimeStamp now;
     
     // calculate the offset
-    toff = ((int) tv.tv_sec) - time;
-    utoff = ((int) tv.tv_usec) - timeusec;
+    toff = ((int) now.get_seconds()) - time;
+    utoff = ((int) now.get_usec()) - timeusec;
     while (utoff < 0) {
         toff--;
         utoff += 1000000;
@@ -254,11 +253,9 @@ MPPlayer::SetPosition
         const double left_aileron, const double right_aileron, const double elevator, const double rudder,
         //const double rpms[6],
         const double rateH, const double rateR, const double rateP,
-               const double accN, const double accE, const double accD
+        const double accN, const double accE, const double accD
     )
 {
-    int toff, utoff;
-    
     // Save the position matrix and update time
     if (m_Initialised)
     {
@@ -283,13 +280,13 @@ MPPlayer::SetPosition
         m_speedN = speedN;
         m_speedE = speedE;
         m_speedD = speedD;
-               m_accN = accN;
-               m_accE = accE;
-               m_accD = accD;
+        m_accN = accN;
+        m_accE = accE;
+        m_accD = accD;
         m_left_aileron = left_aileron;
-               m_right_aileron = right_aileron;
+        m_right_aileron = right_aileron;
         m_elevator = elevator;
-               m_rudder = rudder;
+        m_rudder = rudder;
 
         /*for (int i = 0; i < 6; i++) {
             m_rpms[i] = rpms[i];
@@ -318,7 +315,7 @@ MPPlayer::SetPosition
             // set properties
             SGPropertyNode *root = m_AIModel->getProps();
             root->getNode("surface-positions/left-aileron-pos-norm", true)->setDoubleValue(m_left_aileron);
-                       root->getNode("surface-positions/right-aileron-pos-norm", true)->setDoubleValue(m_right_aileron);
+            root->getNode("surface-positions/right-aileron-pos-norm", true)->setDoubleValue(m_right_aileron);
             root->getNode("surface-positions/elevator-pos-norm", true)->setDoubleValue(m_elevator);
             root->getNode("surface-positions/rudder-pos-norm", true)->setDoubleValue(m_rudder);
             /*root->getNode("engines/engine/rpm", true)->setDoubleValue(m_rpms[0]);
@@ -331,10 +328,10 @@ MPPlayer::SetPosition
             // Adjust by the last offset
             //cout << "OFFSET: " << (m_LastOffset - m_TimeOffset) << endl;
             
-                       //m_AIModel->timewarp(m_LastOffset - m_TimeOffset);
+            //m_AIModel->timewarp(m_LastOffset - m_TimeOffset);
 
-                       // set the timestamp for the data update (sim elapsed time (secs))
-                       m_AIModel->setTimeStamp();
+            // set the timestamp for the data update (sim elapsed time (secs))
+            m_AIModel->setTimeStamp();
         }
         
         time(&m_LastUpdate);
@@ -358,7 +355,7 @@ void MPPlayer::SetProperty(string property, SGPropertyNode::Type type, double va
     // set the property
     switch (type) {
         case 2:
-            node->setBoolValue((bool) val);
+            node->setBoolValue( val != 0.0 );
             break;
         case 3:
             node->setIntValue((int) val);
@@ -463,13 +460,6 @@ MPPlayer::CompareCallsign(const char *Callsign) const
 void
 MPPlayer::LoadAI(void)
 {
-    // set up the model info
-    FGAIModelEntity aiModel;
-    aiModel.m_type = "aircraft";
-    aiModel.path = m_ModelName;
-    aiModel.acType = "Multiplayer";
-    aiModel.company = m_Callsign;
-   
     // then get the model manager
     FGAIManager *aiModelMgr = (FGAIManager *) globals->get_subsystem("ai_model");
     if (!aiModelMgr) {
@@ -480,7 +470,11 @@ MPPlayer::LoadAI(void)
     
     // then get the model
     fgSetBool("/sim/freeze/clock", true);
-    m_AIModel = (FGAIMultiplayer *) aiModelMgr->createMultiplayer(&aiModel);
+    m_AIModel = new FGAIMultiplayer;
+    m_AIModel->setAcType("Multiplayer");
+    m_AIModel->setCompany(m_Callsign);
+    m_AIModel->setPath(m_ModelName.c_str());
+    aiModelMgr->attach(m_AIModel);
     fgSetBool("/sim/freeze/clock", false);
 }
 
@@ -495,14 +489,13 @@ MPPlayer::FillPosMsg
     T_PositionMsg *PosMsg
     )
 {
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    
+    SGTimeStamp now;
+
     FillMsgHdr(MsgHdr, POS_DATA_ID);
     strncpy(PosMsg->Model, m_ModelName.c_str(), MAX_MODEL_NAME_LEN);
     PosMsg->Model[MAX_MODEL_NAME_LEN - 1] = '\0';
-    PosMsg->time = XDR_encode_uint32 (tv.tv_sec);
-    PosMsg->timeusec = XDR_encode_uint32 (tv.tv_usec);
+    PosMsg->time = XDR_encode_uint32 (now.get_seconds());
+    PosMsg->timeusec = XDR_encode_uint32 (now.get_usec());
     PosMsg->lat = XDR_encode_double (m_lat);
     PosMsg->lon = XDR_encode_double (m_lon);
     PosMsg->alt = XDR_encode_double (m_alt);
@@ -522,7 +515,7 @@ MPPlayer::FillPosMsg
     PosMsg->rateH =  XDR_encode_float ((float) m_rateH);
     PosMsg->rateR =  XDR_encode_float ((float) m_rateR);
     PosMsg->rateP =  XDR_encode_float ((float) m_rateP);
-       PosMsg->accN  =  XDR_encode_float ((float) m_accN);
+    PosMsg->accN  =  XDR_encode_float ((float) m_accN);
     PosMsg->accE =  XDR_encode_float ((float) m_accE);
     PosMsg->accD =  XDR_encode_float ((float) m_accD);
 }