Author Topic: Talon1024 writes his first SEXP  (Read 2932 times)

0 Members and 1 Guest are viewing this topic.

Offline Talon 1024

  • 29
  • How do you turn this on?
    • Mods, Games, and Stuff
Talon1024 writes his first SEXP
So, I wanted to port over a couple of the SEXPs that WCSaga added to their fork of FSO, so I've looked at The E's tutorial for writing SEXPs, and after a couple of hours, I came up with this.

Code: [Select]
Index: code/autopilot/autopilot.cpp
===================================================================
--- code/autopilot/autopilot.cpp (revision 9223)
+++ code/autopilot/autopilot.cpp (working copy)
@@ -1344,6 +1344,17 @@
 }
 
 // ********************************************************************************************
+// Selects a navpoint by name.
+void SelectNav(char *Nav)
+{
+ for (int i = 0; i < MAX_NAVPOINTS; i++)
+ {
+ if (!stricmp(Navs[i].m_NavName, Nav))
+ CurrentNav = i; //Talon1024: What I've wanted to do here is check to see if a navpoint was hidden or restricted, then select it if it was not.  However, I don't know how to do that, as I've never worked with flags in C++ before, so can someone kindly help me?
+ }
+}
+
+// ********************************************************************************************
 // Set A Nav point to "ZERO"
 void ZeroNav(int i)
 {
Index: code/autopilot/autopilot.h
===================================================================
--- code/autopilot/autopilot.h (revision 9223)
+++ code/autopilot/autopilot.h (working copy)
@@ -112,6 +112,9 @@
 // Finds a Nav point by name
 int FindNav(char *Nav);
 
+// Selects a Nav point by name
+void SelectNav(char *Nav);
+
 // Set A Nav point to "ZERO"
 void ZeroNav(int i);
 
Index: code/parse/sexp.cpp
===================================================================
--- code/parse/sexp.cpp (revision 9223)
+++ code/parse/sexp.cpp (working copy)
@@ -583,6 +583,7 @@
  { "is-nav-linked", OP_NAV_ISLINKED, 1, 1 }, //kazan
  { "use-nav-cinematics", OP_NAV_USECINEMATICS, 1, 1 }, //kazan
  { "use-autopilot", OP_NAV_USEAP, 1, 1 }, //kazan
+ { "select-nav", OP_NAV_SELECT, 1, 1 }, //Talon1024
 
  //Cutscene Sub-Category
  { "set-cutscene-bars", OP_CUTSCENES_SET_CUTSCENE_BARS, 0, 1, },
@@ -17951,7 +17952,13 @@
  return DistanceTo(nav_name);
 }
 
+void select_nav(int node)
+{
+ char *nav_name = CTEXT(node);
+ SelectNav(nav_name);
+}
 
+
 //*************************************************************************************************
 
 
@@ -22613,6 +22620,11 @@
  sexp_val = SEXP_TRUE;
  set_use_ap_cinematics(node);
  break;
+
+ case OP_NAV_SELECT:
+ sexp_val = SEXP_TRUE;
+ select_nav(node);
+ break;
 
  case OP_SCRAMBLE_MESSAGES:
  case OP_UNSCRAMBLE_MESSAGES:
@@ -23660,6 +23672,7 @@
  case OP_NAV_UNSET_NEEDSLINK:
  case OP_NAV_USECINEMATICS:
  case OP_NAV_USEAP:
+ case OP_NAV_SELECT:
  case OP_HUD_SET_TEXT:
  case OP_HUD_SET_TEXT_NUM:
  case OP_HUD_SET_MESSAGE:
@@ -25482,6 +25495,7 @@
  case OP_NAV_UNRESTRICT: //kazan
  case OP_NAV_SET_VISITED: //kazan
  case OP_NAV_UNSET_VISITED: //kazan
+ case OP_NAV_SELECT: //Talon1024
  return OPF_STRING;
 
  case OP_NAV_SET_CARRY: //kazan
@@ -27223,6 +27237,7 @@
  case OP_NAV_UNSET_NEEDSLINK:
  case OP_NAV_USECINEMATICS:
  case OP_NAV_USEAP:
+ case OP_NAV_SELECT:
  return CHANGE_SUBCATEGORY_NAV;
 
 
@@ -27496,6 +27511,8 @@
 
  { OP_NAV_USEAP, "Takes 1 boolean argument.\r\n"
  "Set to true to enable autopilot, set to false to disable autopilot." },
