refactor town entrance search into cScenario function

This commit is contained in:
2025-01-21 13:56:47 -06:00
parent 37640bf349
commit 611d00ff63
3 changed files with 46 additions and 26 deletions

View File

@@ -440,33 +440,28 @@ static void handle_scenario_args() {
}
// Try to put the party in an outdoor section from which you can enter the town --
// so when you leave, you'll hopefully be in the right place.
bool found_entrance = false;
for(int x = 0; x < univ.scenario.outdoors.width(); ++x){
for(int y = 0; y < univ.scenario.outdoors.height(); ++y){
for(spec_loc_t& entrance : univ.scenario.outdoors[x][y]->city_locs){
if(entrance.spec == *scen_arg_town){
// Very janky but I don't know how else to make it properly load the right sections and set i_w_c
while(univ.party.outdoor_corner.x > x){
shift_universe_left();
}
while(univ.party.outdoor_corner.x < x){
shift_universe_right();
}
while(univ.party.outdoor_corner.y > y){
shift_universe_up();
}
while(univ.party.outdoor_corner.y < y){
shift_universe_down();
}
outd_move_party(local_to_global(entrance), true);
found_entrance = true;
break;
}
}
if(found_entrance) break;
auto town_entrances = univ.scenario.find_town_entrances(*scen_arg_town);
if(!town_entrances.empty()){
// TODO a dialog could be shown to choose between multiple entrances,
// but maybe that would be janky, because this happens when you're trying to launch
// from INSIDE a town.
town_entrance_t first_entrance_found = town_entrances[0];
int x = first_entrance_found.out_sec.x;
int y = first_entrance_found.out_sec.y;
// Very janky but I don't know how else to make it properly load the right sections and set i_w_c
while(univ.party.outdoor_corner.x > x){
shift_universe_left();
}
if(found_entrance) break;
while(univ.party.outdoor_corner.x < x){
shift_universe_right();
}
while(univ.party.outdoor_corner.y > y){
shift_universe_up();
}
while(univ.party.outdoor_corner.y < y){
shift_universe_down();
}
outd_move_party(local_to_global(first_entrance_found.loc), true);
}
short town_entrance = 0;

View File

@@ -577,3 +577,17 @@ void cScenario::readFrom(const cTagFile& file){
}
}
}
std::vector<town_entrance_t> cScenario::find_town_entrances(int town_num) {
std::vector<town_entrance_t> matching_entrances;
for(int x = 0; x < outdoors.width(); ++x){
for(int y = 0; y < outdoors.height(); ++y){
for(spec_loc_t& entrance : outdoors[x][y]->city_locs){
if(town_num == -1 || entrance.spec == town_num){
matching_entrances.push_back({{x, y}, {entrance.x, entrance.y}, static_cast<int>(entrance.spec)});
}
}
}
}
return matching_entrances;
}

View File

@@ -39,6 +39,13 @@ struct scenario_header_flags {
enum eContentRating {G, PG, R, NC17};
// Used for finding town entrances in the outdoors
struct town_entrance_t {
location out_sec;
location loc;
int town;
};
class cScenario {
public:
class cItemStorage {
@@ -117,6 +124,10 @@ public:
cItem return_treasure(int loot, bool allow_junk_treasure = false) const;
cItem pull_item_of_type(unsigned int loot_max,short min_val,short max_val,const std::vector<eItemType>& types,bool allow_junk_treasure=false) const;
// Debugging/Editing helper: find town entrances in the outdoors. When town_num is specified, only return entrances
// to the town with that number
std::vector<town_entrance_t> find_town_entrances(int town_num = -1);
void reset_version();
explicit cScenario();
~cScenario();