Problem Load and Save State

marcogavazzeni

Addon Developer
Addon Developer
Joined
Jan 5, 2009
Messages
219
Reaction score
0
Points
16
Location
Near Verona
Website
orbiteritalia.forumotion.com
Hi guys
I have a problem with saving the state of the scenario.
I have a door that I would like to open and close, and save the state in the scenario, but every time I start the scenario the door is always closed.
What's wrong?
Code:
// --------------------------------------------------------------
// Read status from scenario file
// --------------------------------------------------------------
void ShuttlePB::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{
 char *line;
 
 while (oapiReadScenario_nextline (scn, line)) {
        if (!strnicmp (line, "HATCH1", 4)) {
   sscanf (line+4, "%d%lf", &hatch1_status, &hatch1_proc);
  } 
 else {
            ParseScenarioLineEx (line, vs);
   // unrecognised option - pass to Orbiter's generic parser
 }
}
}
// --------------------------------------------------------------
// Write status to scenario file
// --------------------------------------------------------------
void ShuttlePB::clbkSaveState (FILEHANDLE scn)
{
 char cbuf[256];
 // Write default vessel parameters
 VESSEL2::clbkSaveState (scn);
 
 if (hatch1_status) {
  sprintf (cbuf, "%d %0.4f", hatch1_status, hatch1_proc);
  oapiWriteScenario_string (scn, "HATCH1", cbuf);
    }  
}
// --------------------------------------------------------------
// Respond to playback event
// --------------------------------------------------------------
bool ShuttlePB::clbkPlaybackEvent (double simt, double event_t, const char *event_type, const char *event)
{
 if (!stricmp (event_type, "HATCH1")) {
  ActivateHatch1 (!stricmp (event, "OPEN") ? DOOR_CLOSING : DOOR_OPENING);
  return true;
  }
 return false;
}
void ShuttlePB::clbkSetStateEx(const void *status)
{
 DefSetStateEx(status);
}
 void ShuttlePB::clbkPostCreation()
{
  //Set animation states
 
  SetAnimation (anim_hatch1, hatch1_proc);
   
}
I thank all those who can help :tiphat:
 

dbeachy1

O-F Administrator
Administrator
Orbiter Contributor
Addon Developer
Donator
Beta Tester
Joined
Jan 14, 2008
Messages
9,214
Reaction score
1,560
Points
203
Location
VA
Website
alteaaerospace.com
Preferred Pronouns
he/him
The problem is that clbkLoadStateEx is parsing the line incorrectly. :) The fix is shown below in red:

Code:
// --------------------------------------------------------------
// Read status from scenario file
// --------------------------------------------------------------
void ShuttlePB::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{
    char *line;

    while (oapiReadScenario_nextline (scn, line)) {
        if (!strnicmp (line, "HATCH1", [COLOR="Red"]6[/COLOR])) {  [COLOR="red"]// use correct length there, although parsing still succeeded just on "HATC" before[/COLOR]
            [COLOR="red"]// must use 'line+6' here to skip over the "HATCH1" and parse the numbers that come after it[/COLOR]
            sscanf (line+[COLOR="red"]6[/COLOR], "%d%lf", &hatch1_status, &hatch1_proc);
        } 
        else {
            ParseScenarioLineEx (line, vs);
            // unrecognised option - pass to Orbiter's generic parser
        }
    }
}

Previously it was trying to parse the int and float at index 4, so it always tried to parse "H1" as the numbers (which would fail), and so hatch1_status and hatch1_proc would remain unchanged by the scenario load.
 

marcogavazzeni

Addon Developer
Addon Developer
Joined
Jan 5, 2009
Messages
219
Reaction score
0
Points
16
Location
Near Verona
Website
orbiteritalia.forumotion.com
Thank you very much
Now saves correctly
Adding more moving parts but are still at the starting point.
What I wrong?:idk:

Code:
#define STRICT
#define ORBITER_MODULE
#include "orbitersdk.h"
#include "ssi.h"
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <fstream.h>
 
// ==============================================================
// Some vessel parameters
// ==============================================================
const double PB_FUELMASS = 750.0;
const double PB_ISP = 5e4;
const double PB_MAXMAINTH = 3e4;
const double PB_MAXHOVERTH = 1.5e4;
const double PB_MAXRCSTH = 2e2;
//const VECTOR3 PB_DOCK_POS   = {0,1.3,-1};      // docking port location [m]
//const VECTOR3 PB_DOCK_DIR   = {0,1,0};         // docking port approach direction
//const VECTOR3 PB_DOCK_ROT   = {0,0,-1};        // docking port alignment direction
 
// ==============================================================
// Shuttle-PB class interface
// ==============================================================
 
 ShuttlePB::ShuttlePB (OBJHANDLE hObj, int fmodel)
: VESSEL2 (hObj, flightmodel)
{
 hatch1_status       = DOOR_CLOSED;
 hatch1_proc         = 0.0; 
 hatch2_status       = DOOR_CLOSED;
 hatch2_proc         = 0.0;
 DefineAnimations ();
 f151   = oapiLoadMeshGlobal ("supportovitale");
 f152   = oapiLoadMeshGlobal ("RoomSV");
 
 int i;
};
//Destructor
ShuttlePB::~ShuttlePB ()
{
 
}
// ==============================================================
// Overloaded callback functions
// ==============================================================
// --------------------------------------------------------------
// Set the capabilities of the vessel class
// --------------------------------------------------------------
void ShuttlePB::clbkSaveState (FILEHANDLE scn)
{
 char cbuf[256];
 int i;
 // Write default vessel parameters
 VESSEL2::clbkSaveState (scn);
 
 if (hatch1_status) {
  sprintf (cbuf, "%d %0.4f", hatch1_status, hatch1_proc);
  oapiWriteScenario_string (scn, "HATCH1", cbuf);
    }  
    if (hatch2_status) { 
  sprintf (cbuf, "%d %0.4f", hatch2_status, hatch2_proc);
  oapiWriteScenario_string (scn, "HATCH2", cbuf);
 } 
}
void ShuttlePB::clbkLoadStateEx (FILEHANDLE scn, void *vs)
{
 char *line;
 
 while (oapiReadScenario_nextline (scn, line)) {
        if (!strnicmp (line, "HATCH1", 6)) {
   sscanf (line+4, "%d%lf", &hatch1_status, &hatch1_proc);
  } else if (!strnicmp (line, "HATCH2", 6)) {
   sscanf (line+5, "%d%lf", &hatch2_status, &hatch2_proc);
  } 
 else {
            ParseScenarioLineEx (line, vs);
   // unrecognised option - pass to Orbiter's generic parser
 }
}
}
void ShuttlePB::clbkPostCreation()
{
 
 
  SetAnimation (anim_hatch1, hatch1_proc);
 
  SetAnimation (anim_hatch2, hatch2_proc);
 
} 
void ShuttlePB::clbkSetClassCaps (FILEHANDLE cfg)
{
 THRUSTER_HANDLE th_main, th_hover, th_rcs[14], th_group[4];
 // physical specs
 SetSize (8);
 SetEmptyMass (21460);
 SetCW (0.3, 0.3, 0.6, 0.9);
// SetWingAspect (0.7);
// SetWingEffectiveness (2.5);
 SetCrossSections (_V(10.5,15.0,5.8));
 SetRotDrag (_V(0.6,0.6,0.35));
 SetPMI (_V(2.28,2.31,0.79));
// SetTrimScale (0.05);
 SetCameraOffset (_V(0,0.350,6.05));
// SetLiftCoeffFunc (LiftCoeff);
// SetTouchdownPoints (_V(0,-1.5,2), _V(-1,-1.5,-1.5), _V(1,-1.5,-1.5));
    Dock0 = CreateDock(_V(5.0,0.0,0.0), _V(0,0,-1), _V(0,1,0));
    Dock1 = CreateDock(_V(0.0,0.434,-6.83), _V(0,0,-1), _V(0,-1,0));
    Dock2 = CreateDock(_V(0.0,0.434,6.83), _V(0,0,1), _V(0,-1,0));
 // propellant resources
 
 // visual specs
 AddMesh (f151);
// SetMeshVisibilityMode( AddMesh (f151,&offset), MESHVIS_EXTERNAL |MESHVIS_VC);
 SetMeshVisibilityMode( AddMesh (f152,&offsetvc), MESHVIS_VC );
 
 
}
void ShuttlePB::clbkPostStep (double simt, double SimDT, double mjd)
{
 // animate hatch1
 if (hatch1_status >= DOOR_CLOSING) {
  double da = oapiGetSimStep() * HATCH1_OPERATING_SPEED;
  if (hatch1_status == DOOR_CLOSING) {
   if (hatch1_proc > 0.0)
    hatch1_proc = max (0.0, hatch1_proc-da);
   else {
    hatch1_status = DOOR_CLOSED;
    }
  } else  { // door opening
   if (hatch1_proc < 1.0)
    hatch1_proc = min (1.0, hatch1_proc+da);
   else {
    hatch1_status = DOOR_OPEN;
   }
  }
  SetAnimation (anim_hatch1, hatch1_proc);
 }
 // animate hatch2
 if (hatch2_status >= DOOR_CLOSING) {
  double da = oapiGetSimStep() * HATCH2_OPERATING_SPEED;
  if (hatch2_status == DOOR_CLOSING) {
   if (hatch2_proc > 0.0)
    hatch2_proc = max (0.0, hatch2_proc-da);
   else {
    hatch2_status = DOOR_CLOSED;
    }
  } else  { // door opening
   if (hatch2_proc < 1.0)
    hatch2_proc = min (1.0, hatch2_proc+da);
   else {
    hatch2_status = DOOR_OPEN;
   }
  }
  SetAnimation (anim_hatch2, hatch2_proc);
 }
}
 