+
+ { OP_NAV_SELECT, "Takes 1 argument: NavPoint Name, and then selects it\r\n" },
 
  // -------------------------- -------------------------- --------------------------
 
Index: code/parse/sexp.h
===================================================================
--- code/parse/sexp.h (revision 9223)
+++ code/parse/sexp.h (working copy)
@@ -701,8 +701,8 @@
 #define OP_DESTROY_SUBSYS_INSTANTLY (0x0016 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Admiral MS
 #define OP_DEBUG (0x0017 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Karajorma
 #define OP_SET_MISSION_MOOD (0x0018 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Karajorma
+#define OP_NAV_SELECT (0x0019 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Talon1024
 
-
 // defined for AI goals
 #define OP_AI_CHASE (0x0000 | OP_CATEGORY_AI | OP_NONCAMPAIGN_FLAG)
 #define OP_AI_DOCK (0x0001 | OP_CATEGORY_AI | OP_NONCAMPAIGN_FLAG)

Obviously, I'm a newbie who hasn't written much C++ code before, so I wasn't able to do what I wanted to do (check to see if the navpoint was hidden or restricted before selecting it), so can someone please help me here?
To understand religion, you need to understand morality first. | WCSaga website | WCSaga Forum | 158th website | 158th forum | Project Leader: WC: Hostile Frontier | WCHF Thread at CIC | Wing Blender | Twist of Fate | Multipart turrets on angled surfaces, tutorial included. | My Google Drive stuff | To convert speeds from WC to WCS, multiply both the cruise speed and the Afterburner speed by 0.15625 (5/32)

FS2 Mods I'm waiting on: Inferno 10th Anniversary
Current Project: Contestant Android app, Learn4Life iOS app, Blender Commander (importer).
The FreeSpace Font Foundry is back in action!

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Talon1024 writes his first SEXP
I can't check at the moment, but the best thing to do would be to go to Sexp_hide_navpoint and see what it is setting. Then you know exactly what you're meant to be looking for.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Talon 1024

  • 29
  • How do you turn this on?
    • Mods, Games, and Stuff
Re: Talon1024 writes his first SEXP
I figured it out :)

The flags being set by nav-hide and nav-restrict are NP_HIDDEN and NP_NOACCESS respectively.

Also, thanks to a helpful topic on the C++ forums, I was able to find out how to work with bit flags.

Here's a new patch file.  I've also added the deselect-nav SEXP.

Code: [Select]
Index: code/autopilot/autopilot.cpp
===================================================================
--- code/autopilot/autopilot.cpp (revision 9223)
+++ code/autopilot/autopilot.cpp (working copy)
@@ -1570,7 +1570,23 @@
  return Nav_UnSet_Flag(Nav, NP_VISITED);
 }
 
+// ********************************************************************************************
+// Selects a navpoint by name.
+void SelectNav(char *Nav)
+{
+ for (int i = 0; i < MAX_NAVPOINTS; i++)
+ {
+ if (!stricmp(Navs[i].m_NavName, Nav))
+ if (!(Nav_Get_Flags(Nav) & NP_HIDDEN || Nav_Get_Flags(Nav) & NP_NOACCESS)) CurrentNav = i;
+ }
+}
 
+// ********************************************************************************************
+// Deselects any navpoint selected.
+void DeselectNav()
+{
+  CurrentNav = 0;
+}
 
 //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
Index: code/autopilot/autopilot.h
===================================================================
--- code/autopilot/autopilot.h (revision 9223)
+++ code/autopilot/autopilot.h (working copy)
@@ -112,6 +112,12 @@
 // Finds a Nav point by name
 int FindNav(char *Nav);
 
+// Selects a Nav point by name
+void SelectNav(char *Nav);
+
+// Deselects any navpoint selected.
+void DeselectNav();
+
 // Set A Nav point to "ZERO"
 void ZeroNav(int i);
 
Index: code/parse/sexp.cpp
===================================================================
--- code/parse/sexp.cpp (revision 9223)
+++ code/parse/sexp.cpp (working copy)
@@ -583,6 +583,8 @@
  { "is-nav-linked", OP_NAV_ISLINKED, 1, 1 }, //kazan
  { "use-nav-cinematics", OP_NAV_USECINEMATICS, 1, 1 }, //kazan
  { "use-autopilot", OP_NAV_USEAP, 1, 1 }, //kazan
