Hard Light Productions Forums

Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: Axem on October 01, 2012, 10:00:01 pm

Title: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: Axem on October 01, 2012, 10:00:01 pm
Quote
[17:30] <battuta> i'm really wanting to set up some retreat tactics for fighters in this mission
[17:31] <battuta> i thought about swapping some of the storm distractors to dracos and having them shoot some trebs/whatever and then run awaaay
[17:31] <@Axem> problem is they dont use afterburners :(
[17:31] <battuta> we could use an attack order...
[17:31] <battuta> ...but then we'd need no dynamic on...
[17:31] <battuta> and there's no sexp for that!
[17:31] <@Axem> can we switch off dynamic orders?
[17:31] <@Axem> hmm
[17:32] <@Axem> [17:31] <battuta> and there's no sexp for that!
[17:32] <@Axem> maybe there SHOULD be
[17:32] * @Axem opens visual studio

A few hours later, copy pasting existing code and getting karajorma's help on how to debug...

Huzzah!

Code: [Select]
Index: sexp.cpp
===================================================================
--- sexp.cpp (revision 9242)
+++ sexp.cpp (working copy)
@@ -362,6 +362,8 @@
  { "player-not-use-ai", OP_PLAYER_NOT_USE_AI, 0, 0 }, // Goober5000
  { "set-player-orders", OP_SET_PLAYER_ORDERS, 3, INT_MAX }, // Karajorma
  { "cap-waypoint-speed", OP_CAP_WAYPOINT_SPEED, 2, 2 },
+ { "dynamic-goals", OP_DYNAMIC_GOALS, 1, INT_MAX}, // Axem
+ { "no-dynamic-goals", OP_NO_DYNAMIC_GOALS, 1, INT_MAX}, // Axem
 
  //Ship Status Sub-Category
  { "protect-ship", OP_PROTECT_SHIP, 1, INT_MAX, },
@@ -13157,6 +13159,53 @@
  sexp_deal_with_ship_flag(n, true, 0, 0, 0, SF2_NO_ETS, 0, P2_SF2_NO_ETS, disable);
 }
 
+void sexp_ingame_ship_no_dynamic(ship *shipp, bool no_dynamic)
+{
+ Assert(shipp);
+
+ ai_info *aip = &Ai_info[shipp->ai_index];
+
+ if (no_dynamic)
+ {
+ aip->ai_flags |= AIF_NO_DYNAMIC;
+ }
+ else
+ {
+ aip->ai_flags &= ~AIF_NO_DYNAMIC;
+ }
+}
+
+// Axem - turns no dynamic goals on or off for a ship
+void sexp_no_dynamic_goals(int n, bool no_dynamic)
+ {
+ sexp_deal_with_ship_flag(n, true, 0, 0, 0, 0, P_AIF_NO_DYNAMIC, 0, no_dynamic);
+
+ for ( ; n != -1; n = CDR(n) )
+ {
+ object_ship_wing_point_team oswpt;
+ sexp_get_object_ship_wing_point_team(&oswpt, CTEXT(n));
+
+ switch (oswpt.type)
+ {
+ // change ingame ship
+ case OSWPT_TYPE_SHIP:
+ {
+ sexp_ingame_ship_no_dynamic(oswpt.shipp, no_dynamic);
+
+ break;
+ }
+
+ case OSWPT_TYPE_WING:
+ {
+ // current ships
+ for (int i = 0; i < oswpt.wingp->current_count; i++)
+ sexp_ingame_ship_no_dynamic(&Ships[oswpt.wingp->ship_index[i]], no_dynamic);
+
+ }
+ }
+ }
+}
+
 // Goober5000 - sets the vaporize flag on a list of ships
 void sexp_ships_vaporize(int n, bool vaporize)
 {
@@ -22807,6 +22856,12 @@
  sexp_set_thrusters(node);
  break;
 
+ case OP_NO_DYNAMIC_GOALS:
+ case OP_DYNAMIC_GOALS:
+ sexp_no_dynamic_goals(node, (op_num == OP_NO_DYNAMIC_GOALS));
+ sexp_val = SEXP_TRUE;
+ break;
+
  default:
  Error(LOCATION, "Looking for SEXP operator, found '%s'.\n", CTEXT(cur_node));
  break;
@@ -23757,6 +23812,8 @@
  case OP_SET_THRUSTERS:
  case OP_SET_PLAYER_THROTTLE_SPEED:
  case OP_DEBUG:
+ case OP_DYNAMIC_GOALS:
+ case OP_NO_DYNAMIC_GOALS:
  return OPR_NULL;
 
  case OP_AI_CHASE:
@@ -25749,6 +25806,10 @@
  else
  return OPF_SHIP;
 
+ case OP_DYNAMIC_GOALS:
+ case OP_NO_DYNAMIC_GOALS:
+ return OPF_SHIP_WING;
+
  default:
  Int3();
  }
@@ -26997,6 +27058,8 @@
  case OP_PLAYER_NOT_USE_AI:
  case OP_SET_PLAYER_ORDERS:
  case OP_CAP_WAYPOINT_SPEED:
