diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/io/translation_loader_po.cpp | 439 | ||||
-rw-r--r-- | core/math/a_star.cpp | 144 | ||||
-rw-r--r-- | core/math/a_star.h | 12 | ||||
-rw-r--r-- | core/register_core_types.cpp | 2 |
4 files changed, 352 insertions, 245 deletions
diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp index 8d3e58cad1..801bd8b0bf 100644 --- a/core/io/translation_loader_po.cpp +++ b/core/io/translation_loader_po.cpp @@ -35,98 +35,160 @@ #include "core/string/translation_po.h" RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { - enum Status { - STATUS_NONE, - STATUS_READING_ID, - STATUS_READING_STRING, - STATUS_READING_CONTEXT, - STATUS_READING_PLURAL, - }; - - Status status = STATUS_NONE; - - String msg_id; - String msg_str; - String msg_context; - Vector<String> msgs_plural; - String config; - if (r_error) { *r_error = ERR_FILE_CORRUPT; } - Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO)); - int line = 1; - int plural_forms = 0; - int plural_index = -1; - bool entered_context = false; - bool skip_this = false; - bool skip_next = false; - bool is_eof = false; const String path = f->get_path(); + Ref<TranslationPO> translation = Ref<TranslationPO>(memnew(TranslationPO)); + String config; - while (!is_eof) { - String l = f->get_line().strip_edges(); - is_eof = f->eof_reached(); + uint32_t magic = f->get_32(); + if (magic == 0x950412de) { + // Load binary MO file. - // If we reached last line and it's not a content line, break, otherwise let processing that last loop - if (is_eof && l.is_empty()) { - if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || (status == STATUS_READING_PLURAL && plural_index != plural_forms - 1)) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line)); - } else { - break; - } + uint16_t version_maj = f->get_16(); + uint16_t version_min = f->get_16(); + if (version_maj > 1) { + ERR_FAIL_V_MSG(RES(), vformat("Unsupported MO file %s, version %d.%d.", path, version_maj, version_min)); } - if (l.begins_with("msgctxt")) { - if (status != STATUS_READING_STRING && status != STATUS_READING_PLURAL) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line)); + uint32_t num_strings = f->get_32(); + uint32_t id_table_offset = f->get_32(); + uint32_t trans_table_offset = f->get_32(); + + // Read string tables. + for (uint32_t i = 0; i < num_strings; i++) { + String msg_id; + String msg_id_plural; + String msg_context; + + // Read id strings and context. + { + Vector<uint8_t> data; + f->seek(id_table_offset + i * 8); + uint32_t str_start = 0; + uint32_t str_len = f->get_32(); + uint32_t str_offset = f->get_32(); + + data.resize(str_len + 1); + f->seek(str_offset); + f->get_buffer(data.ptrw(), str_len); + data.write[str_len] = 0; + + bool is_plural = false; + for (uint32_t j = 0; j < str_len + 1; j++) { + if (data[j] == 0x04) { + msg_context.parse_utf8((const char *)data.ptr(), j); + str_start = j + 1; + } + if (data[j] == 0x00) { + if (is_plural) { + msg_id_plural.parse_utf8((const char *)(data.ptr() + str_start), j - str_start); + } else { + msg_id.parse_utf8((const char *)(data.ptr() + str_start), j - str_start); + is_plural = true; + } + str_start = j + 1; + } + } } - // In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read - // and set "entered_context" to true to prevent adding twice. - if (!skip_this && !msg_id.is_empty()) { - if (status == STATUS_READING_STRING) { - translation->add_message(msg_id, msg_str, msg_context); - } else if (status == STATUS_READING_PLURAL) { - if (plural_index != plural_forms - 1) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + // Read translated strings. + { + Vector<uint8_t> data; + f->seek(trans_table_offset + i * 8); + uint32_t str_start = 0; + uint32_t str_len = f->get_32(); + uint32_t str_offset = f->get_32(); + + data.resize(str_len + 1); + f->seek(str_offset); + f->get_buffer(data.ptrw(), str_len); + data.write[str_len] = 0; + + if (msg_id.is_empty()) { + config = String::utf8((const char *)data.ptr(), str_len); + // Record plural rule. + int p_start = config.find("Plural-Forms"); + if (p_start != -1) { + int p_end = config.find("\n", p_start); + translation->set_plural_rule(config.substr(p_start, p_end - p_start)); + } + } else { + Vector<String> plural_msg; + for (uint32_t j = 0; j < str_len + 1; j++) { + if (data[j] == 0x00) { + if (msg_id_plural.is_empty()) { + translation->add_message(msg_id, String::utf8((const char *)(data.ptr() + str_start), j - str_start), msg_context); + } else { + plural_msg.push_back(String::utf8((const char *)(data.ptr() + str_start), j - str_start)); + } + str_start = j + 1; + } + } + if (!plural_msg.is_empty()) { + translation->add_plural_message(msg_id, plural_msg, msg_context); } - translation->add_plural_message(msg_id, msgs_plural, msg_context); } } - msg_context = ""; - l = l.substr(7, l.length()).strip_edges(); - status = STATUS_READING_CONTEXT; - entered_context = true; } - if (l.begins_with("msgid_plural")) { - if (plural_forms == 0) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "PO file uses 'msgid_plural' but 'Plural-Forms' is invalid or missing in header: " + path + ":" + itos(line)); - } else if (status != STATUS_READING_ID) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line)); - } - // We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already. - // We just have to reset variables related to plurals for "msgstr[]" later on. - l = l.substr(12, l.length()).strip_edges(); - plural_index = -1; - msgs_plural.clear(); - msgs_plural.resize(plural_forms); - status = STATUS_READING_PLURAL; - } else if (l.begins_with("msgid")) { - if (status == STATUS_READING_ID) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line)); + memdelete(f); + } else { + // Try to load as text PO file. + f->seek(0); + + enum Status { + STATUS_NONE, + STATUS_READING_ID, + STATUS_READING_STRING, + STATUS_READING_CONTEXT, + STATUS_READING_PLURAL, + }; + + Status status = STATUS_NONE; + + String msg_id; + String msg_str; + String msg_context; + Vector<String> msgs_plural; + + if (r_error) { + *r_error = ERR_FILE_CORRUPT; + } + + int line = 1; + int plural_forms = 0; + int plural_index = -1; + bool entered_context = false; + bool skip_this = false; + bool skip_next = false; + bool is_eof = false; + + while (!is_eof) { + String l = f->get_line().strip_edges(); + is_eof = f->eof_reached(); + + // If we reached last line and it's not a content line, break, otherwise let processing that last loop + if (is_eof && l.is_empty()) { + if (status == STATUS_READING_ID || status == STATUS_READING_CONTEXT || (status == STATUS_READING_PLURAL && plural_index != plural_forms - 1)) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected EOF while reading PO file at: " + path + ":" + itos(line)); + } else { + break; + } } - if (!msg_id.is_empty()) { - if (!skip_this && !entered_context) { + if (l.begins_with("msgctxt")) { + if (status != STATUS_READING_STRING && status != STATUS_READING_PLURAL) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgctxt', was expecting 'msgid_plural' or 'msgstr' before 'msgctxt' while parsing: " + path + ":" + itos(line)); + } + + // In PO file, "msgctxt" appears before "msgid". If we encounter a "msgctxt", we add what we have read + // and set "entered_context" to true to prevent adding twice. + if (!skip_this && !msg_id.is_empty()) { if (status == STATUS_READING_STRING) { translation->add_message(msg_id, msg_str, msg_context); } else if (status == STATUS_READING_PLURAL) { @@ -137,119 +199,163 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { translation->add_plural_message(msg_id, msgs_plural, msg_context); } } - } else if (config.is_empty()) { - config = msg_str; - // Record plural rule. - int p_start = config.find("Plural-Forms"); - if (p_start != -1) { - int p_end = config.find("\n", p_start); - translation->set_plural_rule(config.substr(p_start, p_end - p_start)); - plural_forms = translation->get_plural_forms(); + msg_context = ""; + l = l.substr(7, l.length()).strip_edges(); + status = STATUS_READING_CONTEXT; + entered_context = true; + } + + if (l.begins_with("msgid_plural")) { + if (plural_forms == 0) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "PO file uses 'msgid_plural' but 'Plural-Forms' is invalid or missing in header: " + path + ":" + itos(line)); + } else if (status != STATUS_READING_ID) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid_plural', was expecting 'msgid' before 'msgid_plural' while parsing: " + path + ":" + itos(line)); + } + // We don't record the message in "msgid_plural" itself as tr_n(), TTRN(), RTRN() interfaces provide the plural string already. + // We just have to reset variables related to plurals for "msgstr[]" later on. + l = l.substr(12, l.length()).strip_edges(); + plural_index = -1; + msgs_plural.clear(); + msgs_plural.resize(plural_forms); + status = STATUS_READING_PLURAL; + } else if (l.begins_with("msgid")) { + if (status == STATUS_READING_ID) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgid', was expecting 'msgstr' while parsing: " + path + ":" + itos(line)); } + + if (!msg_id.is_empty()) { + if (!skip_this && !entered_context) { + if (status == STATUS_READING_STRING) { + translation->add_message(msg_id, msg_str, msg_context); + } else if (status == STATUS_READING_PLURAL) { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); + } + } + } else if (config.is_empty()) { + config = msg_str; + // Record plural rule. + int p_start = config.find("Plural-Forms"); + if (p_start != -1) { + int p_end = config.find("\n", p_start); + translation->set_plural_rule(config.substr(p_start, p_end - p_start)); + plural_forms = translation->get_plural_forms(); + } + } + + l = l.substr(5, l.length()).strip_edges(); + status = STATUS_READING_ID; + // If we did not encounter msgctxt, we reset context to empty to reset it. + if (!entered_context) { + msg_context = ""; + } + msg_id = ""; + msg_str = ""; + skip_this = skip_next; + skip_next = false; + entered_context = false; } - l = l.substr(5, l.length()).strip_edges(); - status = STATUS_READING_ID; - // If we did not encounter msgctxt, we reset context to empty to reset it. - if (!entered_context) { - msg_context = ""; + if (l.begins_with("msgstr[")) { + if (status != STATUS_READING_PLURAL) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line)); + } + plural_index++; // Increment to add to the next slot in vector msgs_plural. + l = l.substr(9, l.length()).strip_edges(); + } else if (l.begins_with("msgstr")) { + if (status != STATUS_READING_ID) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line)); + } + + l = l.substr(6, l.length()).strip_edges(); + status = STATUS_READING_STRING; } - msg_id = ""; - msg_str = ""; - skip_this = skip_next; - skip_next = false; - entered_context = false; - } - if (l.begins_with("msgstr[")) { - if (status != STATUS_READING_PLURAL) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr[]', was expecting 'msgid_plural' before 'msgstr[]' while parsing: " + path + ":" + itos(line)); + if (l.is_empty() || l.begins_with("#")) { + if (l.contains("fuzzy")) { + skip_next = true; + } + line++; + continue; // Nothing to read or comment. } - plural_index++; // Increment to add to the next slot in vector msgs_plural. - l = l.substr(9, l.length()).strip_edges(); - } else if (l.begins_with("msgstr")) { - if (status != STATUS_READING_ID) { + + if (!l.begins_with("\"") || status == STATUS_NONE) { memdelete(f); - ERR_FAIL_V_MSG(RES(), "Unexpected 'msgstr', was expecting 'msgid' before 'msgstr' while parsing: " + path + ":" + itos(line)); + ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line)); } - l = l.substr(6, l.length()).strip_edges(); - status = STATUS_READING_STRING; - } - - if (l.is_empty() || l.begins_with("#")) { - if (l.contains("fuzzy")) { - skip_next = true; - } - line++; - continue; // Nothing to read or comment. - } + l = l.substr(1, l.length()); + // Find final quote, ignoring escaped ones (\"). + // The escape_next logic is necessary to properly parse things like \\" + // where the backslash is the one being escaped, not the quote. + int end_pos = -1; + bool escape_next = false; + for (int i = 0; i < l.length(); i++) { + if (l[i] == '\\' && !escape_next) { + escape_next = true; + continue; + } - if (!l.begins_with("\"") || status == STATUS_NONE) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Invalid line '" + l + "' while parsing: " + path + ":" + itos(line)); - } + if (l[i] == '"' && !escape_next) { + end_pos = i; + break; + } - l = l.substr(1, l.length()); - // Find final quote, ignoring escaped ones (\"). - // The escape_next logic is necessary to properly parse things like \\" - // where the backslash is the one being escaped, not the quote. - int end_pos = -1; - bool escape_next = false; - for (int i = 0; i < l.length(); i++) { - if (l[i] == '\\' && !escape_next) { - escape_next = true; - continue; + escape_next = false; } - if (l[i] == '"' && !escape_next) { - end_pos = i; - break; + if (end_pos == -1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line)); } - escape_next = false; - } + l = l.substr(0, end_pos); + l = l.c_unescape(); - if (end_pos == -1) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Expected '\"' at end of message while parsing: " + path + ":" + itos(line)); - } + if (status == STATUS_READING_ID) { + msg_id += l; + } else if (status == STATUS_READING_STRING) { + msg_str += l; + } else if (status == STATUS_READING_CONTEXT) { + msg_context += l; + } else if (status == STATUS_READING_PLURAL && plural_index >= 0) { + if (plural_index >= plural_forms) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Unexpected plural form while parsing: " + path + ":" + itos(line)); + } + msgs_plural.write[plural_index] = msgs_plural[plural_index] + l; + } - l = l.substr(0, end_pos); - l = l.c_unescape(); - - if (status == STATUS_READING_ID) { - msg_id += l; - } else if (status == STATUS_READING_STRING) { - msg_str += l; - } else if (status == STATUS_READING_CONTEXT) { - msg_context += l; - } else if (status == STATUS_READING_PLURAL && plural_index >= 0) { - msgs_plural.write[plural_index] = msgs_plural[plural_index] + l; + line++; } - line++; - } - - memdelete(f); + memdelete(f); - // Add the last set of data from last iteration. - if (status == STATUS_READING_STRING) { - if (!msg_id.is_empty()) { - if (!skip_this) { - translation->add_message(msg_id, msg_str, msg_context); + // Add the last set of data from last iteration. + if (status == STATUS_READING_STRING) { + if (!msg_id.is_empty()) { + if (!skip_this) { + translation->add_message(msg_id, msg_str, msg_context); + } + } else if (config.is_empty()) { + config = msg_str; } - } else if (config.is_empty()) { - config = msg_str; - } - } else if (status == STATUS_READING_PLURAL) { - if (!skip_this && !msg_id.is_empty()) { - if (plural_index != plural_forms - 1) { - memdelete(f); - ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } else if (status == STATUS_READING_PLURAL) { + if (!skip_this && !msg_id.is_empty()) { + if (plural_index != plural_forms - 1) { + memdelete(f); + ERR_FAIL_V_MSG(RES(), "Number of 'msgstr[]' doesn't match with number of plural forms: " + path + ":" + itos(line)); + } + translation->add_plural_message(msg_id, msgs_plural, msg_context); } - translation->add_plural_message(msg_id, msgs_plural, msg_context); } } @@ -290,6 +396,7 @@ RES TranslationLoaderPO::load(const String &p_path, const String &p_original_pat void TranslationLoaderPO::get_recognized_extensions(List<String> *p_extensions) const { p_extensions->push_back("po"); + p_extensions->push_back("mo"); } bool TranslationLoaderPO::handles_type(const String &p_type) const { @@ -297,7 +404,7 @@ bool TranslationLoaderPO::handles_type(const String &p_type) const { } String TranslationLoaderPO::get_resource_type(const String &p_path) const { - if (p_path.get_extension().to_lower() == "po") { + if (p_path.get_extension().to_lower() == "po" || p_path.get_extension().to_lower() == "mo") { return "Translation"; } return ""; diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index 14057b96be..4212b43621 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -33,7 +33,7 @@ #include "core/math/geometry_3d.h" #include "core/object/script_language.h" -int AStar::get_available_point_id() const { +int AStar3D::get_available_point_id() const { if (points.has(last_free_id)) { int cur_new_id = last_free_id + 1; while (points.has(cur_new_id)) { @@ -45,7 +45,7 @@ int AStar::get_available_point_id() const { return last_free_id; } -void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { +void AStar3D::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { ERR_FAIL_COND_MSG(p_id < 0, vformat("Can't add a point with negative id: %d.", p_id)); ERR_FAIL_COND_MSG(p_weight_scale < 1, vformat("Can't add a point with weight scale less than one: %f.", p_weight_scale)); @@ -68,7 +68,7 @@ void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { } } -Vector3 AStar::get_point_position(int p_id) const { +Vector3 AStar3D::get_point_position(int p_id) const { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_V_MSG(!p_exists, Vector3(), vformat("Can't get point's position. Point with id: %d doesn't exist.", p_id)); @@ -76,7 +76,7 @@ Vector3 AStar::get_point_position(int p_id) const { return p->pos; } -void AStar::set_point_position(int p_id, const Vector3 &p_pos) { +void AStar3D::set_point_position(int p_id, const Vector3 &p_pos) { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_MSG(!p_exists, vformat("Can't set point's position. Point with id: %d doesn't exist.", p_id)); @@ -84,7 +84,7 @@ void AStar::set_point_position(int p_id, const Vector3 &p_pos) { p->pos = p_pos; } -real_t AStar::get_point_weight_scale(int p_id) const { +real_t AStar3D::get_point_weight_scale(int p_id) const { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_V_MSG(!p_exists, 0, vformat("Can't get point's weight scale. Point with id: %d doesn't exist.", p_id)); @@ -92,7 +92,7 @@ real_t AStar::get_point_weight_scale(int p_id) const { return p->weight_scale; } -void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) { +void AStar3D::set_point_weight_scale(int p_id, real_t p_weight_scale) { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_MSG(!p_exists, vformat("Can't set point's weight scale. Point with id: %d doesn't exist.", p_id)); @@ -101,7 +101,7 @@ void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) { p->weight_scale = p_weight_scale; } -void AStar::remove_point(int p_id) { +void AStar3D::remove_point(int p_id) { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_MSG(!p_exists, vformat("Can't remove point. Point with id: %d doesn't exist.", p_id)); @@ -127,7 +127,7 @@ void AStar::remove_point(int p_id) { last_free_id = p_id; } -void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) { +void AStar3D::connect_points(int p_id, int p_with_id, bool bidirectional) { ERR_FAIL_COND_MSG(p_id == p_with_id, vformat("Can't connect point with id: %d to itself.", p_id)); Point *a; @@ -165,7 +165,7 @@ void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) { segments.insert(s); } -void AStar::disconnect_points(int p_id, int p_with_id, bool bidirectional) { +void AStar3D::disconnect_points(int p_id, int p_with_id, bool bidirectional) { Point *a; bool a_exists = points.lookup(p_id, a); ERR_FAIL_COND_MSG(!a_exists, vformat("Can't disconnect points. Point with id: %d doesn't exist.", p_id)); @@ -205,11 +205,11 @@ void AStar::disconnect_points(int p_id, int p_with_id, bool bidirectional) { } } -bool AStar::has_point(int p_id) const { +bool AStar3D::has_point(int p_id) const { return points.has(p_id); } -Array AStar::get_point_ids() { +Array AStar3D::get_point_ids() { Array point_list; for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { @@ -219,7 +219,7 @@ Array AStar::get_point_ids() { return point_list; } -Vector<int> AStar::get_point_connections(int p_id) { +Vector<int> AStar3D::get_point_connections(int p_id) { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_V_MSG(!p_exists, Vector<int>(), vformat("Can't get point's connections. Point with id: %d doesn't exist.", p_id)); @@ -233,7 +233,7 @@ Vector<int> AStar::get_point_connections(int p_id) { return point_list; } -bool AStar::are_points_connected(int p_id, int p_with_id, bool bidirectional) const { +bool AStar3D::are_points_connected(int p_id, int p_with_id, bool bidirectional) const { Segment s(p_id, p_with_id); const Set<Segment>::Element *element = segments.find(s); @@ -241,7 +241,7 @@ bool AStar::are_points_connected(int p_id, int p_with_id, bool bidirectional) co (bidirectional || (element->get().direction & s.direction) == s.direction); } -void AStar::clear() { +void AStar3D::clear() { last_free_id = 0; for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) { memdelete(*(it.value)); @@ -250,21 +250,21 @@ void AStar::clear() { points.clear(); } -int AStar::get_point_count() const { +int AStar3D::get_point_count() const { return points.get_num_elements(); } -int AStar::get_point_capacity() const { +int AStar3D::get_point_capacity() const { return points.get_capacity(); } -void AStar::reserve_space(int p_num_nodes) { +void AStar3D::reserve_space(int p_num_nodes) { ERR_FAIL_COND_MSG(p_num_nodes <= 0, vformat("New capacity must be greater than 0, new was: %d.", p_num_nodes)); ERR_FAIL_COND_MSG((uint32_t)p_num_nodes < points.get_capacity(), vformat("New capacity must be greater than current capacity: %d, new was: %d.", points.get_capacity(), p_num_nodes)); points.reserve(p_num_nodes); } -int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const { +int AStar3D::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const { int closest_id = -1; real_t closest_dist = 1e20; @@ -289,7 +289,7 @@ int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) co return closest_id; } -Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { +Vector3 AStar3D::get_closest_position_in_segment(const Vector3 &p_point) const { real_t closest_dist = 1e20; Vector3 closest_point; @@ -318,7 +318,7 @@ Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { return closest_point; } -bool AStar::_solve(Point *begin_point, Point *end_point) { +bool AStar3D::_solve(Point *begin_point, Point *end_point) { pass++; if (!end_point->enabled) { @@ -380,7 +380,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { return found_route; } -real_t AStar::_estimate_cost(int p_from_id, int p_to_id) { +real_t AStar3D::_estimate_cost(int p_from_id, int p_to_id) { real_t scost; if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_to_id, scost)) { return scost; @@ -397,7 +397,7 @@ real_t AStar::_estimate_cost(int p_from_id, int p_to_id) { return from_point->pos.distance_to(to_point->pos); } -real_t AStar::_compute_cost(int p_from_id, int p_to_id) { +real_t AStar3D::_compute_cost(int p_from_id, int p_to_id) { real_t scost; if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) { return scost; @@ -414,7 +414,7 @@ real_t AStar::_compute_cost(int p_from_id, int p_to_id) { return from_point->pos.distance_to(to_point->pos); } -Vector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { +Vector<Vector3> AStar3D::get_point_path(int p_from_id, int p_to_id) { Point *a; bool from_exists = points.lookup(p_from_id, a); ERR_FAIL_COND_V_MSG(!from_exists, Vector<Vector3>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id)); @@ -463,7 +463,7 @@ Vector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { return path; } -Vector<int> AStar::get_id_path(int p_from_id, int p_to_id) { +Vector<int> AStar3D::get_id_path(int p_from_id, int p_to_id) { Point *a; bool from_exists = points.lookup(p_from_id, a); ERR_FAIL_COND_V_MSG(!from_exists, Vector<int>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id)); @@ -512,7 +512,7 @@ Vector<int> AStar::get_id_path(int p_from_id, int p_to_id) { return path; } -void AStar::set_point_disabled(int p_id, bool p_disabled) { +void AStar3D::set_point_disabled(int p_id, bool p_disabled) { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_MSG(!p_exists, vformat("Can't set if point is disabled. Point with id: %d doesn't exist.", p_id)); @@ -520,7 +520,7 @@ void AStar::set_point_disabled(int p_id, bool p_disabled) { p->enabled = !p_disabled; } -bool AStar::is_point_disabled(int p_id) const { +bool AStar3D::is_point_disabled(int p_id) const { Point *p; bool p_exists = points.lookup(p_id, p); ERR_FAIL_COND_V_MSG(!p_exists, false, vformat("Can't get if point is disabled. Point with id: %d doesn't exist.", p_id)); @@ -528,41 +528,41 @@ bool AStar::is_point_disabled(int p_id) const { return !p->enabled; } -void AStar::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id); - ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0)); - ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position); - ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar::set_point_position); - ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale); - ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar::set_point_weight_scale); - ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point); - ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point); - ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar::get_point_connections); - ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar::get_point_ids); +void AStar3D::_bind_methods() { + ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar3D::get_available_point_id); + ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar3D::add_point, DEFVAL(1.0)); + ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar3D::get_point_position); + ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar3D::set_point_position); + ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar3D::get_point_weight_scale); + ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar3D::set_point_weight_scale); + ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar3D::remove_point); + ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar3D::has_point); + ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar3D::get_point_connections); + ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar3D::get_point_ids); - ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar::set_point_disabled, DEFVAL(true)); - ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar::is_point_disabled); + ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar3D::set_point_disabled, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar3D::is_point_disabled); - ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true)); - ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar::disconnect_points, DEFVAL(true)); - ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar::are_points_connected, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar3D::connect_points, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar3D::disconnect_points, DEFVAL(true)); + ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar3D::are_points_connected, DEFVAL(true)); - ClassDB::bind_method(D_METHOD("get_point_count"), &AStar::get_point_count); - ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar::get_point_capacity); - ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar::reserve_space); - ClassDB::bind_method(D_METHOD("clear"), &AStar::clear); + ClassDB::bind_method(D_METHOD("get_point_count"), &AStar3D::get_point_count); + ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar3D::get_point_capacity); + ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar3D::reserve_space); + ClassDB::bind_method(D_METHOD("clear"), &AStar3D::clear); - ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar::get_closest_point, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment); + ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar3D::get_closest_point, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar3D::get_closest_position_in_segment); - ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path); - ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path); + ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar3D::get_point_path); + ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar3D::get_id_path); GDVIRTUAL_BIND(_estimate_cost, "from_id", "to_id") GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id") } -AStar::~AStar() { +AStar3D::~AStar3D() { clear(); } @@ -660,11 +660,11 @@ real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) { return scost; } - AStar::Point *from_point; + AStar3D::Point *from_point; bool from_exists = astar.points.lookup(p_from_id, from_point); ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id)); - AStar::Point *to_point; + AStar3D::Point *to_point; bool to_exists = astar.points.lookup(p_to_id, to_point); ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_to_id)); @@ -677,11 +677,11 @@ real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) { return scost; } - AStar::Point *from_point; + AStar3D::Point *from_point; bool from_exists = astar.points.lookup(p_from_id, from_point); ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id)); - AStar::Point *to_point; + AStar3D::Point *to_point; bool to_exists = astar.points.lookup(p_to_id, to_point); ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id)); @@ -689,11 +689,11 @@ real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) { } Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { - AStar::Point *a; + AStar3D::Point *a; bool from_exists = astar.points.lookup(p_from_id, a); ERR_FAIL_COND_V_MSG(!from_exists, Vector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id)); - AStar::Point *b; + AStar3D::Point *b; bool to_exists = astar.points.lookup(p_to_id, b); ERR_FAIL_COND_V_MSG(!to_exists, Vector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_to_id)); @@ -702,15 +702,15 @@ Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { return ret; } - AStar::Point *begin_point = a; - AStar::Point *end_point = b; + AStar3D::Point *begin_point = a; + AStar3D::Point *end_point = b; bool found_route = _solve(begin_point, end_point); if (!found_route) { return Vector<Vector2>(); } - AStar::Point *p = end_point; + AStar3D::Point *p = end_point; int pc = 1; // Begin point while (p != begin_point) { pc++; @@ -723,7 +723,7 @@ Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { { Vector2 *w = path.ptrw(); - AStar::Point *p2 = end_point; + AStar3D::Point *p2 = end_point; int idx = pc - 1; while (p2 != begin_point) { w[idx--] = Vector2(p2->pos.x, p2->pos.y); @@ -737,11 +737,11 @@ Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) { } Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) { - AStar::Point *a; + AStar3D::Point *a; bool from_exists = astar.points.lookup(p_from_id, a); ERR_FAIL_COND_V_MSG(!from_exists, Vector<int>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id)); - AStar::Point *b; + AStar3D::Point *b; bool to_exists = astar.points.lookup(p_to_id, b); ERR_FAIL_COND_V_MSG(!to_exists, Vector<int>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_to_id)); @@ -751,15 +751,15 @@ Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) { return ret; } - AStar::Point *begin_point = a; - AStar::Point *end_point = b; + AStar3D::Point *begin_point = a; + AStar3D::Point *end_point = b; bool found_route = _solve(begin_point, end_point); if (!found_route) { return Vector<int>(); } - AStar::Point *p = end_point; + AStar3D::Point *p = end_point; int pc = 1; // Begin point while (p != begin_point) { pc++; @@ -785,7 +785,7 @@ Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) { return path; } -bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { +bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point) { astar.pass++; if (!end_point->enabled) { @@ -794,15 +794,15 @@ bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { bool found_route = false; - Vector<AStar::Point *> open_list; - SortArray<AStar::Point *, AStar::SortPoints> sorter; + Vector<AStar3D::Point *> open_list; + SortArray<AStar3D::Point *, AStar3D::SortPoints> sorter; begin_point->g_score = 0; begin_point->f_score = _estimate_cost(begin_point->id, end_point->id); open_list.push_back(begin_point); while (!open_list.is_empty()) { - AStar::Point *p = open_list[0]; // The currently processed point + AStar3D::Point *p = open_list[0]; // The currently processed point if (p == end_point) { found_route = true; @@ -813,8 +813,8 @@ bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { open_list.remove_at(open_list.size() - 1); p->closed_pass = astar.pass; // Mark the point as closed - for (OAHashMap<int, AStar::Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { - AStar::Point *e = *(it.value); // The neighbour point + for (OAHashMap<int, AStar3D::Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) { + AStar3D::Point *e = *(it.value); // The neighbour point if (!e->enabled || e->closed_pass == astar.pass) { continue; diff --git a/core/math/a_star.h b/core/math/a_star.h index 130c202a61..bb7112fb09 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -40,8 +40,8 @@ A* pathfinding algorithm. */ -class AStar : public RefCounted { - GDCLASS(AStar, RefCounted); +class AStar3D : public RefCounted { + GDCLASS(AStar3D, RefCounted); friend class AStar2D; struct Point { @@ -156,15 +156,15 @@ public: Vector<Vector3> get_point_path(int p_from_id, int p_to_id); Vector<int> get_id_path(int p_from_id, int p_to_id); - AStar() {} - ~AStar(); + AStar3D() {} + ~AStar3D(); }; class AStar2D : public RefCounted { GDCLASS(AStar2D, RefCounted); - AStar astar; + AStar3D astar; - bool _solve(AStar::Point *begin_point, AStar::Point *end_point); + bool _solve(AStar3D::Point *begin_point, AStar3D::Point *end_point); protected: static void _bind_methods(); diff --git a/core/register_core_types.cpp b/core/register_core_types.cpp index d05280f6a5..1a306e88fc 100644 --- a/core/register_core_types.cpp +++ b/core/register_core_types.cpp @@ -227,7 +227,7 @@ void register_core_types() { GDREGISTER_CLASS(PackedDataContainer); GDREGISTER_ABSTRACT_CLASS(PackedDataContainerRef); - GDREGISTER_CLASS(AStar); + GDREGISTER_CLASS(AStar3D); GDREGISTER_CLASS(AStar2D); GDREGISTER_CLASS(EncodedObjectAsID); GDREGISTER_CLASS(RandomNumberGenerator); |