//==========================================================================================
void ShuttlePB::DefineAnimations ()
{
   static UINT hatch1[1] = {4};                //  rot.point      rot.axis        angle
   static MGROUP_ROTATE htch1 (0,hatch1, 1, _V(0.0,0.98,5.33), _V(1,0,0), (float)(105*RAD));
 
   anim_hatch1 = CreateAnimation (0.0);
   AddAnimationComponent  (anim_hatch1, 0, 1,&htch1);
 
   static UINT hatch2[1] = {5};
   static MGROUP_ROTATE htch2 (0,hatch2,1, _V(0.0,1.2,-6.1), _V(1,0,0), (float)(-105*RAD));
 
   anim_hatch2 = CreateAnimation (0.0);
   AddAnimationComponent  (anim_hatch2, 0, 1,&htch2);
  } 
 //=========================================================================================== 
 void ShuttlePB::ActivateHatch1 (DoorStatus action)
{
 hatch1_status = action;
}
void ShuttlePB::RevertHatch1 ()
{
 ActivateHatch1 (hatch1_status == DOOR_CLOSED || hatch1_status == DOOR_CLOSING ?
               DOOR_OPENING : DOOR_CLOSING);
}
//===========================================================================================
void ShuttlePB::ActivateHatch2 (DoorStatus action)
{
 hatch2_status = action;
}
void ShuttlePB::RevertHatch2 ()
{
 ActivateHatch2 (hatch2_status == DOOR_CLOSED || hatch2_status == DOOR_CLOSING ?
               DOOR_OPENING : DOOR_CLOSING);
}
//==========================================================================================
    int ShuttlePB::clbkConsumeBufferedKey(DWORD key, bool down, char *kstate)
{
 
 if (!down) return 0;
 switch (key) {
 
  case OAPI_KEY_K:  // operate hatch1
    RevertHatch1();
  return 1;
 
  case OAPI_KEY_G:  // operate hatch1
    RevertHatch2();
  return 1; 
 
  }
return 0;
  }
 
bool ShuttlePB::clbkLoadVC (int id)
{
 SetCameraOffset (_V(0,0.350,6.05));
 SetCameraDefaultDirection (_V(0,0,1));
    SetCameraRotationRange (RAD*90, RAD*90, RAD*90, RAD*90);
    static VCMFDSPEC mfds_left = {1, 2};
    static VCMFDSPEC mfds_right = {1, 1};
    oapiVCRegisterMFD (MFD_LEFT, &mfds_left);
    oapiVCRegisterMFD (MFD_RIGHT, &mfds_right);
 static VCHUDSPEC hud_pilot  = {1, 0,{0,0.350,6.50},0.5};
 oapiVCRegisterHUD (&hud_pilot);
 return true;
}
// ==============================================================
// API callback interface
// ==============================================================
// --------------------------------------------------------------
// Vessel initialisation
// --------------------------------------------------------------
DLLCLBK VESSEL *ovcInit (OBJHANDLE hvessel, int flightmodel)
{
 return new ShuttlePB (hvessel, flightmodel);
}
// --------------------------------------------------------------
// Vessel cleanup
// --------------------------------------------------------------
DLLCLBK void ovcExit (VESSEL *vessel)
{
 if (vessel) delete (ShuttlePB*)vessel;
 

Xyon

Puts the Fun in Dysfunctional
Administrator
Moderator
Orbiter Contributor
Addon Developer
Webmaster
GFX Staff
Beta Tester
Joined
Aug 9, 2009
Messages
6,922
Reaction score
788
Points
203
Location
10.0.0.1
Website
www.orbiter-radio.co.uk
Preferred Pronouns
she/her
I think it's the same issue again.

Code:
while (oapiReadScenario_nextline (scn, line)) {
        if (!strnicmp (line, "HATCH1", 6)) {
   sscanf (line+[color="red"]4[/color], "%d%lf", &hatch1_status, &hatch1_proc);
  } else if (!strnicmp (line, "HATCH2", 6)) {
   sscanf (line+[color="red"]5[/color], "%d%lf", &hatch2_status, &hatch2_proc);
  }

Both of these red numbers should indicate the end of your "line" string, which is 6 characters long, so they should be "6", not 4 and 5.
 
Top