From 7adac568eb31d8faf950361dda0f15160bab3fa5 Mon Sep 17 00:00:00 2001 From: ihm-ts Date: Tue, 16 Feb 2021 15:36:14 +0100 Subject: [PATCH 001/250] Final JavaScript version --- CMakeLists.txt | 31 ++ src/noggit/MapChunk.cpp | 44 +++ src/noggit/MapChunk.h | 15 +- src/noggit/MapTile.cpp | 24 ++ src/noggit/MapTile.h | 3 + src/noggit/MapView.cpp | 37 +- src/noggit/MapView.h | 8 + src/noggit/World.cpp | 59 +++ src/noggit/World.h | 13 + src/noggit/map_index.hpp | 19 + src/noggit/scripting/global.d.ts | 196 ++++++++++ src/noggit/scripting/script_chunk.cpp | 161 ++++++++ src/noggit/scripting/script_chunk.hpp | 47 +++ src/noggit/scripting/script_context.cpp | 224 +++++++++++ src/noggit/scripting/script_context.hpp | 90 +++++ src/noggit/scripting/script_exception.hpp | 13 + src/noggit/scripting/script_filesystem.cpp | 100 +++++ src/noggit/scripting/script_filesystem.hpp | 33 ++ src/noggit/scripting/script_image.cpp | 84 +++++ src/noggit/scripting/script_image.hpp | 35 ++ src/noggit/scripting/script_loader.cpp | 266 +++++++++++++ src/noggit/scripting/script_loader.hpp | 50 +++ src/noggit/scripting/script_math.cpp | 57 +++ src/noggit/scripting/script_math.hpp | 43 +++ src/noggit/scripting/script_model.cpp | 208 +++++++++++ src/noggit/scripting/script_model.hpp | 60 +++ src/noggit/scripting/script_noggit_module.hpp | 311 ++++++++++++++++ src/noggit/scripting/script_noise.cpp | 94 +++++ src/noggit/scripting/script_noise.hpp | 45 +++ src/noggit/scripting/script_printf.cpp | 116 ++++++ src/noggit/scripting/script_printf.hpp | 10 + src/noggit/scripting/script_random.cpp | 75 ++++ src/noggit/scripting/script_random.hpp | 34 ++ src/noggit/scripting/script_selection.cpp | 321 ++++++++++++++++ src/noggit/scripting/script_selection.hpp | 88 +++++ src/noggit/scripting/script_setup.cpp | 59 +++ src/noggit/scripting/script_setup.hpp | 58 +++ src/noggit/scripting/script_texture_index.hpp | 154 ++++++++ src/noggit/scripting/script_vec.cpp | 37 ++ src/noggit/scripting/script_vec.hpp | 29 ++ src/noggit/scripting/script_vert.cpp | 148 ++++++++ src/noggit/scripting/script_vert.hpp | 51 +++ src/noggit/scripting/scripting_tool.cpp | 351 ++++++++++++++++++ src/noggit/scripting/scripting_tool.hpp | 86 +++++ src/noggit/texture_set.hpp | 20 +- src/noggit/tool_enums.hpp | 3 + src/noggit/ui/Toolbar.cpp | 3 + 47 files changed, 4005 insertions(+), 8 deletions(-) create mode 100644 src/noggit/scripting/global.d.ts create mode 100644 src/noggit/scripting/script_chunk.cpp create mode 100644 src/noggit/scripting/script_chunk.hpp create mode 100644 src/noggit/scripting/script_context.cpp create mode 100644 src/noggit/scripting/script_context.hpp create mode 100644 src/noggit/scripting/script_exception.hpp create mode 100644 src/noggit/scripting/script_filesystem.cpp create mode 100644 src/noggit/scripting/script_filesystem.hpp create mode 100644 src/noggit/scripting/script_image.cpp create mode 100644 src/noggit/scripting/script_image.hpp create mode 100644 src/noggit/scripting/script_loader.cpp create mode 100644 src/noggit/scripting/script_loader.hpp create mode 100644 src/noggit/scripting/script_math.cpp create mode 100644 src/noggit/scripting/script_math.hpp create mode 100644 src/noggit/scripting/script_model.cpp create mode 100644 src/noggit/scripting/script_model.hpp create mode 100644 src/noggit/scripting/script_noggit_module.hpp create mode 100644 src/noggit/scripting/script_noise.cpp create mode 100644 src/noggit/scripting/script_noise.hpp create mode 100644 src/noggit/scripting/script_printf.cpp create mode 100644 src/noggit/scripting/script_printf.hpp create mode 100644 src/noggit/scripting/script_random.cpp create mode 100644 src/noggit/scripting/script_random.hpp create mode 100644 src/noggit/scripting/script_selection.cpp create mode 100644 src/noggit/scripting/script_selection.hpp create mode 100644 src/noggit/scripting/script_setup.cpp create mode 100644 src/noggit/scripting/script_setup.hpp create mode 100644 src/noggit/scripting/script_texture_index.hpp create mode 100644 src/noggit/scripting/script_vec.cpp create mode 100644 src/noggit/scripting/script_vec.hpp create mode 100644 src/noggit/scripting/script_vert.cpp create mode 100644 src/noggit/scripting/script_vert.hpp create mode 100644 src/noggit/scripting/scripting_tool.cpp create mode 100644 src/noggit/scripting/scripting_tool.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index dde41e8da..a59b32ea5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -244,6 +244,20 @@ set ( noggit_root_sources src/noggit/wmo_liquid.cpp src/noggit/world_model_instances_storage.cpp src/noggit/world_tile_update_queue.cpp + src/noggit/scripting/script_printf.cpp + src/noggit/scripting/script_random.cpp + src/noggit/scripting/script_selection.cpp + src/noggit/scripting/script_setup.cpp + src/noggit/scripting/script_vec.cpp + src/noggit/scripting/scripting_tool.cpp + src/noggit/scripting/script_context.cpp + src/noggit/scripting/script_image.cpp + src/noggit/scripting/script_loader.cpp + src/noggit/scripting/script_math.cpp + src/noggit/scripting/script_noise.cpp + src/noggit/scripting/script_filesystem.cpp + ${DUKTAPE_PATH}/duktape.c + ${LODEPNG_PATH}/lodepng.cpp ) set ( noggit_ui_sources @@ -340,6 +354,19 @@ set ( noggit_root_headers src/noggit/wmo_liquid.hpp src/noggit/world_model_instances_storage.hpp src/noggit/world_tile_update_queue.hpp + src/noggit/scripting/script_filesystem.hpp + src/noggit/scripting/script_noise.hpp + src/noggit/scripting/script_printf.hpp + src/noggit/scripting/script_random.hpp + src/noggit/scripting/script_selection.hpp + src/noggit/scripting/script_setup.hpp + src/noggit/scripting/script_texture_index.hpp + src/noggit/scripting/script_vec.hpp + src/noggit/scripting/scripting_tool.hpp + src/noggit/scripting/script_context.hpp + src/noggit/scripting/script_image.hpp + src/noggit/scripting/script_loader.hpp + src/noggit/scripting/script_math.hpp ) set ( noggit_ui_headers @@ -577,3 +604,7 @@ add_executable (math-matrix_4x4.test test/math/matrix_4x4.cpp) target_compile_definitions (math-matrix_4x4.test PRIVATE "-DBOOST_TEST_MODULE=\"math\"") target_link_libraries (math-matrix_4x4.test Boost::unit_test_framework noggit::math) add_test (NAME math-matrix_4x4 COMMAND $) + + +target_include_directories (noggit SYSTEM PUBLIC ${FASTNOISE_INCLUDE} ${LODEPNG_PATH} ${DUKTAPE_PATH}) +target_link_libraries (noggit ${FASTNOISE_LIB}) \ No newline at end of file diff --git a/src/noggit/MapChunk.cpp b/src/noggit/MapChunk.cpp index 7e017e939..bd3ec8583 100644 --- a/src/noggit/MapChunk.cpp +++ b/src/noggit/MapChunk.cpp @@ -764,6 +764,22 @@ bool MapChunk::changeTerrain(math::vector_3d const& pos, float change, float rad return changed; } +// @tswow-begin +void MapChunk::CreateMCCV() +{ + if(!hasMCCV) + { + for(int i=0;i (_mccv_vbo, sizeof(mccv), mccv, GL_STATIC_DRAW); + _need_vao_update = true; + } +} +// @tswow-end + math::vector_3d MapChunk::pickMCCV(math::vector_3d const& pos) { float dist; @@ -1438,6 +1465,23 @@ void MapChunk::selectVertex(math::vector_3d const& pos, float radius, std::set& vertices) +{ + LogDebug << "In a chunk right now\n"; + for(int i = 0; i< mapbufsize; ++i) + { + if( + pos1.x<=mVertices[i].x && pos2.x>=mVertices[i].x && + pos1.z<=mVertices[i].z && pos2.z>=mVertices[i].z + ) + { + vertices.emplace(&mVertices[i]); + } + } +} +// @tswow-end + void MapChunk::fixVertices(std::set& selected) { std::vector ids ={ 0, 1, 17, 18 }; diff --git a/src/noggit/MapChunk.h b/src/noggit/MapChunk.h index be65b209d..18100a243 100644 --- a/src/noggit/MapChunk.h +++ b/src/noggit/MapChunk.h @@ -52,7 +52,9 @@ class MapChunk std::map> strip_lods; math::vector_3d mNormals[mapbufsize]; - math::vector_3d mccv[mapbufsize]; + // @tswow-begin + // (moved mccv to public) + // @tswow-end std::vector compressed_shadow_map() const; bool has_shadows() const; @@ -104,6 +106,9 @@ class MapChunk std::unique_ptr texture_set; math::vector_3d mVertices[mapbufsize]; + // @tswow-begin + math::vector_3d mccv[mapbufsize]; + // @tswow-end bool is_visible ( const float& cull_distance , const math::frustum& frustum @@ -142,6 +147,10 @@ class MapChunk void intersect (math::ray const&, selection_result*); bool ChangeMCCV(math::vector_3d const& pos, math::vector_4d const& color, float change, float radius, bool editMode); + // @tswow-begin + void CreateMCCV(); + void UpdateMCCV(); + // @tswow-end math::vector_3d pickMCCV(math::vector_3d const& pos); ChunkWater* liquid_chunk() const; @@ -194,4 +203,8 @@ class MapChunk bool fixGapLeft(const MapChunk* chunk); // fix the gaps with the chunk above bool fixGapAbove(const MapChunk* chunk); + + // @tswow-begin + void selectVertex(math::vector_3d const& minPos, math::vector_3d const& maxPos, std::set& vertices); + // @tswow-end }; diff --git a/src/noggit/MapTile.cpp b/src/noggit/MapTile.cpp index 96813f8be..72419e448 100644 --- a/src/noggit/MapTile.cpp +++ b/src/noggit/MapTile.cpp @@ -508,6 +508,30 @@ std::vector MapTile::chunks_in_range (math::vector_3d const& pos, flo return chunks; } +// @tswow-begin +std::vector MapTile::chunks_between (math::vector_3d const& pos1, math::vector_3d const& pos2) const +{ + std::vector chunks; + + for (size_t ty (0); ty < 16; ++ty) + { + for (size_t tx (0); tx < 16; ++tx) + { + auto minX = mChunks[ty][tx]->xbase; + auto minZ = mChunks[ty][tx]->zbase; + auto maxX = minX+CHUNKSIZE; + auto maxZ = minZ+CHUNKSIZE; + if(minX <= pos2.x && maxX >= pos1.x && + minZ <= pos2.z && maxZ >= pos1.z) + { + chunks.emplace_back(mChunks[ty][tx].get()); + } + } + } + + return chunks; +} +// @tswow-end bool MapTile::GetVertex(float x, float z, math::vector_3d *V) { diff --git a/src/noggit/MapTile.h b/src/noggit/MapTile.h index 2622914a9..e09e1c005 100644 --- a/src/noggit/MapTile.h +++ b/src/noggit/MapTile.h @@ -65,6 +65,9 @@ class MapTile : public AsyncObject MapChunk* getChunk(unsigned int x, unsigned int z); //! \todo map_index style iterators std::vector chunks_in_range (math::vector_3d const& pos, float radius) const; + // @tswow-begin + std::vector chunks_between (math::vector_3d const& pos1, math::vector_3d const& pos2) const; + // @tswow-end const tile_index index; float xbase, zbase; diff --git a/src/noggit/MapView.cpp b/src/noggit/MapView.cpp index 00fbeee15..373302a28 100644 --- a/src/noggit/MapView.cpp +++ b/src/noggit/MapView.cpp @@ -34,6 +34,9 @@ #include #include #include +// @tswow-begin +#include +// @tswow-end #include #include "revision.h" @@ -49,6 +52,7 @@ #include #include #include +#include #include #include @@ -120,6 +124,8 @@ void MapView::setToolPropertyWidgetVisibility(editing_mode mode) case editing_mode::object: _object_editor_dock->setVisible(!ui_hidden); break; + case editing_mode::scripting: + _script_tool_dock->setVisible(!ui_hidden); } @@ -185,7 +191,12 @@ QWidgetAction* MapView::createTextSeparator(const QString& text) void MapView::createGUI() { - // create tool widgets + // @tswow-begin + _script_tool_dock = new QDockWidget("Scripting", this); + scriptingTool = new noggit::scripting::scripting_tool(_script_tool_dock); + _script_tool_dock->setWidget(scriptingTool); + _tool_properties_docks.insert(_script_tool_dock); + // @tswow-end _terrain_tool_dock = new QDockWidget("Raise / Lower", this); terrainTool = new noggit::ui::terrain_tool(_terrain_tool_dock); @@ -495,8 +506,6 @@ void MapView::createGUI() } \ while (false) - - ADD_ACTION (file_menu, "Save current tile", "Ctrl+Shift+S", [this] { save(save_mode::current); }); ADD_ACTION (file_menu, "Save changed tiles", QKeySequence::Save, [this] { save(save_mode::changed); }); ADD_ACTION (file_menu, "Save all tiles", "Ctrl+Shift+A", [this] { save(save_mode::all); }); @@ -1770,6 +1779,22 @@ void MapView::tick (float dt) for (auto& selection : currentSelection) { + if(selection.which() == eEntry_MapChunk && terrainMode == editing_mode::scripting) + { + scriptingTool->sendUpdate( + _world.get(), + _cursor_pos, + &_camera, + 7.5f*dt, + leftMouse, + rightMouse, + _mod_shift_down, + _mod_ctrl_down, + _mod_alt_down, + _mod_space_down + ); + } + if (leftMouse && selection.which() == eEntry_MapChunk) { bool underMap = _world->isUnderMap(_cursor_pos); @@ -2348,6 +2373,12 @@ void MapView::draw_map() case editing_mode::mccv: radius = shaderTool->brushRadius(); break; + // @tswow-begin + case editing_mode::scripting: + radius = scriptingTool->brushRadius(); + inner_radius = scriptingTool->innerRadius(); + break; + // @tswow-end } //! \note Select terrain below mouse, if no item selected or the item is map. diff --git a/src/noggit/MapView.h b/src/noggit/MapView.h index 4577deb71..58e3eace3 100644 --- a/src/noggit/MapView.h +++ b/src/noggit/MapView.h @@ -12,6 +12,7 @@ #include #include #include +#include #include @@ -48,6 +49,9 @@ namespace noggit class water; class zone_id_browser; class texture_palette_small; + // @tswow-begin + class scripting_tool; + // @tswow-end struct main_window; struct tileset_chooser; } @@ -306,4 +310,8 @@ public slots: QDockWidget* _vertex_shading_dock; noggit::ui::texturing_tool* texturingTool; QDockWidget* _texturing_dock; + // @tswow-begin + noggit::scripting::scripting_tool* scriptingTool; + QDockWidget* _script_tool_dock; + // @tswow-end }; diff --git a/src/noggit/World.cpp b/src/noggit/World.cpp index 453a7b81a..cdb16f102 100644 --- a/src/noggit/World.cpp +++ b/src/noggit/World.cpp @@ -384,6 +384,7 @@ void World::scale_selected_models(float v, m2_scaling_type type) void World::move_selected_models(float dx, float dy, float dz) { + for (auto& entry : _current_selection) { auto type = entry.which(); @@ -2124,6 +2125,64 @@ void World::selectVertices(math::vector_3d const& pos, float radius) }); } +// @tswow-begin +void World::selectVertices(math::vector_3d const& pos1, math::vector_3d const& pos2) +{ + math::vector_3d pos_min = math::vector_3d(std::min(pos1.x,pos2.x),std::min(pos1.y,pos2.y),std::min(pos1.z,pos2.z)); + math::vector_3d pos_max = math::vector_3d(std::max(pos1.x,pos2.x),std::max(pos1.y,pos2.y),std::max(pos1.z,pos2.z)); + LogDebug << "Function is being called\n"; + _vertex_center_updated = false; + _vertex_border_updated = false; + + for_all_chunks_between(pos_min, pos_max, [&](MapChunk* chunk){ + _vertex_chunks.emplace(chunk); + _vertex_tiles.emplace(chunk->mt); + chunk->selectVertex(pos_min, pos_max, _vertices_selected); + return true; + }); +} + +void World::select_all_chunks_between(math::vector_3d const& pos1, math::vector_3d const& pos2, std::vector& chunks_in) +{ + math::vector_3d pos_min = math::vector_3d(std::min(pos1.x,pos2.x),std::min(pos1.y,pos2.y),std::min(pos1.z,pos2.z)); + math::vector_3d pos_max = math::vector_3d(std::max(pos1.x,pos2.x),std::max(pos1.y,pos2.y),std::max(pos1.z,pos2.z)); + + for_all_chunks_between(pos_min, pos_max, [&](MapChunk* chunk){ + chunks_in.push_back(chunk); + return true; + }); +} + +std::set* World::getSelectedVertices() +{ + return &_vertices_selected; +} + +template +bool World::for_all_chunks_between (math::vector_3d const& pos1, math::vector_3d const& pos2,Fun&& fun) +{ + bool changed (false); + + for (MapTile* tile : mapIndex.tiles_between (pos1, pos2)) + { + if (!tile->finishedLoading()) + { + continue; + } + + for (MapChunk* chunk : tile->chunks_between(pos1, pos2)) + { + if (fun (chunk)) + { + changed = true; + mapIndex.setChanged (tile); + } + } + } + return changed; +} +// @tswow-end + bool World::deselectVertices(math::vector_3d const& pos, float radius) { _vertex_center_updated = false; diff --git a/src/noggit/World.h b/src/noggit/World.h index 1ca3d3d2b..6a2033c78 100644 --- a/src/noggit/World.h +++ b/src/noggit/World.h @@ -299,6 +299,19 @@ class World bool deselectVertices(math::vector_3d const& pos, float radius); void selectVertices(math::vector_3d const& pos, float radius); + // @tswow-begin + void selectVertices(math::vector_3d const& pos1, math::vector_3d const& pos2); + std::set* getSelectedVertices(); + + template + bool for_all_chunks_between ( math::vector_3d const& pos1, + math::vector_3d const& pos2, + Fun&& /* MapChunk* -> bool changed */ + ); + + void select_all_chunks_between(math::vector_3d const& pos1, math::vector_3d const& pos2, std::vector& chunks_in); + + // @tswow-end void moveVertices(float h); void orientVertices ( math::vector_3d const& ref_pos diff --git a/src/noggit/map_index.hpp b/src/noggit/map_index.hpp index 255032f0a..f7cbf84ee 100644 --- a/src/noggit/map_index.hpp +++ b/src/noggit/map_index.hpp @@ -148,6 +148,25 @@ class MapIndex ); } + // @tswow-begin + auto tiles_between (math::vector_3d const& pos1, math::vector_3d const& pos2) + { + return tiles + ( [this, pos1, pos2] (tile_index const& index, MapTile*) + { + auto minX = index.x*TILESIZE; + auto maxX = index.x*TILESIZE+TILESIZE; + auto minZ = index.z*TILESIZE; + auto maxZ = index.z*TILESIZE+TILESIZE; + + return hasTile(index) && + minX <= pos2.x && maxX >= pos1.x && + minZ <= pos2.z && maxZ >= pos1.z; + } + ); + } + // @tswow-end + MapIndex(const std::string& pBasename, int map_id, World*); void enterTile(const tile_index& tile); diff --git a/src/noggit/scripting/global.d.ts b/src/noggit/scripting/global.d.ts new file mode 100644 index 000000000..fb76fe852 --- /dev/null +++ b/src/noggit/scripting/global.d.ts @@ -0,0 +1,196 @@ +// script_math +declare function round(arg: number): number; +declare function pow(a1: number, a2: number): number; +declare function log10(arg: number): number; +declare function log(arg: number): number; +declare function ceil(arg: number): number; +declare function floor(arg: number): number; +declare function exp(arg: number): number; +declare function cbrt(arg: number): number; +declare function acosh(arg: number): number; +declare function asinh(arg: number): number; +declare function atanh(arg: number): number; +declare function cosh(arg: number): number; +declare function sinh(arg: number): number; +declare function tanh(arg: number): number; +declare function acos(arg: number): number; +declare function asin(arg: number): number; +declare function atan(arg: number): number; +declare function cos(arg: number): number; +declare function sin(arg: number): number; +declare function tan(arg: number): number; +declare function sqrt(arg: number): number; +declare function abs(arg: number): number; + +// script_vec +declare class script_vec { + x(): number; + y(): number; + z(): number; + add(x: number, y: number, z: number): script_vec; +} +declare function vec(x: number, y: number, z: number): script_vec; + +// script_setup +declare class Holder{ + get(): T; +} +declare function add_script(name: string, callback: ()=>void); +declare function add_desc(desc: string); +declare function param_double(name: string, min: number, max: number, def: number): Holder; +declare function param_int(name: string, min: number, max: number, def: number): Holder; +declare function param_bool(name: string, def: boolean): Holder; +declare function param_string(name: string, def: boolean): Holder; + +// script_selection +declare class script_selection { + is_on_chunk(): boolean; + is_on_vertex(): boolean; + is_on_tex(): boolean; + + next_chunk(); + next_vertex(); + next_tex(); + + reset_cur_chunk(); + reset_cur_vertex(); + reset_cur_tex(); + + chunk_add_texture(texture: string): number; + chunk_clear_textures(); + chunk_remove_texture(index: number); + chunk_get_texture(index: number): string; + chunk_apply_textures(); + chunk_apply_heightmap(); + chunk_apply_vertex_color(); + chunk_apply_all(); + chunk_set_impassable(add: boolean); + chunk_get_area_id(): number; + chunk_set_area_id(value: number); + + vert_get_x(): number; + vert_get_y(): number; + vert_get_z(): number; + + vert_set_y(value: number); + vert_set_color(r: number, g: number, b: number); + vert_set_water(type: number, height: number); + vert_is_water_aligned(): boolean; + vert_set_hole(add: boolean): void; + tex_get_alpha(index: number): number; + tex_set_alpha(index: number, alpha: number); +} + +// script_random +declare class script_random { + get_int32(low: number, high: number): number; + get_uint32(low: number, high: number): number; + + get_double(low: number, high: number): number; + get_float(low: number, high: number): number; +} +declare function random_from_seed(seed: string): script_random; +declare function random_from_time(): script_random; + +// script_print +declare function print(...args: any[]); + +// script_noise +declare class script_noise_2d { + /** Gets a value from integer parameters (faster) */ + get(x: number, y: number); + /** Gets a value from float parameters (slower) */ + get_f(x: number, y: number); + set(x: number, y: number, value: number); + get_start_x(): number; + get_start_y(): number; + get_width(): number; + get_height(): number; +} + +declare class script_noise_generator { + uniform_2d(seed: string, xStart: number, yStart: number, xSize: number, ySize: number, frequency: number); +} + +declare function noise_simplex(): script_noise_generator; +declare function noise_perlin(): script_noise_generator; +declare function noise_value(): script_noise_generator; +declare function noise_fractal(): script_noise_generator; +declare function noise_cellular(): script_noise_generator; +declare function noise_white(): script_noise_generator; +declare function noise_custom(encodedNodeTree: string): script_noise_generator; + +// script_image +declare class script_image { + get_pixel(x: number, y: number): number; + set_pixel(x: number, y: number, value: number): number; + save(path: string); + get_width(): number; + get_height(): number; +} + +declare function load_image(path: string): script_image; +declare function create_image(width: number, height: number): script_image; + +// script_filesystem +declare class script_file_iterator { + is_on_file(): boolean; + get_file(): string; + next_file(); +} + +declare function write_file(path: string, content: string); +declare function append_file(path: string, content: string); +declare function read_directory(path: string): script_file_iterator; +declare function read_file(path: string): string; +declare function path_exists(path: string): boolean; + +// script_context +declare class script_context { + pos(): script_vec; + select(origin_x: number, origin_z: number, inner_radius: number, outer_raduis: number): script_selection; + change_terrain(vec: script_vec, change: number, radius: number, inner_radius: number, brush_type: number); + add_m2(filename: string, pos: script_vec, scale: number, rotation: script_vec); + add_wmo(filename: string, pos: script_vec, rotation: script_vec); + get_map_id(): number; + get_area_id(): number; + set_area_id(pos: script_vec, id: number, adt?: boolean); + change_vertex_color(pos: script_vec, color: script_vec, alpha: number, change: number, radius: number, edit_mode: boolean); + get_vertex_color(pos: script_vec): script_vec; + flatten_terrain(pos: script_vec, remain: number, radius: number, brush_type: number, lower: boolean, raise: boolean, origin: script_vec, angle: number, orientation: number); + blur_terrain(pos: script_vec, remain: number, radius: number, brush_type: number, lower: boolean, raise: boolean); + erase_textures(pos: script_vec); + clear_shadows(pos: script_vec); + clear_textures(pos: script_vec); + clear_height(pos: script_vec); + /** + * @param pos + * @param big: If the hole should cover the whole chunk + * @param hole: true = create hole, false = remove hole + */ + set_hole(pos: script_vec, big: boolean, hole: boolean); + set_hole_adt(pos: script_vec, hole: boolean); + deselect_vertices_radius(pos: script_vec, radius: number); + clear_vertex_selection(); + move_vertices(amount: number); + flatten_vertices(amount: number); + update_vertices(); + paint_texture(pos: script_vec, strength: number, pressure: number, hardness: number, radius: number, texture: string); + + cam_pitch(): number; + cam_yaw(): number; + + holding_alt(): boolean; + holding_shift(): boolean; + holding_ctrl(): boolean; + holding_space(): boolean; +} + +// basic events +declare function on_left_click(callback: (arg: script_context)=>void); +declare function on_left_hold(callback: (arg: script_context)=>void); +declare function on_left_release(callback: (arg: script_context)=>void); + +declare function on_right_click(callback: (arg: script_context)=>void); +declare function on_right_hold(callback: (arg: script_context)=>void); +declare function on_right_release(callback: (arg: script_context)=>void); \ No newline at end of file diff --git a/src/noggit/scripting/script_chunk.cpp b/src/noggit/scripting/script_chunk.cpp new file mode 100644 index 000000000..977df373d --- /dev/null +++ b/src/noggit/scripting/script_chunk.cpp @@ -0,0 +1,161 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace noggit::scripting; + +noggit::scripting::script_chunk::script_chunk(script_selection *sel, MapChunk *chunk) +{ + _sel = sel; + _chunk = chunk; +} + +void noggit::scripting::chunk_set_hole(script_chunk &chunk, bool hole) +{ + chunk._chunk->setHole(math::vector_3d(0, 0, 0), true, hole); +} + +int noggit::scripting::chunk_add_texture(script_chunk &chunk, const char *texture) +{ + std::string tex = std::string(texture); + if (!boost::starts_with(tex, "tileset/")) + { + tex = "tileset/" + tex; + } + return chunk._chunk->texture_set->addTexture(scoped_blp_texture_reference(tex)); +} + +void noggit::scripting::chunk_clear_textures(script_chunk &chunk) +{ + chunk._chunk->texture_set->eraseTextures(); +} + +void noggit::scripting::chunk_remove_texture(script_chunk &chunk, int index) +{ + chunk._chunk->texture_set->eraseTexture(index); +} + +const char *noggit::scripting::chunk_get_texture(script_chunk &chunk, int index) +{ + return (chunk._chunk->texture_set->texture(index)->filename).c_str(); +} + +void noggit::scripting::chunk_apply_heightmap(script_chunk &chunk) +{ + chunk._chunk->updateVerticesData(); + get_ctx()->_world->recalc_norms(chunk._chunk); +} + +void noggit::scripting::chunk_apply_textures(script_chunk &chunk) +{ + chunk._chunk->texture_set->apply_alpha_changes(); +} + +void noggit::scripting::chunk_apply_vertex_color(script_chunk &chunk) +{ + chunk._chunk->UpdateMCCV(); +} + +void noggit::scripting::chunk_apply_all(script_chunk &chunk) +{ + chunk_apply_heightmap(chunk); + chunk_apply_textures(chunk); + chunk_apply_vertex_color(chunk); +} + +int noggit::scripting::chunk_get_area_id(script_chunk &chunk) +{ + return chunk._chunk->getAreaID(); +} + +void noggit::scripting::chunk_set_area_id(script_chunk &chunk, int value) +{ + return chunk._chunk->setAreaID(value); +} + +void noggit::scripting::chunk_set_impassable(script_chunk &chunk, bool add) +{ + chunk._chunk->setFlag(add, 0x2); +} + +static bool is_on_vert(script_chunk &chunk) +{ + return chunk._vert_index < 145; +} + +static bool is_on_tex(script_chunk &chunk) +{ + return chunk._tex_index < 4096; +} + +static void skip_vertices(script_chunk &chunk) +{ + auto sel = chunk._sel; + if (sel == nullptr) + return; + while (is_on_vert(chunk)) + { + auto &vert = chunk._chunk->mVertices[chunk._vert_index]; + if (vert.x <= sel->_min.x || vert.x >= sel->_max.x || + vert.z <= sel->_min.z || vert.z >= sel->_max.z) + { + ++chunk._vert_index; + } + else + { + break; + } + } +} + +bool noggit::scripting::chunk_next_vert(script_chunk &chunk) +{ + ++chunk._vert_index; + if (is_on_vert(chunk)) + { + skip_vertices(chunk); + return is_on_vert(chunk); + } + else + { + return false; + } +} + +bool noggit::scripting::chunk_next_tex(script_chunk &chunk) +{ + ++chunk._tex_index; + return is_on_tex(chunk); +} + +void noggit::scripting::chunk_reset_vert_itr(script_chunk &chunk) +{ + chunk._vert_index = -1; +} + +void noggit::scripting::chunk_reset_tex_itr(script_chunk &chunk) +{ + chunk._tex_index = -1; +} + +script_vert noggit::scripting::chunk_get_vert(script_chunk &chunk) +{ + return script_vert(chunk._chunk, chunk._vert_index); +} + +script_tex noggit::scripting::chunk_get_tex(script_chunk &chunk) +{ + return script_tex(chunk._chunk, chunk._tex_index); +} + +void noggit::scripting::chunk_clear_colors(script_chunk &chunk) +{ + for(int i=0;imccv[i] = math::vector_3d(1.0,1.0,1.0); + } +} \ No newline at end of file diff --git a/src/noggit/scripting/script_chunk.hpp b/src/noggit/scripting/script_chunk.hpp new file mode 100644 index 000000000..4b74de6d7 --- /dev/null +++ b/src/noggit/scripting/script_chunk.hpp @@ -0,0 +1,47 @@ +#pragma once + +#include +#include + +namespace noggit +{ + namespace scripting + { + struct script_selection; + + struct script_chunk + { + script_chunk(script_selection *sel, MapChunk *chunk); + script_chunk() {} + MapChunk *_chunk; + script_selection *_sel; + int _vert_index = -1; + int _tex_index = -1; + }; + + void chunk_remove_texture(script_chunk &chunk, int index); + const char *chunk_get_texture(script_chunk &chunk, int index); + int chunk_add_texture(script_chunk &chunk, const char *texture); + void chunk_clear_textures(script_chunk &chunk); + + void chunk_set_hole(script_chunk &chunk, bool hole); + + void chunk_clear_colors(script_chunk &chunk); + void chunk_apply_textures(script_chunk &chunk); + void chunk_apply_heightmap(script_chunk &chunk); + void chunk_apply_vertex_color(script_chunk &chunk); + void chunk_apply_all(script_chunk &chunk); + void chunk_set_impassable(script_chunk &chunk, bool add); + int chunk_get_area_id(script_chunk &chunk); + void chunk_set_area_id(script_chunk &chunk, int value); + + bool chunk_next_vert(script_chunk &chunk); + bool chunk_next_tex(script_chunk &chunk); + + void chunk_reset_vert_itr(script_chunk &chunk); + void chunk_reset_tex_itr(script_chunk &chunk); + + script_vert chunk_get_vert(script_chunk &chunk); + script_tex chunk_get_tex(script_chunk &chunk); + } // namespace scripting +} // namespace noggit \ No newline at end of file diff --git a/src/noggit/scripting/script_context.cpp b/src/noggit/scripting/script_context.cpp new file mode 100644 index 000000000..4dc729f7c --- /dev/null +++ b/src/noggit/scripting/script_context.cpp @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace noggit::scripting; + +/** script_context */ + +noggit::scripting::script_context::script_context(World *world, math::vector_3d pos, float outer_radius, float inner_radius, noggit::camera *camera, bool alt, bool shift, bool ctrl, bool space) +: _world(world), _pos(pos), _camera(camera), + _holding_alt(alt), _holding_shift(shift), + _holding_ctrl(ctrl), _holding_space(space), + _outer_radius(outer_radius), _inner_radius(inner_radius) + {} + +float noggit::scripting::script_context::cam_pitch() +{ + return _camera->pitch()._; +} + +float noggit::scripting::script_context::outer_radius() +{ + return _outer_radius; +} + +float noggit::scripting::script_context::inner_radius() +{ + return _inner_radius; +} + +float noggit::scripting::script_context::cam_yaw() +{ + return _camera->yaw()._; +} + +std::shared_ptr noggit::scripting::script_context::select(float origin_x, float origin_z, float inner_radius, float outer_radius) +{ + return std::make_shared(_world,origin_x,origin_z,inner_radius,outer_radius); +} + +void noggit::scripting::script_context::change_terrain(vecptr pos, float change, float radius, float inner_radius, int brush_type) +{ + _world->changeTerrain(pos->_vec,change,radius,brush_type,inner_radius); +} + +void noggit::scripting::script_context::add_m2(std::string filename, vecptr pos, float scale, vecptr rotation) +{ + auto p = object_paste_params(); + _world->addM2(filename,pos->_vec,scale,rotation->_vec,&p); +} + +void noggit::scripting::script_context::add_wmo(std::string filename, vecptr pos, vecptr rotation) +{ + _world->addWMO(filename,pos->_vec,rotation->_vec); +} + +unsigned int noggit::scripting::script_context::get_map_id() +{ + return _world->getMapID(); +} + +unsigned int noggit::scripting::script_context::get_area_id(vecptr pos) +{ + return _world->getAreaID(pos->_vec); +} + +void noggit::scripting::script_context::set_area_id(vecptr pos, int id, bool adt) +{ + _world->setAreaID(pos->_vec,id,adt); +} + +void noggit::scripting::script_context::change_vertex_color(vecptr pos, vecptr color, float alpha, float change, float radius, bool editMode) +{ + auto v = color->_vec; + _world->changeShader(pos->_vec,math::vector_4d(v.x,v.y,v.z,alpha),change,radius,editMode); +} + +vecptr noggit::scripting::script_context::get_vertex_color(vecptr pos) +{ + return std::make_shared(_world->pickShaderColor(pos->_vec)); +} + +void noggit::scripting::script_context::flatten_terrain(vecptr pos, float remain, float radius, int brush_type, bool lower, bool raise, vecptr origin, double angle, double orientation) +{ + _world->flattenTerrain(pos->_vec,remain,radius,brush_type,flatten_mode(raise,lower),origin->_vec,math::degrees(angle),math::degrees(orientation)); +} + +void noggit::scripting::script_context::blur_terrain(vecptr pos, float remain, float radius, int brush_type, bool lower, bool raise) +{ + _world->blurTerrain(pos->_vec,remain,radius,brush_type,flatten_mode(raise,lower)); +} + +void noggit::scripting::script_context::erase_textures(vecptr pos) +{ + _world->eraseTextures(pos->_vec); +} + +void noggit::scripting::script_context::clear_shadows(vecptr pos) +{ + _world->clear_shadows(pos->_vec); +} + +void noggit::scripting::script_context::clear_textures(vecptr pos) +{ + _world->clearTextures(pos->_vec); +} + +void noggit::scripting::script_context::clear_height(vecptr pos) +{ + _world->clearHeight(pos->_vec); +} + +void noggit::scripting::script_context::set_hole(vecptr pos, bool big, bool hole) +{ + _world->setHole(pos->_vec,big,hole); +} + +void noggit::scripting::script_context::set_hole_adt(vecptr pos, bool hole) +{ + _world->setHoleADT(pos->_vec,hole); +} + +void noggit::scripting::script_context::update_vertices() +{ + _world->updateVertexCenter(); + _world->updateSelectedVertices(); +} + +void noggit::scripting::script_context::deselect_vertices_radius(vecptr pos, float radius) +{ + _world->deselectVertices(pos->_vec,radius); +} + +void noggit::scripting::script_context::move_vertices(float h) +{ + _world->moveVertices(h); +} + +void noggit::scripting::script_context::flatten_vertices(float h) +{ + _world->flattenVertices(h); +} + +void noggit::scripting::script_context::clear_vertex_selection() +{ + _world->clearVertexSelection(); +} + +void noggit::scripting::script_context::paint_texture(vecptr pos, float strength, float pressure, float hardness, float radius, std::string texture) +{ + auto brush = Brush(); + brush.setHardness(hardness); + brush.setRadius(radius); + _world->paintTexture(pos->_vec,&brush,strength,pressure,scoped_blp_texture_reference(texture)); +} + +bool noggit::scripting::script_context::holding_alt() +{ + return _holding_alt; +} +bool noggit::scripting::script_context::holding_shift() +{ + return _holding_shift; +} +bool noggit::scripting::script_context::holding_ctrl() +{ + return _holding_ctrl; +} +bool noggit::scripting::script_context::holding_space() +{ + return _holding_space; +} + +std::shared_ptr script_context::pos() +{ + return std::make_shared(_pos); +} + +/** registry */ +void noggit::scripting::register_context_functions(duk_context *ctx) +{ + GLUE_METHOD(ctx, script_context, update_vertices); + GLUE_METHOD(ctx, script_context, pos); + GLUE_METHOD(ctx, script_context, change_terrain); + GLUE_METHOD(ctx, script_context, add_m2); + GLUE_METHOD(ctx, script_context, add_wmo); + GLUE_METHOD(ctx, script_context, get_map_id); + GLUE_METHOD(ctx, script_context, get_area_id); + GLUE_METHOD(ctx, script_context, set_area_id); + GLUE_METHOD(ctx, script_context, change_vertex_color); + GLUE_METHOD(ctx, script_context, get_vertex_color); + GLUE_METHOD(ctx, script_context, flatten_terrain); + GLUE_METHOD(ctx, script_context, blur_terrain); + GLUE_METHOD(ctx, script_context, erase_textures); + GLUE_METHOD(ctx, script_context, clear_shadows); + GLUE_METHOD(ctx, script_context, clear_textures); + GLUE_METHOD(ctx, script_context, clear_height); + GLUE_METHOD(ctx, script_context, set_hole); + GLUE_METHOD(ctx, script_context, set_hole_adt); + GLUE_METHOD(ctx, script_context, deselect_vertices_radius); + GLUE_METHOD(ctx, script_context, clear_vertex_selection); + GLUE_METHOD(ctx, script_context, move_vertices); + GLUE_METHOD(ctx, script_context, flatten_vertices); + GLUE_METHOD(ctx, script_context, cam_pitch); + GLUE_METHOD(ctx, script_context, cam_yaw); + + GLUE_METHOD(ctx, script_context, outer_radius); + GLUE_METHOD(ctx, script_context, inner_radius); + GLUE_METHOD(ctx, script_context, select); + + GLUE_METHOD(ctx, script_context, holding_alt); + GLUE_METHOD(ctx, script_context, holding_shift); + GLUE_METHOD(ctx, script_context, holding_ctrl); + GLUE_METHOD(ctx, script_context, holding_space); + GLUE_METHOD(ctx, script_context, paint_texture); +} \ No newline at end of file diff --git a/src/noggit/scripting/script_context.hpp b/src/noggit/scripting/script_context.hpp new file mode 100644 index 000000000..2303366be --- /dev/null +++ b/src/noggit/scripting/script_context.hpp @@ -0,0 +1,90 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +/** + * script_context.hpp + * + * Contains library functions that scripts can call. + */ +class World; +namespace math { + struct vector_3d; +} + +struct duk_hthread; + +namespace noggit +{ + class camera; + + namespace scripting + { + struct script_vec; + typedef std::shared_ptr vecptr; + + class script_context { + public: + script_context(World *world, math::vector_3d pos, float outer_radius, float inner_radius, noggit::camera *camera, bool alt, bool shift, bool ctrl, bool space); + std::shared_ptr pos(); + std::shared_ptr select(float origin_x, float origin_z, float inner_radius, float outer_radius); + void change_terrain(vecptr, float change, float radius, float inner_radius, int brush_type); + void add_m2(std::string filename, vecptr pos, float scale, vecptr rotation); + void add_wmo(std::string filename, vecptr pos, vecptr rotation); + unsigned int get_map_id(); + unsigned int get_area_id(vecptr); + void set_area_id(vecptr, int id, bool adt = false); + void change_vertex_color(vecptr pos, vecptr color, float alpha, float change, float radius, bool editMode); + vecptr get_vertex_color(vecptr pos); + void flatten_terrain(vecptr pos, float remain, float radius, int brush_type, bool lower, bool raise, vecptr origin, double angle, double orientation); + void blur_terrain(vecptr pos, float remain, float radius, int brush_type, bool lower, bool raise); + void erase_textures(vecptr pos); + void clear_shadows(vecptr pos); + void clear_textures(vecptr pos); + void clear_height(vecptr pos); + void set_hole(vecptr pos, bool big, bool hole); + void set_hole_adt(vecptr pos, bool hole); + void deselect_vertices_radius(vecptr pos, float radius); + void clear_vertex_selection(); + void move_vertices(float h); + void flatten_vertices(float h); + void update_vertices(); + + void paint_texture(vecptr pos, float strength, float pressure, float hardness, float radius, std::string texture); + + float cam_pitch(); + float cam_yaw(); + + float outer_radius(); + float inner_radius(); + + bool holding_alt(); + bool holding_shift(); + bool holding_ctrl(); + bool holding_space(); + private: + World *_world; + bool _holding_alt; + bool _holding_shift; + bool _holding_ctrl; + bool _holding_space; + noggit::camera *_camera; + math::vector_3d _pos; + float _outer_radius; + float _inner_radius; + }; + + void register_context_functions(duk_hthread* ctx); + } +} \ No newline at end of file diff --git a/src/noggit/scripting/script_exception.hpp b/src/noggit/scripting/script_exception.hpp new file mode 100644 index 000000000..3684a9004 --- /dev/null +++ b/src/noggit/scripting/script_exception.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace noggit { + namespace scripting { + class script_exception : public std::exception { + public: + script_exception(const char* msg) : std::exception(msg){} + script_exception(std::string msg): std::exception(msg.c_str()){} + }; + } +} \ No newline at end of file diff --git a/src/noggit/scripting/script_filesystem.cpp b/src/noggit/scripting/script_filesystem.cpp new file mode 100644 index 000000000..3d5a82beb --- /dev/null +++ b/src/noggit/scripting/script_filesystem.cpp @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +using namespace noggit::scripting; +namespace fs = boost::filesystem; + +std::string noggit::scripting::read_file(std::string path) +{ + if(!fs::exists(path)) + { + throw script_exception("No such file:"+path); + } + std::ifstream t(path); + std::string str((std::istreambuf_iterator(t)), + std::istreambuf_iterator()); + return str; +} + +static void mkdirs(std::string pathstr) +{ + auto path = fs::path(pathstr); + auto parent_path = path.parent_path(); + if(parent_path.string().size()>0) + { + fs::create_directories(path.parent_path()); + } +} + +void noggit::scripting::write_file(std::string path, std::string input) +{ + mkdirs(path); + std::ofstream(path) << input; +} + +void noggit::scripting::append_file(std::string path, std::string input) +{ + mkdirs(path); + std::ofstream outfile; + outfile.open(path, std::ios_base::app); // append instead of overwrite + outfile << input; +} + +bool noggit::scripting::path_exists(std::string path) +{ + return fs::exists(path); +} + +std::shared_ptr noggit::scripting::read_directory(std::string path) +{ + fs::recursive_directory_iterator dir(path), end; + return std::make_shared(dir,end); +} + + +void noggit::scripting::script_file_iterator::skip_dirs() +{ + while(_dir != _end && boost::filesystem::is_directory(_dir->path())) + { + ++_dir; + } +} + +noggit::scripting::script_file_iterator::script_file_iterator +(fs::recursive_directory_iterator dir, fs::recursive_directory_iterator end): _dir(dir), _end(end) +{ + skip_dirs(); +} + +bool noggit::scripting::script_file_iterator::is_on_file() +{ + return _dir != _end; +} + +std::string noggit::scripting::script_file_iterator::get_file() +{ + return _dir->path().string(); +} + +void noggit::scripting::script_file_iterator::next_file() +{ + if(_dir != _end) + { + ++_dir; + skip_dirs(); + } +} + +void noggit::scripting::register_filesystem_functions(duk_context* ctx) +{ + GLUE_FUNCTION(ctx,read_file); + GLUE_FUNCTION(ctx,write_file); + GLUE_FUNCTION(ctx,read_directory); + GLUE_FUNCTION(ctx,append_file); + + GLUE_METHOD(ctx,script_file_iterator,is_on_file); + GLUE_METHOD(ctx,script_file_iterator,get_file); + GLUE_METHOD(ctx,script_file_iterator,next_file); +} \ No newline at end of file diff --git a/src/noggit/scripting/script_filesystem.hpp b/src/noggit/scripting/script_filesystem.hpp new file mode 100644 index 000000000..6a915b19b --- /dev/null +++ b/src/noggit/scripting/script_filesystem.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include +#include + +struct duk_hthread; + +namespace noggit { + namespace scripting { + namespace fs = boost::filesystem; + + class script_file_iterator { + public: + script_file_iterator(fs::recursive_directory_iterator dir, fs::recursive_directory_iterator end); + bool is_on_file(); + std::string get_file(); + void next_file(); + private: + fs::recursive_directory_iterator _dir; + fs::recursive_directory_iterator _end; + void skip_dirs(); + }; + + void write_file(std::string path, std::string content); + void append_file(std::string path, std::string content); + std::shared_ptr read_directory(std::string path); + std::string read_file(std::string path); + bool path_exists(std::string path); + + void register_filesystem_functions(duk_hthread *ctx); + + } +} \ No newline at end of file diff --git a/src/noggit/scripting/script_image.cpp b/src/noggit/scripting/script_image.cpp new file mode 100644 index 000000000..8b705c6c2 --- /dev/null +++ b/src/noggit/scripting/script_image.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include + +using namespace noggit::scripting; + +noggit::scripting::script_image::script_image(std::string path) +{ + unsigned error = lodepng::decode(image,width,height,path); + if(error) + { + get_cur_tool()->addLog("[error]: failed to load image with error "+error); + } +} + +noggit::scripting::script_image::script_image(unsigned width, unsigned height) +{ + image.resize(width*height*4); + std::fill(image.begin(),image.end(),0); +} + +unsigned noggit::scripting::script_image::get_width() +{ + return width; +} + +unsigned noggit::scripting::script_image::get_height() +{ + return height; +} + +unsigned noggit::scripting::script_image::get_index(unsigned x, unsigned y) +{ + return ((x+y*width)*4); +} + +unsigned int noggit::scripting::script_image::get_pixel(unsigned x, unsigned y) +{ + unsigned index = get_index(x,y); + return image[index] << 24 | image[index+1] << 16 | image[index+2] << 8 | image[index+3]; +} + +void noggit::scripting::script_image::set_pixel(unsigned x, unsigned y, unsigned int value) +{ + unsigned index = get_index(x,y); + image[index] = (value << 24); + image[index+1] = (value << 16)&0xff; + image[index+2] = (value << 8)&0xff; + image[index+3] = (value)&0xff; +} + +void noggit::scripting::script_image::save(std::string filename) +{ + unsigned error = lodepng::encode(filename.c_str(), image, width, height); + if(error) + { + get_cur_tool()->addLog("[error]: failed to save image with error "+error); + } +} + +std::shared_ptr noggit::scripting::load_image(std::string path) +{ + return std::make_shared(path); +} + +std::shared_ptr noggit::scripting::create_image(unsigned width, unsigned height) +{ + return std::make_shared(width,height); +} + +void noggit::scripting::register_image_functions(duk_context* ctx) +{ + GLUE_METHOD(ctx,script_image,get_index); + GLUE_METHOD(ctx,script_image,get_pixel); + GLUE_METHOD(ctx,script_image,set_pixel); + GLUE_METHOD(ctx,script_image,save); + GLUE_METHOD(ctx,script_image,get_width); + GLUE_METHOD(ctx,script_image,get_height); + + GLUE_FUNCTION(ctx,load_image); + GLUE_FUNCTION(ctx,create_image); +} \ No newline at end of file diff --git a/src/noggit/scripting/script_image.hpp b/src/noggit/scripting/script_image.hpp new file mode 100644 index 000000000..7cbb07418 --- /dev/null +++ b/src/noggit/scripting/script_image.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include +#include + +#include + +struct duk_hthread; + +namespace noggit { + namespace scripting { + class script_image + { + public: + script_image(std::string path); + script_image(unsigned width, unsigned height); + unsigned get_index(unsigned x, unsigned y); + unsigned get_pixel(unsigned x, unsigned y); + void set_pixel(unsigned x, unsigned y, unsigned value); + void save(std::string filename); + + unsigned get_width(); + unsigned get_height(); + private: + std::vector image; + unsigned width; + unsigned height; + }; + + std::shared_ptr load_image(std::string path); + std::shared_ptr create_image(unsigned width, unsigned height); + + void register_image_functions(duk_hthread* ctx); + } +} diff --git a/src/noggit/scripting/script_loader.cpp b/src/noggit/scripting/script_loader.cpp new file mode 100644 index 000000000..441af897e --- /dev/null +++ b/src/noggit/scripting/script_loader.cpp @@ -0,0 +1,266 @@ +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace noggit::scripting; + +static duk_context *ctx = nullptr; +static std::vector scripts; +static int selected_script = -1; + +static void handle_error(int value) +{ + if (value) + { + throw script_exception((std::string(duk_safe_to_string(ctx,-1))).c_str()); + } +} + +std::string filename(std::string filename) +{ + return boost::filesystem::path(filename).filename().string(); +} + +std::string dirname(std::string filename) +{ + return boost::filesystem::path(filename).parent_path().string(); +} + +std::string resolve(std::string path) +{ + return boost::filesystem::relative( + boost::filesystem::absolute( + boost::filesystem::path(path)), + boost::filesystem::path("./")) + .string(); +} + +void register_script_functions(duk_context* ctx) +{ + register_setup_functions(ctx); + register_vec_functions(ctx); + register_misc_functions(ctx); + register_image_functions(ctx); + register_noise_functions(ctx); + register_random_functions(ctx); + register_print_functions(ctx); + register_context_functions(ctx); + register_selection_functions(ctx); + register_filesystem_functions(ctx); +} + +#define CLICK_EVENT(type) \ + static bool has_##type##_handler = false; \ + duk_ret_t on_##type(duk_context *ctx) \ + { \ + has_##type##_handler = true; \ + duk_dup(ctx, 0); \ + duk_put_global_string(ctx, #type); \ + return 0; \ + } \ + void send_##type(script_context *script) \ + { \ + if (has_##type##_handler) \ + { \ + duk_get_global_string(ctx, #type); \ + dukglue_push(ctx, script); \ + duk_int_t rc = duk_pcall(ctx, 1); \ + handle_error(rc); \ + duk_pop(ctx); \ + } \ + } + +CLICK_EVENT(left_click) +CLICK_EVENT(left_hold) +CLICK_EVENT(left_release) + +CLICK_EVENT(right_click) +CLICK_EVENT(right_hold) +CLICK_EVENT(right_release) + +/** + * Require Stuff + * + * This segment can be useful for anyone trying to use duktape with CommonJS + */ + +struct ExportsObject {}; + +std::map exports; +std::vector module_stack; + +ExportsObject* __get_exports(std::string key) +{ + return &exports[key]; +} + +inline bool ends_with(std::string const& value, std::string const& ending) +{ + if (ending.size() > value.size()) return false; + return std::equal(ending.rbegin(), ending.rend(), value.rbegin()); +} + +ExportsObject* require(std::string path) +{ + auto parent = module_stack.back(); + auto out = resolve((boost::filesystem::path(dirname(parent)) / boost::filesystem::path(path)).string()); + std::replace(out.begin(), out.end(), '\\', '/'); + + // the first element is just the root and will be duplicate, so ignore it. + if (module_stack.size() > 1) + { + auto index = std::find(module_stack.begin()+1, module_stack.end(), out); + if (index != module_stack.end()) + { + std::string error_msg = "Circular dependency in module: " + out+"\nRequire stack (grows down): \n "; + index = module_stack.begin()+1; + while (index != module_stack.end()) + { + error_msg += (*index); + if (*index == out) error_msg += " << seen here"; + error_msg += "\n ->"; + index++; + } + error_msg += out + " << and again here (error)\n"; + throw script_exception(error_msg.c_str()); + } + } + + auto exports_object = &(exports[out] = ExportsObject()); + module_stack.push_back(out); + // isolate the script context + auto fullstr = "(function(){var exports = __get_exports('" + out + "'); " + read_file(out + ".js") + ";}());"; + int res = duk_peval_string(ctx,fullstr.c_str()); + handle_error(res); + module_stack.pop_back(); + return exports_object; +} + +int noggit::scripting::load_scripts() +{ + exports.clear(); + module_stack.clear(); + + auto tool = get_cur_tool(); + // TODO: error + if (tool == nullptr) + { + return -1; + } + std::string lastName = ""; + if (selected_script != -1) + lastName = scripts[selected_script]; + + LogDebug << "Loading scripts" << std::endl; + if (ctx != nullptr) + { + duk_destroy_heap(ctx); + } + scripts.clear(); + + ctx = duk_create_heap_default(); + duk_push_global_object(ctx); + + // Events + TAPE_FUNCTION(ctx, on_left_click, 1); + TAPE_FUNCTION(ctx, on_left_hold, 1); + TAPE_FUNCTION(ctx, on_left_release, 1); + TAPE_FUNCTION(ctx, on_right_click, 1); + TAPE_FUNCTION(ctx, on_right_hold, 1); + TAPE_FUNCTION(ctx, on_right_release, 1); + + GLUE_FUNCTION(ctx,require); + GLUE_FUNCTION(ctx,__get_exports); + + register_script_functions(ctx); + tool->clearLog(); + + using namespace boost::filesystem; + if(boost::filesystem::exists("scripts") && boost::filesystem::is_directory("scripts")) + { + boost::filesystem::recursive_directory_iterator dir("scripts"), end; + while(dir != end) + { + if(!boost::filesystem::is_directory(dir->path()) && ends_with(dir->path().string(),".js")) + { + auto path = dir->path().string(); + path = path.substr(0,path.size()-3); + module_stack.push_back(path); + require("./"+filename(path)); + module_stack.pop_back(); + } + boost::system::error_code ec; + dir.increment(ec); + if(ec) + { + throw script_exception(("Error accessing " + dir->path().string() + "::" + ec.message() + "\n").c_str()); + } + } + } + + tool->clearDescription(); + if (selected_script != -1) + { + auto it = std::find(scripts.begin(), scripts.end(), lastName); + if (it != scripts.end()) + { + selected_script = -1; + select_script(it - scripts.begin()); + return selected_script; + } + } + selected_script = -1; + select_script(0); + return selected_script; +} + +void noggit::scripting::select_script(int index) +{ + auto tool = get_cur_tool(); + if (tool == nullptr || index == selected_script || index >= scripts.size()) + { + return; + } + + tool->removeScriptWidgets(); + + selected_script = index; + tool->clearDescription(); + tool->clearLog(); + + has_left_click_handler = false; + has_left_hold_handler = false; + has_left_release_handler = false; + has_right_click_handler = false; + has_right_hold_handler = false; + has_right_release_handler = false; + + auto name = scripts[index]; + duk_get_global_string(ctx, name.c_str()); + duk_int_t rc = duk_pcall(ctx, 0); + handle_error(rc); +} + +std::vector *noggit::scripting::get_scripts() +{ + return &scripts; +} \ No newline at end of file diff --git a/src/noggit/scripting/script_loader.hpp b/src/noggit/scripting/script_loader.hpp new file mode 100644 index 000000000..5e1aae2a6 --- /dev/null +++ b/src/noggit/scripting/script_loader.hpp @@ -0,0 +1,50 @@ +// licensed under GNU General Public License (version 3). + +#pragma once + +#include +#include + +#include +#include +#include +#include + +// Registers a duktape-style function +#define TAPE_FUNCTION(ctx, fun, args) \ + duk_push_c_function(ctx, fun, args); \ + duk_put_prop_string(ctx, -2, #fun); + +// Registers a normal c++ function +#define GLUE_FUNCTION(ctx, fun) \ + dukglue_register_function(ctx, fun, #fun); + +// Registers a c++ method +#define GLUE_METHOD(ctx, obj, fun) \ + dukglue_register_method(ctx, &obj::fun, #fun); + +/** + * script_loader.hpp + * + * This file manages the current script context and all event listeners. + * + */ +namespace noggit +{ + namespace scripting + { + class script_context; + + int load_scripts(); + void select_script(int index); + std::vector* get_scripts(); + } // namespace scripting +} // namespace noggit + +void send_left_click(noggit::scripting::script_context*); +void send_left_hold(noggit::scripting::script_context*); +void send_left_release(noggit::scripting::script_context*); + +void send_right_click(noggit::scripting::script_context*); +void send_right_hold(noggit::scripting::script_context*); +void send_right_release(noggit::scripting::script_context*); \ No newline at end of file diff --git a/src/noggit/scripting/script_math.cpp b/src/noggit/scripting/script_math.cpp new file mode 100644 index 000000000..7a65c353a --- /dev/null +++ b/src/noggit/scripting/script_math.cpp @@ -0,0 +1,57 @@ +#include +#include +#include +#include +#include + +using namespace noggit::scripting; + +float noggit::scripting::round(float a1) { return ::round(a1); } +float noggit::scripting::pow(float a1, float a2) { return ::pow(a1, a2); } +float noggit::scripting::log10(float arg) { return ::log10(arg); } +float noggit::scripting::log(float arg) { return ::log(arg); } +float noggit::scripting::ceil(float arg) { return (int) ::ceil(arg); } +float noggit::scripting::floor(float arg) { return (int) ::floor(arg); } +float noggit::scripting::exp(float arg) { return ::exp(arg); } +float noggit::scripting::cbrt(float arg) { return ::cbrt(arg); } +float noggit::scripting::acosh(float arg) { return ::acosh(arg); } +float noggit::scripting::asinh(float arg) { return ::asinh(arg); } +float noggit::scripting::atanh(float arg) { return ::atanh(arg); } +float noggit::scripting::cosh(float arg) { return ::cosh(arg); } +float noggit::scripting::sinh(float arg) { return ::sinh(arg); } +float noggit::scripting::tanh(float arg) { return ::tanh(arg); } +float noggit::scripting::acos(float arg) { return ::acos(arg); } +float noggit::scripting::asin(float arg) { return ::asin(arg); } +float noggit::scripting::atan(float arg) { return ::atan(arg); } +float noggit::scripting::cos(float arg) { return ::cos(arg); } +float noggit::scripting::sin(float arg) { return ::sin(arg); } +float noggit::scripting::tan(float arg) { return ::tan(arg); } +float noggit::scripting::sqrt(float arg) { return ::sqrt(arg); } +float noggit::scripting::abs(float arg) { return ::abs(arg); } + +void noggit::scripting::register_misc_functions(duk_context *ctx) +{ + + GLUE_FUNCTION(ctx,round); + GLUE_FUNCTION(ctx,pow); + GLUE_FUNCTION(ctx,log10); + GLUE_FUNCTION(ctx,log); + GLUE_FUNCTION(ctx,ceil); + GLUE_FUNCTION(ctx,floor); + GLUE_FUNCTION(ctx,exp); + GLUE_FUNCTION(ctx,cbrt); + GLUE_FUNCTION(ctx,acosh); + GLUE_FUNCTION(ctx,asinh); + GLUE_FUNCTION(ctx,atanh); + GLUE_FUNCTION(ctx,cosh); + GLUE_FUNCTION(ctx,sinh); + GLUE_FUNCTION(ctx,tanh); + GLUE_FUNCTION(ctx,acos); + GLUE_FUNCTION(ctx,asin); + GLUE_FUNCTION(ctx,atan); + GLUE_FUNCTION(ctx,cos); + GLUE_FUNCTION(ctx,sin); + GLUE_FUNCTION(ctx,tan); + GLUE_FUNCTION(ctx,sqrt); + GLUE_FUNCTION(ctx,abs); +} \ No newline at end of file diff --git a/src/noggit/scripting/script_math.hpp b/src/noggit/scripting/script_math.hpp new file mode 100644 index 000000000..a48d21d0d --- /dev/null +++ b/src/noggit/scripting/script_math.hpp @@ -0,0 +1,43 @@ +#pragma once + +#include +#include +#include + +struct script_vec; +class script_random; +class script_image; +class script_noise_generator; +struct duk_hthread; + +namespace noggit { + namespace scripting { + + // math stuff + float round(float a1); + float pow(float a1, float a2); + float log10(float arg); + float log(float arg); + float ceil(float arg); + float floor(float arg); + float exp(float arg); + float cbrt(float arg); + float acosh(float arg); + float asinh(float arg); + float atanh(float arg); + float cosh(float arg); + float sinh(float arg); + float tanh(float arg); + float acos(float arg); + float asin(float arg); + float atan(float arg); + float cos(float arg); + float sin(float arg); + float tan(float arg); + float sqrt(float arg); + float abs(float arg); + + // Registry function (not available from script) + void register_misc_functions(duk_hthread *ctx); + } +} \ No newline at end of file diff --git a/src/noggit/scripting/script_model.cpp b/src/noggit/scripting/script_model.cpp new file mode 100644 index 000000000..267451110 --- /dev/null +++ b/src/noggit/scripting/script_model.cpp @@ -0,0 +1,208 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace noggit::scripting; + +noggit::scripting::script_model::script_model(ModelInstance *model) + : _model((void *)model), _is_wmo(false) {} + +noggit::scripting::script_model::script_model(WMOInstance *model) + : _model((void *)model), _is_wmo(true) {} + +static WMOInstance *wmo(script_model &model) +{ + return (WMOInstance *)model._model; +} + +static ModelInstance *m2(script_model &model) +{ + return (ModelInstance *)model._model; +} + +math::vector_3d noggit::scripting::model_get_pos(script_model &model) +{ + if (model._is_wmo) + { + return wmo(model)->pos; + } + else + { + return m2(model)->pos; + } +} + +void noggit::scripting::model_set_pos(script_model &model, math::vector_3d &pos) +{ + if (model._is_wmo) + { + wmo(model)->pos = pos; + } + else + { + m2(model)->pos = pos; + } +} + +math::vector_3d noggit::scripting::model_get_rot(script_model &model) +{ + if (model._is_wmo) + { + return wmo(model)->dir; + } + else + { + return m2(model)->dir; + } +} + +void noggit::scripting::model_set_rot(script_model &model, math::vector_3d &rot) +{ + if (model._is_wmo) + { + wmo(model)->dir = rot; + } + else + { + m2(model)->dir = rot; + } +} + +float noggit::scripting::model_get_scale(script_model &model) +{ + if (model._is_wmo) + { + return 1; + } + else + { + return m2(model)->scale; + } +} + +void noggit::scripting::model_set_scale(script_model &model, float scale) +{ + if (model._is_wmo) + { + return; + } + else + { + m2(model)->scale = scale; + } +} + +unsigned noggit::scripting::model_get_uid(script_model &model) +{ + if (model._is_wmo) + { + return wmo(model)->mUniqueID; + } + else + { + return m2(model)->uid; + } +} + +const char *noggit::scripting::model_get_filename(script_model &model) +{ + if (model._is_wmo) + { + return wmo(model)->wmo->filename.c_str(); + } + else + { + return m2(model)->model->filename.c_str(); + } +} + +void noggit::scripting::model_remove(script_model &model) +{ + std::vector type; + if (model._is_wmo) + { + type.push_back((WMOInstance *)model._model); + } + else + { + type.push_back((ModelInstance *)model._model); + } + get_ctx()->_world->delete_models(type); +} + +void noggit::scripting::model_set_filename(script_model &model, const char *filename) +{ + if (model_get_filename(model) == filename) + { + return; + } + + model_remove(model); + + if (boost::ends_with(filename, ".wmo")) + { + auto wmo = get_ctx()->_world->addWMO(filename, model_get_pos(model), model_get_rot(model)); + model._model = wmo; + model._is_wmo = true; + } + else + { + auto params = object_paste_params(); + auto m2 = get_ctx()->_world->addM2(filename, model_get_pos(model), model_get_scale(model), model_get_rot(model), ¶ms); + model._model = m2; + model._is_wmo = false; + } +} + +noggit::scripting::script_model_iterator::script_model_iterator(World *world, math::vector_3d min, math::vector_3d max) + : _world(world), _min(min), _max(max) {} + +void noggit::scripting::script_model_iterator::query() +{ + _models.clear(); + _world->for_each_m2_instance([&](ModelInstance &model) { + if (model.pos.x >= _min.x && model.pos.x <= _max.x && model.pos.z >= _min.z && model.pos.z <= _max.z) + { + _models.push_back(script_model(&model)); + } + }); + _world->for_each_wmo_instance([&](WMOInstance &model) { + if (model.pos.x >= _min.x && model.pos.x <= _max.x && model.pos.z >= _min.z && model.pos.z <= _max.z) + { + _models.push_back(script_model(&model)); + } + }); + _initialized = true; + reset_itr(); +} + +void noggit::scripting::script_model_iterator::reset_itr() +{ + _model_index = -1; +} + +bool noggit::scripting::script_model_iterator::next() +{ + if (!_initialized) + { + query(); + } + + if (_model_index >= int(_models.size())) + { + return false; + } + + ++_model_index; + return _model_index < _models.size(); +} + +script_model noggit::scripting::script_model_iterator::get() +{ + return _models[_model_index]; +} \ No newline at end of file diff --git a/src/noggit/scripting/script_model.hpp b/src/noggit/scripting/script_model.hpp new file mode 100644 index 000000000..1f0350132 --- /dev/null +++ b/src/noggit/scripting/script_model.hpp @@ -0,0 +1,60 @@ +#pragma once + +#include + +class World; + +class ModelInstance; +class WMOInstance; + +namespace noggit +{ + namespace scripting + { + struct script_model + { + script_model(ModelInstance *model); + script_model(WMOInstance *model); + script_model() {} + + void *_model = nullptr; + bool _is_wmo = false; + }; + + struct script_model_iterator + { + script_model_iterator(World *world, math::vector_3d min, math::vector_3d max); + script_model_iterator() {} + + World *_world; + math::vector_3d _min; + math::vector_3d _max; + + std::vector _models; + + bool _initialized = false; + int _model_index = -1; + + bool next(); + void reset_itr(); + void query(); + script_model get(); + }; + + math::vector_3d model_get_pos(script_model &model); + void model_set_pos(script_model &model, math::vector_3d &pos); + + math::vector_3d model_get_rot(script_model &model); + void model_set_rot(script_model &model, math::vector_3d &rot); + + float model_get_scale(script_model &model); + void model_set_scale(script_model &model, float scale); + + unsigned model_get_uid(script_model &model); + + void model_remove(script_model &model); + + const char *model_get_filename(script_model &model); + void model_set_filename(script_model &model, const char *filename); + } // namespace scripting +} // namespace noggit \ No newline at end of file diff --git a/src/noggit/scripting/script_noggit_module.hpp b/src/noggit/scripting/script_noggit_module.hpp new file mode 100644 index 000000000..cc9db1f0a --- /dev/null +++ b/src/noggit/scripting/script_noggit_module.hpp @@ -0,0 +1,311 @@ +// Note: This file should only be included by script_loader.cpp +#pragma once + +#include + +#include "daScript/daScript.h" +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +using namespace das; +using namespace noggit::scripting; +using namespace math; + +// TODO: Rename this one, it's the odd one +#define FUNC(name, side_effect) \ + addExtern(*this, lib, #name, SideEffects::side_effect, #name) + +#define FUNC_RETVALUE(name, side_effect) \ + addExtern(*this, lib, #name, SideEffects::side_effect, #name) + +#define FUNC_SCOPED(name, side_effect) \ + addExtern(*this, lib, #name, SideEffects::side_effect, #name) + +#define FIELD(name) \ + addField(#name); + +#define CLASS(name, ...) \ + MAKE_TYPE_FACTORY(name, name); \ + struct name##_annotation : public ManagedStructureAnnotation \ + { \ + name##_annotation(ModuleLibrary &ml) : ManagedStructureAnnotation(#name, ml) \ + { \ + __VA_ARGS__ \ + } \ + virtual bool isLocal() const override { return true; } \ + virtual bool canCopy() const override { return true; } \ + virtual bool canMove() const override { return true; } \ + }; + +#define CLASS_TEMPLATE(name, template, regname, ...) \ + MAKE_TYPE_FACTORY(nameDescription: Prints out texture paths and effect ids in the clicked chunk") +texture_printer:add_null_tag() + +function texture_printer:on_left_click(evt) + local sel = select_origin(evt:pos(), 1, 1) + local chunks = sel:chunks() + while chunks:next() do + local chunk = chunks:get() + + if chunk:get_texture_count() == 0 then + print("Chunk has no textures") + end + + print("== Chunk Textures ==") + for i=0,chunk:get_texture_count()-1 do + local tex = chunk:get_texture(i) + local eff = chunk:get_effect(i) + print("Layer "..i..":") + print(" Texture: "..tex) + print(" Effect: "..eff) + end + print("") + end +end \ No newline at end of file From 2da7f048498f9c8dd11bbee87ab481188dce0bf2 Mon Sep 17 00:00:00 2001 From: ihm-ts Date: Mon, 31 May 2021 11:49:14 +0200 Subject: [PATCH 235/250] scripting: update api docs --- scripts/docs/api/classes/chunk.md | 57 +++++++++++++++++-- scripts/docs/api/classes/image.md | 22 +++++++ scripts/docs/api/classes/script_brush.md | 35 ++++++++++++ .../docs/api/classes/script_brush_event.md | 34 +++++++++++ scripts/docs/api/modules.md | 17 ++++-- 5 files changed, 156 insertions(+), 9 deletions(-) diff --git a/scripts/docs/api/classes/chunk.md b/scripts/docs/api/classes/chunk.md index f489a96e0..9651c3e05 100644 --- a/scripts/docs/api/classes/chunk.md +++ b/scripts/docs/api/classes/chunk.md @@ -18,9 +18,12 @@ Represents a chunk in the world - [clear\_colors](chunk.md#clear_colors) - [clear\_textures](chunk.md#clear_textures) - [get\_area\_id](chunk.md#get_area_id) +- [get\_effect](chunk.md#get_effect) - [get\_texture](chunk.md#get_texture) +- [get\_texture\_count](chunk.md#get_texture_count) - [remove\_texture](chunk.md#remove_texture) - [set\_area\_id](chunk.md#set_area_id) +- [set\_effect](chunk.md#set_effect) - [set\_hole](chunk.md#set_hole) - [set\_impassable](chunk.md#set_impassable) @@ -36,7 +39,7 @@ Represents a chunk in the world ### add\_texture -▸ **add_texture**(`texture`: *string*): *number* +▸ **add_texture**(`texture`: *string*, `effect`: *number*): *number* Adds a new texture at the current topmost layer. @@ -44,12 +47,15 @@ Adds a new texture at the current topmost layer. #### Parameters: -Name | Type | -:------ | :------ | -`texture` | *string* | +Name | Type | Description | +:------ | :------ | :------ | +`texture` | *string* | | +`effect` | *number* | effect id to add - -2 (default): does not change effect - -1: clears current effect index - 0+: change to this effect index | **Returns:** *number* +texture index added to + ___ ### apply\_all @@ -122,6 +128,22 @@ Returns the area id of a chunk ___ +### get\_effect + +▸ **get_effect**(`layer`: *number*): *number* + +Returns the effect id at a texture layer + +#### Parameters: + +Name | Type | +:------ | :------ | +`layer` | *number* | + +**Returns:** *number* + +___ + ### get\_texture ▸ **get_texture**(`index`: *number*): *string* @@ -138,6 +160,16 @@ Name | Type | ___ +### get\_texture\_count + +▸ **get_texture_count**(): *number* + +Returns the amount of textures on this chunk + +**Returns:** *number* + +___ + ### remove\_texture ▸ **remove_texture**(`index`: *number*): *void* @@ -171,6 +203,23 @@ Name | Type | ___ +### set\_effect + +▸ **set_effect**(`layer`: *number*, `effect`: *number*): *any* + +Changes the effect id at a texture layer + +#### Parameters: + +Name | Type | Description | +:------ | :------ | :------ | +`layer` | *number* | | +`effect` | *number* | effect id to set (-1 to remove effects) | + +**Returns:** *any* + +___ + ### set\_hole ▸ **set_hole**(`hole`: *boolean*): *void* diff --git a/scripts/docs/api/classes/image.md b/scripts/docs/api/classes/image.md index e1af5577b..c968dab6c 100644 --- a/scripts/docs/api/classes/image.md +++ b/scripts/docs/api/classes/image.md @@ -21,6 +21,7 @@ images can be created from create_image or load_png - [height](image.md#height) - [save](image.md#save) - [set\_pixel](image.md#set_pixel) +- [set\_pixel\_floats](image.md#set_pixel_floats) - [width](image.md#width) ## Constructors @@ -178,6 +179,27 @@ Name | Type | ___ +### set\_pixel\_floats + +▸ **set_pixel_floats**(`x`: *number*, `y`: *number*, `r`: *number*, `g`: *number*, `b`: *number*, `a`: *number*): *void* + +Sets the pixel value at an image coordinate + +#### Parameters: + +Name | Type | Description | +:------ | :------ | :------ | +`x` | *number* | | +`y` | *number* | | +`r` | *number* | should be between 0-1 | +`g` | *number* | should be between 0-1 | +`b` | *number* | should be between 0-1 | +`a` | *number* | should be between 0-1 | + +**Returns:** *void* + +___ + ### width ▸ **width**(): *number* diff --git a/scripts/docs/api/classes/script_brush.md b/scripts/docs/api/classes/script_brush.md index 05724d7df..7298e402b 100644 --- a/scripts/docs/api/classes/script_brush.md +++ b/scripts/docs/api/classes/script_brush.md @@ -19,6 +19,8 @@ Represents a script brush in the script window. ### Methods +- [add\_bool\_tag](script_brush.md#add_bool_tag) +- [add\_description](script_brush.md#add_description) - [add\_int\_tag](script_brush.md#add_int_tag) - [add\_null\_tag](script_brush.md#add_null_tag) - [add\_real\_tag](script_brush.md#add_real_tag) @@ -91,6 +93,39 @@ in the world with this brush ## Methods +### add\_bool\_tag + +▸ **add_bool_tag**(`item`: *string*, `def`: *boolean*): [*tag*](tag.md) + +Adds a bool tag to the settings panel + +#### Parameters: + +Name | Type | +:------ | :------ | +`item` | *string* | +`def` | *boolean* | + +**Returns:** [*tag*](tag.md) + +___ + +### add\_description + +▸ **add_description**(`text`: *string*): *any* + +Adds a description row to the brush window + +#### Parameters: + +Name | Type | +:------ | :------ | +`text` | *string* | + +**Returns:** *any* + +___ + ### add\_int\_tag ▸ **add_int_tag**(`item`: *string*, `low`: *number*, `high`: *number*, `def`: *number*): [*tag*](tag.md) diff --git a/scripts/docs/api/classes/script_brush_event.md b/scripts/docs/api/classes/script_brush_event.md index d81bc2560..b0c77e9b0 100644 --- a/scripts/docs/api/classes/script_brush_event.md +++ b/scripts/docs/api/classes/script_brush_event.md @@ -14,6 +14,8 @@ Represents the event context passed to brush click events. - [inner\_radius](script_brush_event.md#inner_radius) - [outer\_radius](script_brush_event.md#outer_radius) - [pos](script_brush_event.md#pos) +- [set\_inner\_radius](script_brush_event.md#set_inner_radius) +- [set\_outer\_radius](script_brush_event.md#set_outer_radius) ## Constructors @@ -63,3 +65,35 @@ Returns world position of this click event. i.e. the world position where the user clicked, held or released a mouse button **Returns:** [*vector\_3d*](vector_3d.md) + +___ + +### set\_inner\_radius + +▸ **set_inner_radius**(`value`: *number*): *any* + +Sets the outer radius in the settings panel for this brush + +#### Parameters: + +Name | Type | Description | +:------ | :------ | :------ | +`value` | *number* | should be between 0-1 | + +**Returns:** *any* + +___ + +### set\_outer\_radius + +▸ **set_outer_radius**(`value`: *number*): *any* + +Sets the outer radius in the settings panel for this brush + +#### Parameters: + +Name | Type | +:------ | :------ | +`value` | *number* | + +**Returns:** *any* diff --git a/scripts/docs/api/modules.md b/scripts/docs/api/modules.md index ec3ed4bcd..4fdc39aca 100644 --- a/scripts/docs/api/modules.md +++ b/scripts/docs/api/modules.md @@ -1,9 +1,5 @@ # -This is the documentation for the Noggit scripting API. -Functions not connected to a class are global and can be called from -anywhere in a script. - ## Table of contents ### Classes @@ -29,6 +25,7 @@ anywhere in a script. - [brush\_callback](modules.md#brush_callback) - [callback](modules.md#callback) +- [nil](modules.md#nil) ### Functions @@ -100,7 +97,7 @@ ___ ### callback -Ƭ **callback**: T \| *undefined* +Ƭ **callback**: T \| [*nil*](modules.md#nil) Callback functions are unassigned by default, but may be assigned to by the user @@ -111,6 +108,16 @@ Name | :------ | `T` | +___ + +### nil + +Ƭ **nil**: *undefined* + +This is the documentation for the Noggit scripting API. +Functions not connected to a class are global and can be called from +anywhere in a script. + ## Functions ### abs From e46195ee8b939e822400dd43404bc733449cd9e8 Mon Sep 17 00:00:00 2001 From: ihm-ts Date: Mon, 31 May 2021 11:57:06 +0200 Subject: [PATCH 236/250] scripting: add script docs build instructions --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 96ef81376..962170831 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,19 @@ If the build pass correctly without errors, you can go into build/bin/ and run noggit. Note that `make install` will probably work but is not tested, and nobody has built distributable packages in years. +# Building the Scripting documentation # +To generate the scripting API documentation, install any new version of Node.js and run: + +``` +npm i -g typedoc typedoc-plugin-markdown +``` + +Then, enter the "scripting" directory in this repository and run this command: + +``` +typedoc --disableSources --plugin typedoc-plugin-markdown --hideBreadcrumbs --out docs/api global.d.ts && rm docs/api/README.md +``` + # DEVELOPMENT # Feel free to ask the owner of the official repository (https://github.com/wowdev/noggit3) for write access or From 4be7eb113338db60a5063f5cd8dac5fb12573329 Mon Sep 17 00:00:00 2001 From: ihm-ts Date: Mon, 31 May 2021 12:07:31 +0200 Subject: [PATCH 237/250] scripting: improve documentation for texture_brush --- scripts/texture_brush.lua | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/texture_brush.lua b/scripts/texture_brush.lua index 95da85cfe..5a9d823d5 100644 --- a/scripts/texture_brush.lua +++ b/scripts/texture_brush.lua @@ -1,4 +1,10 @@ local texture_brush = brush("Texture Brush") +texture_brush:add_description("Description:") +texture_brush:add_description("- Uses a black / white texture to paint alpha layers. ") +texture_brush:add_description("- The painting is only additive at the moment,") +texture_brush:add_description(" meaning it cannot paint layers from \"underneath\"") +texture_brush:add_description(" another layer.") + local texture = texture_brush:add_string_tag("Brush Texture", "") local pressure = texture_brush:add_real_tag("Pressure", 0, 1000, 1, 2) local index = texture_brush:add_int_tag("Texture Index", 1, 3, 1) From 55521305eeb8488d3ea25b521eb8fc116473a3ba Mon Sep 17 00:00:00 2001 From: ihm-ts Date: Mon, 31 May 2021 12:34:20 +0200 Subject: [PATCH 238/250] scripting: split up description in texture printer --- scripts/texture_printer.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scripts/texture_printer.lua b/scripts/texture_printer.lua index c50f9a9fe..9213a532a 100644 --- a/scripts/texture_printer.lua +++ b/scripts/texture_printer.lua @@ -1,6 +1,8 @@ local texture_printer = brush("Texture Printer"); -texture_printer:add_description("Description: Prints out texture paths and effect ids in the clicked chunk") +texture_printer:add_description("Description:") +texture_printer:add_description("Prints out texture paths and effect ids ") +texture_printer:add_description("in the clicked chunk.") texture_printer:add_null_tag() function texture_printer:on_left_click(evt) From 300d195e74c9036ad906da26b9bf97b4ec24b228 Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Fri, 6 Aug 2021 16:56:24 +0200 Subject: [PATCH 239/250] scripting: replace iterators with tables - add script_procedures module to hold paint_texture (moved from tex_iterator) - add bounds check to tex selections - make selection noise padding smaller (10 -> 2) - remove some magic numbers when iterating tex/verts --- CMakeLists.txt | 2 + scripts/clean.lua | 12 +-- scripts/global.d.ts | 120 ++------------------- scripts/height_noise.lua | 4 +- scripts/image_brush.lua | 5 +- scripts/prop_placer.lua | 9 +- scripts/texture_brush.lua | 8 +- scripts/texture_printer.lua | 6 +- src/noggit/scripting/script_chunk.cpp | 31 ------ src/noggit/scripting/script_chunk.hpp | 13 +-- src/noggit/scripting/script_image.cpp | 34 +++--- src/noggit/scripting/script_image.hpp | 21 ++-- src/noggit/scripting/script_model.cpp | 77 +++---------- src/noggit/scripting/script_model.hpp | 28 ++--- src/noggit/scripting/script_procedures.cpp | 55 ++++++++++ src/noggit/scripting/script_procedures.hpp | 24 +++++ src/noggit/scripting/script_registry.ipp | 2 + src/noggit/scripting/script_selection.cpp | 105 +++++++++++------- src/noggit/scripting/script_selection.hpp | 17 +-- src/noggit/scripting/script_tex.cpp | 99 ++++------------- src/noggit/scripting/script_tex.hpp | 8 ++ src/noggit/scripting/script_vert.cpp | 90 ++-------------- src/noggit/scripting/script_vert.hpp | 24 +---- 23 files changed, 268 insertions(+), 526 deletions(-) create mode 100644 src/noggit/scripting/script_procedures.cpp create mode 100644 src/noggit/scripting/script_procedures.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c8f7c246..cb6212912 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -403,6 +403,7 @@ set ( scripting_sources src/noggit/scripting/script_profiles.cpp src/noggit/scripting/script_settings.cpp src/noggit/scripting/script_exception.cpp + src/noggit/scripting/script_procedures.cpp ) set ( scripting_headers @@ -424,6 +425,7 @@ set ( scripting_headers src/noggit/scripting/script_math.hpp src/noggit/scripting/script_profiles.hpp src/noggit/scripting/script_settings.hpp + src/noggit/scripting/script_procedures.hpp ) set ( opengl_headers diff --git a/scripts/clean.lua b/scripts/clean.lua index a3a1f4920..62cf0e5d3 100644 --- a/scripts/clean.lua +++ b/scripts/clean.lua @@ -15,9 +15,7 @@ local prop_layer_4 = clean:add_int_tag("Effect layer 4",-2,9999999999,-2) function clean:on_left_hold(evt) local sel = select_origin(evt:pos(), evt:outer_radius(), evt:outer_radius()) if(holding_shift()) then - local chunks = sel:chunks() - while chunks:next() do - local chunk = chunks:get() + for i,chunk in pairs(sel:chunks()) do chunk:clear_textures() chunk:clear_colors() if tex_layer_1:get() ~= "" then chunk:add_texture(tex_layer_1:get(),prop_layer_1:get()) end @@ -29,17 +27,13 @@ function clean:on_left_hold(evt) end if(holding_ctrl()) then - local verts = sel:verts() - while verts:next() do - local vert = verts:get() + for i,vert in pairs(sel:verts()) do vert:set_height(def_height:get()) end end if(holding_alt()) then - local models = sel:models() - while models:next() do - local model = models:get() + for i,model in pairs(sel:models()) do model:remove() end end diff --git a/scripts/global.d.ts b/scripts/global.d.ts index 53d299b17..b3760cacd 100644 --- a/scripts/global.d.ts +++ b/scripts/global.d.ts @@ -283,36 +283,6 @@ declare class chunk { set_area_id(value: number): void; } -/** - * An iterator of chunks in the world. - * - * This iterator always starts out "before" the first entry, - * meaning you must call "next" before you call "get": - * - * @example (lua) - * while chunk_iter:next() do - * local chunk = chunk_iter:get() - * end - */ -declare class chunk_iterator { - /** - * Selects the next chunk in this iterator. - */ - next(): boolean; - - /** - * Returns the currently selected chunk. - * - * May only be called after a call to 'next' has returned true. - */ - get(): chunk; - - /** - * Resets this iterator to again point _before_ the first entry. - */ - reset(): void; -} - /** * Writes text to a file * @param file @@ -617,36 +587,6 @@ declare class model { replace(filename: string); } -/** - * An iterator of models in the world. Iterates both m2 and wmo models. - * - * This iterator always starts out "before" the first entry, - * meaning you must call "next" before you call "get": - * - * @example (lua) - * while model_iter:next() do - * local model = model_iter:get() - * end - */ -declare class model_iterator { - /** - * Selects the next model in this iterator. - */ - next(): boolean; - - /** - * Resets this iterator to again point _before_ the first entry. - */ - reset(): void; - - /** - * Returns the currently selected vertex. - * - * May only be called after a call to 'next' has returned true. - */ - get(): model; -} - /** * Represents a map of floats values, typically use with a * noise generator. @@ -743,22 +683,22 @@ declare class selection { /** * Creates and returns an iterator for all models inside this selection */ - models(): model_iterator; + models(): model[]; /** * Creates and returns an iterator for all vertices inside this selection */ - verts(): vert_iterator; + verts(): vert[]; /** * Creates and returns an iterator for all texture units inside this selection */ - tex(): tex_iterator; + tex(): tex[]; /** * Creates and returns an iterator for all chunks inside this selection */ - chunks(): chunk_iterator; + chunks(): chunk[]; /** * Applies all changes made inside this selection. @@ -808,31 +748,6 @@ declare class tex { get_pos_2d(): vector_3d; } -/** - * An iterator of texture units in the worlds texture layers. - * - * This iterator always starts out "before" the first entry, - * meaning you must call "next" before you call "get": - * - * @example (lua) - * while tex_iter:next() do - * local tex_unit = tex_iter:get() - * end - */ -declare class tex_iterator { - /** - * Selects the next vertex in this iterator. - */ - next(): boolean; - - /** - * Returns the currently selected vertex. - * - * May only be called after a call to 'next' has returned true. - */ - get(): tex; -} - /** * Represents a single heightmap vertex in the world. * @@ -905,27 +820,8 @@ declare class vert { is_water_aligned(): boolean; } -/** - * An iterator of vertices in the worlds heightmap. - * - * This iterator always starts out "before" the first entry, - * meaning you must call "next" before you call "get": - * - * @example (lua) - * while vert_iter:next() do - * local vert_unit = vert_iter:get() - * end - */ -declare class vert_iterator { - /** - * Selects the next vertex in this iterator. - */ - next(): boolean; +declare class procedures_class { + paint_texture(sel: selection, img: image, layer: number, pressure: number, angle: number); +} - /** - * Returns the currently selected vertex. - * - * May only be called after a call to 'next' has returned true. - */ - get(): vert; -} \ No newline at end of file +declare const procedures: procedures_class; \ No newline at end of file diff --git a/scripts/height_noise.lua b/scripts/height_noise.lua index a3b26cb02..53f2577a0 100644 --- a/scripts/height_noise.lua +++ b/scripts/height_noise.lua @@ -12,14 +12,12 @@ function noise_brush:on_left_hold(evt) evt:outer_radius(), evt:outer_radius() ) - local verts = sel:verts() local map = sel:make_noise( frequency:get(), algo:get(), seed:get() ) - while verts:next() do - local vert = verts:get() + for i,vert in pairs(sel:verts()) do local height = map:get( vert:get_pos() ) * amplitude:get() diff --git a/scripts/image_brush.lua b/scripts/image_brush.lua index aa6986a23..d9d159b3a 100644 --- a/scripts/image_brush.lua +++ b/scripts/image_brush.lua @@ -12,9 +12,8 @@ function painter_brush:on_left_click(evt) local half_width = width / 2 local half_height = height / 2 local sel = select_origin(origin, width, height) - local verts = sel:verts() - while verts:next() do - local vert = verts:get() + + for i,vert in pairs(sel:verts()) do local angle = cam_yaw() - 90 local pos = rotate_2d( vert:get_pos(), diff --git a/scripts/prop_placer.lua b/scripts/prop_placer.lua index a7689414a..7151c8166 100644 --- a/scripts/prop_placer.lua +++ b/scripts/prop_placer.lua @@ -42,9 +42,8 @@ function prop_placer:on_left_hold(evt) local sel = select_origin(evt:pos(),evt:outer_radius(),evt:outer_radius()) for _,v in pairs(cur_props) do - local models = sel:models() - while models:next() do - local model = models:get() + + for i,model in pairs(sel:models()) do if( (delete_all:get() or model:has_filename(v)) and @@ -68,9 +67,7 @@ function prop_placer:on_left_hold(evt) , seed:get() ) - local verts = sel:verts() - while verts:next() do - local vert = verts:get() + for i,vert in pairs(sel:verts()) do if (not circle_brush:get()) or dist_2d( vert:get_pos(), evt:pos() diff --git a/scripts/texture_brush.lua b/scripts/texture_brush.lua index 5a9d823d5..21061a099 100644 --- a/scripts/texture_brush.lua +++ b/scripts/texture_brush.lua @@ -18,12 +18,6 @@ texture_brush.on_left_hold = function(brush, evt) evt:outer_radius(), evt:outer_radius() ) - local texes = sel:tex() - texes:paint_image( - image, - index:get(), - pressure:get(), - cam_yaw() - 90 - ) + procedures:paint_texture(sel,image,index:get(),pressure:get(),cam_yaw()-90) sel:apply() end \ No newline at end of file diff --git a/scripts/texture_printer.lua b/scripts/texture_printer.lua index 9213a532a..951f816a9 100644 --- a/scripts/texture_printer.lua +++ b/scripts/texture_printer.lua @@ -7,10 +7,8 @@ texture_printer:add_null_tag() function texture_printer:on_left_click(evt) local sel = select_origin(evt:pos(), 1, 1) - local chunks = sel:chunks() - while chunks:next() do - local chunk = chunks:get() - + + for i,chunk in pairs(sel:chunks()) do if chunk:get_texture_count() == 0 then print("Chunk has no textures") end diff --git a/src/noggit/scripting/script_chunk.cpp b/src/noggit/scripting/script_chunk.cpp index 07d296c63..b3f66efb9 100644 --- a/src/noggit/scripting/script_chunk.cpp +++ b/src/noggit/scripting/script_chunk.cpp @@ -135,31 +135,6 @@ namespace noggit ); } - chunk_iterator::chunk_iterator(script_context * ctx, std::shared_ptr> chunks) - : script_object(ctx) - , _chunks(chunks) - {} - - bool chunk_iterator::next() - { - if(_cur>=int(_chunks->size()-1)) - { - return false; - } - ++_cur; - return _cur<_chunks->size(); - } - - chunk chunk_iterator::get() - { - return chunk(state(), (*_chunks)[_cur]); - } - - void chunk_iterator::reset() - { - _cur = -1; - } - std::shared_ptr chunk::to_selection() { return std::make_shared(state(), "chunk#to_selection", _chunk->vmin,_chunk->vmax); @@ -186,12 +161,6 @@ namespace noggit , "set_area_id", &chunk::set_area_id , "to_selection", &chunk::to_selection ); - - state->new_usertype("chunk_iterator" - , "next", &chunk_iterator::next - , "get", &chunk_iterator::get - , "reset", &chunk_iterator::reset - ); } } // namespace scripting } // namespace noggit diff --git a/src/noggit/scripting/script_chunk.hpp b/src/noggit/scripting/script_chunk.hpp index 2b99fa8fa..565ebb479 100644 --- a/src/noggit/scripting/script_chunk.hpp +++ b/src/noggit/scripting/script_chunk.hpp @@ -34,18 +34,7 @@ namespace noggit std::shared_ptr to_selection(); private: MapChunk* _chunk; - }; - - class chunk_iterator : public script_object - { - public: - chunk_iterator(script_context* ctx, std::shared_ptr> chunks); - bool next(); - chunk get(); - void reset(); - private: - int _cur = -1; - std::shared_ptr> _chunks; + friend class selection; }; void register_chunk(script_context * state); diff --git a/src/noggit/scripting/script_image.cpp b/src/noggit/scripting/script_image.cpp index c20fb2bc1..845b6974a 100644 --- a/src/noggit/scripting/script_image.cpp +++ b/src/noggit/scripting/script_image.cpp @@ -50,17 +50,17 @@ namespace noggit resize(width,height); } - int image::width() + int image::width() const { return _width; } - int image::height() + int image::height() const { return _height; } - int image::get_index(int x, int y) + int image::get_index(int x, int y) const { int index = ((x + y * _width) * 4); if(index<0||index>=_size) @@ -79,7 +79,7 @@ namespace noggit return index; } - unsigned image::get_pixel(int x, int y) + unsigned image::get_pixel(int x, int y) const { unsigned index = get_index(x, y); return get_image()[index] << 24 @@ -88,22 +88,22 @@ namespace noggit | get_image()[index + 3]; } - float image::get_alpha(int x, int y) + float image::get_alpha(int x, int y) const { return float(get_pixel(x,y) & 0xff)/255.0; } - float image::get_blue(int x, int y) + float image::get_blue(int x, int y) const { return float((get_pixel(x,y) >> 8) & 0xff)/255.0; } - float image::get_green(int x, int y) + float image::get_green(int x, int y) const { return float((get_pixel(x,y) >> 16) & 0xff)/255.0; } - float image::get_red(int x, int y) + float image::get_red(int x, int y) const { return float((get_pixel(x,y) >> 24) & 0xff)/255.0; } @@ -111,19 +111,19 @@ namespace noggit void image::set_pixel(int x, int y, unsigned value) { unsigned index = get_index(x, y); - get_image()[index] = (value << 24); - get_image()[index + 1] = (value << 16) & 0xff; - get_image()[index + 2] = (value << 8) & 0xff; - get_image()[index + 3] = (value) & 0xff; + get_image_w()[index] = (value << 24); + get_image_w()[index + 1] = (value << 16) & 0xff; + get_image_w()[index + 2] = (value << 8) & 0xff; + get_image_w()[index + 3] = (value) & 0xff; } void image::set_pixel_floats(int x, int y, float r, float g, float b, float a) { unsigned index = get_index(x, y); - get_image()[index] = std::max(std::min(int(r * 255),255),0); - get_image()[index + 1] = std::max(std::min(int(g * 255),255),0); - get_image()[index + 2] = std::max(std::min(int(b * 255),255),0); - get_image()[index + 3] = std::max(std::min(int(a * 255),255),0); + get_image_w()[index] = std::max(std::min(int(r * 255),255),0); + get_image_w()[index + 1] = std::max(std::min(int(g * 255),255),0); + get_image_w()[index + 2] = std::max(std::min(int(b * 255),255),0); + get_image_w()[index + 3] = std::max(std::min(int(a * 255),255),0); } void image::save(std::string const& filename) @@ -139,7 +139,7 @@ namespace noggit } } - float image::gradient_scale(float rel) + float image::gradient_scale(float rel) const { if(rel<0||rel>=1) { diff --git a/src/noggit/scripting/script_image.hpp b/src/noggit/scripting/script_image.hpp index 6e887116c..2c84a85e8 100644 --- a/src/noggit/scripting/script_image.hpp +++ b/src/noggit/scripting/script_image.hpp @@ -19,21 +19,22 @@ namespace noggit image(script_context * ctx, std::string const& path); image(script_context * ctx, int width, int height); std::vector _image; - int get_index(int x, int y); - unsigned get_pixel(int x, int y); - float gradient_scale(float rel); + int get_index(int x, int y) const; + unsigned get_pixel(int x, int y) const; + float gradient_scale(float rel) const; void set_pixel(int x, int y, unsigned value); void set_pixel_floats(int x, int y, float r, float g, float b, float a = 1.0); void save(std::string const& filename); - int width(); - int height(); - float get_blue(int x, int y); - float get_green(int x, int y); - float get_red(int x, int y); - float get_alpha(int x, int y); + int width() const; + int height() const; + float get_blue(int x, int y) const; + float get_green(int x, int y) const; + float get_red(int x, int y) const; + float get_alpha(int x, int y) const; private: void resize(int width, int height); - unsigned char* get_image() {return _image.data();} + unsigned char const * get_image() const {return _image.data();} + unsigned char * get_image_w() {return _image.data();} unsigned _width = 0; unsigned _height = 0; unsigned _size = 0; diff --git a/src/noggit/scripting/script_model.cpp b/src/noggit/scripting/script_model.cpp index 0c76e6f82..3ec135343 100644 --- a/src/noggit/scripting/script_model.cpp +++ b/src/noggit/scripting/script_model.cpp @@ -132,67 +132,28 @@ namespace noggit } } - model_iterator::model_iterator( - script_context * ctx - , math::vector_3d min - , math::vector_3d max - ) - : script_object(ctx) - , _min(min), _max(max) {} - - void model_iterator::query() - { - _models.clear(); - world()->for_each_m2_instance([&](ModelInstance& mod) { - if (mod.pos.x >= _min.x && mod.pos.x <= _max.x - && mod.pos.z >= _min.z && mod.pos.z <= _max.z) + void collect_models( + script_context * ctx + , World * world + , math::vector_3d const& min + , math::vector_3d const& max + , std::vector & vec + ) + { + world->for_each_m2_instance([&](ModelInstance& mod) { + if (mod.pos.x >= min.x && mod.pos.x <= max.x + && mod.pos.z >= min.z && mod.pos.z <= max.z) { - _models.push_back(model(state(),&mod)); + vec.push_back(model(ctx, &mod)); } }); - world()->for_each_wmo_instance([&](WMOInstance& mod) { - if (mod.pos.x >= _min.x && mod.pos.x <= _max.x - && mod.pos.z >= _min.z && mod.pos.z <= _max.z) + world->for_each_wmo_instance([&](WMOInstance& mod) { + if (mod.pos.x >= min.x && mod.pos.x <= max.x + && mod.pos.z >= min.z && mod.pos.z <= max.z) { - _models.push_back(model(state(),&mod)); + vec.push_back(model(ctx, &mod)); } }); - - _initialized = true; - reset(); - } - - void model_iterator::reset() - { - _model_index = -1; - } - - bool model_iterator::next() - { - if (!_initialized) - { - query(); - } - - if (_model_index >= int(_models.size())) - { - return false; - } - - ++_model_index; - return _model_index < int(_models.size()); - } - - model model_iterator::get() - { - if(_model_index >= int(_models.size())) - { - throw script_exception( - "model_iterator#get", - "accessing invalid model: iterator is done"); - } - - return _models[_model_index]; } void register_model(script_context * state) @@ -210,12 +171,6 @@ namespace noggit , "has_filename", &model::has_filename , "replace", &model::replace ); - - state->new_usertype("model_iterator" - , "next", &model_iterator::next - , "reset", &model_iterator::reset - , "get", &model_iterator::get - ); } } // namespace scripting } // namespace noggit diff --git a/src/noggit/scripting/script_model.hpp b/src/noggit/scripting/script_model.hpp index c9fa79b73..9a8b94c71 100644 --- a/src/noggit/scripting/script_model.hpp +++ b/src/noggit/scripting/script_model.hpp @@ -46,27 +46,13 @@ namespace noggit boost::variant _impl; }; - class model_iterator: public script_object - { - public: - model_iterator(script_context * ctx - , math::vector_3d min - , math::vector_3d max - ); - - bool next(); - void reset(); - void query(); - model get(); - - private: - math::vector_3d _min; - math::vector_3d _max; - std::vector _models; - - bool _initialized = false; - int _model_index = -1; - }; + void collect_models( + script_context * ctx + , World * world + , math::vector_3d const& min + , math::vector_3d const& max + , std::vector& vec + ); void register_model(script_context * state); } // namespace scripting diff --git a/src/noggit/scripting/script_procedures.cpp b/src/noggit/scripting/script_procedures.cpp new file mode 100644 index 000000000..80a0bf135 --- /dev/null +++ b/src/noggit/scripting/script_procedures.cpp @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include + +#include + +namespace noggit { + namespace scripting { + void procedures::paint_texture( + selection & sel + , image const& img + , int layer + , float pressure + , float angle + ) + { + auto center = math::vector_3d(sel.min().x + (sel.max().x - sel.min().x) / 2, 0, sel.min().z + (sel.max().z - sel.min().z) / 2); + auto outer_radius = (sel.max().x - sel.min().x) / 2; + int width = img.width(); + int height = img.height(); + float half_width = float(width) / 2; + float half_height = float(height) / 2; + for (auto & tex : sel.textures_raw()) + { + auto global_pos = tex.get_pos_2d(); + auto dist = dist_2d(global_pos, center) / outer_radius; + global_pos = rotate_2d(global_pos, center, angle); + + auto rel_x = (global_pos.x - center.x) / outer_radius; + auto rel_z = (global_pos.z - center.z) / outer_radius; + + if (!(rel_x < -1 || rel_x > 1 || rel_z < -1 || rel_z > 1)) + { + auto img_x = round(half_width + half_width * rel_x); + auto img_z = round(half_height + half_height * rel_z); + if (img_x >= 0 && img_x < width && img_z >= 0 && img_z < height) { + auto old = tex.get_alpha(layer); + tex.set_alpha(layer, old + (img.get_red(img_x, img_z) * pressure * (1 - dist))); + } + } + } + } + + void register_procedures(script_context* state) + { + state->new_usertype("procedures_class" + , "paint_texture", &procedures::paint_texture + ); + state->set("procedures", procedures()); + } + } +} \ No newline at end of file diff --git a/src/noggit/scripting/script_procedures.hpp b/src/noggit/scripting/script_procedures.hpp new file mode 100644 index 000000000..1977715b4 --- /dev/null +++ b/src/noggit/scripting/script_procedures.hpp @@ -0,0 +1,24 @@ +// This file is part of Noggit3, licensed under GNU General Public License (version 3). +#pragma once + +#include + +namespace noggit { + namespace scripting { + class selection; + class image; + class script_context; + class procedures { + public: + void paint_texture( + selection & sel + , image const& image + , int layer + , float pressure + , float angle + ); + }; + + void register_procedures(script_context * state); + } +} \ No newline at end of file diff --git a/src/noggit/scripting/script_registry.ipp b/src/noggit/scripting/script_registry.ipp index f54682363..fc68c8677 100644 --- a/src/noggit/scripting/script_registry.ipp +++ b/src/noggit/scripting/script_registry.ipp @@ -16,6 +16,7 @@ #include #include #include +#include #include @@ -38,6 +39,7 @@ namespace noggit { register_image(lua); register_filesystem(lua); register_standard_brush(lua); + register_procedures(lua); register_settings(lua); } } diff --git a/src/noggit/scripting/script_selection.cpp b/src/noggit/scripting/script_selection.cpp index 4f2afbe30..a8006ebcc 100644 --- a/src/noggit/scripting/script_selection.cpp +++ b/src/noggit/scripting/script_selection.cpp @@ -49,67 +49,94 @@ namespace noggit return _size; } - std::shared_ptr> selection::get_chunks() + std::shared_ptr selection::make_noise( + float frequency + , std::string const& algorithm + , std::string const& seed) + { + return noggit::scripting::make_noise( + state() + // padding to ensure we get no rounding errors (should be smaller?) + , std::floor(_min.x)-2 + , std::floor(_min.z)-2 + , std::ceil(_size.x)+4 + , std::ceil(_size.z)+4 + , frequency + , algorithm + , seed + ); + } + + std::vector selection::chunks_raw() + { + std::vector mapChunks; + _world->select_all_chunks_between(_min, _max, mapChunks); + std::vector chunks; + chunks.reserve(mapChunks.size()); + for (auto& chnk : mapChunks) chunks.emplace_back(state(), chnk); + return chunks; + } + + std::vector selection::verts_raw() { - if(_chunks==nullptr) + std::vector verts; + for (auto chunk : chunks_raw()) { - _chunks = std::make_shared>(); - _world->select_all_chunks_between(_min,_max,*_chunks); + for (int i = 0; i < mapbufsize; ++i) + { + auto& v = chunk._chunk->mVertices[i]; + if (v.x >= _min.x && v.x <= _max.x && + v.z >= _min.z && v.z <= _max.z) + { + verts.emplace_back(state(),chunk._chunk,i); + } + } } - return _chunks; + return verts; } - std::shared_ptr selection::get_model_iterator() + std::vector selection::textures_raw() { - return std::make_shared(state(),_min, _max); + std::vector texvec; + for (auto chnk : chunks_raw()) + { + collect_textures(state(), chnk._chunk, texvec, _min, _max); + } + return texvec; } - std::shared_ptr selection::get_vert_iterator() + std::vector selection::models_raw() { - return std::make_shared(state(),get_chunks(), _min, _max); + std::vector models; + collect_models(state(), world(), _min, _max, models); + return models; } - std::shared_ptr selection::get_tex_iterator() + sol::as_table_t> selection::chunks() { - return std::make_shared(state(),get_chunks(), _min, _max); + return sol::as_table(chunks_raw()); } - std::shared_ptr selection::get_chunk_iterator() + sol::as_table_t> selection::verts() { - return std::make_shared(state(),get_chunks()); + return sol::as_table(verts_raw()); } - void selection::apply_noise(std::shared_ptr noise, float ratio) + sol::as_table_t> selection::textures() { - auto verts = this->get_vert_iterator(); - while(verts->next()) - { - auto vert = verts->get(); - } + return sol::as_table(textures_raw()); } - std::shared_ptr selection::make_noise( - float frequency - , std::string const& algorithm - , std::string const& seed) + sol::as_table_t> selection::models() { - return noggit::scripting::make_noise( - state() - , std::floor(_min.x)-10 - , std::floor(_min.z)-10 - , std::ceil(_size.x)+20 - , std::ceil(_size.z)+20 - , frequency - , algorithm - , seed - ); + return sol::as_table(models_raw()); } void selection::apply() { - for(auto& chnk: *get_chunks()) + for (auto& chnk : chunks_raw()) { - chunk(state(),chnk).apply_all(); + chnk.apply_all(); } } @@ -121,10 +148,10 @@ namespace noggit , "max", &selection::max , "size", &selection::size , "apply", &selection::apply - , "verts", &selection::get_vert_iterator - , "tex", &selection::get_tex_iterator - , "models", &selection::get_model_iterator - , "chunks", &selection::get_chunk_iterator + , "verts", &selection::verts + , "tex", &selection::textures + , "models", &selection::models + , "chunks", &selection::chunks , "make_noise", &selection::make_noise ); diff --git a/src/noggit/scripting/script_selection.hpp b/src/noggit/scripting/script_selection.hpp index 4235437b9..bcc58c41b 100644 --- a/src/noggit/scripting/script_selection.hpp +++ b/src/noggit/scripting/script_selection.hpp @@ -39,23 +39,24 @@ namespace noggit , std::string const& seed ); - void apply_noise(std::shared_ptr noise, float ratio = 1); - math::vector_3d center(); math::vector_3d min(); math::vector_3d max(); math::vector_3d size(); - std::shared_ptr get_model_iterator(); - std::shared_ptr get_vert_iterator(); - std::shared_ptr get_tex_iterator(); - std::shared_ptr get_chunk_iterator(); + std::vector chunks_raw(); + std::vector verts_raw(); + std::vector textures_raw(); + std::vector models_raw(); + + sol::as_table_t> chunks(); + sol::as_table_t> verts(); + sol::as_table_t> textures(); + sol::as_table_t> models(); void apply(); private: - std::shared_ptr> _chunks = nullptr; - std::shared_ptr> get_chunks(); World* _world; math::vector_3d _center; math::vector_3d _min; diff --git a/src/noggit/scripting/script_tex.cpp b/src/noggit/scripting/script_tex.cpp index 44c576f43..83bca304e 100644 --- a/src/noggit/scripting/script_tex.cpp +++ b/src/noggit/scripting/script_tex.cpp @@ -10,6 +10,7 @@ // amount of texunits per chunk length #define TEXTURE_UNITS_WIDTH 64 +#define TEXTURE_UNITS_PER_CHUNK 4096 namespace noggit { namespace scripting { @@ -43,89 +44,39 @@ namespace noggit { ts->tmp_edit_values.get()[index][_index] = value; } + namespace { + math::vector_3d tex_location(MapChunk* chnk, int index) + { + float cx = chnk->xbase; + float cz = chnk->zbase; + float x = index % TEXTURE_UNITS_WIDTH; + float z = (float(index) / float(TEXTURE_UNITS_WIDTH)); + return math::vector_3d(cx + x * TEXDETAILSIZE, 0, cz + z * TEXDETAILSIZE); + } + } + math::vector_3d tex::get_pos_2d() { - float cx = _chunk->xbase; - float cz = _chunk->zbase; - float x = _index % TEXTURE_UNITS_WIDTH; - float z = (float(_index) / float(TEXTURE_UNITS_WIDTH)); - return math::vector_3d(cx + x * TEXDETAILSIZE, 0, cz + z * TEXDETAILSIZE); + return tex_location(_chunk, _index); } - tex_iterator::tex_iterator( + void collect_textures( script_context * ctx - , std::shared_ptr> chunks + , MapChunk* chnk + , std::vector& vec , math::vector_3d const& min - , math::vector_3d const& max) - : script_object(ctx) - , _chunks(chunks) - , _chunk_iter(_chunks->begin()) - , _min(min) - , _max(max) - {} - - void tex_iterator::paint_image(image & img, int layer, float pressure, float angle) + , math::vector_3d const& max + ) { - tex_iterator cpy(state(), _chunks, _min, _max); - auto center = math::vector_3d(_min.x + (_max.x-_min.x)/2,0,_min.z + (_max.z-_min.z)/2); - auto outer_radius = (_max.x - _min.x) / 2; - - int width = img.width(); - int height = img.height(); - - float half_width = float(width) / 2; - float half_height = float(height) / 2; - - while(cpy.next()) + for (int i = 0; i < TEXTURE_UNITS_PER_CHUNK; ++i) { - auto tex = cpy.get(); - auto global_pos = tex.get_pos_2d(); - auto dist = dist_2d(global_pos, center) / outer_radius; - global_pos = rotate_2d(global_pos,center,angle); - - auto rel_x = (global_pos.x - center.x) / outer_radius; - auto rel_z = (global_pos.z - center.z) / outer_radius; - - if(!(rel_x < -1 || rel_x > 1 || rel_z < -1 || rel_z > 1)) + math::vector_3d loc = tex_location(chnk, i); + if (loc.x >= min.x && loc.x <= max.x && loc.z >= min.z && loc.z <= max.z) { - auto img_x = round(half_width + half_width * rel_x); - auto img_z = round(half_height + half_height * rel_z); - if(img_x>=0 && img_x=0 && img_zend()) - { - return false; - } - - ++_tex_iter; - while(_tex_iter < 4096) - { - // TODO: Implement skipping - break; - } - - if(_tex_iter >= 4096) - { - ++_chunk_iter; - _tex_iter = -1; - return next(); - } - - return true; - } - - tex tex_iterator::get() - { - return tex(state(), *_chunk_iter,_tex_iter); - } void register_tex(script_context * state) { @@ -134,12 +85,6 @@ namespace noggit { , "get_alpha", &tex::get_alpha , "get_pos_2d", &tex::get_pos_2d ); - - state->new_usertype("tex_iterator" - , "next", &tex_iterator::next - , "get", &tex_iterator::get - , "paint_image", &tex_iterator::paint_image - ); } } } \ No newline at end of file diff --git a/src/noggit/scripting/script_tex.hpp b/src/noggit/scripting/script_tex.hpp index 5bf3531a0..d0805b629 100644 --- a/src/noggit/scripting/script_tex.hpp +++ b/src/noggit/scripting/script_tex.hpp @@ -26,6 +26,14 @@ namespace noggit { int _index; }; + void collect_textures( + script_context* ctx + , MapChunk* chnk + , std::vector& vec + , math::vector_3d const& min + , math::vector_3d const& max + ); + class tex_iterator : public script_object { public: tex_iterator( script_context * ctx diff --git a/src/noggit/scripting/script_vert.cpp b/src/noggit/scripting/script_vert.cpp index e3a3e3dd9..43a4a90dd 100644 --- a/src/noggit/scripting/script_vert.cpp +++ b/src/noggit/scripting/script_vert.cpp @@ -148,84 +148,15 @@ namespace noggit return (_index % VERTS_PER_TWO_ROWS) > VERTS_ON_ODD_ROWS; } - bool vert::is_tex_done() + sol::as_table_t> vert::textures() { - return _tex_index >= MAX_TEXUNITS_PER_VERT - || texture_index[_index].indices[_tex_index] == -1; - } - - void vert::reset_tex() - { - _tex_index = -1; - } - - bool vert::next_tex() - { - ++_tex_index; - return !is_tex_done(); - } - - tex vert::get_tex() - { - if(is_tex_done()) + std::vector texVec; + for (int i = 0; i < MAX_TEXUNITS_PER_VERT; ++i) { - throw script_exception( - "vert::get_tex", - "accessing invalid texture unit: iterator is done"); + if (texture_index[_index].indices[i] == -1) break; + texVec.emplace_back(state(), _chunk, texture_index[_index].indices[i]); } - return tex(state(), _chunk, texture_index[_index].indices[_tex_index]); - } - - vert_iterator::vert_iterator( - script_context * ctx - , std::shared_ptr> chunks - , math::vector_3d const& min - , math::vector_3d const& max) - : script_object(ctx) - , _chunks(chunks) - , _chunk_iter(_chunks->begin()) - , _min(min) - , _max(max) - {} - - bool vert_iterator::next() - { - if(_chunk_iter==_chunks->end()) - { - return false; - } - - ++_vert_iter; - - // skip verts outside of the selection - while(_vert_iter < 145) - { - auto& vert = (*_chunk_iter)->mVertices[_vert_iter]; - if (vert.x <= _min.x || vert.x >= _max.x || - vert.z <= _min.z || vert.z >= _max.z) - { - ++_vert_iter; - } - else - { - break; - } - } - - // restart if we used too many verts - if(_vert_iter >= 145) - { - ++_chunk_iter; - _vert_iter = -1; - return next(); - } - - return true; - } - - vert vert_iterator::get() - { - return vert(state(), *_chunk_iter,_vert_iter); + return sol::as_table(texVec); } void register_vert(script_context * state) @@ -241,16 +172,9 @@ namespace noggit , "set_hole", &vert::set_hole , "set_alpha", &vert::set_alpha , "get_alpha", &vert::get_alpha - , "next_tex", &vert::next_tex - , "reset_tex", &vert::reset_tex - , "get_tex", &vert::get_tex + , "tex", &vert::textures , "is_water_aligned", &vert::is_water_aligned ); - - state->new_usertype("vert_iterator" - , "next", &vert_iterator::next - , "get", &vert_iterator::get - ); } } // namespace scripting } // namespace noggit diff --git a/src/noggit/scripting/script_vert.hpp b/src/noggit/scripting/script_vert.hpp index cd2e361b4..574c69364 100644 --- a/src/noggit/scripting/script_vert.hpp +++ b/src/noggit/scripting/script_vert.hpp @@ -29,34 +29,12 @@ namespace noggit void set_hole(bool add); void set_alpha(int index, float alpha); float get_alpha(int index); - bool next_tex(); - void reset_tex(); - - tex get_tex(); + sol::as_table_t> textures(); bool is_water_aligned(); private: - bool is_tex_done(); MapChunk* _chunk; int _index; - int _tex_index = -1; - }; - - class vert_iterator: public script_object { - public: - vert_iterator( - script_context * ctx - , std::shared_ptr> chunks - , math::vector_3d const& min - , math::vector_3d const& max); - bool next(); - vert get(); - private: - int _vert_iter = -1; - std::shared_ptr> _chunks; - std::vector::iterator _chunk_iter; - math::vector_3d const& _min; - math::vector_3d const& _max; }; void register_vert(script_context * state); From 201df1918f727b07e30697b2e6c8ce93ed83e2b7 Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Fri, 6 Aug 2021 17:05:29 +0200 Subject: [PATCH 240/250] scripting: update documentation --- scripts/docs/api/classes/chunk_iterator.md | 63 -------------------- scripts/docs/api/classes/model_iterator.md | 63 -------------------- scripts/docs/api/classes/procedures_class.md | 41 +++++++++++++ scripts/docs/api/classes/selection.md | 16 ++--- scripts/docs/api/classes/tex_iterator.md | 52 ---------------- scripts/docs/api/classes/vert_iterator.md | 52 ---------------- scripts/docs/api/modules.md | 17 ++++-- scripts/global.d.ts | 8 +++ 8 files changed, 70 insertions(+), 242 deletions(-) delete mode 100644 scripts/docs/api/classes/chunk_iterator.md delete mode 100644 scripts/docs/api/classes/model_iterator.md create mode 100644 scripts/docs/api/classes/procedures_class.md delete mode 100644 scripts/docs/api/classes/tex_iterator.md delete mode 100644 scripts/docs/api/classes/vert_iterator.md diff --git a/scripts/docs/api/classes/chunk_iterator.md b/scripts/docs/api/classes/chunk_iterator.md deleted file mode 100644 index 361bff9bb..000000000 --- a/scripts/docs/api/classes/chunk_iterator.md +++ /dev/null @@ -1,63 +0,0 @@ -# Class: chunk\_iterator - -An iterator of chunks in the world. - -This iterator always starts out "before" the first entry, -meaning you must call "next" before you call "get": - -**`example`** (lua) -while chunk_iter:next() do - local chunk = chunk_iter:get() -end - -## Table of contents - -### Constructors - -- [constructor](chunk_iterator.md#constructor) - -### Methods - -- [get](chunk_iterator.md#get) -- [next](chunk_iterator.md#next) -- [reset](chunk_iterator.md#reset) - -## Constructors - -### constructor - -\+ **new chunk_iterator**(): [*chunk\_iterator*](chunk_iterator.md) - -**Returns:** [*chunk\_iterator*](chunk_iterator.md) - -## Methods - -### get - -▸ **get**(): [*chunk*](chunk.md) - -Returns the currently selected chunk. - -May only be called after a call to 'next' has returned true. - -**Returns:** [*chunk*](chunk.md) - -___ - -### next - -▸ **next**(): *boolean* - -Selects the next chunk in this iterator. - -**Returns:** *boolean* - -___ - -### reset - -▸ **reset**(): *void* - -Resets this iterator to again point _before_ the first entry. - -**Returns:** *void* diff --git a/scripts/docs/api/classes/model_iterator.md b/scripts/docs/api/classes/model_iterator.md deleted file mode 100644 index f144ad292..000000000 --- a/scripts/docs/api/classes/model_iterator.md +++ /dev/null @@ -1,63 +0,0 @@ -# Class: model\_iterator - -An iterator of models in the world. Iterates both m2 and wmo models. - -This iterator always starts out "before" the first entry, -meaning you must call "next" before you call "get": - -**`example`** (lua) -while model_iter:next() do - local model = model_iter:get() -end - -## Table of contents - -### Constructors - -- [constructor](model_iterator.md#constructor) - -### Methods - -- [get](model_iterator.md#get) -- [next](model_iterator.md#next) -- [reset](model_iterator.md#reset) - -## Constructors - -### constructor - -\+ **new model_iterator**(): [*model\_iterator*](model_iterator.md) - -**Returns:** [*model\_iterator*](model_iterator.md) - -## Methods - -### get - -▸ **get**(): [*model*](model.md) - -Returns the currently selected vertex. - -May only be called after a call to 'next' has returned true. - -**Returns:** [*model*](model.md) - -___ - -### next - -▸ **next**(): *boolean* - -Selects the next model in this iterator. - -**Returns:** *boolean* - -___ - -### reset - -▸ **reset**(): *void* - -Resets this iterator to again point _before_ the first entry. - -**Returns:** *void* diff --git a/scripts/docs/api/classes/procedures_class.md b/scripts/docs/api/classes/procedures_class.md new file mode 100644 index 000000000..43729d51d --- /dev/null +++ b/scripts/docs/api/classes/procedures_class.md @@ -0,0 +1,41 @@ +# Class: procedures\_class + +Contains some general-purpose procedures that don't fit anywhere else. + +Access these functions through the global singleton "procedures". + +## Table of contents + +### Constructors + +- [constructor](procedures_class.md#constructor) + +### Methods + +- [paint\_texture](procedures_class.md#paint_texture) + +## Constructors + +### constructor + +\+ **new procedures_class**(): [*procedures\_class*](procedures_class.md) + +**Returns:** [*procedures\_class*](procedures_class.md) + +## Methods + +### paint\_texture + +▸ **paint_texture**(`sel`: [*selection*](selection.md), `img`: [*image*](image.md), `layer`: *number*, `pressure`: *number*, `angle`: *number*): *any* + +#### Parameters: + +Name | Type | +:------ | :------ | +`sel` | [*selection*](selection.md) | +`img` | [*image*](image.md) | +`layer` | *number* | +`pressure` | *number* | +`angle` | *number* | + +**Returns:** *any* diff --git a/scripts/docs/api/classes/selection.md b/scripts/docs/api/classes/selection.md index c424abae8..c0ecce7e9 100644 --- a/scripts/docs/api/classes/selection.md +++ b/scripts/docs/api/classes/selection.md @@ -56,11 +56,11 @@ ___ ### chunks -▸ **chunks**(): [*chunk\_iterator*](chunk_iterator.md) +▸ **chunks**(): [*chunk*](chunk.md)[] Creates and returns an iterator for all chunks inside this selection -**Returns:** [*chunk\_iterator*](chunk_iterator.md) +**Returns:** [*chunk*](chunk.md)[] ___ @@ -104,11 +104,11 @@ ___ ### models -▸ **models**(): [*model\_iterator*](model_iterator.md) +▸ **models**(): [*model*](model.md)[] Creates and returns an iterator for all models inside this selection -**Returns:** [*model\_iterator*](model_iterator.md) +**Returns:** [*model*](model.md)[] ___ @@ -126,18 +126,18 @@ ___ ### tex -▸ **tex**(): [*tex\_iterator*](tex_iterator.md) +▸ **tex**(): [*tex*](tex.md)[] Creates and returns an iterator for all texture units inside this selection -**Returns:** [*tex\_iterator*](tex_iterator.md) +**Returns:** [*tex*](tex.md)[] ___ ### verts -▸ **verts**(): [*vert\_iterator*](vert_iterator.md) +▸ **verts**(): [*vert*](vert.md)[] Creates and returns an iterator for all vertices inside this selection -**Returns:** [*vert\_iterator*](vert_iterator.md) +**Returns:** [*vert*](vert.md)[] diff --git a/scripts/docs/api/classes/tex_iterator.md b/scripts/docs/api/classes/tex_iterator.md deleted file mode 100644 index fb136321d..000000000 --- a/scripts/docs/api/classes/tex_iterator.md +++ /dev/null @@ -1,52 +0,0 @@ -# Class: tex\_iterator - -An iterator of texture units in the worlds texture layers. - -This iterator always starts out "before" the first entry, -meaning you must call "next" before you call "get": - -**`example`** (lua) -while tex_iter:next() do - local tex_unit = tex_iter:get() -end - -## Table of contents - -### Constructors - -- [constructor](tex_iterator.md#constructor) - -### Methods - -- [get](tex_iterator.md#get) -- [next](tex_iterator.md#next) - -## Constructors - -### constructor - -\+ **new tex_iterator**(): [*tex\_iterator*](tex_iterator.md) - -**Returns:** [*tex\_iterator*](tex_iterator.md) - -## Methods - -### get - -▸ **get**(): [*tex*](tex.md) - -Returns the currently selected vertex. - -May only be called after a call to 'next' has returned true. - -**Returns:** [*tex*](tex.md) - -___ - -### next - -▸ **next**(): *boolean* - -Selects the next vertex in this iterator. - -**Returns:** *boolean* diff --git a/scripts/docs/api/classes/vert_iterator.md b/scripts/docs/api/classes/vert_iterator.md deleted file mode 100644 index e077a164b..000000000 --- a/scripts/docs/api/classes/vert_iterator.md +++ /dev/null @@ -1,52 +0,0 @@ -# Class: vert\_iterator - -An iterator of vertices in the worlds heightmap. - -This iterator always starts out "before" the first entry, -meaning you must call "next" before you call "get": - -**`example`** (lua) -while vert_iter:next() do - local vert_unit = vert_iter:get() -end - -## Table of contents - -### Constructors - -- [constructor](vert_iterator.md#constructor) - -### Methods - -- [get](vert_iterator.md#get) -- [next](vert_iterator.md#next) - -## Constructors - -### constructor - -\+ **new vert_iterator**(): [*vert\_iterator*](vert_iterator.md) - -**Returns:** [*vert\_iterator*](vert_iterator.md) - -## Methods - -### get - -▸ **get**(): [*vert*](vert.md) - -Returns the currently selected vertex. - -May only be called after a call to 'next' has returned true. - -**Returns:** [*vert*](vert.md) - -___ - -### next - -▸ **next**(): *boolean* - -Selects the next vertex in this iterator. - -**Returns:** *boolean* diff --git a/scripts/docs/api/modules.md b/scripts/docs/api/modules.md index 4fdc39aca..dedef4ccb 100644 --- a/scripts/docs/api/modules.md +++ b/scripts/docs/api/modules.md @@ -5,21 +5,18 @@ ### Classes - [chunk](classes/chunk.md) -- [chunk\_iterator](classes/chunk_iterator.md) - [image](classes/image.md) - [model](classes/model.md) -- [model\_iterator](classes/model_iterator.md) - [noisemap](classes/noisemap.md) +- [procedures\_class](classes/procedures_class.md) - [random](classes/random.md) - [script\_brush](classes/script_brush.md) - [script\_brush\_event](classes/script_brush_event.md) - [selection](classes/selection.md) - [tag](classes/tag.md) - [tex](classes/tex.md) -- [tex\_iterator](classes/tex_iterator.md) - [vector\_3d](classes/vector_3d.md) - [vert](classes/vert.md) -- [vert\_iterator](classes/vert_iterator.md) ### Type aliases @@ -27,6 +24,10 @@ - [callback](modules.md#callback) - [nil](modules.md#nil) +### Variables + +- [procedures](modules.md#procedures) + ### Functions - [abs](modules.md#abs) @@ -118,6 +119,14 @@ This is the documentation for the Noggit scripting API. Functions not connected to a class are global and can be called from anywhere in a script. +## Variables + +### procedures + +• `Const` **procedures**: [*procedures\_class*](classes/procedures_class.md) + +singleton object + ## Functions ### abs diff --git a/scripts/global.d.ts b/scripts/global.d.ts index b3760cacd..a008b3e89 100644 --- a/scripts/global.d.ts +++ b/scripts/global.d.ts @@ -820,8 +820,16 @@ declare class vert { is_water_aligned(): boolean; } +/** + * Contains some general-purpose procedures that don't fit anywhere else. + * + * Access these functions through the global singleton "procedures". + */ declare class procedures_class { paint_texture(sel: selection, img: image, layer: number, pressure: number, angle: number); } +/** + * singleton + */ declare const procedures: procedures_class; \ No newline at end of file From e1cca373313b6ae12963d78978d2aa92aba32774 Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Fri, 6 Aug 2021 17:12:59 +0200 Subject: [PATCH 241/250] lol --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b83018af3..c518760ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,7 +73,6 @@ add_global_compiler_flag_if_supported (-Werror=mismatched-tags) add_global_compiler_flag_if_supported (-Wno-multichar) # declaration of 'identifier' hides previous: -<<<<<<< HEAD #add_noggit_compiler_flag_if_supported (/we4456) # local declaration #add_noggit_compiler_flag_if_supported (/we4457) # function parameter From 477e573f2cf1cdac1a4a04b2e35aa64a59fc68a5 Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Sat, 7 Aug 2021 08:59:41 +0200 Subject: [PATCH 242/250] scripting: fix vert::textures return type --- src/noggit/scripting/script_vert.cpp | 2 +- src/noggit/scripting/script_vert.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/noggit/scripting/script_vert.cpp b/src/noggit/scripting/script_vert.cpp index 43a4a90dd..cd400a326 100644 --- a/src/noggit/scripting/script_vert.cpp +++ b/src/noggit/scripting/script_vert.cpp @@ -148,7 +148,7 @@ namespace noggit return (_index % VERTS_PER_TWO_ROWS) > VERTS_ON_ODD_ROWS; } - sol::as_table_t> vert::textures() + sol::as_table_t> vert::textures() { std::vector texVec; for (int i = 0; i < MAX_TEXUNITS_PER_VERT; ++i) diff --git a/src/noggit/scripting/script_vert.hpp b/src/noggit/scripting/script_vert.hpp index 574c69364..44f98c20c 100644 --- a/src/noggit/scripting/script_vert.hpp +++ b/src/noggit/scripting/script_vert.hpp @@ -29,7 +29,7 @@ namespace noggit void set_hole(bool add); void set_alpha(int index, float alpha); float get_alpha(int index); - sol::as_table_t> textures(); + sol::as_table_t> textures(); bool is_water_aligned(); private: From c3f4423f4815cf1fa6b603fafa9990a6059982db Mon Sep 17 00:00:00 2001 From: ihm-tswow <76849026+ihm-tswow@users.noreply.github.com> Date: Sat, 7 Aug 2021 09:47:07 +0200 Subject: [PATCH 243/250] scripting: add windows build instructions --- README.md | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 962170831..884eeca62 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Further following libraries are required for MySQL GUID Storage builds: The following libraries are automatically installed: * StormLib (by Ladislav Zezula) * FastNoise2 (scripting only) -* daScript (scripting only) +* sol2 (scripting only) * lodepng (scripting only) * nlohmann/json (scripting only) @@ -56,10 +56,21 @@ Note that during installation you only need **one** version of Qt and also only **one** compiler version. If download size is noticably large (more than a few hundred MB), you're probably downloading way too much. +### LuaJIT ### +_(Not necessary if disabling `NOGGIT_WITH_SCRIPTING`)_ +* Clone luajit to `` using git: `git clone https://luajit.org/git/luajit.git` +* Open up the visual studio command prompt (cmd / powershell does **NOT** work) + * The program is usually called something like "x64 Native Tools Command Prompt for VS 20xx" + * Usually enough to search for "x64 native" in the windows start menu + * It will open a command prompt that looks like the normal windows command prompt, but has a bunch of necessary visual studio variables set. +* Navigate to the `/src` (with the visual studio command prompt) +* Run "msvcbuild.bat" (with the visual studio command prompt) +* If successful, should give a message similar to `=== Successfully built LuaJIT for Windows/x64 ===` + ### Noggit ### * open CMake GUI -* set `CMAKE_PREFIX_PATH` (path) to `";"`, - e.g. `"C:/Qt/5.6/msvc2015;D:/StormLib/install"` +* set `CMAKE_PREFIX_PATH` (path) to `""`, + e.g. `"C:/Qt/5.6/msvc2015"` * set `BOOST_ROOT` (path) to ``, e.g. `"C:/local/boost_1_60_0"` * (**unlikely to be required:**) move the libraries of Boost from where they are into `BOOST_ROOT/lib` so that CMake finds them automatically or @@ -67,11 +78,13 @@ also only **one** compiler version. If download size is noticably large is **highly** unlikely to be required. * set `CMAKE_INSTALL_PREFIX` (path) to an empty destination, e.g. `"C:/Users/blurb/Documents/noggitinstall` +* set `LUA_INCLUDE_DIR` to `/src` +* set `LUA_LIBRARY` to `/src/lua51.lib` * configure, generate * open solution with visual studio * build ALL_BUILD * build INSTALL - + To launch noggit you will need the following DLLs from Qt loadable. Install them in the system, or copy them from `C:/Qt/X.X/msvcXXXX/bin` into the directory containing noggit.exe, i.e. `CMAKE_INSTALL_PREFIX` configured. @@ -79,6 +92,8 @@ directory containing noggit.exe, i.e. `CMAKE_INSTALL_PREFIX` configured. * release: Qt5Core, Qt5OpenGL, Qt5Widgets, Qt5Gui * debug: Qt5Cored, Qt5OpenGLd, Qt5Widgetsd, Qt5Guid +* If using scripting, you will also need to copy `/src/lua51.dll` to `CMAKE_INSTALL_PREFIX` + ## Linux ## On **Ubuntu** you can install the building requirements using: From bd5b2f6cce5a9e87bf02d8472010f8bc04e4d97a Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Sun, 8 Aug 2021 11:09:50 +0000 Subject: [PATCH 244/250] scripting: missing include --- src/noggit/scripting/scripting_tool.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/noggit/scripting/scripting_tool.hpp b/src/noggit/scripting/scripting_tool.hpp index 1a123eb1a..aa86ca3ff 100644 --- a/src/noggit/scripting/scripting_tool.hpp +++ b/src/noggit/scripting/scripting_tool.hpp @@ -1,6 +1,7 @@ // This file is part of Noggit3, licensed under GNU General Public License (version 3). #pragma once +#include #include #include From 64f7a70436dbc61ec6b79705393829b0ee85f389 Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Sun, 8 Aug 2021 11:11:56 +0000 Subject: [PATCH 245/250] fix: remove unused variable --- src/noggit/texture_set.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/noggit/texture_set.cpp b/src/noggit/texture_set.cpp index ac6111052..3d6895373 100644 --- a/src/noggit/texture_set.cpp +++ b/src/noggit/texture_set.cpp @@ -407,8 +407,6 @@ bool TextureSet::paintTexture(float xbase, float zbase, float x, float z, Brush* if (dist <= radius) { - std::size_t offset = i + 64 * j; - // use double for more precision std::array alpha_values; double total = 0.; From a4a911f7adb2e35521311041a72addbeea4effcf Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Sun, 8 Aug 2021 11:14:01 +0000 Subject: [PATCH 246/250] cmake: disable -Werror=shadow check (breaks boost headers) --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c518760ab..fea33f0ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,7 +88,7 @@ add_global_compiler_flag_if_supported (/MP) add_noggit_compiler_flag_if_supported (/we4457) # function parameter add_noggit_compiler_flag_if_supported (/we4458) # class members add_noggit_compiler_flag_if_supported (/we4459) # global declaration -add_noggit_compiler_flag_if_supported (-Werror=shadow) +#add_noggit_compiler_flag_if_supported (-Werror=shadow) if( NOGGIT_ALL_WARNINGS ) MESSAGE( STATUS "Spilling out mass warnings." ) From 0a02836e89c358816354fa4199a5af70c21b3e9e Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Sun, 8 Aug 2021 12:23:01 +0000 Subject: [PATCH 247/250] fix: gcc variant ambiguity errors --- src/util/visit.ipp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/util/visit.ipp b/src/util/visit.ipp index 38f12dba0..e49967c07 100644 --- a/src/util/visit.ipp +++ b/src/util/visit.ipp @@ -23,11 +23,14 @@ namespace util template auto visit (Variant& variant, Funs... funs) { - struct only_inherit_funs : Funs... {}; + struct only_inherit_funs : Funs... { + using Funs::operator()...; + }; using Ret = typename detail::require_all_results_same_for_variant_apply, only_inherit_funs>::type; struct lambda_visitor : boost::static_visitor, Funs... { + using Funs::operator()...; lambda_visitor (Funs... funs) : Funs (funs)... {} From c192bf5d5a6f25336a9d8abcef681db2c278d995 Mon Sep 17 00:00:00 2001 From: ihm-tswow <76849026+ihm-tswow@users.noreply.github.com> Date: Sun, 8 Aug 2021 14:56:29 +0200 Subject: [PATCH 248/250] scripting: documentation: update iteration examples --- scripts/docs/README.md | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/scripts/docs/README.md b/scripts/docs/README.md index 497436b52..091073e27 100644 --- a/scripts/docs/README.md +++ b/scripts/docs/README.md @@ -69,50 +69,27 @@ function selection_brush:on_left_click(evt) local sel = select_origin(evt:pos(), evt:outer_radius()) -- iterate selected chunks - local chunks = sel:chunks() - while(chunks:next()) do - local chunk = chunks:get() + for i,chunk in pairs(sel:chunks()) do -- do something to the chunk here end -- iterate selected vertices - local verts = sel:verts() - while(verts:next()) do - local vert = verts:get() + for i,vert in pairs(sel:verts()) do -- do something to the vertex here end -- iterate selected texture units - local tex_units = sel:tex() - while(tex_units:next()) do - local tex = tex_units:get() + for i,tex in pairs(sel:tex()) do -- do something to the texture unit here end - -- iterate selected models - local models = sel:models() - while(models:next()) do - local model = models:get() + -- iterate selected models (m2 and wmo) + for i,model in pairs(sel:model()) do -- do something to the model here end end ``` -### Small warning about iterators - -It is important that we actually create iterators **before** we start looping them, since each time we call the function we create a new iterator. The following code illustrates the difference: - -```lua --- INCORRECT: creates a new reset iterator every loop, so this code will never finish -while(sel:models():next()) do -end - --- CORRECT: create the iterator first, then iterate it -local models = sel:models() -while(models:next()) do -end -``` - ## Noggit API To see all the predefined functions you can call from scripts, see the [API documentation](api/modules.md). @@ -121,4 +98,4 @@ To see all the predefined functions you can call from scripts, see the [API docu The functions [image::save](api/classes/image####save) and [write_file](api/modules.md####write_file) can be used to write to the users file systems. By default, when a script tries to call either of these functions the user will be prompted with a popup to confirm they wish to write to that file. If they answer no, an exception will be thrown and stop execution of the script, and if they answer yes the file will be written, and can be written to again without asking until Noggit is restarted. -This default behavior can be disabled in the Noggit settings under `Allow scripts to write to any file`. \ No newline at end of file +This default behavior can be disabled in the Noggit settings under `Allow scripts to write to any file`. From 330c3236e6f8636b9042e363b2461c0d4efaefcf Mon Sep 17 00:00:00 2001 From: ihm-tswow Date: Mon, 9 Aug 2021 12:09:34 +0200 Subject: [PATCH 249/250] scripting: fix: change default arguments to sol2 overloads - fix scripting on linux (tested on ubuntu 20.1) - (add overload definitions to the headers to get as close to the real default arguments as possible) --- src/noggit/scripting/script_brush.cpp | 22 ++++++++-- src/noggit/scripting/script_brush.hpp | 58 +++++++++++++++++++++------ src/noggit/scripting/script_chunk.cpp | 5 ++- src/noggit/scripting/script_chunk.hpp | 4 +- src/noggit/scripting/script_image.cpp | 5 ++- src/noggit/scripting/script_image.hpp | 6 ++- 6 files changed, 80 insertions(+), 20 deletions(-) diff --git a/src/noggit/scripting/script_brush.cpp b/src/noggit/scripting/script_brush.cpp index 07c58ef28..2f004f415 100644 --- a/src/noggit/scripting/script_brush.cpp +++ b/src/noggit/scripting/script_brush.cpp @@ -156,10 +156,24 @@ namespace noggit { , sol::meta_function::new_index, &script_brush::set , sol::meta_function::index, &script_brush::get ,"get_name",&script_brush::get_name - ,"add_int_tag",&script_brush::add_int_tag - ,"add_bool_tag",&script_brush::add_bool_tag - ,"add_real_tag",&script_brush::add_real_tag - ,"add_string_tag",&script_brush::add_string_tag + ,"add_int_tag",sol::overload( + &script_brush::add_int_tag + , &script_brush::add_int_tag_1 + , &script_brush::add_int_tag_2 + ) + ,"add_bool_tag",sol::overload( + &script_brush::add_bool_tag + , &script_brush::add_bool_tag_1 + ) + ,"add_real_tag", sol::overload( + &script_brush::add_real_tag + , &script_brush::add_real_tag_1 + , &script_brush::add_real_tag_2 + ) + ,"add_string_tag", sol::overload( + &script_brush::add_string_tag + , &script_brush::add_string_tag_1 + ) ,"add_string_list_tag",&script_brush::add_string_list_tag ,"add_null_tag",&script_brush::add_null_tag ,"add_description",&script_brush::add_description diff --git a/src/noggit/scripting/script_brush.hpp b/src/noggit/scripting/script_brush.hpp index 5d7175251..7af60eae3 100644 --- a/src/noggit/scripting/script_brush.hpp +++ b/src/noggit/scripting/script_brush.hpp @@ -59,30 +59,64 @@ namespace noggit { std::string const& item , int low , int high - , int def - , bool has_slider = false + , int def /* = low */ + , bool has_slider /* = false */ ); + std::shared_ptr add_int_tag_1( + std::string const& item + , int low + , int high + , int def + ) { return add_int_tag(item,low,high,def, false);} + std::shared_ptr add_int_tag_2( + std::string const& item + , int low + , int high + ) { return add_int_tag_1(item,low,high,low);} - std::shared_ptr add_real_tag(std::string const& item - , double low - , double high - , double def - , int zeros = 5 - , bool has_slider = false - ); + std::shared_ptr add_real_tag( + std::string const& item + , double low + , double high + , double def + , int zeros /* = 5 */ + , bool has_slider /* = false */ + ); + std::shared_ptr add_real_tag_1( + std::string const& item + , double low + , double high + , double def + , int zeros + ) + { return add_real_tag(item,low,high,def,zeros, false); } + std::shared_ptr add_real_tag_2( + std::string const& item + , double low + , double high + , double def + ) + { return add_real_tag_1(item,low,high,def,5); } std::shared_ptr add_string_tag( std::string const& item - , std::string const& def); - + , std::string const& def /* = "" */ + ); + std::shared_ptr add_string_tag_1( + std::string const& item) + { return add_string_tag(item,"");} + std::shared_ptr add_string_list_tag( std::string const& item , sol::variadic_args va); std::shared_ptr add_bool_tag( std::string const& item - , bool def + , bool def /* = false */ ); + std::shared_ptr add_bool_tag_1( + std::string const& item + ) { return add_bool_tag(item,false); } void add_null_tag(); void add_description(std::string const& text); diff --git a/src/noggit/scripting/script_chunk.cpp b/src/noggit/scripting/script_chunk.cpp index b3f66efb9..8e1fd8ecd 100644 --- a/src/noggit/scripting/script_chunk.cpp +++ b/src/noggit/scripting/script_chunk.cpp @@ -148,7 +148,10 @@ namespace noggit , "get_texture", &chunk::get_texture , "get_effect", &chunk::get_effect , "set_effect", &chunk::set_effect - , "add_texture", &chunk::add_texture + , "add_texture", sol::overload( + &chunk::add_texture + , &chunk::add_texture_1 + ) , "clear_textures", &chunk::clear_textures , "set_hole", &chunk::set_hole , "clear_colors", &chunk::clear_colors diff --git a/src/noggit/scripting/script_chunk.hpp b/src/noggit/scripting/script_chunk.hpp index 565ebb479..c7a332e4f 100644 --- a/src/noggit/scripting/script_chunk.hpp +++ b/src/noggit/scripting/script_chunk.hpp @@ -19,7 +19,9 @@ namespace noggit std::string get_texture(int index); int get_effect(int index); void set_effect(int index, int effectID); - int add_texture(std::string const& texture, int effectID = -2); + int add_texture(std::string const& texture, int effectID /* = -2*/); + int add_texture_1(std::string const& texture) + { return add_texture(texture,-2);} int get_texture_count(); void clear_textures(); void set_hole(bool hole); diff --git a/src/noggit/scripting/script_image.cpp b/src/noggit/scripting/script_image.cpp index 845b6974a..7f41e097c 100644 --- a/src/noggit/scripting/script_image.cpp +++ b/src/noggit/scripting/script_image.cpp @@ -165,7 +165,10 @@ namespace noggit , "get_pixel", &image::get_pixel , "gradient_scale", &image::gradient_scale , "set_pixel", &image::set_pixel - , "set_pixel_floats", &image::set_pixel_floats + , "set_pixel_floats", sol::overload( + &image::set_pixel_floats + , &image::set_pixel_floats_1 + ) , "save", &image::save , "width", &image::width , "height", &image::height diff --git a/src/noggit/scripting/script_image.hpp b/src/noggit/scripting/script_image.hpp index 2c84a85e8..c1aa568d8 100644 --- a/src/noggit/scripting/script_image.hpp +++ b/src/noggit/scripting/script_image.hpp @@ -23,7 +23,11 @@ namespace noggit unsigned get_pixel(int x, int y) const; float gradient_scale(float rel) const; void set_pixel(int x, int y, unsigned value); - void set_pixel_floats(int x, int y, float r, float g, float b, float a = 1.0); + void set_pixel_floats(int x, int y, float r, float g, float b, float a /*= 1.0*/); + void set_pixel_floats_1(int x, int y, float r, float g, float b) + { + set_pixel_floats(x,y,r,g,b,1.0); + } void save(std::string const& filename); int width() const; int height() const; From 8386c9df12e01692e42c2f7bdbb05977c79080e9 Mon Sep 17 00:00:00 2001 From: ihm-tswow <76849026+ihm-tswow@users.noreply.github.com> Date: Mon, 9 Aug 2021 14:25:14 +0200 Subject: [PATCH 250/250] documentation: add new linux build instructions - building with luajit and the new dependencies --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 884eeca62..0b7bb381a 100644 --- a/README.md +++ b/README.md @@ -95,27 +95,61 @@ directory containing noggit.exe, i.e. `CMAKE_INSTALL_PREFIX` configured. * If using scripting, you will also need to copy `/src/lua51.dll` to `CMAKE_INSTALL_PREFIX` ## Linux ## + +These instructions assume a working directory ``, for example `/home/myuser/` + +### Dependencies ### + On **Ubuntu** you can install the building requirements using: ```bash -sudo apt install freeglut3-dev libboost-all-dev qt5-default libstorm-dev +sudo apt install freeglut3-dev libboost-all-dev qt5-default libsdl2-dev libbz2-dev +``` + +### LuaJIT ### + +From ``, build luajit using: +```bash +git clone https://luajit.org/git/luajit.git +cd luajit +make +cd .. +``` + +`/luajit` should now exist. + +### Build Noggit ### + +From ``, clone Noggit3 (change repo as needed): +```bash +git clone https://github.com/wowdev/noggit3 ``` -Compile and build using: +`/noggit3` should now exist. + +From ``, compile and build using the following commands. +Note that `` should be written as the **full** path in the commands below. +For example: `cmake -DLUA_LIBRARIES=/home/myuser/luajit/src/libluajit.so -DLUA_INCLUDE_DIR=/home/myuser/luajit/src ../noggit3` ```bash mkdir build cd build -cmake .. +cmake -DLUA_LIBRARIES=/luajit/src/libluajit.so -DLUA_INCLUDE_DIR=/luajit/src ../noggit3 make -j $(nproc) ``` Instead of `make -j $(nproc)` you may want to pick a bigger number than `$(nproc)`, e.g. the number of `CPU cores * 1.5`. -If the build pass correctly without errors, you can go into build/bin/ -and run noggit. Note that `make install` will probably work but is not -tested, and nobody has built distributable packages in years. +From `/build`, if the build pass correctly without errors, you can finish the installation using: +```bash +cp -r ../noggit3/scripts ./bin/scripts +cp ../luajit/src/libluajit.a ./bin/libluajit.a +``` + +You can now go into `/build/bin` and run noggit. + +Note that `make install` will probably not work. # Building the Scripting documentation # To generate the scripting API documentation, install any new version of Node.js and run: