Hard Light Productions Forums
Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: Talon 1024 on September 24, 2012, 10:51:39 pm
-
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.
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?
-
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.
-
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.
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]
-
One last change...
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)
-
I'll check this out today.
-
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.
-
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. ;)
-
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.
-
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?
-
I switched the name of the SEXP to match the one in the code. Evidently I should have done that the other way round.