diff options
Diffstat (limited to 'core')
69 files changed, 1729 insertions, 225 deletions
diff --git a/core/config/engine.h b/core/config/engine.h index 0d9aa02f28..c0fd64fcc4 100644 --- a/core/config/engine.h +++ b/core/config/engine.h @@ -50,7 +50,7 @@ private: uint64_t frames_drawn = 0; uint32_t _frame_delay = 0; uint64_t _frame_ticks = 0; - float _frame_step = 0; + float _process_step = 0; int ips = 60; float physics_jitter_fix = 0.5; @@ -62,7 +62,7 @@ private: bool abort_on_gpu_errors = false; bool use_validation_layers = false; - uint64_t _idle_frames = 0; + uint64_t _process_frames = 0; bool _in_physics = false; List<Singleton> singletons; @@ -89,10 +89,10 @@ public: uint64_t get_frames_drawn(); uint64_t get_physics_frames() const { return _physics_frames; } - uint64_t get_idle_frames() const { return _idle_frames; } + uint64_t get_process_frames() const { return _process_frames; } bool is_in_physics_frame() const { return _in_physics; } - uint64_t get_idle_frame_ticks() const { return _frame_ticks; } - float get_idle_frame_step() const { return _frame_step; } + uint64_t get_frame_ticks() const { return _frame_ticks; } + float get_process_step() const { return _process_step; } float get_physics_interpolation_fraction() const { return _physics_interpolation_fraction; } void set_time_scale(float p_scale); diff --git a/core/core_bind.cpp b/core/core_bind.cpp index 259d899d39..d769bd28b6 100644 --- a/core/core_bind.cpp +++ b/core/core_bind.cpp @@ -2278,8 +2278,8 @@ uint64_t _Engine::get_physics_frames() const { return Engine::get_singleton()->get_physics_frames(); } -uint64_t _Engine::get_idle_frames() const { - return Engine::get_singleton()->get_idle_frames(); +uint64_t _Engine::get_process_frames() const { + return Engine::get_singleton()->get_process_frames(); } void _Engine::set_time_scale(float p_scale) { @@ -2358,7 +2358,7 @@ void _Engine::_bind_methods() { ClassDB::bind_method(D_METHOD("get_frames_drawn"), &_Engine::get_frames_drawn); ClassDB::bind_method(D_METHOD("get_frames_per_second"), &_Engine::get_frames_per_second); ClassDB::bind_method(D_METHOD("get_physics_frames"), &_Engine::get_physics_frames); - ClassDB::bind_method(D_METHOD("get_idle_frames"), &_Engine::get_idle_frames); + ClassDB::bind_method(D_METHOD("get_process_frames"), &_Engine::get_process_frames); ClassDB::bind_method(D_METHOD("get_main_loop"), &_Engine::get_main_loop); diff --git a/core/core_bind.h b/core/core_bind.h index f3a77a4fa6..08634339ae 100644 --- a/core/core_bind.h +++ b/core/core_bind.h @@ -635,7 +635,7 @@ public: float get_frames_per_second() const; uint64_t get_physics_frames() const; - uint64_t get_idle_frames() const; + uint64_t get_process_frames() const; int get_frames_drawn(); diff --git a/core/core_constants.cpp b/core/core_constants.cpp index 02055012ad..555908ea79 100644 --- a/core/core_constants.cpp +++ b/core/core_constants.cpp @@ -112,10 +112,10 @@ VARIANT_ENUM_CAST(JoyAxisList); VARIANT_ENUM_CAST(MidiMessageList); void register_global_constants() { - BIND_CORE_ENUM_CONSTANT(MARGIN_LEFT); - BIND_CORE_ENUM_CONSTANT(MARGIN_TOP); - BIND_CORE_ENUM_CONSTANT(MARGIN_RIGHT); - BIND_CORE_ENUM_CONSTANT(MARGIN_BOTTOM); + BIND_CORE_ENUM_CONSTANT(SIDE_LEFT); + BIND_CORE_ENUM_CONSTANT(SIDE_TOP); + BIND_CORE_ENUM_CONSTANT(SIDE_RIGHT); + BIND_CORE_ENUM_CONSTANT(SIDE_BOTTOM); BIND_CORE_ENUM_CONSTANT(CORNER_TOP_LEFT); BIND_CORE_ENUM_CONSTANT(CORNER_TOP_RIGHT); @@ -525,6 +525,7 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_COLOR_NO_ALPHA); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_IMAGE_COMPRESS_LOSSY); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_IMAGE_COMPRESS_LOSSLESS); + BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_TYPE_STRING); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_STORAGE); BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_EDITOR); diff --git a/core/debugger/engine_debugger.cpp b/core/debugger/engine_debugger.cpp index 4bf31aa55f..9fef2ee9ea 100644 --- a/core/debugger/engine_debugger.cpp +++ b/core/debugger/engine_debugger.cpp @@ -117,9 +117,9 @@ void EngineDebugger::line_poll() { poll_every++; } -void EngineDebugger::iteration(uint64_t p_frame_ticks, uint64_t p_idle_ticks, uint64_t p_physics_ticks, float p_physics_frame_time) { +void EngineDebugger::iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, float p_physics_frame_time) { frame_time = USEC_TO_SEC(p_frame_ticks); - idle_time = USEC_TO_SEC(p_idle_ticks); + process_time = USEC_TO_SEC(p_process_ticks); physics_time = USEC_TO_SEC(p_physics_ticks); physics_frame_time = p_physics_frame_time; // Notify tick to running profilers @@ -128,14 +128,14 @@ void EngineDebugger::iteration(uint64_t p_frame_ticks, uint64_t p_idle_ticks, ui if (!p.active || !p.tick) { continue; } - p.tick(p.data, frame_time, idle_time, physics_time, physics_frame_time); + p.tick(p.data, frame_time, process_time, physics_time, physics_frame_time); } singleton->poll_events(true); } void EngineDebugger::initialize(const String &p_uri, bool p_skip_breakpoints, Vector<String> p_breakpoints) { register_uri_handler("tcp://", RemoteDebuggerPeerTCP::create); // TCP is the default protocol. Platforms/modules can add more. - if (p_uri.empty()) { + if (p_uri.is_empty()) { return; } if (p_uri == "local://") { diff --git a/core/debugger/engine_debugger.h b/core/debugger/engine_debugger.h index 10f04bf97a..96fb494484 100644 --- a/core/debugger/engine_debugger.h +++ b/core/debugger/engine_debugger.h @@ -44,7 +44,7 @@ class ScriptDebugger; class EngineDebugger { public: typedef void (*ProfilingToggle)(void *p_user, bool p_enable, const Array &p_opts); - typedef void (*ProfilingTick)(void *p_user, float p_frame_time, float p_idle_time, float p_physics_time, float p_physics_frame_time); + typedef void (*ProfilingTick)(void *p_user, float p_frame_time, float p_process_time, float p_physics_time, float p_physics_frame_time); typedef void (*ProfilingAdd)(void *p_user, const Array &p_arr); typedef Error (*CaptureFunc)(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured); @@ -86,7 +86,7 @@ public: private: float frame_time = 0.0; - float idle_time = 0.0; + float process_time = 0.0; float physics_time = 0.0; float physics_frame_time = 0.0; @@ -120,7 +120,7 @@ public: static void register_uri_handler(const String &p_protocol, CreatePeerFunc p_func); - void iteration(uint64_t p_frame_ticks, uint64_t p_idle_ticks, uint64_t p_physics_ticks, float p_physics_frame_time); + void iteration(uint64_t p_frame_ticks, uint64_t p_process_ticks, uint64_t p_physics_ticks, float p_physics_frame_time); void profiler_enable(const StringName &p_name, bool p_enabled, const Array &p_opts = Array()); Error capture_parse(const StringName &p_name, const String &p_msg, const Array &p_args, bool &r_captured); diff --git a/core/debugger/local_debugger.cpp b/core/debugger/local_debugger.cpp index 876be79418..e8401655f1 100644 --- a/core/debugger/local_debugger.cpp +++ b/core/debugger/local_debugger.cpp @@ -117,7 +117,7 @@ struct LocalDebugger::ScriptsProfiler { void LocalDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) { ScriptLanguage *script_lang = script_debugger->get_break_language(); - if (!target_function.empty()) { + if (!target_function.is_empty()) { String current_function = script_lang->debug_get_stack_level_function(0); if (current_function != target_function) { script_debugger->set_depth(0); @@ -259,7 +259,7 @@ void LocalDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) { String source = breakpoint.first; int linenr = breakpoint.second; - if (source.empty()) { + if (source.is_empty()) { continue; } @@ -285,7 +285,7 @@ void LocalDebugger::debug(bool p_can_continue, bool p_is_error_breakpoint) { String source = breakpoint.first; int linenr = breakpoint.second; - if (source.empty()) { + if (source.is_empty()) { continue; } @@ -323,7 +323,7 @@ void LocalDebugger::print_variables(const List<String> &names, const List<Varian for (const List<String>::Element *E = names.front(); E; E = E->next()) { value = String(V->get()); - if (variable_prefix.empty()) { + if (variable_prefix.is_empty()) { print_line(E->get() + ": " + String(V->get())); } else { print_line(E->get() + ":"); @@ -359,7 +359,7 @@ void LocalDebugger::send_message(const String &p_message, const Array &p_args) { } void LocalDebugger::send_error(const String &p_func, const String &p_file, int p_line, const String &p_err, const String &p_descr, ErrorHandlerType p_type) { - print_line("ERROR: '" + (p_descr.empty() ? p_err : p_descr) + "'"); + print_line("ERROR: '" + (p_descr.is_empty() ? p_err : p_descr) + "'"); } LocalDebugger::LocalDebugger() { diff --git a/core/debugger/remote_debugger.cpp b/core/debugger/remote_debugger.cpp index ff89517497..05307ebf4c 100644 --- a/core/debugger/remote_debugger.cpp +++ b/core/debugger/remote_debugger.cpp @@ -317,7 +317,7 @@ struct RemoteDebugger::ServersProfiler { void _send_frame_data(bool p_final) { DebuggerMarshalls::ServersProfilerFrame frame; - frame.frame_number = Engine::get_singleton()->get_idle_frames(); + frame.frame_number = Engine::get_singleton()->get_process_frames(); frame.frame_time = frame_time; frame.idle_time = idle_time; frame.physics_time = physics_time; @@ -553,7 +553,7 @@ void RemoteDebugger::flush_output() { for (int i = 0; i < output_strings.size(); i++) { const OutputString &output_string = output_strings[i]; if (output_string.type == MESSAGE_TYPE_ERROR) { - if (!joined_log_strings.empty()) { + if (!joined_log_strings.is_empty()) { strings.push_back(String("\n").join(joined_log_strings)); types.push_back(MESSAGE_TYPE_LOG); joined_log_strings.clear(); @@ -565,7 +565,7 @@ void RemoteDebugger::flush_output() { } } - if (!joined_log_strings.empty()) { + if (!joined_log_strings.is_empty()) { strings.push_back(String("\n").join(joined_log_strings)); types.push_back(MESSAGE_TYPE_LOG); } diff --git a/core/input/input.cpp b/core/input/input.cpp index 153656e44a..9759cd4888 100644 --- a/core/input/input.cpp +++ b/core/input/input.cpp @@ -247,7 +247,7 @@ bool Input::is_action_just_pressed(const StringName &p_action) const { if (Engine::get_singleton()->is_in_physics_frame()) { return E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames(); } else { - return E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames(); + return E->get().pressed && E->get().process_frame == Engine::get_singleton()->get_process_frames(); } } @@ -260,7 +260,7 @@ bool Input::is_action_just_released(const StringName &p_action) const { if (Engine::get_singleton()->is_in_physics_frame()) { return !E->get().pressed && E->get().physics_frame == Engine::get_singleton()->get_physics_frames(); } else { - return !E->get().pressed && E->get().idle_frame == Engine::get_singleton()->get_idle_frames(); + return !E->get().pressed && E->get().process_frame == Engine::get_singleton()->get_process_frames(); } } @@ -588,7 +588,7 @@ void Input::_parse_input_event_impl(const Ref<InputEvent> &p_event, bool p_is_em if (!p_event->is_echo() && is_action_pressed(E->key()) != p_event->is_action_pressed(E->key())) { Action action; action.physics_frame = Engine::get_singleton()->get_physics_frames(); - action.idle_frame = Engine::get_singleton()->get_idle_frames(); + action.process_frame = Engine::get_singleton()->get_process_frames(); action.pressed = p_event->is_action_pressed(E->key()); action.strength = 0.0f; action.raw_strength = 0.0f; @@ -714,7 +714,7 @@ void Input::action_press(const StringName &p_action, float p_strength) { Action action; action.physics_frame = Engine::get_singleton()->get_physics_frames(); - action.idle_frame = Engine::get_singleton()->get_idle_frames(); + action.process_frame = Engine::get_singleton()->get_process_frames(); action.pressed = true; action.strength = p_strength; @@ -725,7 +725,7 @@ void Input::action_release(const StringName &p_action) { Action action; action.physics_frame = Engine::get_singleton()->get_physics_frames(); - action.idle_frame = Engine::get_singleton()->get_idle_frames(); + action.process_frame = Engine::get_singleton()->get_process_frames(); action.pressed = false; action.strength = 0.f; @@ -806,7 +806,7 @@ void Input::accumulate_input_event(const Ref<InputEvent> &p_event) { parse_input_event(p_event); return; } - if (!accumulated_events.empty() && accumulated_events.back()->get()->accumulate(p_event)) { + if (!accumulated_events.is_empty() && accumulated_events.back()->get()->accumulate(p_event)) { return; //event was accumulated, exit } diff --git a/core/input/input.h b/core/input/input.h index 1b2df49ac0..39dc0bceff 100644 --- a/core/input/input.h +++ b/core/input/input.h @@ -114,7 +114,7 @@ private: struct Action { uint64_t physics_frame; - uint64_t idle_frame; + uint64_t process_frame; bool pressed; float strength; float raw_strength; diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp index 82bfaa82a5..bdfa5dff5f 100644 --- a/core/input/input_event.cpp +++ b/core/input/input_event.cpp @@ -210,7 +210,7 @@ String InputEventWithModifiers::as_text() const { mod_names.push_back(find_keycode_name(KEY_META)); } - if (!mod_names.empty()) { + if (!mod_names.is_empty()) { return String("+").join(mod_names); } else { return ""; diff --git a/core/input/input_map.cpp b/core/input/input_map.cpp index 979809c7af..45e2ddc71d 100644 --- a/core/input/input_map.cpp +++ b/core/input/input_map.cpp @@ -71,7 +71,7 @@ void InputMap::erase_action(const StringName &p_action) { Array InputMap::_get_actions() { Array ret; List<StringName> actions = get_actions(); - if (actions.empty()) { + if (actions.is_empty()) { return ret; } @@ -84,7 +84,7 @@ Array InputMap::_get_actions() { List<StringName> InputMap::get_actions() const { List<StringName> actions = List<StringName>(); - if (input_map.empty()) { + if (input_map.is_empty()) { return actions; } diff --git a/core/io/config_file.cpp b/core/io/config_file.cpp index 8be39178db..a53431cab2 100644 --- a/core/io/config_file.cpp +++ b/core/io/config_file.cpp @@ -67,7 +67,7 @@ void ConfigFile::set_value(const String &p_section, const String &p_key, const V return; // ? } values[p_section].erase(p_key); - if (values[p_section].empty()) { + if (values[p_section].is_empty()) { values.erase(p_section); } diff --git a/core/io/file_access_network.cpp b/core/io/file_access_network.cpp index 1e9266f118..207d5757ec 100644 --- a/core/io/file_access_network.cpp +++ b/core/io/file_access_network.cpp @@ -350,7 +350,7 @@ void FileAccessNetwork::_queue_page(int p_page) const { if (p_page >= pages.size()) { return; } - if (pages[p_page].buffer.empty() && !pages[p_page].queued) { + if (pages[p_page].buffer.is_empty() && !pages[p_page].queued) { FileAccessNetworkClient *nc = FileAccessNetworkClient::singleton; { MutexLock lock(nc->blockrequest_mutex); @@ -386,7 +386,7 @@ int FileAccessNetwork::get_buffer(uint8_t *p_dst, int p_length) const { if (page != last_page) { buffer_mutex.lock(); - if (pages[page].buffer.empty()) { + if (pages[page].buffer.is_empty()) { waiting_on_page = page; for (int j = 0; j < read_ahead; j++) { _queue_page(page + j); diff --git a/core/io/file_access_pack.cpp b/core/io/file_access_pack.cpp index a025ca5730..30c94d6a4d 100644 --- a/core/io/file_access_pack.cpp +++ b/core/io/file_access_pack.cpp @@ -89,7 +89,7 @@ void PackedData::add_path(const String &pkg_path, const String &path, uint64_t o } String filename = path.get_file(); // Don't add as a file if the path points to a directory - if (!filename.empty()) { + if (!filename.is_empty()) { cd->files.insert(filename); } } diff --git a/core/io/image.cpp b/core/io/image.cpp index 56d84325b5..fec7a6a0c5 100644 --- a/core/io/image.cpp +++ b/core/io/image.cpp @@ -375,7 +375,7 @@ Image::Image3DValidateError Image::validate_3d_image(Image::Format p_format, int if (idx >= p_images.size()) { return VALIDATE_3D_ERR_MISSING_IMAGES; } - if (p_images[idx].is_null() || p_images[idx]->empty()) { + if (p_images[idx].is_null() || p_images[idx]->is_empty()) { return VALIDATE_3D_ERR_IMAGE_EMPTY; } if (p_images[idx]->get_format() != p_format) { @@ -1756,7 +1756,7 @@ Error Image::generate_mipmap_roughness(RoughnessChannel p_roughness_channel, con double *normal_sat = nullptr; //summed area table for normalmap int normal_w = 0, normal_h = 0; - ERR_FAIL_COND_V_MSG(p_normal_map.is_null() || p_normal_map->empty(), ERR_INVALID_PARAMETER, "Must provide a valid normalmap for roughness mipmaps"); + ERR_FAIL_COND_V_MSG(p_normal_map.is_null() || p_normal_map->is_empty(), ERR_INVALID_PARAMETER, "Must provide a valid normalmap for roughness mipmaps"); Ref<Image> nm = p_normal_map->duplicate(); if (nm->is_compressed()) { @@ -1950,7 +1950,7 @@ void Image::clear_mipmaps() { return; } - if (empty()) { + if (is_empty()) { return; } @@ -1961,7 +1961,7 @@ void Image::clear_mipmaps() { mipmaps = false; } -bool Image::empty() const { +bool Image::is_empty() const { return (data.size() == 0); } @@ -3090,7 +3090,7 @@ void Image::_bind_methods() { ClassDB::bind_method(D_METHOD("create", "width", "height", "use_mipmaps", "format"), &Image::_create_empty); ClassDB::bind_method(D_METHOD("create_from_data", "width", "height", "use_mipmaps", "format", "data"), &Image::_create_from_data); - ClassDB::bind_method(D_METHOD("is_empty"), &Image::empty); + ClassDB::bind_method(D_METHOD("is_empty"), &Image::is_empty); ClassDB::bind_method(D_METHOD("load", "path"), &Image::load); ClassDB::bind_method(D_METHOD("save_png", "path"), &Image::save_png); @@ -3585,7 +3585,7 @@ Image::Image(const uint8_t *p_mem_png_jpg, int p_len) { copy_internals_from(_png_mem_loader_func(p_mem_png_jpg, p_len)); } - if (empty() && _jpg_mem_loader_func) { + if (is_empty() && _jpg_mem_loader_func) { copy_internals_from(_jpg_mem_loader_func(p_mem_png_jpg, p_len)); } } diff --git a/core/io/image.h b/core/io/image.h index 6b4488bd66..7280500c85 100644 --- a/core/io/image.h +++ b/core/io/image.h @@ -285,7 +285,7 @@ public: /** * returns true when the image is empty (0,0) in size */ - bool empty() const; + bool is_empty() const; Vector<uint8_t> get_data() const; diff --git a/core/io/ip.cpp b/core/io/ip.cpp index 9f3540efad..beb3c7fc2e 100644 --- a/core/io/ip.cpp +++ b/core/io/ip.cpp @@ -189,7 +189,7 @@ void IP::erase_resolve_item(ResolverID p_id) { void IP::clear_cache(const String &p_hostname) { MutexLock lock(resolver->mutex); - if (p_hostname.empty()) { + if (p_hostname.is_empty()) { resolver->cache.clear(); } else { resolver->cache.erase(_IP_ResolverPrivate::get_cache_key(p_hostname, IP::TYPE_NONE)); diff --git a/core/io/json.cpp b/core/io/json.cpp index d61c2b8236..f9d96c5e1f 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -47,7 +47,7 @@ const char *JSON::tk_name[TK_MAX] = { static String _make_indent(const String &p_indent, int p_size) { String indent_text = ""; - if (!p_indent.empty()) { + if (!p_indent.is_empty()) { for (int i = 0; i < p_size; i++) { indent_text += p_indent; } @@ -59,7 +59,7 @@ String JSON::_print_var(const Variant &p_var, const String &p_indent, int p_cur_ String colon = ":"; String end_statement = ""; - if (!p_indent.empty()) { + if (!p_indent.is_empty()) { colon += " "; end_statement += "\n"; } diff --git a/core/io/pck_packer.cpp b/core/io/pck_packer.cpp index 5480d3c535..f2b0c80aab 100644 --- a/core/io/pck_packer.cpp +++ b/core/io/pck_packer.cpp @@ -53,7 +53,7 @@ void PCKPacker::_bind_methods() { } Error PCKPacker::pck_start(const String &p_file, int p_alignment, const String &p_key, bool p_encrypt_directory) { - ERR_FAIL_COND_V_MSG((p_key.empty() || !p_key.is_valid_hex_number(false) || p_key.length() != 64), ERR_CANT_CREATE, "Invalid Encryption Key (must be 64 characters long)."); + ERR_FAIL_COND_V_MSG((p_key.is_empty() || !p_key.is_valid_hex_number(false) || p_key.length() != 64), ERR_CANT_CREATE, "Invalid Encryption Key (must be 64 characters long)."); String _key = p_key.to_lower(); key.resize(32); diff --git a/core/io/translation_loader_po.cpp b/core/io/translation_loader_po.cpp index 34cccca540..88cd089663 100644 --- a/core/io/translation_loader_po.cpp +++ b/core/io/translation_loader_po.cpp @@ -70,7 +70,7 @@ RES TranslationLoaderPO::load_translation(FileAccess *f, Error *r_error) { 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.empty()) { + 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)); diff --git a/core/math/a_star.cpp b/core/math/a_star.cpp index b4410acf7d..6b97cba9f9 100644 --- a/core/math/a_star.cpp +++ b/core/math/a_star.cpp @@ -35,7 +35,7 @@ #include "scene/scene_string_names.h" int AStar::get_available_point_id() const { - if (points.empty()) { + if (points.is_empty()) { return 1; } @@ -341,7 +341,7 @@ bool AStar::_solve(Point *begin_point, Point *end_point) { begin_point->f_score = _estimate_cost(begin_point->id, end_point->id); open_list.push_back(begin_point); - while (!open_list.empty()) { + while (!open_list.is_empty()) { Point *p = open_list[0]; // The currently processed point if (p == end_point) { @@ -805,7 +805,7 @@ bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) { begin_point->f_score = _estimate_cost(begin_point->id, end_point->id); open_list.push_back(begin_point); - while (!open_list.empty()) { + while (!open_list.is_empty()) { AStar::Point *p = open_list[0]; // The currently processed point if (p == end_point) { diff --git a/core/math/aabb.h b/core/math/aabb.h index 474304eae2..24908ae59d 100644 --- a/core/math/aabb.h +++ b/core/math/aabb.h @@ -107,6 +107,9 @@ public: Variant intersects_segment_bind(const Vector3 &p_from, const Vector3 &p_to) const; Variant intersects_ray_bind(const Vector3 &p_from, const Vector3 &p_dir) const; + _FORCE_INLINE_ void quantize(float p_unit); + _FORCE_INLINE_ AABB quantized(float p_unit) const; + _FORCE_INLINE_ void set_end(const Vector3 &p_end) { size = p_end - position; } @@ -427,4 +430,28 @@ void AABB::grow_by(real_t p_amount) { size.z += 2.0 * p_amount; } +void AABB::quantize(float p_unit) { + size += position; + + position.x -= Math::fposmodp(position.x, p_unit); + position.y -= Math::fposmodp(position.y, p_unit); + position.z -= Math::fposmodp(position.z, p_unit); + + size.x -= Math::fposmodp(size.x, p_unit); + size.y -= Math::fposmodp(size.y, p_unit); + size.z -= Math::fposmodp(size.z, p_unit); + + size.x += p_unit; + size.y += p_unit; + size.z += p_unit; + + size -= position; +} + +AABB AABB::quantized(float p_unit) const { + AABB ret = *this; + ret.quantize(p_unit); + return ret; +} + #endif // AABB_H diff --git a/core/math/dynamic_bvh.cpp b/core/math/dynamic_bvh.cpp new file mode 100644 index 0000000000..23e713dd2d --- /dev/null +++ b/core/math/dynamic_bvh.cpp @@ -0,0 +1,431 @@ +/*************************************************************************/ +/* dynamic_bvh.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#include "dynamic_bvh.h" + +void DynamicBVH::_delete_node(Node *p_node) { + node_allocator.free(p_node); +} + +void DynamicBVH::_recurse_delete_node(Node *p_node) { + if (!p_node->is_leaf()) { + _recurse_delete_node(p_node->childs[0]); + _recurse_delete_node(p_node->childs[1]); + } + if (p_node == bvh_root) { + bvh_root = nullptr; + } + _delete_node(p_node); +} + +DynamicBVH::Node *DynamicBVH::_create_node(Node *p_parent, void *p_data) { + Node *node = node_allocator.alloc(); + node->parent = p_parent; + node->data = p_data; + return (node); +} + +DynamicBVH::Node *DynamicBVH::_create_node_with_volume(Node *p_parent, const Volume &p_volume, void *p_data) { + Node *node = _create_node(p_parent, p_data); + node->volume = p_volume; + return node; +} + +void DynamicBVH::_insert_leaf(Node *p_root, Node *p_leaf) { + if (!bvh_root) { + bvh_root = p_leaf; + p_leaf->parent = 0; + } else { + if (!p_root->is_leaf()) { + do { + p_root = p_root->childs[p_leaf->volume.select_by_proximity( + p_root->childs[0]->volume, + p_root->childs[1]->volume)]; + } while (!p_root->is_leaf()); + } + Node *prev = p_root->parent; + Node *node = _create_node_with_volume(prev, p_leaf->volume.merge(p_root->volume), 0); + if (prev) { + prev->childs[p_root->get_index_in_parent()] = node; + node->childs[0] = p_root; + p_root->parent = node; + node->childs[1] = p_leaf; + p_leaf->parent = node; + do { + if (!prev->volume.contains(node->volume)) { + prev->volume = prev->childs[0]->volume.merge(prev->childs[1]->volume); + } else { + break; + } + node = prev; + } while (0 != (prev = node->parent)); + } else { + node->childs[0] = p_root; + p_root->parent = node; + node->childs[1] = p_leaf; + p_leaf->parent = node; + bvh_root = node; + } + } +} + +DynamicBVH::Node *DynamicBVH::_remove_leaf(Node *leaf) { + if (leaf == bvh_root) { + bvh_root = 0; + return (0); + } else { + Node *parent = leaf->parent; + Node *prev = parent->parent; + Node *sibling = parent->childs[1 - leaf->get_index_in_parent()]; + if (prev) { + prev->childs[parent->get_index_in_parent()] = sibling; + sibling->parent = prev; + _delete_node(parent); + while (prev) { + const Volume pb = prev->volume; + prev->volume = prev->childs[0]->volume.merge(prev->childs[1]->volume); + if (pb.is_not_equal_to(prev->volume)) { + prev = prev->parent; + } else + break; + } + return (prev ? prev : bvh_root); + } else { + bvh_root = sibling; + sibling->parent = 0; + _delete_node(parent); + return (bvh_root); + } + } +} + +void DynamicBVH::_fetch_leaves(Node *p_root, LocalVector<Node *> &r_leaves, int p_depth) { + if (p_root->is_internal() && p_depth) { + _fetch_leaves(p_root->childs[0], r_leaves, p_depth - 1); + _fetch_leaves(p_root->childs[1], r_leaves, p_depth - 1); + _delete_node(p_root); + } else { + r_leaves.push_back(p_root); + } +} + +// Partitions leaves such that leaves[0, n) are on the +// left of axis, and leaves[n, count) are on the right +// of axis. returns N. +int DynamicBVH::_split(Node **leaves, int p_count, const Vector3 &p_org, const Vector3 &p_axis) { + int begin = 0; + int end = p_count; + for (;;) { + while (begin != end && leaves[begin]->is_left_of_axis(p_org, p_axis)) { + ++begin; + } + + if (begin == end) { + break; + } + + while (begin != end && !leaves[end - 1]->is_left_of_axis(p_org, p_axis)) { + --end; + } + + if (begin == end) { + break; + } + + // swap out of place nodes + --end; + Node *temp = leaves[begin]; + leaves[begin] = leaves[end]; + leaves[end] = temp; + ++begin; + } + + return begin; +} + +DynamicBVH::Volume DynamicBVH::_bounds(Node **leaves, int p_count) { + Volume volume = leaves[0]->volume; + for (int i = 1, ni = p_count; i < ni; ++i) { + volume = volume.merge(leaves[i]->volume); + } + return (volume); +} + +void DynamicBVH::_bottom_up(Node **leaves, int p_count) { + while (p_count > 1) { + real_t minsize = Math_INF; + int minidx[2] = { -1, -1 }; + for (int i = 0; i < p_count; ++i) { + for (int j = i + 1; j < p_count; ++j) { + const real_t sz = leaves[i]->volume.merge(leaves[j]->volume).get_size(); + if (sz < minsize) { + minsize = sz; + minidx[0] = i; + minidx[1] = j; + } + } + } + Node *n[] = { leaves[minidx[0]], leaves[minidx[1]] }; + Node *p = _create_node_with_volume(nullptr, n[0]->volume.merge(n[1]->volume), nullptr); + p->childs[0] = n[0]; + p->childs[1] = n[1]; + n[0]->parent = p; + n[1]->parent = p; + leaves[minidx[0]] = p; + leaves[minidx[1]] = leaves[p_count - 1]; + --p_count; + } +} + +DynamicBVH::Node *DynamicBVH::_top_down(Node **leaves, int p_count, int p_bu_threshold) { + static const Vector3 axis[] = { Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1) }; + + ERR_FAIL_COND_V(p_bu_threshold <= 1, nullptr); + if (p_count > 1) { + if (p_count > p_bu_threshold) { + const Volume vol = _bounds(leaves, p_count); + const Vector3 org = vol.get_center(); + int partition; + int bestaxis = -1; + int bestmidp = p_count; + int splitcount[3][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 } }; + int i; + for (i = 0; i < p_count; ++i) { + const Vector3 x = leaves[i]->volume.get_center() - org; + for (int j = 0; j < 3; ++j) { + ++splitcount[j][x.dot(axis[j]) > 0 ? 1 : 0]; + } + } + for (i = 0; i < 3; ++i) { + if ((splitcount[i][0] > 0) && (splitcount[i][1] > 0)) { + const int midp = (int)Math::abs(real_t(splitcount[i][0] - splitcount[i][1])); + if (midp < bestmidp) { + bestaxis = i; + bestmidp = midp; + } + } + } + if (bestaxis >= 0) { + partition = _split(leaves, p_count, org, axis[bestaxis]); + ERR_FAIL_COND_V(partition == 0 || partition == p_count, nullptr); + } else { + partition = p_count / 2 + 1; + } + + Node *node = _create_node_with_volume(nullptr, vol, nullptr); + node->childs[0] = _top_down(&leaves[0], partition, p_bu_threshold); + node->childs[1] = _top_down(&leaves[partition], p_count - partition, p_bu_threshold); + node->childs[0]->parent = node; + node->childs[1]->parent = node; + return (node); + } else { + _bottom_up(leaves, p_count); + return (leaves[0]); + } + } + return (leaves[0]); +} + +DynamicBVH::Node *DynamicBVH::_node_sort(Node *n, Node *&r) { + Node *p = n->parent; + ERR_FAIL_COND_V(!n->is_internal(), nullptr); + if (p > n) { + const int i = n->get_index_in_parent(); + const int j = 1 - i; + Node *s = p->childs[j]; + Node *q = p->parent; + ERR_FAIL_COND_V(n != p->childs[i], nullptr); + if (q) + q->childs[p->get_index_in_parent()] = n; + else + r = n; + s->parent = n; + p->parent = n; + n->parent = q; + p->childs[0] = n->childs[0]; + p->childs[1] = n->childs[1]; + n->childs[0]->parent = p; + n->childs[1]->parent = p; + n->childs[i] = p; + n->childs[j] = s; + SWAP(p->volume, n->volume); + return (p); + } + return (n); +} + +void DynamicBVH::clear() { + if (bvh_root) { + _recurse_delete_node(bvh_root); + } + lkhd = -1; + opath = 0; +} + +void DynamicBVH::optimize_bottom_up() { + if (bvh_root) { + LocalVector<Node *> leaves; + _fetch_leaves(bvh_root, leaves); + _bottom_up(&leaves[0], leaves.size()); + bvh_root = leaves[0]; + } +} + +void DynamicBVH::optimize_top_down(int bu_threshold) { + if (bvh_root) { + LocalVector<Node *> leaves; + _fetch_leaves(bvh_root, leaves); + bvh_root = _top_down(&leaves[0], leaves.size(), bu_threshold); + } +} + +void DynamicBVH::optimize_incremental(int passes) { + if (passes < 0) + passes = total_leaves; + if (bvh_root && (passes > 0)) { + do { + Node *node = bvh_root; + unsigned bit = 0; + while (node->is_internal()) { + node = _node_sort(node, bvh_root)->childs[(opath >> bit) & 1]; + bit = (bit + 1) & (sizeof(unsigned) * 8 - 1); + } + _update(node); + ++opath; + } while (--passes); + } +} + +DynamicBVH::ID DynamicBVH::insert(const AABB &p_box, void *p_userdata) { + Volume volume; + volume.min = p_box.position; + volume.max = p_box.position + p_box.size; + + Node *leaf = _create_node_with_volume(nullptr, volume, p_userdata); + _insert_leaf(bvh_root, leaf); + ++total_leaves; + + ID id; + id.node = leaf; + + return id; +} + +void DynamicBVH::_update(Node *leaf, int lookahead) { + Node *root = _remove_leaf(leaf); + if (root) { + if (lookahead >= 0) { + for (int i = 0; (i < lookahead) && root->parent; ++i) { + root = root->parent; + } + } else + root = bvh_root; + } + _insert_leaf(root, leaf); +} + +bool DynamicBVH::update(const ID &p_id, const AABB &p_box) { + ERR_FAIL_COND_V(!p_id.is_valid(), false); + Node *leaf = p_id.node; + + Volume volume; + volume.min = p_box.position; + volume.max = p_box.position + p_box.size; + + if (leaf->volume.min.is_equal_approx(volume.min) && leaf->volume.max.is_equal_approx(volume.max)) { + // noop + return false; + } + + Node *base = _remove_leaf(leaf); + if (base) { + if (lkhd >= 0) { + for (int i = 0; (i < lkhd) && base->parent; ++i) { + base = base->parent; + } + } else + base = bvh_root; + } + leaf->volume = volume; + _insert_leaf(base, leaf); + return true; +} + +void DynamicBVH::remove(const ID &p_id) { + ERR_FAIL_COND(!p_id.is_valid()); + Node *leaf = p_id.node; + _remove_leaf(leaf); + _delete_node(leaf); + --total_leaves; +} + +void DynamicBVH::_extract_leaves(Node *p_node, List<ID> *r_elements) { + if (p_node->is_internal()) { + _extract_leaves(p_node->childs[0], r_elements); + _extract_leaves(p_node->childs[1], r_elements); + } else { + ID id; + id.node = p_node; + r_elements->push_back(id); + } +} + +void DynamicBVH::set_index(uint32_t p_index) { + ERR_FAIL_COND(bvh_root != nullptr); + index = p_index; +} + +uint32_t DynamicBVH::get_index() const { + return index; +} + +void DynamicBVH::get_elements(List<ID> *r_elements) { + if (bvh_root) { + _extract_leaves(bvh_root, r_elements); + } +} + +int DynamicBVH::get_leaf_count() const { + return total_leaves; +} +int DynamicBVH::get_max_depth() const { + if (bvh_root) { + int depth = 1; + int max_depth = 0; + bvh_root->get_max_depth(depth, max_depth); + return max_depth; + } else { + return 0; + } +} + +DynamicBVH::~DynamicBVH() { + clear(); +} diff --git a/core/math/dynamic_bvh.h b/core/math/dynamic_bvh.h new file mode 100644 index 0000000000..8b0d390465 --- /dev/null +++ b/core/math/dynamic_bvh.h @@ -0,0 +1,468 @@ +/*************************************************************************/ +/* dynamic_bvh.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef DYNAMICBVH_H +#define DYNAMICBVH_H + +#include "core/math/aabb.h" +#include "core/templates/list.h" +#include "core/templates/local_vector.h" +#include "core/templates/paged_allocator.h" +#include "core/typedefs.h" + +// Based on bullet Dbvh + +/* +Bullet Continuous Collision Detection and Physics Library +Copyright (c) 2003-2013 Erwin Coumans http://bulletphysics.org + +This software is provided 'as-is', without any express or implied warranty. +In no event will the authors be held liable for any damages arising from the use of this software. +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it freely, +subject to the following restrictions: + +1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. +2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. +3. This notice may not be removed or altered from any source distribution. +*/ + +///DynamicBVH implementation by Nathanael Presson +// The DynamicBVH class implements a fast dynamic bounding volume tree based on axis aligned bounding boxes (aabb tree). + +class DynamicBVH { + struct Node; + +public: + struct ID { + Node *node = nullptr; + + public: + _FORCE_INLINE_ bool is_valid() const { return node != nullptr; } + }; + +private: + struct Volume { + Vector3 min, max; + + _FORCE_INLINE_ Vector3 get_center() const { return ((min + max) / 2); } + _FORCE_INLINE_ Vector3 get_length() const { return (max - min); } + + _FORCE_INLINE_ bool contains(const Volume &a) const { + return ((min.x <= a.min.x) && + (min.y <= a.min.y) && + (min.z <= a.min.z) && + (max.x >= a.max.x) && + (max.y >= a.max.y) && + (max.z >= a.max.z)); + } + + _FORCE_INLINE_ Volume merge(const Volume &b) const { + Volume r; + for (int i = 0; i < 3; ++i) { + if (min[i] < b.min[i]) + r.min[i] = min[i]; + else + r.min[i] = b.min[i]; + if (max[i] > b.max[i]) + r.max[i] = max[i]; + else + r.max[i] = b.max[i]; + } + return r; + } + + _FORCE_INLINE_ real_t get_size() const { + const Vector3 edges = get_length(); + return (edges.x * edges.y * edges.z + + edges.x + edges.y + edges.z); + } + + _FORCE_INLINE_ bool is_not_equal_to(const Volume &b) const { + return ((min.x != b.min.x) || + (min.y != b.min.y) || + (min.z != b.min.z) || + (max.x != b.max.x) || + (max.y != b.max.y) || + (max.z != b.max.z)); + } + + _FORCE_INLINE_ real_t get_proximity_to(const Volume &b) const { + const Vector3 d = (min + max) - (b.min + b.max); + return (Math::abs(d.x) + Math::abs(d.y) + Math::abs(d.z)); + } + + _FORCE_INLINE_ int select_by_proximity(const Volume &a, const Volume &b) const { + return (get_proximity_to(a) < get_proximity_to(b) ? 0 : 1); + } + + // + _FORCE_INLINE_ bool intersects(const Volume &b) const { + return ((min.x <= b.max.x) && + (max.x >= b.min.x) && + (min.y <= b.max.y) && + (max.y >= b.min.y) && + (min.z <= b.max.z) && + (max.z >= b.min.z)); + } + + _FORCE_INLINE_ bool intersects_convex(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count) const { + Vector3 half_extents = (max - min) * 0.5; + Vector3 ofs = min + half_extents; + + for (int i = 0; i < p_plane_count; i++) { + const Plane &p = p_planes[i]; + Vector3 point( + (p.normal.x > 0) ? -half_extents.x : half_extents.x, + (p.normal.y > 0) ? -half_extents.y : half_extents.y, + (p.normal.z > 0) ? -half_extents.z : half_extents.z); + point += ofs; + if (p.is_point_over(point)) { + return false; + } + } + + // Make sure all points in the shape aren't fully separated from the AABB on + // each axis. + int bad_point_counts_positive[3] = { 0 }; + int bad_point_counts_negative[3] = { 0 }; + + for (int k = 0; k < 3; k++) { + for (int i = 0; i < p_point_count; i++) { + if (p_points[i].coord[k] > ofs.coord[k] + half_extents.coord[k]) { + bad_point_counts_positive[k]++; + } + if (p_points[i].coord[k] < ofs.coord[k] - half_extents.coord[k]) { + bad_point_counts_negative[k]++; + } + } + + if (bad_point_counts_negative[k] == p_point_count) { + return false; + } + if (bad_point_counts_positive[k] == p_point_count) { + return false; + } + } + + return true; + } + }; + + struct Node { + Volume volume; + Node *parent = nullptr; + union { + Node *childs[2]; + void *data; + }; + + _FORCE_INLINE_ bool is_leaf() const { return childs[1] == nullptr; } + _FORCE_INLINE_ bool is_internal() const { return (!is_leaf()); } + + _FORCE_INLINE_ int get_index_in_parent() const { + ERR_FAIL_COND_V(!parent, 0); + return (parent->childs[1] == this) ? 1 : 0; + } + void get_max_depth(int depth, int &maxdepth) { + if (is_internal()) { + childs[0]->get_max_depth(depth + 1, maxdepth); + childs[1]->get_max_depth(depth + 1, maxdepth); + } else { + maxdepth = MAX(maxdepth, depth); + } + } + + // + int count_leaves() const { + if (is_internal()) + return childs[0]->count_leaves() + childs[1]->count_leaves(); + else + return (1); + } + + bool is_left_of_axis(const Vector3 &org, const Vector3 &axis) const { + return axis.dot(volume.get_center() - org) <= 0; + } + + Node() { + childs[0] = nullptr; + childs[1] = nullptr; + } + }; + + PagedAllocator<Node> node_allocator; + // Fields + Node *bvh_root = nullptr; + int lkhd = -1; + int total_leaves = 0; + uint32_t opath = 0; + uint32_t index = 0; + + enum { + ALLOCA_STACK_SIZE = 128 + }; + + _FORCE_INLINE_ void _delete_node(Node *p_node); + void _recurse_delete_node(Node *p_node); + _FORCE_INLINE_ Node *_create_node(Node *p_parent, void *p_data); + _FORCE_INLINE_ DynamicBVH::Node *_create_node_with_volume(Node *p_parent, const Volume &p_volume, void *p_data); + _FORCE_INLINE_ void _insert_leaf(Node *p_root, Node *p_leaf); + _FORCE_INLINE_ Node *_remove_leaf(Node *leaf); + void _fetch_leaves(Node *p_root, LocalVector<Node *> &r_leaves, int p_depth = -1); + static int _split(Node **leaves, int p_count, const Vector3 &p_org, const Vector3 &p_axis); + static Volume _bounds(Node **leaves, int p_count); + void _bottom_up(Node **leaves, int p_count); + Node *_top_down(Node **leaves, int p_count, int p_bu_threshold); + Node *_node_sort(Node *n, Node *&r); + + _FORCE_INLINE_ void _update(Node *leaf, int lookahead = -1); + + void _extract_leaves(Node *p_node, List<ID> *r_elements); + + _FORCE_INLINE_ bool _ray_aabb(const Vector3 &rayFrom, const Vector3 &rayInvDirection, const unsigned int raySign[3], const Vector3 bounds[2], real_t &tmin, real_t lambda_min, real_t lambda_max) { + real_t tmax, tymin, tymax, tzmin, tzmax; + tmin = (bounds[raySign[0]].x - rayFrom.x) * rayInvDirection.x; + tmax = (bounds[1 - raySign[0]].x - rayFrom.x) * rayInvDirection.x; + tymin = (bounds[raySign[1]].y - rayFrom.y) * rayInvDirection.y; + tymax = (bounds[1 - raySign[1]].y - rayFrom.y) * rayInvDirection.y; + + if ((tmin > tymax) || (tymin > tmax)) + return false; + + if (tymin > tmin) + tmin = tymin; + + if (tymax < tmax) + tmax = tymax; + + tzmin = (bounds[raySign[2]].z - rayFrom.z) * rayInvDirection.z; + tzmax = (bounds[1 - raySign[2]].z - rayFrom.z) * rayInvDirection.z; + + if ((tmin > tzmax) || (tzmin > tmax)) + return false; + if (tzmin > tmin) + tmin = tzmin; + if (tzmax < tmax) + tmax = tzmax; + return ((tmin < lambda_max) && (tmax > lambda_min)); + } + +public: + // Methods + void clear(); + bool is_empty() const { return (0 == bvh_root); } + void optimize_bottom_up(); + void optimize_top_down(int bu_threshold = 128); + void optimize_incremental(int passes); + ID insert(const AABB &p_box, void *p_userdata); + bool update(const ID &p_id, const AABB &p_box); + void remove(const ID &p_id); + void get_elements(List<ID> *r_elements); + + int get_leaf_count() const; + int get_max_depth() const; + + /* Discouraged, but works as a reference on how it must be used */ + struct DefaultQueryResult { + virtual bool operator()(void *p_data) = 0; //return true whether you want to continue the query + virtual ~DefaultQueryResult() {} + }; + + template <class QueryResult> + _FORCE_INLINE_ void aabb_query(const AABB &p_aabb, QueryResult &r_result); + template <class QueryResult> + _FORCE_INLINE_ void convex_query(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, QueryResult &r_result); + template <class QueryResult> + _FORCE_INLINE_ void ray_query(const Vector3 &p_from, const Vector3 &p_to, QueryResult &r_result); + + void set_index(uint32_t p_index); + uint32_t get_index() const; + + ~DynamicBVH(); +}; + +template <class QueryResult> +void DynamicBVH::aabb_query(const AABB &p_box, QueryResult &r_result) { + if (!bvh_root) { + return; + } + + Volume volume; + volume.min = p_box.position; + volume.max = p_box.position + p_box.size; + + const Node **stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *)); + stack[0] = bvh_root; + int32_t depth = 1; + int32_t threshold = ALLOCA_STACK_SIZE - 2; + + LocalVector<const Node *> aux_stack; //only used in rare occasions when you run out of alloca memory because tree is too unbalanced. Should correct itself over time. + + do { + depth--; + const Node *n = stack[depth]; + if (n->volume.intersects(volume)) { + if (n->is_internal()) { + if (depth > threshold) { + if (aux_stack.is_empty()) { + aux_stack.resize(ALLOCA_STACK_SIZE * 2); + copymem(aux_stack.ptr(), stack, ALLOCA_STACK_SIZE * sizeof(const Node *)); + } else { + aux_stack.resize(aux_stack.size() * 2); + } + stack = aux_stack.ptr(); + threshold = aux_stack.size() - 2; + } + stack[depth++] = n->childs[0]; + stack[depth++] = n->childs[1]; + } else { + if (r_result(n->data)) { + return; + } + } + } + } while (depth > 0); +} + +template <class QueryResult> +void DynamicBVH::convex_query(const Plane *p_planes, int p_plane_count, const Vector3 *p_points, int p_point_count, QueryResult &r_result) { + if (!bvh_root) { + return; + } + + //generate a volume anyway to improve pre-testing + Volume volume; + for (int i = 0; i < p_point_count; i++) { + if (i == 0) { + volume.min = p_points[0]; + volume.max = p_points[0]; + } else { + volume.min.x = MIN(volume.min.x, p_points[i].x); + volume.min.y = MIN(volume.min.y, p_points[i].y); + volume.min.z = MIN(volume.min.z, p_points[i].z); + + volume.max.x = MAX(volume.max.x, p_points[i].x); + volume.max.y = MAX(volume.max.y, p_points[i].y); + volume.max.z = MAX(volume.max.z, p_points[i].z); + } + } + + const Node **stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *)); + stack[0] = bvh_root; + int32_t depth = 1; + int32_t threshold = ALLOCA_STACK_SIZE - 2; + + LocalVector<const Node *> aux_stack; //only used in rare occasions when you run out of alloca memory because tree is too unbalanced. Should correct itself over time. + + do { + depth--; + const Node *n = stack[depth]; + if (n->volume.intersects(volume) && n->volume.intersects_convex(p_planes, p_plane_count, p_points, p_point_count)) { + if (n->is_internal()) { + if (depth > threshold) { + if (aux_stack.is_empty()) { + aux_stack.resize(ALLOCA_STACK_SIZE * 2); + copymem(aux_stack.ptr(), stack, ALLOCA_STACK_SIZE * sizeof(const Node *)); + } else { + aux_stack.resize(aux_stack.size() * 2); + } + stack = aux_stack.ptr(); + threshold = aux_stack.size() - 2; + } + stack[depth++] = n->childs[0]; + stack[depth++] = n->childs[1]; + } else { + if (r_result(n->data)) { + return; + } + } + } + } while (depth > 0); +} +template <class QueryResult> +void DynamicBVH::ray_query(const Vector3 &p_from, const Vector3 &p_to, QueryResult &r_result) { + if (!bvh_root) { + return; + } + + Vector3 ray_dir = (p_to - p_from); + ray_dir.normalize(); + + ///what about division by zero? --> just set rayDirection[i] to INF/B3_LARGE_FLOAT + Vector3 inv_dir; + inv_dir[0] = ray_dir[0] == real_t(0.0) ? real_t(1e20) : real_t(1.0) / ray_dir[0]; + inv_dir[1] = ray_dir[1] == real_t(0.0) ? real_t(1e20) : real_t(1.0) / ray_dir[1]; + inv_dir[2] = ray_dir[2] == real_t(0.0) ? real_t(1e20) : real_t(1.0) / ray_dir[2]; + unsigned int signs[3] = { inv_dir[0] < 0.0, inv_dir[1] < 0.0, inv_dir[2] < 0.0 }; + + real_t lambda_max = ray_dir.dot(p_to - p_from); + + Vector3 bounds[2]; + + const Node **stack = (const Node **)alloca(ALLOCA_STACK_SIZE * sizeof(const Node *)); + stack[0] = bvh_root; + int32_t depth = 1; + int32_t threshold = ALLOCA_STACK_SIZE - 2; + + LocalVector<const Node *> aux_stack; //only used in rare occasions when you run out of alloca memory because tree is too unbalanced. Should correct itself over time. + + do { + depth--; + const Node *node = stack[depth]; + bounds[0] = node->volume.min; + bounds[1] = node->volume.max; + real_t tmin = 1.f, lambda_min = 0.f; + unsigned int result1 = false; + result1 = _ray_aabb(p_from, inv_dir, signs, bounds, tmin, lambda_min, lambda_max); + if (result1) { + if (node->is_internal()) { + if (depth > threshold) { + if (aux_stack.is_empty()) { + aux_stack.resize(ALLOCA_STACK_SIZE * 2); + copymem(aux_stack.ptr(), stack, ALLOCA_STACK_SIZE * sizeof(const Node *)); + } else { + aux_stack.resize(aux_stack.size() * 2); + } + stack = aux_stack.ptr(); + threshold = aux_stack.size() - 2; + } + stack[depth++] = node->childs[0]; + stack[depth++] = node->childs[1]; + } else { + if (r_result(node->data)) { + return; + } + } + } + } while (depth > 0); +} + +#endif // DYNAMICBVH_H diff --git a/core/math/math_defs.h b/core/math/math_defs.h index 4d97f72ebf..0478de732a 100644 --- a/core/math/math_defs.h +++ b/core/math/math_defs.h @@ -83,11 +83,11 @@ enum VAlign { VALIGN_BOTTOM }; -enum Margin { - MARGIN_LEFT, - MARGIN_TOP, - MARGIN_RIGHT, - MARGIN_BOTTOM +enum Side { + SIDE_LEFT, + SIDE_TOP, + SIDE_RIGHT, + SIDE_BOTTOM }; enum Corner { diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 827637bf2b..471aa58996 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -198,6 +198,23 @@ public: value += 0.0; return value; } + static _ALWAYS_INLINE_ float fposmodp(float p_x, float p_y) { + float value = Math::fmod(p_x, p_y); + if (value < 0) { + value += p_y; + } + value += 0.0; + return value; + } + static _ALWAYS_INLINE_ double fposmodp(double p_x, double p_y) { + double value = Math::fmod(p_x, p_y); + if (value < 0) { + value += p_y; + } + value += 0.0; + return value; + } + static _ALWAYS_INLINE_ int posmod(int p_x, int p_y) { int value = p_x % p_y; if ((value < 0 && p_y > 0) || (value > 0 && p_y < 0)) { diff --git a/core/math/octree.h b/core/math/octree.h index be1e7d6a61..f7ff4faef3 100644 --- a/core/math/octree.h +++ b/core/math/octree.h @@ -572,7 +572,7 @@ bool Octree<T, use_pairs, AL>::_remove_element_from_octant(Element *p_element, O Octant *parent = p_octant->parent; - if (p_octant->children_count == 0 && p_octant->elements.empty() && p_octant->pairable_elements.empty()) { + if (p_octant->children_count == 0 && p_octant->elements.is_empty() && p_octant->pairable_elements.is_empty()) { // erase octant if (p_octant == root) { // won't have a parent, just erase @@ -942,7 +942,7 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p return; //pointless } - if (!p_octant->elements.empty()) { + if (!p_octant->elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); @@ -965,7 +965,7 @@ void Octree<T, use_pairs, AL>::_cull_convex(Octant *p_octant, _CullConvexData *p } } - if (use_pairs && !p_octant->pairable_elements.empty()) { + if (use_pairs && !p_octant->pairable_elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); @@ -1001,7 +1001,7 @@ void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, return; //pointless } - if (!p_octant->elements.empty()) { + if (!p_octant->elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); for (; I; I = I->next()) { @@ -1027,7 +1027,7 @@ void Octree<T, use_pairs, AL>::_cull_aabb(Octant *p_octant, const AABB &p_aabb, } } - if (use_pairs && !p_octant->pairable_elements.empty()) { + if (use_pairs && !p_octant->pairable_elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); for (; I; I = I->next()) { @@ -1065,7 +1065,7 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ return; //pointless } - if (!p_octant->elements.empty()) { + if (!p_octant->elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); for (; I; I = I->next()) { @@ -1091,7 +1091,7 @@ void Octree<T, use_pairs, AL>::_cull_segment(Octant *p_octant, const Vector3 &p_ } } - if (use_pairs && !p_octant->pairable_elements.empty()) { + if (use_pairs && !p_octant->pairable_elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); for (; I; I = I->next()) { @@ -1132,7 +1132,7 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po return; //pointless } - if (!p_octant->elements.empty()) { + if (!p_octant->elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->elements.front(); for (; I; I = I->next()) { @@ -1158,7 +1158,7 @@ void Octree<T, use_pairs, AL>::_cull_point(Octant *p_octant, const Vector3 &p_po } } - if (use_pairs && !p_octant->pairable_elements.empty()) { + if (use_pairs && !p_octant->pairable_elements.is_empty()) { typename List<Element *, AL>::Element *I; I = p_octant->pairable_elements.front(); for (; I; I = I->next()) { diff --git a/core/math/random_pcg.cpp b/core/math/random_pcg.cpp index e0768b9403..c39037747a 100644 --- a/core/math/random_pcg.cpp +++ b/core/math/random_pcg.cpp @@ -51,16 +51,8 @@ float RandomPCG::random(float p_from, float p_to) { } int RandomPCG::random(int p_from, int p_to) { - int range; - int min; - if (p_to > p_from) { - range = p_to - p_from + 1; - min = p_from; - } else if (p_to < p_from) { - range = p_from - p_to + 1; - min = p_to; - } else { // from == to + if (p_from == p_to) { return p_from; } - return rand(range) + min; + return rand(abs(p_from - p_to) + 1) + MIN(p_from, p_to); } diff --git a/core/math/rect2.h b/core/math/rect2.h index f36084ec74..aecba9e88c 100644 --- a/core/math/rect2.h +++ b/core/math/rect2.h @@ -189,17 +189,17 @@ struct Rect2 { return g; } - inline Rect2 grow_margin(Margin p_margin, real_t p_amount) const { + inline Rect2 grow_margin(Side p_side, real_t p_amount) const { Rect2 g = *this; - g = g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0, - (MARGIN_TOP == p_margin) ? p_amount : 0, - (MARGIN_RIGHT == p_margin) ? p_amount : 0, - (MARGIN_BOTTOM == p_margin) ? p_amount : 0); + g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0, + (SIDE_TOP == p_side) ? p_amount : 0, + (SIDE_RIGHT == p_side) ? p_amount : 0, + (SIDE_BOTTOM == p_side) ? p_amount : 0); return g; } - inline Rect2 grow_margin_bind(uint32_t p_margin, real_t p_amount) const { - return grow_margin(Margin(p_margin), p_amount); + inline Rect2 grow_margin_bind(uint32_t p_side, real_t p_amount) const { + return grow_margin(Side(p_side), p_amount); } inline Rect2 grow_individual(real_t p_left, real_t p_top, real_t p_right, real_t p_bottom) const { @@ -431,17 +431,17 @@ struct Rect2i { return g; } - inline Rect2i grow_margin(Margin p_margin, int p_amount) const { + inline Rect2i grow_margin(Side p_side, int p_amount) const { Rect2i g = *this; - g = g.grow_individual((MARGIN_LEFT == p_margin) ? p_amount : 0, - (MARGIN_TOP == p_margin) ? p_amount : 0, - (MARGIN_RIGHT == p_margin) ? p_amount : 0, - (MARGIN_BOTTOM == p_margin) ? p_amount : 0); + g = g.grow_individual((SIDE_LEFT == p_side) ? p_amount : 0, + (SIDE_TOP == p_side) ? p_amount : 0, + (SIDE_RIGHT == p_side) ? p_amount : 0, + (SIDE_BOTTOM == p_side) ? p_amount : 0); return g; } - inline Rect2i grow_margin_bind(uint32_t p_margin, int p_amount) const { - return grow_margin(Margin(p_margin), p_amount); + inline Rect2i grow_margin_bind(uint32_t p_side, int p_amount) const { + return grow_margin(Side(p_side), p_amount); } inline Rect2i grow_individual(int p_left, int p_top, int p_right, int p_bottom) const { diff --git a/core/object/class_db.cpp b/core/object/class_db.cpp index f5171f60ec..6cdaacf164 100644 --- a/core/object/class_db.cpp +++ b/core/object/class_db.cpp @@ -377,7 +377,7 @@ uint64_t ClassDB::get_api_hash(APIType p_api) { while ((k = t->method_map.next(k))) { String name = k->operator String(); - ERR_CONTINUE(name.empty()); + ERR_CONTINUE(name.is_empty()); if (name[0] == '_') { continue; // Ignore non-virtual methods that start with an underscore diff --git a/core/object/object.cpp b/core/object/object.cpp index 681e1188ff..a53925f990 100644 --- a/core/object/object.cpp +++ b/core/object/object.cpp @@ -506,7 +506,7 @@ Variant Object::get(const StringName &p_name, bool *r_valid) const { } void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_value, bool *r_valid) { - if (p_names.empty()) { + if (p_names.is_empty()) { if (r_valid) { *r_valid = false; } @@ -561,11 +561,11 @@ void Object::set_indexed(const Vector<StringName> &p_names, const Variant &p_val set(p_names[0], value_stack.back()->get(), r_valid); value_stack.pop_back(); - ERR_FAIL_COND(!value_stack.empty()); + ERR_FAIL_COND(!value_stack.is_empty()); } Variant Object::get_indexed(const Vector<StringName> &p_names, bool *r_valid) const { - if (p_names.empty()) { + if (p_names.is_empty()) { if (r_valid) { *r_valid = false; } @@ -599,7 +599,7 @@ void Object::get_property_list(List<PropertyInfo> *p_list, bool p_reversed) cons if (!is_class("Script")) { // can still be set, but this is for userfriendlyness p_list->push_back(PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT)); } - if (!metadata.empty()) { + if (!metadata.is_empty()) { p_list->push_back(PropertyInfo(Variant::DICTIONARY, "__meta__", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL)); } if (script_instance && !p_reversed) { @@ -1089,7 +1089,7 @@ Error Object::emit_signal(const StringName &p_name, const Variant **p_args, int } } - while (!disconnect_data.empty()) { + while (!disconnect_data.is_empty()) { const _ObjectSignalDisconnectData &dd = disconnect_data.front()->get(); _disconnect(dd.signal, dd.callable); @@ -1373,7 +1373,7 @@ void Object::_disconnect(const StringName &p_signal, const Callable &p_callable, target_object->connections.erase(slot->cE); s->slot_map.erase(*p_callable.get_base_comparator()); - if (s->slot_map.empty() && ClassDB::has_signal(get_class_name(), p_signal)) { + if (s->slot_map.is_empty() && ClassDB::has_signal(get_class_name(), p_signal)) { //not user signal, delete signal_map.erase(p_signal); } diff --git a/core/object/object.h b/core/object/object.h index dc004f38a9..6e2328848b 100644 --- a/core/object/object.h +++ b/core/object/object.h @@ -482,8 +482,6 @@ private: void _set_indexed_bind(const NodePath &p_name, const Variant &p_value); Variant _get_indexed_bind(const NodePath &p_name) const; - void property_list_changed_notify(); - _FORCE_INLINE_ void _construct_object(bool p_reference); friend class Reference; @@ -525,6 +523,7 @@ protected: static void get_valid_parents_static(List<String> *p_parents); static void _get_valid_parents_static(List<String> *p_parents); + void property_list_changed_notify(); virtual void _changed_callback(Object *p_changed, const char *p_prop); //Variant _call_bind(const StringName& p_name, const Variant& p_arg1 = Variant(), const Variant& p_arg2 = Variant(), const Variant& p_arg3 = Variant(), const Variant& p_arg4 = Variant()); diff --git a/core/object/script_language.cpp b/core/object/script_language.cpp index 17ac75e19f..f9cc517448 100644 --- a/core/object/script_language.cpp +++ b/core/object/script_language.cpp @@ -275,7 +275,7 @@ void ScriptServer::save_global_classes() { gcarr.push_back(d); } - if (gcarr.empty()) { + if (gcarr.is_empty()) { if (ProjectSettings::get_singleton()->has_setting("_global_script_classes")) { ProjectSettings::get_singleton()->clear("_global_script_classes"); } diff --git a/core/object/undo_redo.cpp b/core/object/undo_redo.cpp index 1dcbb0cd6b..65bae6f6e8 100644 --- a/core/object/undo_redo.cpp +++ b/core/object/undo_redo.cpp @@ -30,6 +30,7 @@ #include "undo_redo.h" +#include "core/io/resource.h" #include "core/os/os.h" void UndoRedo::_discard_redo() { @@ -104,8 +105,8 @@ void UndoRedo::add_do_method(Object *p_object, const StringName &p_method, VARIA ERR_FAIL_COND((current_action + 1) >= actions.size()); Operation do_op; do_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) { - do_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + if (Object::cast_to<Reference>(p_object)) { + do_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object)); } do_op.type = Operation::TYPE_METHOD; @@ -130,8 +131,8 @@ void UndoRedo::add_undo_method(Object *p_object, const StringName &p_method, VAR Operation undo_op; undo_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) { - undo_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + if (Object::cast_to<Reference>(p_object)) { + undo_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object)); } undo_op.type = Operation::TYPE_METHOD; @@ -149,8 +150,8 @@ void UndoRedo::add_do_property(Object *p_object, const StringName &p_property, c ERR_FAIL_COND((current_action + 1) >= actions.size()); Operation do_op; do_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) { - do_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + if (Object::cast_to<Reference>(p_object)) { + do_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object)); } do_op.type = Operation::TYPE_PROPERTY; @@ -171,8 +172,8 @@ void UndoRedo::add_undo_property(Object *p_object, const StringName &p_property, Operation undo_op; undo_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) { - undo_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + if (Object::cast_to<Reference>(p_object)) { + undo_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object)); } undo_op.type = Operation::TYPE_PROPERTY; @@ -187,8 +188,8 @@ void UndoRedo::add_do_reference(Object *p_object) { ERR_FAIL_COND((current_action + 1) >= actions.size()); Operation do_op; do_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) { - do_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + if (Object::cast_to<Reference>(p_object)) { + do_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object)); } do_op.type = Operation::TYPE_REFERENCE; @@ -207,8 +208,8 @@ void UndoRedo::add_undo_reference(Object *p_object) { Operation undo_op; undo_op.object = p_object->get_instance_id(); - if (Object::cast_to<Resource>(p_object)) { - undo_op.resref = Ref<Resource>(Object::cast_to<Resource>(p_object)); + if (Object::cast_to<Reference>(p_object)) { + undo_op.ref = Ref<Reference>(Object::cast_to<Reference>(p_object)); } undo_op.type = Operation::TYPE_REFERENCE; diff --git a/core/object/undo_redo.h b/core/object/undo_redo.h index 68d78e0d7d..06c2759a65 100644 --- a/core/object/undo_redo.h +++ b/core/object/undo_redo.h @@ -31,8 +31,8 @@ #ifndef UNDO_REDO_H #define UNDO_REDO_H -#include "core/io/resource.h" #include "core/object/class_db.h" +#include "core/object/reference.h" class UndoRedo : public Object { GDCLASS(UndoRedo, Object); @@ -61,7 +61,7 @@ private: }; Type type; - Ref<Resource> resref; + Ref<Reference> ref; ObjectID object; StringName name; Variant args[VARIANT_ARG_MAX]; diff --git a/core/os/main_loop.cpp b/core/os/main_loop.cpp index 9252ba0840..0247b6a1d8 100644 --- a/core/os/main_loop.cpp +++ b/core/os/main_loop.cpp @@ -34,8 +34,8 @@ void MainLoop::_bind_methods() { BIND_VMETHOD(MethodInfo("_initialize")); - BIND_VMETHOD(MethodInfo(Variant::BOOL, "_iteration", PropertyInfo(Variant::FLOAT, "delta"))); - BIND_VMETHOD(MethodInfo(Variant::BOOL, "_idle", PropertyInfo(Variant::FLOAT, "delta"))); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "_physics_process", PropertyInfo(Variant::FLOAT, "delta"))); + BIND_VMETHOD(MethodInfo(Variant::BOOL, "_process", PropertyInfo(Variant::FLOAT, "delta"))); BIND_VMETHOD(MethodInfo("_finalize")); BIND_CONSTANT(NOTIFICATION_OS_MEMORY_WARNING); @@ -52,13 +52,13 @@ void MainLoop::_bind_methods() { ADD_SIGNAL(MethodInfo("on_request_permissions_result", PropertyInfo(Variant::STRING, "permission"), PropertyInfo(Variant::BOOL, "granted"))); }; -void MainLoop::set_init_script(const Ref<Script> &p_init_script) { - init_script = p_init_script; +void MainLoop::set_initialize_script(const Ref<Script> &p_initialize_script) { + initialize_script = p_initialize_script; } -void MainLoop::init() { - if (init_script.is_valid()) { - set_script(init_script); +void MainLoop::initialize() { + if (initialize_script.is_valid()) { + set_script(initialize_script); } if (get_script_instance()) { @@ -66,23 +66,23 @@ void MainLoop::init() { } } -bool MainLoop::iteration(float p_time) { +bool MainLoop::physics_process(float p_time) { if (get_script_instance()) { - return get_script_instance()->call("_iteration", p_time); + return get_script_instance()->call("_physics_process", p_time); } return false; } -bool MainLoop::idle(float p_time) { +bool MainLoop::process(float p_time) { if (get_script_instance()) { - return get_script_instance()->call("_idle", p_time); + return get_script_instance()->call("_process", p_time); } return false; } -void MainLoop::finish() { +void MainLoop::finalize() { if (get_script_instance()) { get_script_instance()->call("_finalize"); set_script(Variant()); //clear script diff --git a/core/os/main_loop.h b/core/os/main_loop.h index 7bc91fbb83..d48663b68a 100644 --- a/core/os/main_loop.h +++ b/core/os/main_loop.h @@ -39,7 +39,7 @@ class MainLoop : public Object { GDCLASS(MainLoop, Object); OBJ_CATEGORY("Main Loop"); - Ref<Script> init_script; + Ref<Script> initialize_script; protected: static void _bind_methods(); @@ -59,12 +59,12 @@ public: NOTIFICATION_TEXT_SERVER_CHANGED = 2018, }; - virtual void init(); - virtual bool iteration(float p_time); - virtual bool idle(float p_time); - virtual void finish(); + virtual void initialize(); + virtual bool physics_process(float p_time); + virtual bool process(float p_time); + virtual void finalize(); - void set_init_script(const Ref<Script> &p_init_script); + void set_initialize_script(const Ref<Script> &p_initialize_script); MainLoop() {} virtual ~MainLoop() {} diff --git a/core/string/string_buffer.h b/core/string/string_buffer.h index 1317b538d4..6f58e128be 100644 --- a/core/string/string_buffer.h +++ b/core/string/string_buffer.h @@ -40,7 +40,7 @@ class StringBuffer { int string_length = 0; _FORCE_INLINE_ char32_t *current_buffer_ptr() { - return static_cast<String &>(buffer).empty() ? short_buffer : buffer.ptrw(); + return static_cast<String &>(buffer).is_empty() ? short_buffer : buffer.ptrw(); } public: @@ -122,7 +122,7 @@ StringBuffer<SHORT_BUFFER_SIZE> &StringBuffer<SHORT_BUFFER_SIZE>::reserve(int p_ return *this; } - bool need_copy = string_length > 0 && buffer.empty(); + bool need_copy = string_length > 0 && buffer.is_empty(); buffer.resize(next_power_of_2(p_size)); if (need_copy) { memcpy(buffer.ptrw(), short_buffer, string_length * sizeof(char32_t)); @@ -139,7 +139,7 @@ int StringBuffer<SHORT_BUFFER_SIZE>::length() const { template <int SHORT_BUFFER_SIZE> String StringBuffer<SHORT_BUFFER_SIZE>::as_string() { current_buffer_ptr()[string_length] = '\0'; - if (buffer.empty()) { + if (buffer.is_empty()) { return String(short_buffer); } else { buffer.resize(string_length + 1); diff --git a/core/string/string_name.h b/core/string/string_name.h index 320f63bf68..1c0a4e582d 100644 --- a/core/string/string_name.h +++ b/core/string/string_name.h @@ -83,7 +83,7 @@ class StringName { StringName(_Data *p_data) { _data = p_data; } public: - operator const void *() const { return (_data && (_data->cname || !_data->name.empty())) ? (void *)1 : nullptr; } + operator const void *() const { return (_data && (_data->cname || !_data->name.is_empty())) ? (void *)1 : nullptr; } bool operator==(const String &p_name) const; bool operator==(const char *p_name) const; diff --git a/core/string/translation.cpp b/core/string/translation.cpp index 7b8c28e2e2..5828f0e127 100644 --- a/core/string/translation.cpp +++ b/core/string/translation.cpp @@ -851,7 +851,7 @@ void Translation::add_message(const StringName &p_src_text, const StringName &p_ void Translation::add_plural_message(const StringName &p_src_text, const Vector<String> &p_plural_xlated_texts, const StringName &p_context) { WARN_PRINT("Translation class doesn't handle plural messages. Calling add_plural_message() on a Translation instance is probably a mistake. \nUse a derived Translation class that handles plurals, such as TranslationPO class"); - ERR_FAIL_COND_MSG(p_plural_xlated_texts.empty(), "Parameter vector p_plural_xlated_texts passed in is empty."); + ERR_FAIL_COND_MSG(p_plural_xlated_texts.is_empty(), "Parameter vector p_plural_xlated_texts passed in is empty."); translation_map[p_src_text] = p_plural_xlated_texts[0]; } diff --git a/core/string/translation_po.cpp b/core/string/translation_po.cpp index 203f29026b..0892741021 100644 --- a/core/string/translation_po.cpp +++ b/core/string/translation_po.cpp @@ -230,7 +230,7 @@ StringName TranslationPO::get_message(const StringName &p_src_text, const String if (!translation_map.has(p_context) || !translation_map[p_context].has(p_src_text)) { return StringName(); } - ERR_FAIL_COND_V_MSG(translation_map[p_context][p_src_text].empty(), StringName(), "Source text \"" + String(p_src_text) + "\" is registered but doesn't have a translation. Please report this bug."); + ERR_FAIL_COND_V_MSG(translation_map[p_context][p_src_text].is_empty(), StringName(), "Source text \"" + String(p_src_text) + "\" is registered but doesn't have a translation. Please report this bug."); return translation_map[p_context][p_src_text][0]; } @@ -246,7 +246,7 @@ StringName TranslationPO::get_plural_message(const StringName &p_src_text, const if (!translation_map.has(p_context) || !translation_map[p_context].has(p_src_text)) { return StringName(); } - ERR_FAIL_COND_V_MSG(translation_map[p_context][p_src_text].empty(), StringName(), "Source text \"" + String(p_src_text) + "\" is registered but doesn't have a translation. Please report this bug."); + ERR_FAIL_COND_V_MSG(translation_map[p_context][p_src_text].is_empty(), StringName(), "Source text \"" + String(p_src_text) + "\" is registered but doesn't have a translation. Please report this bug."); if (translation_map[p_context][p_src_text].size() == 1) { WARN_PRINT("Source string \"" + String(p_src_text) + "\" doesn't have plural translations. Use singular translation API for such as tr(), TTR() to translate \"" + String(p_src_text) + "\""); diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index d630e987ea..3276038c4d 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -427,12 +427,12 @@ String operator+(char32_t p_chr, const String &p_str) { } String &String::operator+=(const String &p_str) { - if (empty()) { + if (is_empty()) { *this = p_str; return *this; } - if (p_str.empty()) { + if (p_str.is_empty()) { return *this; } @@ -519,7 +519,7 @@ bool String::operator==(const char *p_str) const { if (length() != len) { return false; } - if (empty()) { + if (is_empty()) { return true; } @@ -558,7 +558,7 @@ bool String::operator==(const char32_t *p_str) const { if (length() != len) { return false; } - if (empty()) { + if (is_empty()) { return true; } @@ -580,7 +580,7 @@ bool String::operator==(const String &p_str) const { if (length() != p_str.length()) { return false; } - if (empty()) { + if (is_empty()) { return true; } @@ -605,7 +605,7 @@ bool String::operator==(const StrRange &p_str_range) const { if (length() != len) { return false; } - if (empty()) { + if (is_empty()) { return true; } @@ -678,20 +678,20 @@ bool String::operator>=(const String &p_str) const { } bool String::operator<(const char *p_str) const { - if (empty() && p_str[0] == 0) { + if (is_empty() && p_str[0] == 0) { return false; } - if (empty()) { + if (is_empty()) { return true; } return is_str_less(get_data(), p_str); } bool String::operator<(const wchar_t *p_str) const { - if (empty() && p_str[0] == 0) { + if (is_empty() && p_str[0] == 0) { return false; } - if (empty()) { + if (is_empty()) { return true; } @@ -705,10 +705,10 @@ bool String::operator<(const wchar_t *p_str) const { } bool String::operator<(const char32_t *p_str) const { - if (empty() && p_str[0] == 0) { + if (is_empty() && p_str[0] == 0) { return false; } - if (empty()) { + if (is_empty()) { return true; } @@ -720,13 +720,13 @@ bool String::operator<(const String &p_str) const { } signed char String::nocasecmp_to(const String &p_str) const { - if (empty() && p_str.empty()) { + if (is_empty() && p_str.is_empty()) { return 0; } - if (empty()) { + if (is_empty()) { return -1; } - if (p_str.empty()) { + if (p_str.is_empty()) { return 1; } @@ -752,13 +752,13 @@ signed char String::nocasecmp_to(const String &p_str) const { } signed char String::casecmp_to(const String &p_str) const { - if (empty() && p_str.empty()) { + if (is_empty() && p_str.is_empty()) { return 0; } - if (empty()) { + if (is_empty()) { return -1; } - if (p_str.empty()) { + if (p_str.is_empty()) { return 1; } @@ -949,10 +949,10 @@ String String::get_with_code_lines() const { } int String::get_slice_count(String p_splitter) const { - if (empty()) { + if (is_empty()) { return 0; } - if (p_splitter.empty()) { + if (p_splitter.is_empty()) { return 0; } @@ -968,7 +968,7 @@ int String::get_slice_count(String p_splitter) const { } String String::get_slice(String p_splitter, int p_slice) const { - if (empty() || p_splitter.empty()) { + if (is_empty() || p_splitter.is_empty()) { return ""; } @@ -1008,7 +1008,7 @@ String String::get_slice(String p_splitter, int p_slice) const { } String String::get_slicec(char32_t p_splitter, int p_slice) const { - if (empty()) { + if (is_empty()) { return String(); } @@ -2585,7 +2585,7 @@ int64_t String::to_int(const char32_t *p_str, int p_len, bool p_clamp) { } double String::to_float() const { - if (empty()) { + if (is_empty()) { return 0; } return built_in_strtod<char32_t>(get_data()); @@ -2767,7 +2767,7 @@ String String::substr(int p_from, int p_chars) const { p_chars = length() - p_from; } - if (empty() || p_from < 0 || p_from >= length() || p_chars <= 0) { + if (is_empty() || p_from < 0 || p_from >= length() || p_chars <= 0) { return ""; } @@ -3142,7 +3142,7 @@ bool String::is_quoted() const { } int String::_count(const String &p_string, int p_from, int p_to, bool p_case_insensitive) const { - if (p_string.empty()) { + if (p_string.is_empty()) { return 0; } int len = length(); @@ -4241,7 +4241,7 @@ bool String::is_valid_ip_address() const { Vector<String> ip = split(":"); for (int i = 0; i < ip.size(); i++) { String n = ip[i]; - if (n.empty()) { + if (n.is_empty()) { continue; } if (n.is_valid_hex_number(false)) { @@ -4285,7 +4285,10 @@ bool String::is_rel_path() const { } String String::get_base_dir() const { - int basepos = find("://"); + int basepos = find(":/"); + if (basepos == -1) { + basepos = find(":\\"); + } String rs; String base; if (basepos != -1) { @@ -4328,7 +4331,7 @@ String String::get_extension() const { } String String::plus_file(const String &p_file) const { - if (empty()) { + if (is_empty()) { return p_file; } if (operator[](length() - 1) == '/' || (p_file.size() > 0 && p_file.operator[](0) == '/')) { @@ -4757,7 +4760,7 @@ String String::unquote() const { Vector<uint8_t> String::to_ascii_buffer() const { const String *s = this; - if (s->empty()) { + if (s->is_empty()) { return Vector<uint8_t>(); } CharString charstr = s->ascii(); @@ -4773,7 +4776,7 @@ Vector<uint8_t> String::to_ascii_buffer() const { Vector<uint8_t> String::to_utf8_buffer() const { const String *s = this; - if (s->empty()) { + if (s->is_empty()) { return Vector<uint8_t>(); } CharString charstr = s->utf8(); @@ -4789,7 +4792,7 @@ Vector<uint8_t> String::to_utf8_buffer() const { Vector<uint8_t> String::to_utf16_buffer() const { const String *s = this; - if (s->empty()) { + if (s->is_empty()) { return Vector<uint8_t>(); } Char16String charstr = s->utf16(); @@ -4805,7 +4808,7 @@ Vector<uint8_t> String::to_utf16_buffer() const { Vector<uint8_t> String::to_utf32_buffer() const { const String *s = this; - if (s->empty()) { + if (s->is_empty()) { return Vector<uint8_t>(); } diff --git a/core/string/ustring.h b/core/string/ustring.h index 7ff78b2d86..b42814a0a3 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -394,7 +394,7 @@ public: Vector<uint8_t> sha1_buffer() const; Vector<uint8_t> sha256_buffer() const; - _FORCE_INLINE_ bool empty() const { return length() == 0; } + _FORCE_INLINE_ bool is_empty() const { return length() == 0; } // path functions bool is_abs_path() const; diff --git a/core/templates/cowdata.h b/core/templates/cowdata.h index d5eb08286d..705eae8f9f 100644 --- a/core/templates/cowdata.h +++ b/core/templates/cowdata.h @@ -135,7 +135,7 @@ public: } _FORCE_INLINE_ void clear() { resize(0); } - _FORCE_INLINE_ bool empty() const { return _ptr == nullptr; } + _FORCE_INLINE_ bool is_empty() const { return _ptr == nullptr; } _FORCE_INLINE_ void set(int p_index, const T &p_elem) { CRASH_BAD_INDEX(p_index, size()); diff --git a/core/templates/hash_map.h b/core/templates/hash_map.h index e1ba381595..2e98302809 100644 --- a/core/templates/hash_map.h +++ b/core/templates/hash_map.h @@ -497,7 +497,7 @@ public: return elements; } - inline bool empty() const { + inline bool is_empty() const { return elements == 0; } diff --git a/core/templates/list.h b/core/templates/list.h index 8e14aaa90d..bab5198380 100644 --- a/core/templates/list.h +++ b/core/templates/list.h @@ -373,7 +373,7 @@ public: /** * return whether the list is empty */ - _FORCE_INLINE_ bool empty() const { + _FORCE_INLINE_ bool is_empty() const { return (!_data || !_data->size_cache); } diff --git a/core/templates/local_vector.h b/core/templates/local_vector.h index 4ef040dc77..1bf4161f87 100644 --- a/core/templates/local_vector.h +++ b/core/templates/local_vector.h @@ -104,7 +104,7 @@ public: capacity = 0; } } - _FORCE_INLINE_ bool empty() const { return count == 0; } + _FORCE_INLINE_ bool is_empty() const { return count == 0; } _FORCE_INLINE_ void reserve(U p_size) { p_size = nearest_power_of_2_templated(p_size); if (p_size > capacity) { diff --git a/core/templates/map.h b/core/templates/map.h index c454d69256..4e002b67f8 100644 --- a/core/templates/map.h +++ b/core/templates/map.h @@ -625,7 +625,7 @@ public: return e; } - inline bool empty() const { return _data.size_cache == 0; } + inline bool is_empty() const { return _data.size_cache == 0; } inline int size() const { return _data.size_cache; } int calculate_depth() const { diff --git a/core/templates/oa_hash_map.h b/core/templates/oa_hash_map.h index d9d632b4ce..49551ffc2d 100644 --- a/core/templates/oa_hash_map.h +++ b/core/templates/oa_hash_map.h @@ -190,7 +190,7 @@ public: _FORCE_INLINE_ uint32_t get_capacity() const { return capacity; } _FORCE_INLINE_ uint32_t get_num_elements() const { return num_elements; } - bool empty() const { + bool is_empty() const { return num_elements == 0; } diff --git a/core/templates/ordered_hash_map.h b/core/templates/ordered_hash_map.h index 9398868b01..fce9bcfbfc 100644 --- a/core/templates/ordered_hash_map.h +++ b/core/templates/ordered_hash_map.h @@ -265,7 +265,7 @@ public: return ConstElement(list.back()); } - inline bool empty() const { return list.empty(); } + inline bool is_empty() const { return list.is_empty(); } inline int size() const { return list.size(); } const void *id() const { diff --git a/core/templates/paged_allocator.h b/core/templates/paged_allocator.h new file mode 100644 index 0000000000..ab9945dd3b --- /dev/null +++ b/core/templates/paged_allocator.h @@ -0,0 +1,129 @@ +/*************************************************************************/ +/* paged_allocator.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef PAGED_ALLOCATOR_H +#define PAGED_ALLOCATOR_H + +#include "core/os/memory.h" +#include "core/os/spin_lock.h" +#include "core/typedefs.h" + +template <class T, bool thread_safe = false> +class PagedAllocator { + T **page_pool = nullptr; + T ***available_pool = nullptr; + uint32_t pages_allocated = 0; + uint32_t allocs_available = 0; + + uint32_t page_shift = 0; + uint32_t page_mask = 0; + uint32_t page_size = 0; + SpinLock spin_lock; + +public: + T *alloc() { + if (thread_safe) { + spin_lock.lock(); + } + if (unlikely(allocs_available == 0)) { + uint32_t pages_used = pages_allocated; + + pages_allocated++; + page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated); + available_pool = (T ***)memrealloc(available_pool, sizeof(T **) * pages_allocated); + + page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size); + available_pool[pages_used] = (T **)memalloc(sizeof(T *) * page_size); + + for (uint32_t i = 0; i < page_size; i++) { + available_pool[0][i] = &page_pool[pages_used][i]; + } + allocs_available += page_size; + } + + allocs_available--; + T *alloc = available_pool[allocs_available >> page_shift][allocs_available & page_mask]; + if (thread_safe) { + spin_lock.unlock(); + } + memnew_placement(alloc, T); + return alloc; + } + + void free(T *p_mem) { + if (thread_safe) { + spin_lock.lock(); + } + p_mem->~T(); + available_pool[allocs_available >> page_shift][allocs_available & page_mask] = p_mem; + if (thread_safe) { + spin_lock.unlock(); + } + allocs_available++; + } + + void reset() { + ERR_FAIL_COND(allocs_available < pages_allocated * page_size); + if (pages_allocated) { + for (uint32_t i = 0; i < pages_allocated; i++) { + memfree(page_pool[i]); + memfree(available_pool[i]); + } + memfree(page_pool); + memfree(available_pool); + page_pool = nullptr; + available_pool = nullptr; + pages_allocated = 0; + allocs_available = 0; + } + } + bool is_configured() const { + return page_size > 0; + } + + void configure(uint32_t p_page_size) { + ERR_FAIL_COND(page_pool != nullptr); //sanity check + ERR_FAIL_COND(p_page_size == 0); + page_size = nearest_power_of_2_templated(p_page_size); + page_mask = page_size - 1; + page_shift = get_shift_from_power_of_2(page_size); + } + + PagedAllocator(uint32_t p_page_size = 4096) { // power of 2 recommended because of alignment with OS page sizes. Even if element is bigger, its still a multiple and get rounded amount of pages + configure(p_page_size); + } + + ~PagedAllocator() { + ERR_FAIL_COND_MSG(allocs_available < pages_allocated * page_size, "Pages in use exist at exit in PagedAllocator"); + reset(); + } +}; + +#endif // PAGED_ALLOCATOR_H diff --git a/core/templates/paged_array.h b/core/templates/paged_array.h new file mode 100644 index 0000000000..9bbaac35c4 --- /dev/null +++ b/core/templates/paged_array.h @@ -0,0 +1,367 @@ +/*************************************************************************/ +/* paged_array.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef PAGED_ARRAY_H +#define PAGED_ARRAY_H + +#include "core/os/memory.h" +#include "core/os/spin_lock.h" +#include "core/typedefs.h" + +// PagedArray is used mainly for filling a very large array from multiple threads efficiently and without causing major fragmentation + +// PageArrayPool manages central page allocation in a thread safe matter + +template <class T> +class PagedArrayPool { + T **page_pool = nullptr; + uint32_t pages_allocated = 0; + + uint32_t *available_page_pool = nullptr; + uint32_t pages_available = 0; + + uint32_t page_size = 0; + SpinLock spin_lock; + +public: + uint32_t alloc_page() { + spin_lock.lock(); + if (unlikely(pages_available == 0)) { + uint32_t pages_used = pages_allocated; + + pages_allocated++; + page_pool = (T **)memrealloc(page_pool, sizeof(T *) * pages_allocated); + available_page_pool = (uint32_t *)memrealloc(available_page_pool, sizeof(uint32_t) * pages_allocated); + + page_pool[pages_used] = (T *)memalloc(sizeof(T) * page_size); + available_page_pool[0] = pages_used; + + pages_available++; + } + + pages_available--; + uint32_t page = available_page_pool[pages_available]; + spin_lock.unlock(); + + return page; + } + T *get_page(uint32_t p_page_id) { + return page_pool[p_page_id]; + } + + void free_page(uint32_t p_page_id) { + spin_lock.lock(); + available_page_pool[pages_available] = p_page_id; + pages_available++; + spin_lock.unlock(); + } + + uint32_t get_page_size_shift() const { + return get_shift_from_power_of_2(page_size); + } + + uint32_t get_page_size_mask() const { + return page_size - 1; + } + + void reset() { + ERR_FAIL_COND(pages_available < pages_allocated); + if (pages_allocated) { + for (uint32_t i = 0; i < pages_allocated; i++) { + memfree(page_pool[i]); + } + memfree(page_pool); + memfree(available_page_pool); + page_pool = nullptr; + available_page_pool = nullptr; + pages_allocated = 0; + pages_available = 0; + } + } + bool is_configured() const { + return page_size > 0; + } + + void configure(uint32_t p_page_size) { + ERR_FAIL_COND(page_pool != nullptr); //sanity check + ERR_FAIL_COND(p_page_size == 0); + page_size = nearest_power_of_2_templated(p_page_size); + } + + PagedArrayPool(uint32_t p_page_size = 4096) { // power of 2 recommended because of alignment with OS page sizes. Even if element is bigger, its still a multiple and get rounded amount of pages + configure(p_page_size); + } + + ~PagedArrayPool() { + ERR_FAIL_COND_MSG(pages_available < pages_allocated, "Pages in use exist at exit in PagedArrayPool"); + reset(); + } +}; + +// PageArray is a local array that is optimized to grow in place, then be cleared often. +// It does so by allocating pages from a PagedArrayPool. +// It is safe to use multiple PagedArrays from different threads, sharing a single PagedArrayPool + +template <class T> +class PagedArray { + PagedArrayPool<T> *page_pool = nullptr; + + T **page_data = nullptr; + uint32_t *page_ids = nullptr; + uint32_t max_pages_used = 0; + uint32_t page_size_shift = 0; + uint32_t page_size_mask = 0; + uint64_t count = 0; + + _FORCE_INLINE_ uint32_t _get_pages_in_use() const { + if (count == 0) { + return 0; + } else { + return ((count - 1) >> page_size_shift) + 1; + } + } + + void _grow_page_array() { + //no more room in the page array to put the new page, make room + if (max_pages_used == 0) { + max_pages_used = 1; + } else { + max_pages_used *= 2; // increase in powers of 2 to keep allocations to minimum + } + page_data = (T **)memrealloc(page_data, sizeof(T *) * max_pages_used); + page_ids = (uint32_t *)memrealloc(page_ids, sizeof(uint32_t) * max_pages_used); + } + +public: + _FORCE_INLINE_ const T &operator[](uint64_t p_index) const { + CRASH_BAD_UNSIGNED_INDEX(p_index, count); + uint32_t page = p_index >> page_size_shift; + uint32_t offset = p_index & page_size_mask; + + return page_data[page][offset]; + } + _FORCE_INLINE_ T &operator[](uint64_t p_index) { + CRASH_BAD_UNSIGNED_INDEX(p_index, count); + uint32_t page = p_index >> page_size_shift; + uint32_t offset = p_index & page_size_mask; + + return page_data[page][offset]; + } + + _FORCE_INLINE_ void push_back(const T &p_value) { + uint32_t remainder = count & page_size_mask; + if (unlikely(remainder == 0)) { + // at 0, so time to request a new page + uint32_t page_count = _get_pages_in_use(); + uint32_t new_page_count = page_count + 1; + + if (unlikely(new_page_count > max_pages_used)) { + ERR_FAIL_COND(page_pool == nullptr); //sanity check + + _grow_page_array(); //keep out of inline + } + + uint32_t page_id = page_pool->alloc_page(); + page_data[page_count] = page_pool->get_page(page_id); + page_ids[page_count] = page_id; + } + + // place the new value + uint32_t page = count >> page_size_shift; + uint32_t offset = count & page_size_mask; + + if (!__has_trivial_constructor(T)) { + memnew_placement(&page_data[page][offset], T(p_value)); + } else { + page_data[page][offset] = p_value; + } + + count++; + } + + _FORCE_INLINE_ void pop_back() { + ERR_FAIL_COND(count == 0); + + if (!__has_trivial_destructor(T)) { + uint32_t page = (count - 1) >> page_size_shift; + uint32_t offset = (count - 1) & page_size_mask; + page_data[page][offset].~T(); + } + + uint32_t remainder = count & page_size_mask; + if (unlikely(remainder == 1)) { + // one element remained, so page must be freed. + uint32_t last_page = _get_pages_in_use() - 1; + page_pool->free_page(page_ids[last_page]); + } + count--; + } + + void clear() { + //destruct if needed + if (!__has_trivial_destructor(T)) { + for (uint64_t i = 0; i < count; i++) { + uint32_t page = i >> page_size_shift; + uint32_t offset = i & page_size_mask; + page_data[page][offset].~T(); + } + } + + //return the pages to the pagepool, so they can be used by another array eventually + uint32_t pages_used = _get_pages_in_use(); + for (uint32_t i = 0; i < pages_used; i++) { + page_pool->free_page(page_ids[i]); + } + + count = 0; + + //note we leave page_data and page_indices intact for next use. If you really want to clear them call reset() + } + + void reset() { + clear(); + if (page_data) { + memfree(page_data); + memfree(page_ids); + page_data = nullptr; + page_ids = nullptr; + max_pages_used = 0; + } + } + + // This takes the pages from a source array and merges them to this one + // resulting order is undefined, but content is merged very efficiently, + // making it ideal to fill content on several threads to later join it. + + void merge_unordered(PagedArray<T> &p_array) { + ERR_FAIL_COND(page_pool != p_array.page_pool); + + uint32_t remainder = count & page_size_mask; + + T *remainder_page = nullptr; + uint32_t remainder_page_id; + + if (remainder > 0) { + uint32_t last_page = _get_pages_in_use() - 1; + remainder_page = page_data[last_page]; + remainder_page_id = page_ids[last_page]; + } + + count -= remainder; + + uint32_t src_pages = p_array._get_pages_in_use(); + uint32_t page_size = page_size_mask + 1; + + for (uint32_t i = 0; i < src_pages; i++) { + uint32_t page_count = _get_pages_in_use(); + uint32_t new_page_count = page_count + 1; + + if (unlikely(new_page_count > max_pages_used)) { + _grow_page_array(); //keep out of inline + } + + page_data[page_count] = p_array.page_data[i]; + page_ids[page_count] = p_array.page_ids[i]; + if (i == src_pages - 1) { + //last page, only increment with remainder + count += p_array.count & page_size_mask; + } else { + count += page_size; + } + } + p_array.count = 0; //take away the other array pages + + //handle the remainder page if exists + if (remainder_page) { + uint32_t new_remainder = count & page_size_mask; + + if (new_remainder > 0) { + //must merge old remainder with new remainder + + T *dst_page = page_data[_get_pages_in_use() - 1]; + uint32_t to_copy = MIN(page_size - new_remainder, remainder); + + for (uint32_t i = 0; i < to_copy; i++) { + if (!__has_trivial_constructor(T)) { + memnew_placement(&dst_page[i + new_remainder], T(remainder_page[i + remainder - to_copy])); + } else { + dst_page[i + new_remainder] = remainder_page[i + remainder - to_copy]; + } + + if (!__has_trivial_destructor(T)) { + remainder_page[i + remainder - to_copy].~T(); + } + } + + remainder -= to_copy; //subtract what was copied from remainder + count += to_copy; //add what was copied to the count + + if (remainder == 0) { + //entire remainder copied, let go of remainder page + page_pool->free_page(remainder_page_id); + remainder_page = nullptr; + } + } + + if (remainder > 0) { + //there is still remainder, append it + uint32_t page_count = _get_pages_in_use(); + uint32_t new_page_count = page_count + 1; + + if (unlikely(new_page_count > max_pages_used)) { + _grow_page_array(); //keep out of inline + } + + page_data[page_count] = remainder_page; + page_ids[page_count] = remainder_page_id; + + count += remainder; + } + } + } + + _FORCE_INLINE_ uint64_t size() const { + return count; + } + + void set_page_pool(PagedArrayPool<T> *p_page_pool) { + ERR_FAIL_COND(max_pages_used > 0); //sanity check + + page_pool = p_page_pool; + page_size_mask = page_pool->get_page_size_mask(); + page_size_shift = page_pool->get_page_size_shift(); + } + + ~PagedArray() { + reset(); + } +}; + +#endif // PAGED_ARRAY_H diff --git a/core/templates/rid_owner.h b/core/templates/rid_owner.h index d1bcb92010..7de4e43648 100644 --- a/core/templates/rid_owner.h +++ b/core/templates/rid_owner.h @@ -346,6 +346,18 @@ public: alloc.free(p_rid); } + _FORCE_INLINE_ uint32_t get_rid_count() const { + return alloc.get_rid_count(); + } + + _FORCE_INLINE_ RID get_rid_by_index(uint32_t p_index) { + return alloc.get_rid_by_index(p_index); + } + + _FORCE_INLINE_ T *get_ptr_by_index(uint32_t p_index) { + return *alloc.get_ptr_by_index(p_index); + } + _FORCE_INLINE_ void get_owned_list(List<RID> *p_owned) { return alloc.get_owned_list(p_owned); } @@ -353,6 +365,7 @@ public: void set_description(const char *p_descrption) { alloc.set_description(p_descrption); } + RID_PtrOwner(uint32_t p_target_chunk_byte_size = 4096) : alloc(p_target_chunk_byte_size) {} }; diff --git a/core/templates/set.h b/core/templates/set.h index d0ac71a710..c323618062 100644 --- a/core/templates/set.h +++ b/core/templates/set.h @@ -576,7 +576,7 @@ public: return e; } - inline bool empty() const { return _data.size_cache == 0; } + inline bool is_empty() const { return _data.size_cache == 0; } inline int size() const { return _data.size_cache; } int calculate_depth() const { diff --git a/core/templates/vector.h b/core/templates/vector.h index 9d45f7c30a..1c06e7e3ab 100644 --- a/core/templates/vector.h +++ b/core/templates/vector.h @@ -79,7 +79,7 @@ public: _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } _FORCE_INLINE_ void clear() { resize(0); } - _FORCE_INLINE_ bool empty() const { return _cowdata.empty(); } + _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); } _FORCE_INLINE_ T get(int p_index) { return _cowdata.get(p_index); } _FORCE_INLINE_ const T &get(int p_index) const { return _cowdata.get(p_index); } @@ -112,6 +112,10 @@ public: sort_custom<_DefaultComparator<T>>(); } + Vector<T> duplicate() { + return *this; + } + void ordered_insert(const T &p_val) { int i; for (i = 0; i < _cowdata.size(); i++) { diff --git a/core/templates/vmap.h b/core/templates/vmap.h index 8d2a3d2a9c..5fdfa1a7b5 100644 --- a/core/templates/vmap.h +++ b/core/templates/vmap.h @@ -54,7 +54,7 @@ private: _FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const { r_exact = false; - if (_cowdata.empty()) { + if (_cowdata.is_empty()) { return 0; } @@ -89,7 +89,7 @@ private: } _FORCE_INLINE_ int _find_exact(const T &p_val) const { - if (_cowdata.empty()) { + if (_cowdata.is_empty()) { return -1; } @@ -147,7 +147,7 @@ public: } _FORCE_INLINE_ int size() const { return _cowdata.size(); } - _FORCE_INLINE_ bool empty() const { return _cowdata.empty(); } + _FORCE_INLINE_ bool is_empty() const { return _cowdata.is_empty(); } const Pair *get_array() const { return _cowdata.ptr(); diff --git a/core/templates/vset.h b/core/templates/vset.h index 4c0b8717b6..f2ea5c814b 100644 --- a/core/templates/vset.h +++ b/core/templates/vset.h @@ -40,7 +40,7 @@ class VSet { _FORCE_INLINE_ int _find(const T &p_val, bool &r_exact) const { r_exact = false; - if (_data.empty()) { + if (_data.is_empty()) { return 0; } @@ -76,7 +76,7 @@ class VSet { } _FORCE_INLINE_ int _find_exact(const T &p_val) const { - if (_data.empty()) { + if (_data.is_empty()) { return -1; } @@ -126,7 +126,7 @@ public: return _find_exact(p_val); } - _FORCE_INLINE_ bool empty() const { return _data.empty(); } + _FORCE_INLINE_ bool is_empty() const { return _data.is_empty(); } _FORCE_INLINE_ int size() const { return _data.size(); } diff --git a/core/variant/array.cpp b/core/variant/array.cpp index 5043868b1d..9b8ef24cb9 100644 --- a/core/variant/array.cpp +++ b/core/variant/array.cpp @@ -86,8 +86,8 @@ int Array::size() const { return _p->array.size(); } -bool Array::empty() const { - return _p->array.empty(); +bool Array::is_empty() const { + return _p->array.is_empty(); } void Array::clear() { @@ -318,7 +318,7 @@ Array Array::slice(int p_begin, int p_end, int p_step, bool p_deep) const { // l ERR_FAIL_COND_V_MSG(p_step == 0, new_arr, "Array slice step size cannot be zero."); - if (empty()) { // Don't try to slice empty arrays. + if (is_empty()) { // Don't try to slice empty arrays. return new_arr; } if (p_step > 0) { @@ -459,7 +459,7 @@ void Array::push_front(const Variant &p_value) { } Variant Array::pop_back() { - if (!_p->array.empty()) { + if (!_p->array.is_empty()) { int n = _p->array.size() - 1; Variant ret = _p->array.get(n); _p->array.resize(n); @@ -469,7 +469,7 @@ Variant Array::pop_back() { } Variant Array::pop_front() { - if (!_p->array.empty()) { + if (!_p->array.is_empty()) { Variant ret = _p->array.get(0); _p->array.remove(0); return ret; diff --git a/core/variant/array.h b/core/variant/array.h index e01ac13168..6d21446b07 100644 --- a/core/variant/array.h +++ b/core/variant/array.h @@ -57,7 +57,7 @@ public: const Variant &get(int p_idx) const; int size() const; - bool empty() const; + bool is_empty() const; void clear(); bool operator==(const Array &p_array) const; diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h index e950709526..95113c89ba 100644 --- a/core/variant/binder_common.h +++ b/core/variant/binder_common.h @@ -87,7 +87,7 @@ VARIANT_ENUM_CAST(Object::ConnectFlags); VARIANT_ENUM_CAST(Vector3::Axis); VARIANT_ENUM_CAST(Error); -VARIANT_ENUM_CAST(Margin); +VARIANT_ENUM_CAST(Side); VARIANT_ENUM_CAST(Corner); VARIANT_ENUM_CAST(Orientation); VARIANT_ENUM_CAST(HAlign); diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp index 2bc1f7a86d..aff3f1326a 100644 --- a/core/variant/dictionary.cpp +++ b/core/variant/dictionary.cpp @@ -40,7 +40,7 @@ struct DictionaryPrivate { }; void Dictionary::get_key_list(List<Variant> *p_keys) const { - if (_p->variant_map.empty()) { + if (_p->variant_map.is_empty()) { return; } @@ -121,7 +121,7 @@ int Dictionary::size() const { return _p->variant_map.size(); } -bool Dictionary::empty() const { +bool Dictionary::is_empty() const { return !_p->variant_map.size(); } @@ -192,7 +192,7 @@ uint32_t Dictionary::hash() const { Array Dictionary::keys() const { Array varr; - if (_p->variant_map.empty()) { + if (_p->variant_map.is_empty()) { return varr; } @@ -209,7 +209,7 @@ Array Dictionary::keys() const { Array Dictionary::values() const { Array varr; - if (_p->variant_map.empty()) { + if (_p->variant_map.is_empty()) { return varr; } diff --git a/core/variant/dictionary.h b/core/variant/dictionary.h index bbe94122ad..8bc559a73d 100644 --- a/core/variant/dictionary.h +++ b/core/variant/dictionary.h @@ -60,7 +60,7 @@ public: Variant get(const Variant &p_key, const Variant &p_default) const; int size() const; - bool empty() const; + bool is_empty() const; void clear(); bool has(const Variant &p_key) const; diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index 741d05c139..8e14886e38 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -912,11 +912,11 @@ bool Variant::is_zero() const { } break; case DICTIONARY: { - return reinterpret_cast<const Dictionary *>(_data._mem)->empty(); + return reinterpret_cast<const Dictionary *>(_data._mem)->is_empty(); } break; case ARRAY: { - return reinterpret_cast<const Array *>(_data._mem)->empty(); + return reinterpret_cast<const Array *>(_data._mem)->is_empty(); } break; @@ -2338,8 +2338,8 @@ Variant::operator Vector<StringName>() const { return to; } -Variant::operator Margin() const { - return (Margin) operator int(); +Variant::operator Side() const { + return (Side) operator int(); } Variant::operator Orientation() const { diff --git a/core/variant/variant.h b/core/variant/variant.h index 76c936a7bd..d5649f8256 100644 --- a/core/variant/variant.h +++ b/core/variant/variant.h @@ -356,7 +356,7 @@ public: operator Vector<Vector2>() const; // some core type enums to convert to - operator Margin() const; + operator Side() const; operator Orientation() const; operator IP_Address() const; @@ -497,6 +497,7 @@ public: static bool is_builtin_method_const(Variant::Type p_type, const StringName &p_method); static bool is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method); static void get_builtin_method_list(Variant::Type p_type, List<StringName> *p_list); + static int get_builtin_method_count(Variant::Type p_type); void call(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error); Variant call(const StringName &p_method, const Variant &p_arg1 = Variant(), const Variant &p_arg2 = Variant(), const Variant &p_arg3 = Variant(), const Variant &p_arg4 = Variant(), const Variant &p_arg5 = Variant()); @@ -535,6 +536,7 @@ public: static bool has_member(Variant::Type p_type, const StringName &p_member); static Variant::Type get_member_type(Variant::Type p_type, const StringName &p_member); static void get_member_list(Type p_type, List<StringName> *r_members); + static int get_member_count(Type p_type); static ValidatedSetter get_member_validated_setter(Variant::Type p_type, const StringName &p_member); static ValidatedGetter get_member_validated_getter(Variant::Type p_type, const StringName &p_member); @@ -628,6 +630,7 @@ public: static bool is_utility_function_vararg(const StringName &p_name); static void get_utility_function_list(List<StringName> *r_functions); + static int get_utility_function_count(); //argsVariant call() @@ -642,6 +645,7 @@ public: void static_assign(const Variant &p_variant); static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants); + static int get_constants_count_for_type(Variant::Type p_type); static bool has_constant(Variant::Type p_type, const StringName &p_value); static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr); diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index 55a34af723..2b6364786e 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -705,6 +705,11 @@ void Variant::get_builtin_method_list(Variant::Type p_type, List<StringName> *p_ } } +int Variant::get_builtin_method_count(Variant::Type p_type) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1); + return builtin_method_names[p_type].size(); +} + Variant::Type Variant::get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method) { ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, Variant::NIL); const VariantBuiltInMethodInfo *method = builtin_method_info[p_type].lookup_ptr(p_method); @@ -799,6 +804,13 @@ void Variant::get_constants_for_type(Variant::Type p_type, List<StringName> *p_c } } +int Variant::get_constants_count_for_type(Variant::Type p_type) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1); + _VariantCall::ConstantData &cd = _VariantCall::constant_data[p_type]; + + return cd.value.size() + cd.variant_value.size(); +} + bool Variant::has_constant(Variant::Type p_type, const StringName &p_value) { ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, false); _VariantCall::ConstantData &cd = _VariantCall::constant_data[p_type]; @@ -928,7 +940,7 @@ static void _register_variant_builtin_methods() { bind_method(String, md5_buffer, sarray(), varray()); bind_method(String, sha1_buffer, sarray(), varray()); bind_method(String, sha256_buffer, sarray(), varray()); - bind_method(String, empty, sarray(), varray()); + bind_method(String, is_empty, sarray(), varray()); // FIXME: Static function, not sure how to bind //bind_method(String, humanize_size, sarray("size"), varray()); @@ -1257,7 +1269,7 @@ static void _register_variant_builtin_methods() { /* Dictionary */ bind_method(Dictionary, size, sarray(), varray()); - bind_method(Dictionary, empty, sarray(), varray()); + bind_method(Dictionary, is_empty, sarray(), varray()); bind_method(Dictionary, clear, sarray(), varray()); bind_method(Dictionary, has, sarray("key"), varray()); bind_method(Dictionary, has_all, sarray("keys"), varray()); @@ -1271,7 +1283,7 @@ static void _register_variant_builtin_methods() { /* Array */ bind_method(Array, size, sarray(), varray()); - bind_method(Array, empty, sarray(), varray()); + bind_method(Array, is_empty, sarray(), varray()); bind_method(Array, clear, sarray(), varray()); bind_method(Array, hash, sarray(), varray()); bind_method(Array, push_back, sarray("value"), varray()); @@ -1304,7 +1316,7 @@ static void _register_variant_builtin_methods() { /* Byte Array */ bind_method(PackedByteArray, size, sarray(), varray()); - bind_method(PackedByteArray, empty, sarray(), varray()); + bind_method(PackedByteArray, is_empty, sarray(), varray()); bind_method(PackedByteArray, set, sarray("index", "value"), varray()); bind_method(PackedByteArray, push_back, sarray("value"), varray()); bind_method(PackedByteArray, append, sarray("value"), varray()); @@ -1316,6 +1328,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedByteArray, invert, sarray(), varray()); bind_method(PackedByteArray, subarray, sarray("from", "to"), varray()); bind_method(PackedByteArray, sort, sarray(), varray()); + bind_method(PackedByteArray, duplicate, sarray(), varray()); bind_function(PackedByteArray, get_string_from_ascii, _VariantCall::func_PackedByteArray_get_string_from_ascii, sarray(), varray()); bind_function(PackedByteArray, get_string_from_utf8, _VariantCall::func_PackedByteArray_get_string_from_utf8, sarray(), varray()); @@ -1329,7 +1342,7 @@ static void _register_variant_builtin_methods() { /* Int32 Array */ bind_method(PackedInt32Array, size, sarray(), varray()); - bind_method(PackedInt32Array, empty, sarray(), varray()); + bind_method(PackedInt32Array, is_empty, sarray(), varray()); bind_method(PackedInt32Array, set, sarray("index", "value"), varray()); bind_method(PackedInt32Array, push_back, sarray("value"), varray()); bind_method(PackedInt32Array, append, sarray("value"), varray()); @@ -1342,11 +1355,12 @@ static void _register_variant_builtin_methods() { bind_method(PackedInt32Array, subarray, sarray("from", "to"), varray()); bind_method(PackedInt32Array, to_byte_array, sarray(), varray()); bind_method(PackedInt32Array, sort, sarray(), varray()); + bind_method(PackedInt32Array, duplicate, sarray(), varray()); /* Int64 Array */ bind_method(PackedInt64Array, size, sarray(), varray()); - bind_method(PackedInt64Array, empty, sarray(), varray()); + bind_method(PackedInt64Array, is_empty, sarray(), varray()); bind_method(PackedInt64Array, set, sarray("index", "value"), varray()); bind_method(PackedInt64Array, push_back, sarray("value"), varray()); bind_method(PackedInt64Array, append, sarray("value"), varray()); @@ -1359,11 +1373,12 @@ static void _register_variant_builtin_methods() { bind_method(PackedInt64Array, subarray, sarray("from", "to"), varray()); bind_method(PackedInt64Array, to_byte_array, sarray(), varray()); bind_method(PackedInt64Array, sort, sarray(), varray()); + bind_method(PackedInt64Array, duplicate, sarray(), varray()); /* Float32 Array */ bind_method(PackedFloat32Array, size, sarray(), varray()); - bind_method(PackedFloat32Array, empty, sarray(), varray()); + bind_method(PackedFloat32Array, is_empty, sarray(), varray()); bind_method(PackedFloat32Array, set, sarray("index", "value"), varray()); bind_method(PackedFloat32Array, push_back, sarray("value"), varray()); bind_method(PackedFloat32Array, append, sarray("value"), varray()); @@ -1376,11 +1391,12 @@ static void _register_variant_builtin_methods() { bind_method(PackedFloat32Array, subarray, sarray("from", "to"), varray()); bind_method(PackedFloat32Array, to_byte_array, sarray(), varray()); bind_method(PackedFloat32Array, sort, sarray(), varray()); + bind_method(PackedFloat32Array, duplicate, sarray(), varray()); /* Float64 Array */ bind_method(PackedFloat64Array, size, sarray(), varray()); - bind_method(PackedFloat64Array, empty, sarray(), varray()); + bind_method(PackedFloat64Array, is_empty, sarray(), varray()); bind_method(PackedFloat64Array, set, sarray("index", "value"), varray()); bind_method(PackedFloat64Array, push_back, sarray("value"), varray()); bind_method(PackedFloat64Array, append, sarray("value"), varray()); @@ -1393,11 +1409,12 @@ static void _register_variant_builtin_methods() { bind_method(PackedFloat64Array, subarray, sarray("from", "to"), varray()); bind_method(PackedFloat64Array, to_byte_array, sarray(), varray()); bind_method(PackedFloat64Array, sort, sarray(), varray()); + bind_method(PackedFloat64Array, duplicate, sarray(), varray()); /* String Array */ bind_method(PackedStringArray, size, sarray(), varray()); - bind_method(PackedStringArray, empty, sarray(), varray()); + bind_method(PackedStringArray, is_empty, sarray(), varray()); bind_method(PackedStringArray, set, sarray("index", "value"), varray()); bind_method(PackedStringArray, push_back, sarray("value"), varray()); bind_method(PackedStringArray, append, sarray("value"), varray()); @@ -1410,11 +1427,12 @@ static void _register_variant_builtin_methods() { bind_method(PackedStringArray, subarray, sarray("from", "to"), varray()); bind_method(PackedStringArray, to_byte_array, sarray(), varray()); bind_method(PackedStringArray, sort, sarray(), varray()); + bind_method(PackedStringArray, duplicate, sarray(), varray()); /* Vector2 Array */ bind_method(PackedVector2Array, size, sarray(), varray()); - bind_method(PackedVector2Array, empty, sarray(), varray()); + bind_method(PackedVector2Array, is_empty, sarray(), varray()); bind_method(PackedVector2Array, set, sarray("index", "value"), varray()); bind_method(PackedVector2Array, push_back, sarray("value"), varray()); bind_method(PackedVector2Array, append, sarray("value"), varray()); @@ -1427,11 +1445,12 @@ static void _register_variant_builtin_methods() { bind_method(PackedVector2Array, subarray, sarray("from", "to"), varray()); bind_method(PackedVector2Array, to_byte_array, sarray(), varray()); bind_method(PackedVector2Array, sort, sarray(), varray()); + bind_method(PackedVector2Array, duplicate, sarray(), varray()); /* Vector3 Array */ bind_method(PackedVector3Array, size, sarray(), varray()); - bind_method(PackedVector3Array, empty, sarray(), varray()); + bind_method(PackedVector3Array, is_empty, sarray(), varray()); bind_method(PackedVector3Array, set, sarray("index", "value"), varray()); bind_method(PackedVector3Array, push_back, sarray("value"), varray()); bind_method(PackedVector3Array, append, sarray("value"), varray()); @@ -1444,11 +1463,12 @@ static void _register_variant_builtin_methods() { bind_method(PackedVector3Array, subarray, sarray("from", "to"), varray()); bind_method(PackedVector3Array, to_byte_array, sarray(), varray()); bind_method(PackedVector3Array, sort, sarray(), varray()); + bind_method(PackedVector3Array, duplicate, sarray(), varray()); /* Color Array */ bind_method(PackedColorArray, size, sarray(), varray()); - bind_method(PackedColorArray, empty, sarray(), varray()); + bind_method(PackedColorArray, is_empty, sarray(), varray()); bind_method(PackedColorArray, set, sarray("index", "value"), varray()); bind_method(PackedColorArray, push_back, sarray("value"), varray()); bind_method(PackedColorArray, append, sarray("value"), varray()); @@ -1461,6 +1481,7 @@ static void _register_variant_builtin_methods() { bind_method(PackedColorArray, subarray, sarray("from", "to"), varray()); bind_method(PackedColorArray, to_byte_array, sarray(), varray()); bind_method(PackedColorArray, sort, sarray(), varray()); + bind_method(PackedColorArray, duplicate, sarray(), varray()); /* Register constants */ diff --git a/core/variant/variant_internal.h b/core/variant/variant_internal.h index 804abf8fbc..57e8f8813f 100644 --- a/core/variant/variant_internal.h +++ b/core/variant/variant_internal.h @@ -743,7 +743,7 @@ VARIANT_ACCESSOR_NUMBER(int64_t) VARIANT_ACCESSOR_NUMBER(uint64_t) VARIANT_ACCESSOR_NUMBER(char32_t) VARIANT_ACCESSOR_NUMBER(Error) -VARIANT_ACCESSOR_NUMBER(Margin) +VARIANT_ACCESSOR_NUMBER(Side) template <> struct VariantInternalAccessor<ObjectID> { diff --git a/core/variant/variant_setget.cpp b/core/variant/variant_setget.cpp index cee7465205..f099c768a0 100644 --- a/core/variant/variant_setget.cpp +++ b/core/variant/variant_setget.cpp @@ -437,6 +437,11 @@ void Variant::get_member_list(Variant::Type p_type, List<StringName> *r_members) } } +int Variant::get_member_count(Type p_type) { + ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, -1); + return variant_setters_getters_names[p_type].size(); +} + Variant::ValidatedSetter Variant::get_member_validated_setter(Variant::Type p_type, const StringName &p_member) { ERR_FAIL_INDEX_V(p_type, Variant::VARIANT_MAX, nullptr); @@ -1472,7 +1477,7 @@ bool Variant::iter_init(Variant &r_iter, bool &valid) const { case STRING: { const String *str = reinterpret_cast<const String *>(_data._mem); - if (str->empty()) { + if (str->is_empty()) { return false; } r_iter = 0; @@ -1480,7 +1485,7 @@ bool Variant::iter_init(Variant &r_iter, bool &valid) const { } break; case DICTIONARY: { const Dictionary *dic = reinterpret_cast<const Dictionary *>(_data._mem); - if (dic->empty()) { + if (dic->is_empty()) { return false; } @@ -1491,7 +1496,7 @@ bool Variant::iter_init(Variant &r_iter, bool &valid) const { } break; case ARRAY: { const Array *arr = reinterpret_cast<const Array *>(_data._mem); - if (arr->empty()) { + if (arr->is_empty()) { return false; } r_iter = 0; @@ -2023,6 +2028,24 @@ Variant Variant::duplicate(bool deep) const { return operator Dictionary().duplicate(deep); case ARRAY: return operator Array().duplicate(deep); + case PACKED_BYTE_ARRAY: + return operator Vector<uint8_t>().duplicate(); + case PACKED_INT32_ARRAY: + return operator Vector<int32_t>().duplicate(); + case PACKED_INT64_ARRAY: + return operator Vector<int64_t>().duplicate(); + case PACKED_FLOAT32_ARRAY: + return operator Vector<float>().duplicate(); + case PACKED_FLOAT64_ARRAY: + return operator Vector<double>().duplicate(); + case PACKED_STRING_ARRAY: + return operator Vector<String>().duplicate(); + case PACKED_VECTOR2_ARRAY: + return operator Vector<Vector2>().duplicate(); + case PACKED_VECTOR3_ARRAY: + return operator Vector<Vector3>().duplicate(); + case PACKED_COLOR_ARRAY: + return operator Vector<Color>().duplicate(); default: return *this; } diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index d54e223a99..46359b80ce 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -1379,3 +1379,7 @@ void Variant::get_utility_function_list(List<StringName> *r_functions) { r_functions->push_back(E->get()); } } + +int Variant::get_utility_function_count() { + return utility_function_name_table.size(); +} |