Skip to content

Commit ffcf078

Browse files
authored
Preload asteroids and debris when config-field sexps are used (#6874)
1 parent e52fa1e commit ffcf078

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

code/asteroid/asteroid.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,31 +2542,29 @@ int get_asteroid_index(const char* asteroid_name)
25422542
return -1;
25432543
}
25442544

2545-
// For FRED. Gets a list of unique asteroid subtype names
2546-
SCP_vector<SCP_string> get_list_valid_asteroid_subtypes()
2545+
// Returns the list of unique asteroid subtype names.
2546+
// List is cached after the first call since Asteroid_info cannot change during an engine instance.
2547+
const SCP_vector<SCP_string>& get_list_valid_asteroid_subtypes()
25472548
{
2548-
SCP_vector<SCP_string> list;
2549-
2550-
for (const auto& this_asteroid : Asteroid_info) {
2551-
if (this_asteroid.type != ASTEROID_TYPE_DEBRIS) {
2552-
for (const auto& subtype : this_asteroid.subtypes) {
2553-
bool exists = false;
2554-
for (const auto& entry : list) {
2555-
if (subtype.type_name == entry) {
2556-
exists = true;
2549+
static SCP_vector<SCP_string> list;
2550+
2551+
if (list.empty()) {
2552+
for (const auto& this_asteroid : Asteroid_info) {
2553+
if (this_asteroid.type != ASTEROID_TYPE_DEBRIS) {
2554+
for (const auto& subtype : this_asteroid.subtypes) {
2555+
// Only add unique names
2556+
if (std::find(list.begin(), list.end(), subtype.type_name) == list.end()) {
2557+
list.push_back(subtype.type_name);
25572558
}
25582559
}
2559-
2560-
if (!exists) {
2561-
list.push_back(subtype.type_name);
2562-
}
25632560
}
25642561
}
25652562
}
25662563

25672564
return list;
25682565
}
25692566

2567+
25702568
static void verify_asteroid_splits()
25712569
{
25722570

code/asteroid/asteroid.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ void asteroid_show_brackets();
179179
void asteroid_target_closest_danger();
180180
void asteroid_add_target(object* objp);
181181
int get_asteroid_index(const char* asteroid_name);
182-
SCP_vector<SCP_string> get_list_valid_asteroid_subtypes();
182+
const SCP_vector<SCP_string>& get_list_valid_asteroid_subtypes();
183+
int get_asteroid_subtype_index_by_name(const SCP_string& name, int asteroid_idx);
183184

184185
// extern for the lab
185186
void asteroid_load(int asteroid_info_index, int asteroid_subtype);

code/parse/sexp.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4426,6 +4426,32 @@ void preload_change_ship_class(const char *text)
44264426
model_page_in_textures(sip->model_num, idx);
44274427
}
44284428

4429+
// MjnMixael
4430+
void preload_asteroid_class(const char* text)
4431+
{
4432+
const auto& list = get_list_valid_asteroid_subtypes();
4433+
4434+
bool valid = std::any_of(list.begin(), list.end(), [&](const SCP_string& item) { return !stricmp(text, item.c_str()); });
4435+
4436+
if (!valid)
4437+
return;
4438+
4439+
asteroid_load(ASTEROID_TYPE_SMALL, get_asteroid_subtype_index_by_name(text, ASTEROID_TYPE_SMALL));
4440+
asteroid_load(ASTEROID_TYPE_MEDIUM, get_asteroid_subtype_index_by_name(text, ASTEROID_TYPE_MEDIUM));
4441+
asteroid_load(ASTEROID_TYPE_LARGE, get_asteroid_subtype_index_by_name(text, ASTEROID_TYPE_LARGE));
4442+
4443+
}
4444+
4445+
// MjnMixael
4446+
void preload_debris_class(const char* text)
4447+
{
4448+
auto idx = get_asteroid_index(text);
4449+
if (idx < 0)
4450+
return;
4451+
4452+
asteroid_load(idx, 0);
4453+
}
4454+
44294455
// Goober5000
44304456
void preload_turret_change_weapon(const char *text)
44314457
{
@@ -4846,6 +4872,30 @@ int get_sexp()
48464872
do_preload_for_arguments(sexp_set_skybox_model_preload, n, arg_handler);
48474873
break;
48484874

4875+
case OP_CONFIG_ASTEROID_FIELD:
4876+
// asteroid types start at argument #17
4877+
n = CDDDDDR(start);
4878+
n = CDDDDDR(n);
4879+
n = CDDDDDR(n);
4880+
n = CDDR(n);
4881+
4882+
// loop through all remaining arguments
4883+
for (int arg = n; arg >= 0; arg = CDR(arg)) {
4884+
do_preload_for_arguments(preload_asteroid_class, arg, arg_handler);
4885+
}
4886+
break;
4887+
4888+
case OP_CONFIG_DEBRIS_FIELD:
4889+
// debris types start at argument #10
4890+
n = CDDDDDR(start);
4891+
n = CDDDDDR(n);
4892+
4893+
// loop through all remaining arguments
4894+
for (int arg = n; arg >= 0; arg = CDR(arg)) {
4895+
do_preload_for_arguments(preload_debris_class, arg, arg_handler);
4896+
}
4897+
break;
4898+
48494899
case OP_TURRET_CHANGE_WEAPON:
48504900
// weapon to change to is arg #3
48514901
n = CDDDR(start);

0 commit comments

Comments
 (0)