refactor AI_GOAL_* #defines into enums, and remove bitwise operations on them [FRED and qtFRED]

This commit is contained in:
Goober5000 committed 2025-01-02 14:05:21 -05:00
1 parent c38882a51e
commit 423042bf11
9 files changed
+212 -57

No files matched your search

+6 -4
View File
@@ -122,10 +122,12 @@ CCriticalSection CS_cur_object_index;
ai_goal_list Ai_goal_list[] = {
{ "Waypoints", AI_GOAL_WAYPOINTS, 0 },
{ "Waypoints once", AI_GOAL_WAYPOINTS_ONCE, 0 },
{ "Attack", AI_GOAL_CHASE | AI_GOAL_CHASE_WING, 0 },
{ "Attack", AI_GOAL_CHASE, 0 },
{ "Attack", AI_GOAL_CHASE_WING, 0 }, // duplicate needed because we can no longer use bitwise operators
{ "Attack any ship", AI_GOAL_CHASE_ANY, 0 },
{ "Attack ship class", AI_GOAL_CHASE_SHIP_CLASS, 0 },
{ "Guard", AI_GOAL_GUARD | AI_GOAL_GUARD_WING, 0 },
{ "Guard", AI_GOAL_GUARD, 0 },
{ "Guard", AI_GOAL_GUARD_WING, 0 }, // duplicate needed because we can no longer use bitwise operators
{ "Disable ship", AI_GOAL_DISABLE_SHIP, 0 },
{ "Disable ship (tactical)",AI_GOAL_DISABLE_SHIP_TACTICAL, 0 },
{ "Disarm ship", AI_GOAL_DISARM_SHIP, 0 },
@@ -2227,7 +2229,7 @@ char *object_name(int obj)
return "*unknown*";
}
const char *get_order_name(int order)
const char *get_order_name(ai_goal_mode order)
{
int i;
@@ -2235,7 +2237,7 @@ const char *get_order_name(int order)
return "None";
for (i=0; i<Ai_goal_list_size; i++)
if (Ai_goal_list[i].def & order)
if (Ai_goal_list[i].def == order)
return Ai_goal_list[i].name;
return "???";
+1 -1
View File
@@ -119,7 +119,7 @@ int reference_handler(const char* name, sexp_ref_type type, int obj);
int orders_reference_handler(sexp_src source, int source_index, char* msg);
int sexp_reference_handler(int node, sexp_src source, int source_index, char* msg);
char* object_name(int obj);
const char* get_order_name(int order);
const char* get_order_name(ai_goal_mode order);
void object_moved(object* ptr);
int invalidate_references(const char* name, sexp_ref_type type);
int query_whole_wing_marked(int wing);
+79 -22
View File
@@ -295,16 +295,14 @@ BOOL ShipGoalsDlg::OnInitDialog()
}
}
// initialize the behavior boxes (they remain constant) and initialize each goal's data
// initialize the data for the behavior boxes (they remain constant while the dialog is open)
init_combo_data(valid);
// initialize the behavior boxes themselves
for (i=0; i<ED_MAX_GOALS; i++) {
m_behavior_box[i] -> ResetContent();
z = m_behavior_box[i] -> AddString("None");
m_behavior_box[i] -> SetItemData(z, (DWORD) AI_GOAL_NONE);
for (j=0; j<Ai_goal_list_size; j++){
if (valid[j]) {
z = m_behavior_box[i] -> AddString(Ai_goal_list[j].name);
m_behavior_box[i] -> SetItemData(z, (DWORD) Ai_goal_list[j].def);
}
for (const auto &entry: m_ai_goal_combo_data) {
m_behavior_box[i]->AddString(entry.first);
}
}
@@ -337,6 +335,56 @@ BOOL ShipGoalsDlg::OnInitDialog()
return TRUE;
}
void ShipGoalsDlg::init_combo_data(int valid[])
{
// don't add more than one of the same string (case-insensitive)
SCP_unordered_map<SCP_string, size_t, SCP_string_lcase_hash, SCP_string_lcase_equal_to> strings_to_indexes;
// start by adding "None"
auto none_str = "None";
strings_to_indexes.emplace(none_str, 0);
SCP_set<ai_goal_mode> none_set{ AI_GOAL_NONE };
m_ai_goal_combo_data.clear();
m_ai_goal_combo_data.emplace_back(none_str, std::move(none_set));
// initialize the data used in the combo boxes in the Initial Orders dialog
for (int i = 0; i < Ai_goal_list_size; ++i)
{
if (!valid[i])
continue;
auto &entry = Ai_goal_list[i];
// see if we already added the string
auto ii = strings_to_indexes.find(entry.name);
if (ii != strings_to_indexes.end())
{
// skip adding the string, but add the entry's goal definition to the combo box data at the existing index
m_ai_goal_combo_data[ii->second].second.insert(entry.def);
}
else
{
// this string will correspond to the index that is about to be created
strings_to_indexes[entry.name] = m_ai_goal_combo_data.size();
// add the entry's goal definition as the first (maybe only) member of the set
SCP_set<ai_goal_mode> new_set{ entry.def };
m_ai_goal_combo_data.emplace_back(entry.name, std::move(new_set));
}
}
}
ai_goal_mode ShipGoalsDlg::get_first_mode_from_combo_box(int which_item)
{
// which_item indicates initial goal 1 through MAX_AI_GOALS, so find that behavior...
int behavior_index = m_behavior[which_item];
// the behavior is the index into the combo box that contains a subset of goals from Ai_goal_list
const auto &set = m_ai_goal_combo_data[behavior_index].second;
// just get the first mode in the set, since chase/chase-wing and guard/guard-wing are handled respectively together
return *(set.begin());
}
void ShipGoalsDlg::initialize_multi()
{
int i, flag = 0;
@@ -403,7 +451,8 @@ void ShipGoalsDlg::initialize_multi()
// perform one-time initialization of data from the goals struct.
void ShipGoalsDlg::initialize(ai_goal *goals, int ship)
{
int i, item, num, inst, flag, mode;
int i, item, num, inst, flag;
ai_goal_mode mode;
object *ptr;
// note that the flag variable is a bitfield:
@@ -431,9 +480,10 @@ void ShipGoalsDlg::initialize(ai_goal *goals, int ship)
m_behavior[item] = 0;
if (mode != AI_GOAL_NONE) {
i = m_behavior_box[item] -> GetCount();
i = static_cast<int>(m_ai_goal_combo_data.size());
while (i-- > 0){
if (mode & (m_behavior_box[item]->GetItemData(i))) {
const auto &set = m_ai_goal_combo_data[i].second;
if (set.find(mode) != set.end()) {
m_behavior[item] = i;
break;
}
@@ -611,7 +661,7 @@ void ShipGoalsDlg::set_item(int item, int init)
return;
}
auto mode = m_behavior_box[item] -> GetItemData(m_behavior[item]);
auto mode = get_first_mode_from_combo_box(item);
m_priority_box[item] -> EnableWindow(TRUE);
if ((mode == AI_GOAL_CHASE_ANY) || (mode == AI_GOAL_UNDOCK) || (mode == AI_GOAL_KEEP_SAFE_DISTANCE) || (mode == AI_GOAL_PLAY_DEAD) || (mode == AI_GOAL_PLAY_DEAD_PERSISTENT) || (mode == AI_GOAL_WARP) ) {
m_object_box[item] -> EnableWindow(FALSE);
@@ -667,9 +717,11 @@ void ShipGoalsDlg::set_item(int item, int init)
// for goals that deal with individual ships
switch (mode) {
case AI_GOAL_DESTROY_SUBSYSTEM:
case AI_GOAL_CHASE | AI_GOAL_CHASE_WING:
case AI_GOAL_CHASE:
case AI_GOAL_CHASE_WING:
case AI_GOAL_DOCK:
case AI_GOAL_GUARD | AI_GOAL_GUARD_WING:
case AI_GOAL_GUARD:
case AI_GOAL_GUARD_WING:
case AI_GOAL_DISABLE_SHIP:
case AI_GOAL_DISABLE_SHIP_TACTICAL:
case AI_GOAL_DISARM_SHIP:
@@ -715,8 +767,10 @@ void ShipGoalsDlg::set_item(int item, int init)
// for goals that deal with wings
switch (mode) {
case AI_GOAL_CHASE | AI_GOAL_CHASE_WING:
case AI_GOAL_GUARD | AI_GOAL_GUARD_WING:
case AI_GOAL_CHASE:
case AI_GOAL_CHASE_WING:
case AI_GOAL_GUARD:
case AI_GOAL_GUARD_WING:
for (i=0; i<MAX_WINGS; i++) {
if (Wings[i].wave_count && i != self_wing) {
z = m_object_box[item] -> AddString(Wings[i].name);
@@ -911,7 +965,7 @@ int ShipGoalsDlg::verify_orders(int ship)
void ShipGoalsDlg::update_item(int item, int multi)
{
char *docker, *dockee, *subsys;
int mode;
ai_goal_mode mode;
char buf[512], save[80];
waypoint_list *wp_list;
@@ -928,7 +982,7 @@ void ShipGoalsDlg::update_item(int item, int multi)
m_behavior[item] = 0;
}
mode = (int)m_behavior_box[item] -> GetItemData(m_behavior[item]);
mode = get_first_mode_from_combo_box(item);
switch (mode) {
case AI_GOAL_NONE:
case AI_GOAL_CHASE_ANY:
@@ -977,7 +1031,8 @@ void ShipGoalsDlg::update_item(int item, int multi)
break;
case AI_GOAL_CHASE | AI_GOAL_CHASE_WING:
case AI_GOAL_CHASE:
case AI_GOAL_CHASE_WING:
switch (m_data[item] & TYPE_MASK) {
case TYPE_SHIP:
case TYPE_PLAYER:
@@ -1028,7 +1083,8 @@ void ShipGoalsDlg::update_item(int item, int multi)
break;
case AI_GOAL_GUARD | AI_GOAL_GUARD_WING:
case AI_GOAL_GUARD:
case AI_GOAL_GUARD_WING:
switch (m_data[item] & TYPE_MASK) {
case TYPE_SHIP:
case TYPE_PLAYER:
@@ -1106,7 +1162,7 @@ void ShipGoalsDlg::OnOK()
UpdateData(TRUE);
for (i=0; i<ED_MAX_GOALS; i++) {
auto mode = m_behavior_box[i] -> GetItemData(m_behavior[i]);
auto mode = get_first_mode_from_combo_box(i);
if ((mode != AI_GOAL_NONE) && (mode != AI_GOAL_CHASE_ANY) && (mode != AI_GOAL_UNDOCK) && (mode != AI_GOAL_KEEP_SAFE_DISTANCE) && (mode != AI_GOAL_PLAY_DEAD) && (mode != AI_GOAL_PLAY_DEAD_PERSISTENT) && (mode != AI_GOAL_WARP) ) {
if (!m_object_box[i] -> GetCount()) // no valid objects?
m_behavior[i] = 0;
@@ -1205,7 +1261,8 @@ void ShipGoalsDlg::set_object(int item)
ship_subsys *subsys;
if (m_behavior[item] > 0) {
auto mode = m_behavior_box[item] -> GetItemData(m_behavior[item]);
auto mode = get_first_mode_from_combo_box(item);
if (!m_object_box[item] -> GetCount())
m_behavior[item] = m_data[item] = 0;
else
+3
View File
@@ -29,6 +29,8 @@ public:
void OnOK();
void update();
void initialize(ai_goal *goals, int ship = cur_ship);
void init_combo_data(int valid[]);
ai_goal_mode get_first_mode_from_combo_box(int which_item);
ShipGoalsDlg(CWnd* pParent = NULL); // standard constructor
int self_ship, self_wing;
@@ -38,6 +40,7 @@ public:
int m_subsys[ED_MAX_GOALS];
int m_dock2[ED_MAX_GOALS];
int m_data[ED_MAX_GOALS];
SCP_vector<std::pair<const char*, SCP_set<ai_goal_mode>>> m_ai_goal_combo_data;
CComboBox *m_behavior_box[ED_MAX_GOALS];
CComboBox *m_object_box[ED_MAX_GOALS];
+11 -5
View File
@@ -67,10 +67,12 @@ std::pair<int, sexp_src> query_referenced_in_ai_goals(sexp_ref_type type, const
ai_goal_list Ai_goal_list[] = {
{ "Waypoints", AI_GOAL_WAYPOINTS, 0 },
{ "Waypoints once", AI_GOAL_WAYPOINTS_ONCE, 0 },
{ "Attack", AI_GOAL_CHASE | AI_GOAL_CHASE_WING, 0 },
{ "Attack", AI_GOAL_CHASE, 0 },
{ "Attack", AI_GOAL_CHASE_WING, 0 }, // duplicate needed because we can no longer use bitwise operators
{ "Attack any ship", AI_GOAL_CHASE_ANY, 0 },
{ "Attack ship class", AI_GOAL_CHASE_SHIP_CLASS, 0 },
{ "Guard", AI_GOAL_GUARD | AI_GOAL_GUARD_WING, 0 },
{ "Guard", AI_GOAL_GUARD, 0 },
{ "Guard", AI_GOAL_GUARD_WING, 0 }, // duplicate needed because we can no longer use bitwise operators
{ "Disable ship", AI_GOAL_DISABLE_SHIP, 0 },
{ "Disable ship (tactical)",AI_GOAL_DISABLE_SHIP_TACTICAL, 0 },
{ "Disarm ship", AI_GOAL_DISARM_SHIP, 0 },
@@ -2906,6 +2908,9 @@ const char* Editor::error_check_initial_orders(ai_goal* goals, int ship, int win
break;
}
default:
break;
}
switch (goals[i].ai_mode) {
@@ -2917,7 +2922,6 @@ const char* Editor::error_check_initial_orders(ai_goal* goals, int ship, int win
else
return "Wing assigned to guard a different team";
}
break;
case AI_GOAL_CHASE:
@@ -2933,19 +2937,21 @@ const char* Editor::error_check_initial_orders(ai_goal* goals, int ship, int win
else
return "Wings assigned to attack same team";
}
break;
default:
break;
}
}
return NULL;
}
const char* Editor::get_order_name(int order) {
const char* Editor::get_order_name(ai_goal_mode order) {
if (order == AI_GOAL_NONE) // special case
return "None";
for (auto& entry : Ai_goal_list)
if (entry.def & order)
if (entry.def == order)
return entry.name;
return "???";
+1 -1
View File
@@ -295,7 +295,7 @@ class Editor : public QObject {
int global_error_check_player_wings(int multi);
const char* get_order_name(int order);
static const char* get_order_name(ai_goal_mode order);
void updateStartingWingLoadoutUseCounts();
};
@@ -9,6 +9,58 @@ namespace fso {
{
initializeData(multi, shipp, wingp);
}
void ShipGoalsDialogModel::init_combo_data()
{
// don't add more than one of the same string (case-insensitive)
SCP_unordered_map<SCP_string, size_t, SCP_string_lcase_hash, SCP_string_lcase_equal_to> strings_to_indexes;
// start by adding "None"
auto none_str = "None";
strings_to_indexes.emplace(none_str, 0);
SCP_set<ai_goal_mode> none_set{ AI_GOAL_NONE };
m_ai_goal_combo_data.clear();
m_ai_goal_combo_data.emplace_back(none_str, std::move(none_set));
// initialize the data used in the combo boxes in the Initial Orders dialog
for (int i = 0; i < Ai_goal_list_size; ++i)
{
if (!valid[i])
continue;
auto &entry = Editor::getAi_goal_list()[i];
// see if we already added the string
auto ii = strings_to_indexes.find(entry.name);
if (ii != strings_to_indexes.end())
{
// skip adding the string, but add the entry's goal definition to the combo box data at the existing index
m_ai_goal_combo_data[ii->second].second.insert(entry.def);
}
else
{
// this string will correspond to the index that is about to be created
strings_to_indexes[entry.name] = m_ai_goal_combo_data.size();
// add the entry's goal definition as the first (maybe only) member of the set
SCP_set<ai_goal_mode> new_set{ entry.def };
m_ai_goal_combo_data.emplace_back(entry.name, std::move(new_set));
}
}
}
const SCP_vector<std::pair<const char*, SCP_set<ai_goal_mode>>> &ShipGoalsDialogModel::get_ai_goal_combo_data()
{
return m_ai_goal_combo_data;
};
ai_goal_mode ShipGoalsDialogModel::get_first_mode_from_combo_box(int which_item)
{
// which_item indicates initial goal 1 through MAX_AI_GOALS, so find that behavior...
int behavior_index = m_behavior[which_item];
// the behavior is the index into the combo box that contains a subset of goals from Ai_goal_list
const auto &set = m_ai_goal_combo_data[behavior_index].second;
// just get the first mode in the set, since chase/chase-wing and guard/guard-wing are handled respectively together
return *(set.begin());
}
bool ShipGoalsDialogModel::apply()
{
int i;
@@ -79,7 +131,7 @@ namespace fso {
void ShipGoalsDialogModel::update_item(const int item)
{
char* docker, * dockee, * subsys;
int mode;
ai_goal_mode mode;
char save[80]{};
SCP_string error_message;
waypoint_list* wp_list;
@@ -90,7 +142,7 @@ namespace fso {
if (!m_multi_edit || m_priority[item] >= 0)
goalp[item].priority = m_priority[item];
mode = m_behavior[item];
mode = get_first_mode_from_combo_box(item);
switch (mode) {
case AI_GOAL_NONE:
case AI_GOAL_CHASE_ANY:
@@ -143,7 +195,8 @@ namespace fso {
break;
case AI_GOAL_CHASE | AI_GOAL_CHASE_WING:
case AI_GOAL_CHASE:
case AI_GOAL_CHASE_WING:
switch (m_object[item] & TYPE_MASK) {
case TYPE_SHIP:
case TYPE_PLAYER:
@@ -198,7 +251,8 @@ namespace fso {
break;
case AI_GOAL_GUARD | AI_GOAL_GUARD_WING:
case AI_GOAL_GUARD:
case AI_GOAL_GUARD_WING:
switch (m_object[item] & TYPE_MASK) {
case TYPE_SHIP:
case TYPE_PLAYER:
@@ -338,6 +392,9 @@ namespace fso {
case AI_GOAL_WAYPOINTS_ONCE:
// case AI_GOAL_WARP:
valid[i] = 0;
break;
default:
break;
}
}
}
@@ -363,6 +420,9 @@ namespace fso {
}
}
// initialize the data for the behavior boxes (they remain constant while the dialog is open)
init_combo_data();
if (self_ship >= 0) {
initialize(Ai_info[Ships[self_ship].ai_index].goals, self_ship);
}
@@ -376,7 +436,8 @@ namespace fso {
}
void ShipGoalsDialogModel::initialize(ai_goal* goals, int ship)
{
int i, item, num, inst, flag, mode;
int i, item, num, inst, flag;
ai_goal_mode mode;
object* ptr;
SCP_vector<SCP_string> docks;
@@ -403,9 +464,16 @@ namespace fso {
m_priority[item] = 50;
}
m_behavior[item] = -1;
m_behavior[item] = 0;
if (mode != AI_GOAL_NONE) {
m_behavior[item] = mode;
i = static_cast<int>(m_ai_goal_combo_data.size());
while (i-- > 0) {
const auto &set = m_ai_goal_combo_data[i].second;
if (set.find(mode) != set.end()) {
m_behavior[item] = i;
break;
}
}
}
switch (mode) {
@@ -548,7 +616,8 @@ namespace fso {
}
}
}
break;
default:
break;
}
@@ -24,6 +24,7 @@ class ShipGoalsDialogModel : public AbstractDialogModel {
int Ai_goal_list_size = Editor::getAigoal_list_size();
void initialize(ai_goal* goals, int ship);
void initialize_multi();
void init_combo_data();
@@ -34,6 +35,7 @@ class ShipGoalsDialogModel : public AbstractDialogModel {
SCP_string m_subsys[ED_MAX_GOALS];
long long m_dock2[ED_MAX_GOALS];
//int m_data[ED_MAX_GOALS];
SCP_vector<std::pair<const char*, SCP_set<ai_goal_mode>>> m_ai_goal_combo_data;
int valid[MAX_VALID];
bool m_multi_edit;
@@ -45,6 +47,10 @@ class ShipGoalsDialogModel : public AbstractDialogModel {
public:
ShipGoalsDialogModel(QObject* parent, EditorViewport* viewport, bool multi, int self_ship, int self_wing);
const SCP_vector<std::pair<const char*, SCP_set<ai_goal_mode>>> &get_ai_goal_combo_data();
ai_goal_mode get_first_mode_from_combo_box(int which_item);
void initializeData(bool multi, int self_ship, int self_wing);
bool apply() override;
void reject() override;
@@ -77,8 +77,7 @@ ShipGoalsDialog::ShipGoalsDialog(QWidget* parent, EditorViewport* viewport, bool
connect(_model.get(), &AbstractDialogModel::modelChanged, this, &ShipGoalsDialog::updateUI);
for (int i = 0; i < ED_MAX_GOALS; i++) {
connect(behaviors[i], QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index) {
int datap = behaviors[i]->itemData(index).value<int>();
_model->setBehavior(i, datap);
_model->setBehavior(i, index);
});
connect(objects[i], QOverload<int>::of(&QComboBox::currentIndexChanged), [=](int index) {
int datap = objects[i]->itemData(index).value<int>();
@@ -119,22 +118,23 @@ void ShipGoalsDialog::rejectHandler()
void ShipGoalsDialog::updateUI()
{
util::SignalBlockers blockers(this);
for (int i = 0; i < ED_MAX_GOALS; i++) {
behaviors[i]->clear();
objects[i]->clear();
subsys[i]->clear();
docks[i]->clear();
auto value = _model->getBehavior(i);
behaviors[i]->addItem("None", QVariant(int(AI_GOAL_NONE)));
for (int j = 0; j < _model->getGoalsSize(); j++) {
if (_model->getValid(j)) {
behaviors[i]->addItem(_model->getGoalTypes()[j].name, QVariant(int(_model->getGoalTypes()[j].def)));
}
for (const auto &entry : _model->get_ai_goal_combo_data()) {
behaviors[i]->addItem(entry.first);
}
behaviors[i]->setCurrentIndex(behaviors[i]->findData(value));
auto mode = value;
auto value = _model->getBehavior(i);
behaviors[i]->setCurrentIndex(value);
auto mode = _model->get_first_mode_from_combo_box(i);
SCP_vector<waypoint_list>::iterator ii;
if (i >= MAX_AI_GOALS)
behaviors[i]->setEnabled(false);
@@ -192,6 +192,9 @@ void ShipGoalsDialog::updateUI()
priority[i]->setValue(_model->getPriority(i));
break;
default:
break;
}
// for goals that deal with ship classes
switch (mode) {
@@ -205,13 +208,17 @@ void ShipGoalsDialog::updateUI()
priority[i]->setEnabled(true);
priority[i]->setValue(_model->getPriority(i));
break;
default:
break;
} // for goals that deal with individual ships
switch (mode) {
case AI_GOAL_DESTROY_SUBSYSTEM:
case AI_GOAL_CHASE | AI_GOAL_CHASE_WING:
case AI_GOAL_CHASE:
case AI_GOAL_CHASE_WING:
case AI_GOAL_DOCK:
case AI_GOAL_GUARD | AI_GOAL_GUARD_WING:
case AI_GOAL_GUARD:
case AI_GOAL_GUARD_WING:
case AI_GOAL_DISABLE_SHIP:
case AI_GOAL_DISABLE_SHIP_TACTICAL:
case AI_GOAL_DISARM_SHIP:
@@ -259,11 +266,15 @@ void ShipGoalsDialog::updateUI()
ptr = GET_NEXT(ptr);
}
break;
default:
break;
}
// for goals that deal with wings
switch (mode) {
case AI_GOAL_CHASE | AI_GOAL_CHASE_WING:
case AI_GOAL_GUARD | AI_GOAL_GUARD_WING:
case AI_GOAL_CHASE:
case AI_GOAL_CHASE_WING:
case AI_GOAL_GUARD:
case AI_GOAL_GUARD_WING:
int j;
for (j = 0; j < MAX_WINGS; j++) {
if (Wings[j].wave_count && j != _model->getWing()) {
@@ -274,7 +285,8 @@ void ShipGoalsDialog::updateUI()
}
priority[i]->setEnabled(true);
priority[i]->setValue(_model->getPriority(i));
break;
default:
break;
}
if (mode == AI_GOAL_DESTROY_SUBSYSTEM) {