Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hud-set-xstr sexp #6406

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions code/mission/missiontraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,10 @@ char *translate_message_token(char *str)
return NULL;
}

void string_replace_tokens_with_keys(SCP_string& text) {
text = message_translate_tokens(text.c_str());
}

/**
* Translates all special tokens in a message, producing the new finalized message to be displayed
*/
Expand Down
1 change: 1 addition & 0 deletions code/mission/missiontraining.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void training_mission_shutdown();
void training_check_objectives();
void message_training_queue(const char *text, TIMESTAMP timestamp, int length = -1);
SCP_string message_translate_tokens(const char *text);
void string_replace_tokens_with_keys(SCP_string& text);
void training_fail();
void message_training_update_frame();

Expand Down
57 changes: 57 additions & 0 deletions code/parse/sexp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ SCP_vector<sexp_oper> Operators = {
{ "hud-set-custom-gauge-active", OP_HUD_SET_CUSTOM_GAUGE_ACTIVE, 2, INT_MAX, SEXP_ACTION_OPERATOR, },
{ "hud-set-builtin-gauge-active", OP_HUD_SET_BUILTIN_GAUGE_ACTIVE, 2, INT_MAX, SEXP_ACTION_OPERATOR, },
{ "hud-set-text", OP_HUD_SET_TEXT, 2, 2, SEXP_ACTION_OPERATOR, }, //WMCoolmon
{ "hud-set-xstr", OP_HUD_SET_XSTR, 3, 3, SEXP_ACTION_OPERATOR, }, //MjnMixael
{ "hud-set-text-num", OP_HUD_SET_TEXT_NUM, 2, 2, SEXP_ACTION_OPERATOR, }, //WMCoolmon
{ "hud-set-message", OP_HUD_SET_MESSAGE, 2, 2, SEXP_ACTION_OPERATOR, }, //The E
{ "hud-set-directive", OP_HUD_SET_DIRECTIVE, 2, 2, SEXP_ACTION_OPERATOR, }, //The E
Expand Down Expand Up @@ -13064,6 +13065,36 @@ void sexp_hud_set_text(int n)
}
}

void sexp_hud_set_xstr(int n)
{
bool is_nan, is_nan_forever;

auto gaugename = CTEXT(n);
auto text = CTEXT(CDR(n));
auto id = eval_num(CDR(n), is_nan, is_nan_forever);
if (is_nan || is_nan_forever) {
id = -1;
}

// Translate the string using tstrings
SCP_string xstr;
sprintf(xstr, "XSTR(\"%s\", %d)", text, id);
SCP_string translated_string;
lcl_ext_localize(xstr, translated_string);

// Now replace tokens and variables
string_replace_tokens_with_keys(translated_string);
sexp_replace_variable_names_with_values(translated_string);
sexp_container_replace_refs_with_values(translated_string);

HudGauge* cg = hud_get_custom_gauge(gaugename);
if (cg) {
cg->updateCustomGaugeText(translated_string.c_str());
} else {
WarningEx(LOCATION, "Could not find a custom hud gauge named %s\n", gaugename);
}
}

void sexp_hud_set_message(int n)
{
auto gaugename = CTEXT(n);
Expand All @@ -13074,6 +13105,7 @@ void sexp_hud_set_message(int n)
if ( !stricmp(text, Messages[i].name) ) {
message = Messages[i].message;

string_replace_tokens_with_keys(message);
sexp_replace_variable_names_with_values(message);
sexp_container_replace_refs_with_values(message);

Expand Down Expand Up @@ -28435,6 +28467,11 @@ int eval_sexp(int cur_node, int referenced_node)
sexp_val = SEXP_TRUE;
break;

case OP_HUD_SET_XSTR:
sexp_hud_set_xstr(node);
sexp_val = SEXP_TRUE;
break;

case OP_HUD_SET_TEXT_NUM:
sexp_hud_set_text_num(node);
sexp_val = SEXP_TRUE;
Expand Down Expand Up @@ -31052,6 +31089,7 @@ int query_operator_return_type(int op)
case OP_NAV_SET_COLOR:
case OP_NAV_SET_VISITED_COLOR:
case OP_HUD_SET_TEXT:
case OP_HUD_SET_XSTR:
case OP_HUD_SET_TEXT_NUM:
case OP_HUD_SET_MESSAGE:
case OP_HUD_SET_COORDS:
Expand Down Expand Up @@ -32397,6 +32435,14 @@ int query_operator_argument_type(int op, int argnum)
else
return OPF_STRING;

case OP_HUD_SET_XSTR:
if (argnum == 0)
return OPF_CUSTOM_HUD_GAUGE;
if (argnum == 1)
return OPF_STRING;
else
return OPF_NUMBER;

case OP_HUD_SET_MESSAGE:
if (argnum == 0)
return OPF_CUSTOM_HUD_GAUGE;
Expand Down Expand Up @@ -36077,6 +36123,7 @@ int get_category(int op_id)
case OP_HUD_DISABLE_EXCEPT_MESSAGES:
case OP_FORCE_JUMP:
case OP_HUD_SET_TEXT:
case OP_HUD_SET_XSTR:
case OP_HUD_SET_TEXT_NUM:
case OP_HUD_SET_COORDS:
case OP_HUD_SET_FRAME:
Expand Down Expand Up @@ -36685,6 +36732,7 @@ int get_subcategory(int op_id)
case OP_HUD_SET_CUSTOM_GAUGE_ACTIVE:
case OP_HUD_SET_BUILTIN_GAUGE_ACTIVE:
case OP_HUD_SET_TEXT:
case OP_HUD_SET_XSTR:
case OP_HUD_SET_TEXT_NUM:
case OP_HUD_SET_MESSAGE:
case OP_HUD_SET_DIRECTIVE:
Expand Down Expand Up @@ -40956,6 +41004,15 @@ SCP_vector<sexp_help_struct> Sexp_help = {
"\t2:\tText to be set"
},

//MjnMixael
{ OP_HUD_SET_XSTR, "hud-set-xstr\r\n"
"\tSets the text value of a given HUD gauge to a translated string and replaces variables.\r\n"
"\tWorks for custom gauges only. Takes 3 arguments...\r\n"
"\t1:\tHUD gauge to be modified\r\n"
"\t2:\tText to be set"
"\t3:\tXSTR ID to lookup"
},

{ OP_HUD_SET_TEXT_NUM, "hud-set-text-num\r\n"
"\tSets the text value of a given HUD gauge to a number. Works for custom gauges only. Takes 2 arguments...\r\n"
"\t1:\tHUD gauge to be modified\r\n"
Expand Down
1 change: 1 addition & 0 deletions code/parse/sexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ enum : int {
OP_HUD_DISABLE_EXCEPT_MESSAGES, // Goober5000
OP_FORCE_JUMP, // Goober5000
OP_HUD_SET_TEXT, //WMC
OP_HUD_SET_XSTR,
OP_HUD_SET_TEXT_NUM, //WMC
OP_HUD_SET_COORDS, //WMC
OP_HUD_SET_FRAME, //WMC
Expand Down
Loading