diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/math/a_star.cpp | 55 | ||||
-rw-r--r-- | core/math/a_star.h | 6 | ||||
-rw-r--r-- | core/translation.cpp | 136 | ||||
-rw-r--r-- | core/translation.h | 3 |
4 files changed, 119 insertions, 81 deletions
diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index ffe1089965..f43af49754 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -42,8 +42,10 @@ int AStar::get_available_point_id() const { } void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) { + ERR_FAIL_COND(p_id < 0); ERR_FAIL_COND(p_weight_scale < 1); + if (!points.has(p_id)) { Point *pt = memnew(Point); pt->id = p_id; @@ -64,12 +66,29 @@ Vector3 AStar::get_point_position(int p_id) const { return points[p_id]->pos; } + +void AStar::set_point_position(int p_id, const Vector3 &p_pos) { + + ERR_FAIL_COND(!points.has(p_id)); + + points[p_id]->pos = p_pos; +} + real_t AStar::get_point_weight_scale(int p_id) const { ERR_FAIL_COND_V(!points.has(p_id), 0); return points[p_id]->weight_scale; } + +void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) { + + ERR_FAIL_COND(!points.has(p_id)); + ERR_FAIL_COND(p_weight_scale < 1); + + points[p_id]->weight_scale = p_weight_scale; +} + void AStar::remove_point(int p_id) { ERR_FAIL_COND(!points.has(p_id)); @@ -130,6 +149,7 @@ bool AStar::has_point(int p_id) const { } Array AStar::get_points() { + Array point_list; for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) { @@ -171,6 +191,7 @@ int AStar::get_closest_point(const Vector3 &p_point) const { return closest_id; } + Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const { real_t closest_dist = 1e20; @@ -222,15 +243,15 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { while (!found_route) { if (open_list.first() == NULL) { - //could not find path sadly + // No path found break; } - //check open list + // Check open list SelfList<Point> *least_cost_point = NULL; real_t least_cost = 1e30; - //this could be faster (cache previous results) + // TODO: Cache previous results for (SelfList<Point> *E = open_list.first(); E; E = E->next()) { Point *p = E->self(); @@ -246,7 +267,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { } Point *p = least_cost_point->self(); - //open the neighbours for search + // Open the neighbours for search int es = p->neighbours.size(); for (int i = 0; i < es; i++) { @@ -256,7 +277,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { real_t distance = _compute_cost(p->id, e->id) * e->weight_scale + p->distance; if (e->last_pass == pass) { - //oh this was visited already, can we win the cost? + // Already visited, is this cheaper? if (e->distance > distance) { @@ -264,15 +285,15 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { e->distance = distance; } } else { - //add to open neighbours + // Add to open neighbours e->prev_point = p; e->distance = distance; - e->last_pass = pass; //mark as used + e->last_pass = pass; // Mark as used open_list.add(&e->list); if (e == end_point) { - //oh my reached end! stop algorithm + // End reached; stop algorithm found_route = true; break; } @@ -285,7 +306,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { open_list.remove(least_cost_point); } - //clear the openf list + // Clear the openf list while (open_list.first()) { open_list.remove(open_list.first()); } @@ -294,6 +315,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { } float AStar::_estimate_cost(int p_from_id, int p_to_id) { + if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id); @@ -301,6 +323,7 @@ float AStar::_estimate_cost(int p_from_id, int p_to_id) { } float AStar::_compute_cost(int p_from_id, int p_to_id) { + if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id); @@ -331,9 +354,9 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { if (!found_route) return PoolVector<Vector3>(); - //midpoints + // Midpoints Point *p = end_point; - int pc = 1; //begin point + int pc = 1; // Begin point while (p != begin_point) { pc++; p = p->prev_point; @@ -352,7 +375,7 @@ PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) { p = p->prev_point; } - w[0] = p->pos; //assign first + w[0] = p->pos; // Assign first } return path; @@ -382,9 +405,9 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { if (!found_route) return PoolVector<int>(); - //midpoints + // Midpoints Point *p = end_point; - int pc = 1; //begin point + int pc = 1; // Begin point while (p != begin_point) { pc++; p = p->prev_point; @@ -403,7 +426,7 @@ PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) { p = p->prev_point; } - w[0] = p->id; //assign first + w[0] = p->id; // Assign first } return path; @@ -414,7 +437,9 @@ 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_points"), &AStar::get_points); diff --git a/core/math/a_star.h b/core/math/a_star.h index 2c1e2e2cf7..23773e82e2 100644 --- a/core/math/a_star.h +++ b/core/math/a_star.h @@ -33,6 +33,8 @@ #include "reference.h" #include "self_list.h" /** + A* pathfinding algorithm + @author Juan Linietsky <reduzio@gmail.com> */ @@ -53,7 +55,7 @@ class AStar : public Reference { Vector<Point *> neighbours; - //used for pathfinding + // Used for pathfinding Point *prev_point; real_t distance; @@ -102,7 +104,9 @@ public: void add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale = 1); Vector3 get_point_position(int p_id) const; + void set_point_position(int p_id, const Vector3 &p_pos); real_t get_point_weight_scale(int p_id) const; + void set_point_weight_scale(int p_id, real_t p_weight_scale); void remove_point(int p_id); bool has_point(int p_id) const; Array get_points(); diff --git a/core/translation.cpp b/core/translation.cpp index 54dbaf6ed5..058db956e5 100644 --- a/core/translation.cpp +++ b/core/translation.cpp @@ -753,65 +753,17 @@ static const char *locale_names[] = { 0 }; -bool TranslationServer::is_locale_valid(const String &p_locale) { - - const char **ptr = locale_list; - - while (*ptr) { - - if (*ptr == p_locale) - return true; - ptr++; - } - - return false; -} - -Vector<String> TranslationServer::get_all_locales() { - - Vector<String> locales; - - const char **ptr = locale_list; - - while (*ptr) { - locales.push_back(*ptr); - ptr++; - } - - return locales; -} - -Vector<String> TranslationServer::get_all_locale_names() { - - Vector<String> locales; - - const char **ptr = locale_names; - - while (*ptr) { - locales.push_back(*ptr); - ptr++; - } - - return locales; -} +static const char *locale_renames[][2] = { + { "no", "nb" }, + { NULL, NULL } +}; static String get_trimmed_locale(const String &p_locale) { return p_locale.substr(0, 2); } -static bool is_valid_locale(const String &p_locale) { - - const char **ptr = locale_list; - - while (*ptr) { - if (p_locale == *ptr) - return true; - ptr++; - } - - return false; -} +/////////////////////////////////////////////// PoolVector<String> Translation::_get_messages() const { @@ -857,14 +809,13 @@ void Translation::_set_messages(const PoolVector<String> &p_messages) { void Translation::set_locale(const String &p_locale) { - // replaces '-' with '_' for macOS Sierra-style locales - String univ_locale = p_locale.replace("-", "_"); + String univ_locale = TranslationServer::standardize_locale(p_locale); - if (!is_valid_locale(univ_locale)) { + if (!TranslationServer::is_locale_valid(univ_locale)) { String trimmed_locale = get_trimmed_locale(univ_locale); - ERR_EXPLAIN("Invalid Locale: " + trimmed_locale); - ERR_FAIL_COND(!is_valid_locale(trimmed_locale)); + ERR_EXPLAIN("Invalid locale: " + trimmed_locale); + ERR_FAIL_COND(!TranslationServer::is_locale_valid(trimmed_locale)); locale = trimmed_locale; } else { @@ -929,16 +880,47 @@ Translation::Translation() /////////////////////////////////////////////// -void TranslationServer::set_locale(const String &p_locale) { +bool TranslationServer::is_locale_valid(const String &p_locale) { + + const char **ptr = locale_list; + + while (*ptr) { + + if (*ptr == p_locale) + return true; + ptr++; + } + + return false; +} - // replaces '-' with '_' for macOS Sierra-style locales +String TranslationServer::standardize_locale(const String &p_locale) { + + // Replaces '-' with '_' for macOS Sierra-style locales String univ_locale = p_locale.replace("-", "_"); - if (!is_valid_locale(univ_locale)) { + // Handles known non-ISO locale names used e.g. on Windows + int idx = 0; + while (locale_renames[idx][0] != NULL) { + if (locale_renames[idx][0] == univ_locale) { + univ_locale = locale_renames[idx][1]; + break; + } + idx++; + } + + return univ_locale; +} + +void TranslationServer::set_locale(const String &p_locale) { + + String univ_locale = standardize_locale(p_locale); + + if (!is_locale_valid(univ_locale)) { String trimmed_locale = get_trimmed_locale(univ_locale); - ERR_EXPLAIN("Invalid Locale: " + trimmed_locale); - ERR_FAIL_COND(!is_valid_locale(trimmed_locale)); + ERR_EXPLAIN("Invalid locale: " + trimmed_locale); + ERR_FAIL_COND(!is_locale_valid(trimmed_locale)); locale = trimmed_locale; } else { @@ -963,6 +945,34 @@ String TranslationServer::get_locale_name(const String &p_locale) const { return locale_name_map[p_locale]; } +Vector<String> TranslationServer::get_all_locales() { + + Vector<String> locales; + + const char **ptr = locale_list; + + while (*ptr) { + locales.push_back(*ptr); + ptr++; + } + + return locales; +} + +Vector<String> TranslationServer::get_all_locale_names() { + + Vector<String> locales; + + const char **ptr = locale_names; + + while (*ptr) { + locales.push_back(*ptr); + ptr++; + } + + return locales; +} + void TranslationServer::add_translation(const Ref<Translation> &p_translation) { translations.insert(p_translation); diff --git a/core/translation.h b/core/translation.h index 5fec1d9f45..0cdab3b0bc 100644 --- a/core/translation.h +++ b/core/translation.h @@ -85,8 +85,6 @@ class TranslationServer : public Object { public: _FORCE_INLINE_ static TranslationServer *get_singleton() { return singleton; } - //yes, portuguese is supported! - void set_enabled(bool p_enabled) { enabled = p_enabled; } _FORCE_INLINE_ bool is_enabled() const { return enabled; } @@ -103,6 +101,7 @@ public: static Vector<String> get_all_locales(); static Vector<String> get_all_locale_names(); static bool is_locale_valid(const String &p_locale); + static String standardize_locale(const String &p_locale); void set_tool_translation(const Ref<Translation> &p_translation); StringName tool_translate(const StringName &p_message) const; |