+ { "select-nav", OP_NAV_SELECT, 1, 1 }, //Talon1024
+ { "deselect-nav", OP_NAV_DESELECT, 0, 0 }, //Talon1024
 
  //Cutscene Sub-Category
  { "set-cutscene-bars", OP_CUTSCENES_SET_CUTSCENE_BARS, 0, 1, },
@@ -17951,7 +17953,18 @@
  return DistanceTo(nav_name);
 }
 
+void select_nav(int node)
+{
+ char *nav_name = CTEXT(node);
+ SelectNav(nav_name);
+}
 
+void deselect_nav()
+{
+ DeselectNav();
+}
+
+
 //*************************************************************************************************
 
 
@@ -22613,6 +22626,18 @@
  sexp_val = SEXP_TRUE;
  set_use_ap_cinematics(node);
  break;
+
+ //Talon1024
+ case OP_NAV_SELECT:
+ sexp_val = SEXP_TRUE;
+ select_nav(node);
+ break;
+
+ //Talon1024
+ case OP_NAV_DESELECT:
+ sexp_val = SEXP_TRUE;
+ deselect_nav();
+ break;
 
  case OP_SCRAMBLE_MESSAGES:
  case OP_UNSCRAMBLE_MESSAGES:
@@ -23660,6 +23685,8 @@
  case OP_NAV_UNSET_NEEDSLINK:
  case OP_NAV_USECINEMATICS:
  case OP_NAV_USEAP:
+ case OP_NAV_SELECT:
+ case OP_NAV_DESELECT:
  case OP_HUD_SET_TEXT:
  case OP_HUD_SET_TEXT_NUM:
  case OP_HUD_SET_MESSAGE:
@@ -23845,6 +23872,7 @@
  case OP_VALIDATE_ALL_ARGUMENTS:
  case OP_NUM_VALID_ARGUMENTS:
  case OP_SUPERNOVA_STOP:
+ case OP_NAV_DESELECT:
  return OPF_NONE;
 
  case OP_AND:
@@ -25482,6 +25510,7 @@
  case OP_NAV_UNRESTRICT: //kazan
  case OP_NAV_SET_VISITED: //kazan
  case OP_NAV_UNSET_VISITED: //kazan
+ case OP_NAV_SELECT: //Talon1024
  return OPF_STRING;
 
  case OP_NAV_SET_CARRY: //kazan
@@ -27223,6 +27252,8 @@
  case OP_NAV_UNSET_NEEDSLINK:
  case OP_NAV_USECINEMATICS:
  case OP_NAV_USEAP:
+ case OP_NAV_SELECT:
+ case OP_NAV_DESELECT:
  return CHANGE_SUBCATEGORY_NAV;
 
 
@@ -27496,6 +27527,10 @@
 
  { OP_NAV_USEAP, "Takes 1 boolean argument.\r\n"
  "Set to true to enable autopilot, set to false to disable autopilot." },
+
+ { OP_NAV_SELECT, "Takes 1 argument: NavPoint Name, and then selects it\r\n" },
+
+ { OP_NAV_DESELECT, "Takes no arguments.  Deselects any navpoint selected\r\n" },
 
  // -------------------------- -------------------------- --------------------------
 
Index: code/parse/sexp.h
===================================================================
--- code/parse/sexp.h (revision 9223)
+++ code/parse/sexp.h (working copy)
@@ -701,8 +701,9 @@
 #define OP_DESTROY_SUBSYS_INSTANTLY (0x0016 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Admiral MS
 #define OP_DEBUG (0x0017 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Karajorma
 #define OP_SET_MISSION_MOOD (0x0018 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Karajorma
+#define OP_NAV_SELECT (0x0019 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Talon1024
+#define OP_NAV_DESELECT (0x0020 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Talon1024
 
-
 // defined for AI goals
 #define OP_AI_CHASE (0x0000 | OP_CATEGORY_AI | OP_NONCAMPAIGN_FLAG)
 #define OP_AI_DOCK (0x0001 | OP_CATEGORY_AI | OP_NONCAMPAIGN_FLAG)

[attachment removed and sold on the black market]
To understand religion, you need to understand morality first. | WCSaga website | WCSaga Forum | 158th website | 158th forum | Project Leader: WC: Hostile Frontier | WCHF Thread at CIC | Wing Blender | Twist of Fate | Multipart turrets on angled surfaces, tutorial included. | My Google Drive stuff | To convert speeds from WC to WCS, multiply both the cruise speed and the Afterburner speed by 0.15625 (5/32)

FS2 Mods I'm waiting on: Inferno 10th Anniversary
Current Project: Contestant Android app, Learn4Life iOS app, Blender Commander (importer).
The FreeSpace Font Foundry is back in action!

 

Offline Talon 1024

  • 29
  • How do you turn this on?
    • Mods, Games, and Stuff
Re: Talon1024 writes his first SEXP
One last change...

Code: [Select]
Index: code/autopilot/autopilot.cpp
===================================================================
--- code/autopilot/autopilot.cpp (revision 9223)
+++ code/autopilot/autopilot.cpp (working copy)
@@ -1570,7 +1570,20 @@
  return Nav_UnSet_Flag(Nav, NP_VISITED);
 }
 
+// ********************************************************************************************
+// Selects a navpoint by name.
+void SelectNav(char *Nav)
+{
+ if(!(Nav_Get_Flags(Nav) & NP_HIDDEN || Nav_Get_Flags(Nav) & NP_NOACCESS))
+ CurrentNav = FindNav(Nav);
+}
 
+// ********************************************************************************************
+// Deselects any navpoint selected.
+void DeselectNav()
+{
+  CurrentNav = 0;
+}
 
 //+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
Index: code/autopilot/autopilot.h
===================================================================
--- code/autopilot/autopilot.h (revision 9223)
+++ code/autopilot/autopilot.h (working copy)
@@ -112,6 +112,12 @@
 // Finds a Nav point by name
 int FindNav(char *Nav);
 
+// Selects a Nav point by name
+void SelectNav(char *Nav);
+
+// Deselects any navpoint selected.
+void DeselectNav();
+
 // Set A Nav point to "ZERO"
 void ZeroNav(int i);
 
Index: code/parse/sexp.cpp
===================================================================
--- code/parse/sexp.cpp (revision 9223)
+++ code/parse/sexp.cpp (working copy)
@@ -583,6 +583,8 @@
  { "is-nav-linked", OP_NAV_ISLINKED, 1, 1 }, //kazan
  { "use-nav-cinematics", OP_NAV_USECINEMATICS, 1, 1 }, //kazan
  { "use-autopilot", OP_NAV_USEAP, 1, 1 }, //kazan
+ { "select-nav", OP_NAV_SELECT, 1, 1 }, //Talon1024
+ { "unselect-nav", OP_NAV_DESELECT, 0, 0 }, //Talon1024
 
  //Cutscene Sub-Category
  { "set-cutscene-bars", OP_CUTSCENES_SET_CUTSCENE_BARS, 0, 1, },
@@ -17951,7 +17953,18 @@
  return DistanceTo(nav_name);
 }
 
+void select_nav(int node)
+{
+ char *nav_name = CTEXT(node);
+ SelectNav(nav_name);
+}
 
+void deselect_nav()
+{
+ DeselectNav();
+}
+
+
 //*************************************************************************************************
 
 
@@ -22613,6 +22626,18 @@
  sexp_val = SEXP_TRUE;
  set_use_ap_cinematics(node);
  break;
+
+ //Talon1024
+ case OP_NAV_SELECT:
+ sexp_val = SEXP_TRUE;
+ select_nav(node);
+ break;
+
+ //Talon1024
+ case OP_NAV_DESELECT:
+ sexp_val = SEXP_TRUE;
+ deselect_nav();
+ break;
 
  case OP_SCRAMBLE_MESSAGES:
  case OP_UNSCRAMBLE_MESSAGES:
@@ -23660,6 +23685,8 @@
  case OP_NAV_UNSET_NEEDSLINK:
  case OP_NAV_USECINEMATICS:
  case OP_NAV_USEAP:
+ case OP_NAV_SELECT:
+ case OP_NAV_DESELECT:
  case OP_HUD_SET_TEXT:
  case OP_HUD_SET_TEXT_NUM:
  case OP_HUD_SET_MESSAGE:
@@ -23845,6 +23872,7 @@
  case OP_VALIDATE_ALL_ARGUMENTS:
  case OP_NUM_VALID_ARGUMENTS:
  case OP_SUPERNOVA_STOP:
+ case OP_NAV_DESELECT:
  return OPF_NONE;
 
  case OP_AND:
@@ -25482,6 +25510,7 @@
  case OP_NAV_UNRESTRICT: //kazan
  case OP_NAV_SET_VISITED: //kazan
  case OP_NAV_UNSET_VISITED: //kazan
+ case OP_NAV_SELECT: //Talon1024
  return OPF_STRING;
 
  case OP_NAV_SET_CARRY: //kazan