+ case OP_DYNAMIC_GOALS:
+ case OP_NO_DYNAMIC_GOALS:
  return CHANGE_SUBCATEGORY_AI_CONTROL;
 
 
@@ -30856,6 +30919,17 @@
  "\t1:\tThe player ship to set the throttle of.\r\n"
  "\t2:\tThe percentage of the player's maximum speed to set their throttle to.\r\n"
  "\t\tThis is capped to either 0 or 100 if outside the valid range."
+ },
+ {OP_DYNAMIC_GOALS , "dynamic-orders\r\n"
+ "\tTells the AI to use dynamic orders\r\n\r\n"
+ "Takes at least 1 argument...\r\n"
+ "\tAll:\tList of ships this sexp applies to\r\n"
+ },
+
+ { OP_NO_DYNAMIC_GOALS, "no-dynamic-orders\r\n"
+ "\tTells the AI to not use dynamic orders\r\n\r\n"
+ "Takes at least 1 argument...\r\n"
+ "\tAll:\tList of ships this sexp applies to\r\n"
  }
 };
 
Index: sexp.h
===================================================================
--- sexp.h (revision 9242)
+++ sexp.h (working copy)
@@ -701,6 +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_DYNAMIC_GOALS (0x0019 | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Axem
+#define OP_NO_DYNAMIC_GOALS (0x001a | OP_CATEGORY_CHANGE2 | OP_NONCAMPAIGN_FLAG) // Axem
 
 
 // defined for AI goals

If you didn't already guess, this is a sexp to turn the No Dynamic Goals flag on and off through sexps. Dynamic goals are things like fighters breaking off their attack on their target to pursue their own attacker. It can be used to make them single minded to do things like attack far away objects (make it look like they're retreating) or other fun things.

And here's a test mission for anyone brave enough to compile the patch!
http://lazymodders.fsmods.net/files/nodynamictest.rar
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: General Battuta on October 01, 2012, 10:01:57 pm
(http://i.somethingawful.com/forumsystem/emoticons/emot-pcgaming.gif)
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: Spoon on October 02, 2012, 08:26:00 pm
(http://i.somethingawful.com/forumsystem/emoticons/emot-pcgaming.gif)
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: Aardwolf on October 02, 2012, 09:01:01 pm
Marginally related: why is it that FreeSpace SEXPs come in pairs, e.g. "dynamic-goals" and "no-dynamic-goals", instead of having a single SEXP which takes a Boolean argument? Is that something mission designers aren't good at? Or what?
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: Axem on October 02, 2012, 09:09:37 pm
I've wondered the same thing. It seems to be mostly when dealing with ship flag related options that there's a sexp pair, but others there's a true/false and then the list...

As someone just copy pasting other sexps to get my own, I dare not get too "creative". :p
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: karajorma on October 03, 2012, 03:30:12 am
To be honest, I think it would be better to move over to using a boolean system simply because it doesn't take up so much space on the screen in FRED.
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: mjn.mixael on October 09, 2012, 03:05:17 pm
This is awesome. I needed this for sure.
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: karajorma on October 09, 2012, 09:26:52 pm
It's probably not going in.

I'll probably add something to the alter-ship-flag stuff I added in Test Builds instead.
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: Droid803 on October 09, 2012, 11:32:05 pm
On a somewhat related note, any specific reason why turret/beam-lock/free-all only takes a single argument and can't list multiple ships?
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: karajorma on October 10, 2012, 12:21:28 am
On a somewhat related note, any specific reason why turret/beam-lock/free-all only takes a single argument and can't list multiple ships?

Cause they take multiple turrets/beams as arguments.

The way the SEXP system works is that every argument except the last one must be constant. So you can have a SEXP like

SEXP Name
-Ship Name
-Turret
-<more turrets>

but not one like

SEXP Name
-Ship Name
-More Ship Names
-Turrets

Technically you might be able to do the above by checking the return type but it would probably be very messy. What might be possible is to fix the SEXPs to accept a single ship name, wing name, or a team name (friendly, hostile, etc). Not much use in the case of this SEXP though but there are other examples where it might work.

In the end though, it's a lot of work for something you can solve just by using <argument>.
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: Droid803 on October 10, 2012, 01:09:23 am
The one to lock/free all turrets takes additional arguments? O_o
What for?
Which ones not to lock/free?
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: karajorma on October 10, 2012, 01:10:46 am
Okay Beam/Turret-lock/free-all takes a list of ships already. Beam/Turret-free/lock can't for the reason I gave above. I assumed you meant the latter cause the former can already take a list of ships.

However, looking into it more deeply, it does appear that the SEXP documentation was never updated to reflect that the former can accept multiple ships, try an add-data though. It should work.
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: Droid803 on October 10, 2012, 01:40:53 am
Last time I tried doing that it threw me errors, which was why I asked, maybe it got changed somewhere along the line though, without me ever noticing O_o.
Title: Re: no-dynamic-goals sexp! Mindlessly narrow the AI's actions on the fly now!
Post by: karajorma on October 10, 2012, 02:00:09 am
I remember adding loops to one (or more) of those SEXPs years ago. So if there is a bug, it's probably still there. I'd appreciate it if someone checked.