From 61d9ce895be392c048437e985cf583ee2fc6161a Mon Sep 17 00:00:00 2001 From: zookeeper Date: Fri, 27 Dec 2013 17:44:39 +0000 Subject: [PATCH] Ported the $On Action and $On Action Stopped scripting hooks from zookeeper branch. Also bumps the char limit for conditions from 32 to 64. git-svn-id: svn://svn.icculus.org/fs2open/trunk/fs2_open@10254 387891d4-d844-0410-90c0-e4c51a9137d3 --- code/controlconfig/controlsconfig.cpp | 35 ++++++++++++++++++++- code/controlconfig/controlsconfig.h | 3 +- code/controlconfig/controlsconfigcommon.cpp | 28 ++++++++++++----- code/freespace2/freespace.cpp | 1 + code/freespace2/freespace.h | 1 + code/globalincs/globals.h | 1 + code/parse/scripting.cpp | 16 +++++++++- code/parse/scripting.h | 9 ++++-- code/parse/sexp.cpp | 6 ++-- 9 files changed, 84 insertions(+), 16 deletions(-) diff --git a/code/controlconfig/controlsconfig.cpp b/code/controlconfig/controlsconfig.cpp index 6dcb9d9e97..6db4073d5a 100644 --- a/code/controlconfig/controlsconfig.cpp +++ b/code/controlconfig/controlsconfig.cpp @@ -28,6 +28,7 @@ #include "globalincs/alphacolors.h" #include "network/multi_pmsg.h" #include "network/multiutil.h" +#include "parse/scripting.h" #ifndef NDEBUG @@ -2132,6 +2133,8 @@ float check_control_timef(int id) // first, see if control actually used (makes sure modifiers match as well) if (!check_control(id)) { + Control_config[id].continuous_ongoing = false; + return 0.0f; } @@ -2146,6 +2149,11 @@ float check_control_timef(int id) } if (t1 + t2) { + // We want to set this to true only after visiting control_used() (above) + // to allow it to tell the difference between an ongoing continuous action + // started before and a continuous action being started right now. + Control_config[id].continuous_ongoing = true; + return t1 + t2; } @@ -2251,6 +2259,17 @@ int check_control(int id, int key) return 1; } + if (Control_config[id].continuous_ongoing) { + // If we reach this point, then it means this is a continuous control + // which has just been released + + Script_system.SetHookVar("Action", 's', Control_config[id].text); + Script_system.RunCondition(CHA_ONACTIONSTOPPED, '\0', NULL, NULL, id); + Script_system.RemHookVar("Action"); + + Control_config[id].continuous_ongoing = false; + } + return 0; } @@ -2306,6 +2325,7 @@ void control_get_axes_readings(int *h, int *p, int *b, int *ta, int *tr) return; } +int Last_frame_timestamp; void control_used(int id) { // if we have set this key to be ignored, ignore it @@ -2313,7 +2333,20 @@ void control_used(int id) return; } - Control_config[id].used = timestamp(); + // This check needs to be done because the control code might call this function more than once per frame, + // and we don't want to run the hooks more than once per frame + if (Control_config[id].used < Last_frame_timestamp) { + if (!Control_config[id].continuous_ongoing) { + Script_system.SetHookVar("Action", 's', Control_config[id].text); + Script_system.RunCondition(CHA_ONACTION, '\0', NULL, NULL, id); + Script_system.RemHookVar("Action"); + + if (Control_config[id].type == CC_TYPE_CONTINUOUS) + Control_config[id].continuous_ongoing = true; + } + + Control_config[id].used = timestamp(); + } } void control_config_clear_used_status() diff --git a/code/controlconfig/controlsconfig.h b/code/controlconfig/controlsconfig.h index 3dc0ff7a9d..1460b107b5 100644 --- a/code/controlconfig/controlsconfig.h +++ b/code/controlconfig/controlsconfig.h @@ -69,6 +69,7 @@ typedef struct config_item { short joy_id; //!< joystick button bound to action int used; //!< has control been used yet in mission? If so, this is the timestamp bool disabled; //!< whether this action should be available at all + bool continuous_ongoing;//!< whether this action is a continuous one and is currently ongoing } config_item; /*! @@ -301,7 +302,7 @@ void control_config_close(); void control_config_cancel_exit(); void control_config_reset_defaults(int presetnum=-1); -int translate_key_to_index(char *key); +int translate_key_to_index(const char *key, bool find_override=true); char *translate_key(char *key); char *textify_scancode(int code); float check_control_timef(int id); diff --git a/code/controlconfig/controlsconfigcommon.cpp b/code/controlconfig/controlsconfigcommon.cpp index 9024d7569d..0812a98796 100644 --- a/code/controlconfig/controlsconfigcommon.cpp +++ b/code/controlconfig/controlsconfigcommon.cpp @@ -375,7 +375,10 @@ void set_modifier_status() } } -int translate_key_to_index(char *key) +// If find_override is set to true, then this returns the index of the action +// which has been bound to the given key. Otherwise, the index of the action +// which has the given key as its default key will be returned. +int translate_key_to_index(const char *key, bool find_override) { int i, index = -1, alt = 0, shift = 0, max_scan_codes; @@ -430,10 +433,19 @@ int translate_key_to_index(char *key) index |= KEY_ALTED; // convert scancode to Control_config index - for (i=0; idata.name, Control_config[action_index].text)) + return false; + break; + } case CHC_VERSION: { // Goober5000: I'm going to assume scripting doesn't care about SVN revision @@ -1327,7 +1341,7 @@ bool script_state::ParseCondition(const char *filename) case CHC_VERSION: case CHC_APPLICATION: default: - stuff_string(sct.data.name, F_NAME, NAME_LENGTH); + stuff_string(sct.data.name, F_NAME, CONDITION_LENGTH); break; } diff --git a/code/parse/scripting.h b/code/parse/scripting.h index f83ca404aa..9d28528bad 100644 --- a/code/parse/scripting.h +++ b/code/parse/scripting.h @@ -34,8 +34,9 @@ struct image_desc #define CHC_WEAPONCLASS 6 #define CHC_OBJECTTYPE 7 #define CHC_KEYPRESS 8 -#define CHC_VERSION 9 -#define CHC_APPLICATION 10 +#define CHC_ACTION 9 +#define CHC_VERSION 10 +#define CHC_APPLICATION 11 //Actions #define CHA_NONE -1 @@ -71,6 +72,8 @@ struct image_desc #define CHA_SECONDARYFIRE 29 #define CHA_ONSHIPARRIVE 30 #define CHA_COLLIDEBEAM 31 +#define CHA_ONACTION 32 +#define CHA_ONACTIONSTOPPED 33 // management stuff void scripting_state_init(); @@ -83,7 +86,7 @@ public: int condition_type; union { - char name[NAME_LENGTH]; + char name[CONDITION_LENGTH]; } data; script_condition() diff --git a/code/parse/sexp.cpp b/code/parse/sexp.cpp index f098030b49..ed2dee7c72 100644 --- a/code/parse/sexp.cpp +++ b/code/parse/sexp.cpp @@ -14693,7 +14693,7 @@ int sexp_key_pressed(int node) int z, t; Assert(node != -1); - z = translate_key_to_index(CTEXT(node)); + z = translate_key_to_index(CTEXT(node), false); if (z < 0){ return SEXP_FALSE; } @@ -14716,7 +14716,7 @@ void sexp_key_reset(int node) for (n = node; n != -1; n = CDR(n)) { - z = translate_key_to_index(CTEXT(n)); + z = translate_key_to_index(CTEXT(n), false); if (z >= 0) Control_config[z].used = 0; } @@ -14736,7 +14736,7 @@ void sexp_ignore_key(int node) node = CDR(node); while (node > -1) { // get the key - ignored_key = translate_key_to_index(CTEXT(node)); + ignored_key = translate_key_to_index(CTEXT(node), false); if (ignored_key > -1) { Ignored_keys[ignored_key] = ignore_count;