@@ -27223,6 +27252,8 @@
  case OP_NAV_UNSET_NEEDSLINK:
  case OP_NAV_USECINEMATICS:
  case OP_NAV_USEAP:
+ case OP_NAV_SELECT:
+ case OP_NAV_DESELECT:
  return CHANGE_SUBCATEGORY_NAV;
 
 
@@ -27496,6 +27527,10 @@
 
  { OP_NAV_USEAP, "Takes 1 boolean argument.\r\n"
  "Set to true to enable autopilot, set to false to disable autopilot." },
+
+ { OP_NAV_SELECT, "Takes 1 argument: NavPoint Name, and then selects it\r\n" },
+
+ { OP_NAV_DESELECT, "Takes no arguments.  Deselects any navpoint selected\r\n" },
 
  // -------------------------- -------------------------- --------------------------
 
Index: code/parse/sexp.h
===================================================================
--- code/parse/sexp.h (revision 9223)
+++ code/parse/sexp.h (working copy)
@@ -701,8 +701,9 @@
 #define OP_DESTROY_SUBSYS_INSTANTLY (0x0016 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Admiral MS
 #define OP_DEBUG (0x0017 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Karajorma
 #define OP_SET_MISSION_MOOD (0x0018 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Karajorma
+#define OP_NAV_SELECT (0x0019 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Talon1024
+#define OP_NAV_DESELECT (0x0020 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Talon1024
 
-
 // defined for AI goals
 #define OP_AI_CHASE (0x0000 | OP_CATEGORY_AI | OP_NONCAMPAIGN_FLAG)
 #define OP_AI_DOCK (0x0001 | OP_CATEGORY_AI | OP_NONCAMPAIGN_FLAG)
To understand religion, you need to understand morality first. | WCSaga website | WCSaga Forum | 158th website | 158th forum | Project Leader: WC: Hostile Frontier | WCHF Thread at CIC | Wing Blender | Twist of Fate | Multipart turrets on angled surfaces, tutorial included. | My Google Drive stuff | To convert speeds from WC to WCS, multiply both the cruise speed and the Afterburner speed by 0.15625 (5/32)

FS2 Mods I'm waiting on: Inferno 10th Anniversary
Current Project: Contestant Android app, Learn4Life iOS app, Blender Commander (importer).
The FreeSpace Font Foundry is back in action!

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Talon1024 writes his first SEXP
I'll check this out today.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Talon1024 writes his first SEXP
Sorry to take so long. SEXP looks fine to me. I'll add it to my codebase and commit it today.

I would have been tempted to make the SelectNav() function in such a way that you could at least log an error if it was passed a nav which didn't exist. But I suppose you can create navs on the fly so that might not always be an error.

EDIT : Also your sexp help isn't in the right format. It doesn't fit with the rest. That's not completely your fault, the other Nav SEXPs also seem to be using the same format change too.  I'll change yours before I commit.
« Last Edit: October 03, 2012, 09:25:42 pm by karajorma »
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Axem

  • 211
Re: Talon1024 writes his first SEXP
One small thing that my eyes caught when svn was trying to merge this into my own changed sexp.cpp. The hex code for nav select is 0x0019 and nav deselect is 0x0020, skipping the whole range of 1A to 1F. I almost made the same mistake when I was making my sexps, until I saw the other lines and saw the hex. ;)

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Talon1024 writes his first SEXP
Yeah, I noticed the mistake in my copy after I committed it. I was going to make sure it was fixed in my next commit.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Talon 1024

  • 29
  • How do you turn this on?
    • Mods, Games, and Stuff
Re: Talon1024 writes his first SEXP
So I noticed you used deselect-nav as the name of the SEXP.  Can you please change it to unselect-nav for the sake of WCSaga compatibility?
To understand religion, you need to understand morality first. | WCSaga website | WCSaga Forum | 158th website | 158th forum | Project Leader: WC: Hostile Frontier | WCHF Thread at CIC | Wing Blender | Twist of Fate | Multipart turrets on angled surfaces, tutorial included. | My Google Drive stuff | To convert speeds from WC to WCS, multiply both the cruise speed and the Afterburner speed by 0.15625 (5/32)

FS2 Mods I'm waiting on: Inferno 10th Anniversary
Current Project: Contestant Android app, Learn4Life iOS app, Blender Commander (importer).
The FreeSpace Font Foundry is back in action!

  

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Re: Talon1024 writes his first SEXP
I switched the name of the SEXP to match the one in the code. Evidently I should have done that the other way round.
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]