Skip to content

Commit cd963a2

Browse files
committed
add props to empty slots if available
1 parent 7edc391 commit cd963a2

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

code/prop/prop.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ int prop_name_lookup(const char *name)
5252
{
5353
Assertion(name != nullptr, "NULL name passed to prop_name_lookup");
5454

55-
for (int i=0; i<static_cast<int>(Props.size()); i++){
55+
for (size_t i=0; i < Props.size(); i++){
5656
auto prop = Props[i] ? &Props[i].value() : nullptr;
5757
if (prop == nullptr) {
5858
continue;
5959
}
6060
if (prop->objnum >= 0){
6161
if (Objects[prop->objnum].type == OBJ_PROP) {
6262
if (!stricmp(name, prop->prop_name)) {
63-
return i;
63+
return static_cast<int>(i);
6464
}
6565
}
6666
}
@@ -70,6 +70,16 @@ int prop_name_lookup(const char *name)
7070
return -1;
7171
}
7272

73+
int find_prop_empty_slot() {
74+
for (size_t i = 0; i < Props.size(); i++) {
75+
if (!Props[i].has_value()) {
76+
return static_cast<int>(i);
77+
}
78+
}
79+
80+
return -1;
81+
}
82+
7383
prop* prop_id_lookup(int id)
7484
{
7585
if (id < 0 || id >= static_cast<int>(Props.size()) || !Props[id].has_value()) {
@@ -423,8 +433,15 @@ int prop_create(matrix* orient, vec3d* pos, int prop_type, const char* name)
423433

424434
pip = &(Prop_info[prop_type]);
425435

426-
Props.emplace_back(prop());
427-
int new_id = static_cast<int>(Props.size()) - 1;
436+
int new_id = find_prop_empty_slot();
437+
438+
if (new_id < 0) {
439+
Props.emplace_back(prop());
440+
new_id = static_cast<int>(Props.size()) - 1;
441+
} else {
442+
Props[new_id] = prop();
443+
}
444+
428445
propp = prop_id_lookup(new_id);
429446
Assertion(propp != nullptr, "Could not create prop!");
430447

0 commit comments

Comments
 (0)