1
0
mirror of https://github.com/Uberi/Minetest-WorldEdit.git synced 2025-06-29 22:50:46 +02:00

77 Commits
1.0 ... master

Author SHA1 Message Date
f10da8c9f6 Version MFF 2018-09-05 00:44:21 +02:00
bb8456b711 Cleanup and fixup
Non-stylistic changes:
  * Add LuaDoc/LDoc support.
  * Fix `clear_objects` area size calculation.
  * Fix `clear_objects` removing player objects.
  * Fix shadowing of marker entity name with player name.
  * Make visualization functions use `swap_node`.
  * Make hidden nodes unwalkable.
  * Prevent `hide` from hiding air.
  * Make deprecated functions log to deprecated stream when called.
  * Fixed `replaceinverse` not using normalized node names.
  * Added .gitignore.
  * Bump version to 1.1.

Stylistic changes:
  * Change `x = function` to `function x`.
  * Change comment format.
  * Make  missing VoxelManip error less obnoxious.
  * Move `sort_pos` into `common.lua`, which is a required module.
  * Remove local copies of `minetest`.
  * Remove `worldedit = worldedit or {}` from modules.
  * Replace replaceinverse with an inverse argument to `replace`.
  * Added `error()`s on on invalid axes.
  * Change `wip` to `TODO`.
  * Rename `clearobjects` to `clear_objects`.
  * Remove `hollow_{sphere,dome,cylinder}` and replace them with a hollow parameter to each function.
  * Add helpers to reduce code duplication.
  * Renamed `Chat Commands.md` to `ChatCommands.md`.
2015-02-01 15:56:16 -05:00
1f277147ca Fix Lua function 2014-12-30 00:11:49 -05:00
04fdf92aca Fix crash when loading schematic in a LuaJIT build in recent Minetest versions (thanks LazyJ & VanessaE!). 2014-12-29 18:26:37 -05:00
7f580611f5 Fix some warnings. 2014-12-29 18:16:36 -05:00
4799068551 Merge pull request #68 from KodexKy/stack2_fix
Fix stack2 param
2014-12-29 17:51:51 -05:00
5b03c83f6b Fix stack2 param
Fix stack2 amount count
Fix stack2 sign recognition for y & z
2014-12-16 02:14:22 -04:30
e4d007199f Fix bug with loading old .we file formats, thanks to @Sokomine 2014-12-13 21:13:11 +01:00
fb49cc4bcc Revert "Use "we-" prefix for commands"
This is currently being discussed, see #62.
This reverts commit 1024eace6f.
2014-10-25 07:53:20 +02:00
eb88bf6ade Merge pull request #61 from ShadowNinja/cleanup-serialization
Clean up serialization
2014-10-24 22:03:31 -04:00
02c36c16d5 Merge pull request #62 from ShadowNinja/command-prefix
Use "we-" prefix for commands
2014-10-24 17:04:00 -04:00
796aa3870d Clean up serialization
* Adds a header to serialized data (to make version checking sane).
  * Removes the duplicate deserialization for `worldedit.deserialize` and `worldedit.allocate`.
  * Optimizes `worldedit.deserialize` by only deserializing the data once.
  * Makes some fields optional.
  * Cleans up the comments and a little of the code style.
2014-10-24 16:45:10 -04:00
1024eace6f Use "we-" prefix for commands 2014-10-24 16:09:51 -04:00
70c24c9501 Fix #57 (thanks Zeno-!). 2014-08-15 19:23:23 -04:00
18efe50e1e Fix typo (thanks donat-b!). 2014-08-05 18:34:43 -04:00
44c9576f8a Merge pull request #53 from ShadowNinja/fix-lua-runtime-error
Fix runtime error checking with lua* commands
2014-07-24 11:19:13 -04:00
e383e8ce00 Fix runtime error checking with lua* commands 2014-07-23 18:21:21 -04:00
82ef580fae Add sort_pos to make code module independent. 2014-07-22 18:42:15 -04:00
38fbc0a046 Fix //mix and improve normalize_nodename. 2014-07-18 16:09:57 -04:00
4b178bf709 Rename the randomized //set to //mix, style update, document changes. 2014-07-18 15:51:09 -04:00
d8aa7e72a7 Oops, fix //set. 2014-07-14 00:11:33 -04:00
58d7a7134e Coding style updates. 2014-07-12 16:35:59 -04:00
420655bd94 Merge pull request #51 from cyisfor/master
I think that's the optimization you mentioned?
2014-07-12 16:31:27 -04:00
ea84eee0e5 Fix typo in README 2014-07-12 07:42:38 +02:00
9ec1a799d8 Fix worldedit_gui not working in Minetest 0.4.10. 2014-07-10 20:17:12 -04:00
Cy
f5b67c5bc2 CPS-ifying stack
Continuation Passing Style lets me use minetest.after, so the server
gets a chance to not hang in between every stack iteration. Could even
set minetest.after(1000,nextone) if you want to see it extend once every
second.
2014-07-09 23:50:41 -07:00
Cy
6084db9335 Slight optimization to //set
Just noticed I box the one type version in a list, to avoid testing
whether it's the one type version, but have to test for that to decide
whether to box it or not. Should shave like a whole 3ms from each //set
command.
2014-07-09 23:34:27 -07:00
b70fd16da4 Oops, fix //set. 2014-07-07 14:06:33 -04:00
9616c7d944 Update stack2 API documentation. 2014-07-06 19:59:56 -04:00
175ac211ca Documentation for //stack2, code style fixes, add author section to README. 2014-07-06 19:42:02 -04:00
b32aadd7fa Merge pull request #47 from cyisfor/master
Stacking in any direction
2014-07-06 19:11:23 -04:00
Cy
c22b556511 Improved stacking
This stack / copy uses a direction vector, so it's not limited to only
along the X/Y/Z axis, and can go diagonally. This enables things like
building staircases.
2014-06-30 16:14:14 -07:00
Cy
174416b010 Randomized set
Can /set node node2 node3 and it will randomly choose between those
three.
2014-06-30 16:13:44 -07:00
2c4a791805 New inventory icon for GUI (thanks VanessaE!). 2014-05-16 16:59:56 -04:00
4660927ca0 Fix safe region functionality (thanks ChaosWormz). 2014-04-19 23:33:20 -04:00
2784a25561 Fix region checking (thanks PenguinDad). 2014-03-15 14:28:47 -04:00
b29523db4d Implement full size checking for every possible command. 2014-03-12 16:11:00 -04:00
0dddffa544 Add warning for really huge regions. 2014-03-10 17:58:15 -04:00
b8b8db42bf Merge pull request #36 from spillz/master
Fix incorrect fixedpos var names in the GUI form
2014-02-24 09:46:26 -05:00
18417ff4fd Fix incorrect fixedpos var names in the GUI form
worldedit_gui_fixed_* -> worldedit_gui_fixedpos_*

Makes setting a fixed position in the GUI work correctly
2014-02-23 20:31:09 -05:00
84f0e23d6c Add worldedit_shortcommands, which adds aliases to commonly used chat commands that allow users to type in long command names faster. Idea is courtesy of @electricface.
For example, `//move ? 5` can now be written as `//m ? 5`.
2014-02-23 14:16:22 -05:00
be913e6d8b Fix chat commands display. 2014-01-28 14:14:36 -05:00
82f39460cc Merge pull request #33 from electricface/master
//fixedpos No commas between the numbers
2014-01-27 22:41:26 -08:00
6b50025ccc //fixedpos No commas between the numbers 2014-01-28 12:23:47 +08:00
829f9eef73 Merge pull request #32 from ShadowNinja/admin-only-lua
Make /lua and /luatransform administrator-only
2014-01-21 18:42:16 -08:00
f8d3614a32 Make /lua and /luatransform administrator-only 2014-01-21 11:14:24 -05:00
86d35e97bf Fix for player signing off before WorldEdit GUI has finished loading. 2014-01-19 00:12:34 -05:00
277b57a82a Merge pull request #28 from cheapie/master
Added compatibility with my version of creative_inventory.
2014-01-02 21:58:30 -08:00
d2635544c6 Added compatibility with my version of creative_inventory. 2014-01-02 22:17:25 -06:00
2ac049c823 Oops, broke the back button with that last commit. 2014-01-02 18:17:28 -05:00
d73b93d8dd Fix the creative inventory being borked if neither Unified Inventory or Inventory++ is installed (thanks cheapie!). 2014-01-02 18:03:52 -05:00
8c0b61194c The WorldEdit GUI now has no hard dependencies. 2013-12-30 14:27:47 -05:00
2e41f0076d Mark the region with an entity cube. 2013-12-24 14:07:42 -05:00
2fcea5a3c0 Add a screenshot to the README. 2013-12-22 13:51:50 -05:00
df6ab06e74 Update worldedit_gui to work with latest unified_inventory (thanks LazyJ!). 2013-12-22 13:48:56 -05:00
4e78ced693 Merge pull request #26 from kaeza/wut
Fix crashes in GUI button.
2013-12-22 09:52:17 -08:00
ad299cacc5 Fix crashes in GUI button. 2013-12-21 03:06:32 -02:00
06cb2934b1 Full unified_inventory and inventory_plus support! 2013-12-20 19:50:13 -05:00
c48cf3c051 All commands in WorldEdit are now accessible through WorldEdit GUI, except the MT Schematics stuff. 2013-12-20 18:42:47 -05:00
674d6473e4 Improve node inspector to show player axis, replace //scale with //stretch, which supports per-axis stretching (full backwards compatibility retained), and secure schematic file loading functions. 2013-12-20 18:41:13 -05:00
3767ea84d2 Include WorldEdit GUI in the documentation, improve privilege handling, simplify main dialog. 2013-12-18 15:56:50 -05:00
36424e3dec Improve worldedit.spiral and the WorldEdit GUI, 2013-12-16 18:55:56 -05:00
3cffae4948 Implement copy/move in the WorldEdit GUI. 2013-12-15 16:56:37 -05:00
a4881225a4 Merge fixedpos and position dialogs in worldedit_gui, add cylinder dialog. 2013-12-15 16:33:39 -05:00
0797c47afb Fix worldedit.hollow_cylinder. 2013-12-15 16:33:31 -05:00
89d4548595 More improvements to worldedit_gui 2013-12-12 16:28:00 -05:00
6c03ab9c4a Refine GUI. 2013-12-12 15:34:05 -05:00
dbc2348014 Better installation instructions. 2013-12-11 15:58:58 -05:00
3fa9dfd19c Improve worldedit_gui. 2013-12-10 17:07:10 -05:00
d4b39cba9b WorldEdit GUI is now live! Currently under construction. 2013-12-10 01:58:53 -05:00
ac7430e02f Version information is now available via the API. 2013-12-10 01:47:32 -05:00
b3ba6f2433 Revert "Change compatibility notices." because we have 0.4.8 now
This reverts commit d5ec1bcefc.
2013-11-24 14:18:04 +01:00
d5ec1bcefc Change compatibility notices. 2013-10-01 16:57:55 -04:00
203c3da2af Add //about. 2013-08-28 20:28:49 -04:00
272541c9da Changes to support the latest schematic probability specification. 2013-08-16 16:57:35 -04:00
9ab52df56a Add worldedit_limited. 2013-08-16 16:56:38 -04:00
4c43bf3600 Add initial code for worldedit_infinity. 2013-08-06 16:29:00 -04:00
38 changed files with 4264 additions and 1943 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*~

View File

@ -1,332 +0,0 @@
Chat Commands
-------------
For more information, see the [README](README.md).
### //inspect
Enable or disable node inspection.
//inspect on
//inspect off
//inspect 1
//inspect 0
//inspect true
//inspect false
//inspect yes
//inspect no
//inspect enable
//inspect disable
### //reset
Reset the region so that it is empty.
//reset
### //mark
Show markers at the region positions.
//mark
### //unmark
Hide markers if currently shown.
//unmark
### //pos1
Set WorldEdit region position 1 to the player's location.
//pos1
### //pos2
Set WorldEdit region position 2 to the player's location.
//pos2
### //p set/set1/set2/get
Set WorldEdit region, WorldEdit position 1, or WorldEdit position 2 by punching nodes, or display the current WorldEdit region.
//p set
//p set1
//p set2
//p get
### //fixedpos set1 x y z
Set a WorldEdit region position to the position at (<x>, <y>, <z>).
//fixedpos set1 0, 0, 0
//fixedpos set1 -30, 5, 28
//fixedpos set2 1004, -200, 432
### //volume
Display the volume of the current WorldEdit region.
//volume
### //set <node>
Set the current WorldEdit region to <node>.
//set air
//set cactus
//set Bronze Block
//set mesecons:wire_00000000_off
### //replace <search node> <replace node>
Replace all instances of <search node> with <replace node> in the current WorldEdit region.
//replace Cobblestone air
//replace lightstone_blue glass
//replace dirt Bronze Block
//replace mesecons:wire_00000000_off flowers:flower_tulip
### //replaceinverse <search node> <replace node>
Replace all nodes other than <search node> with <replace node> in the current WorldEdit region.
//replaceinverse Cobblestone air
//replaceinverse flowers:flower_waterlily glass
//replaceinverse dirt Bronze Block
//replaceinverse mesecons:wire_00000000_off flowers:flower_tulip
### //hollowsphere <radius> <node>
Add hollow sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>.
//hollowsphere 5 Diamond Block
//hollowsphere 12 glass
//hollowsphere 17 mesecons:wire_00000000_off
### //sphere <radius> <node>
Add sphere centered at WorldEdit position 1 with radius <radius>, composed of <node>.
//sphere 5 Diamond Block
//sphere 12 glass
//sphere 17 mesecons:wire_00000000_off
### //hollowdome <radius> <node>
Add hollow dome centered at WorldEdit position 1 with radius <radius>, composed of <node>.
//hollowdome 5 Diamond Block
//hollowdome -12 glass
//hollowdome 17 mesecons:wire_00000000_off
### //dome <radius> <node>
Add dome centered at WorldEdit position 1 with radius <radius>, composed of <node>.
//dome 5 Diamond Block
//dome -12 glass
//dome 17 mesecons:wire_00000000_off
### //hollowcylinder x/y/z/? <length> <radius> <node>
Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>.
//hollowcylinder x +5 8 Bronze Block
//hollowcylinder y 28 10 glass
//hollowcylinder z -12 3 mesecons:wire_00000000_off
//hollowcylinder ? 2 4 default:stone
### //cylinder x/y/z/? <length> <radius> <node>
Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length <length> and radius <radius>, composed of <node>.
//cylinder x +5 8 Bronze Block
//cylinder y 28 10 glass
//cylinder z -12 3 mesecons:wire_00000000_off
//cylinder ? 2 4 default:stone
### //pyramid x/y/z? <height> <node>
Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height <height>, composed of <node>.
//pyramid x 8 Diamond Block
//pyramid y -5 glass
//pyramid z 2 mesecons:wire_00000000_off
//pyramid ? 12 mesecons:wire_00000000_off
### //spiral <length> <height> <spacer> <node>
Add spiral centered at WorldEdit position 1 with side length <length>, height <height>, space between walls <spacer>, composed of <node>.
//spiral 20 5 3 Diamond Block
//spiral 5 2 1 glass
//spiral 7 1 5 mesecons:wire_00000000_off
### //copy x/y/z/? <amount>
Copy the current WorldEdit region along the x/y/z/? axis by <amount> nodes.
//copy x 15
//copy y -7
//copy z +4
//copy ? 8
### //move x/y/z/? <amount>
Move the current WorldEdit positions and region along the x/y/z/? axis by <amount> nodes.
//move x 15
//move y -7
//move z +4
//move ? -1
### //stack x/y/z/? <count>
Stack the current WorldEdit region along the x/y/z/? axis <count> times.
//stack x 3
//stack y -1
//stack z +5
//stack ? 12
### //scale <factor>
Scale the current WorldEdit positions and region by a factor of positive integer <factor> with position 1 as the origin.
//scale 2
//scale 1
//scale 10
### //transpose x/y/z/? x/y/z/?
Transpose the current WorldEdit positions and region along the x/y/z/? and x/y/z/? axes.
//transpose x y
//transpose x z
//transpose y z
//transpose ? y
### //flip x/y/z/?
Flip the current WorldEdit region along the x/y/z/? axis.
//flip x
//flip y
//flip z
//flip ?
### //rotate x/y/z/? <angle>
Rotate the current WorldEdit positions and region along the x/y/z/? axis by angle <angle> (90 degree increment).
//rotate x 90
//rotate y 180
//rotate z 270
//rotate ? -90
### //orient <angle>
Rotate oriented nodes in the current WorldEdit region around the Y axis by angle <angle> (90 degree increment)
//orient 90
//orient 180
//orient 270
//orient -90
### //fixlight
Fixes the lighting in the current WorldEdit region.
//fixlight
### //hide
Hide all nodes in the current WorldEdit region non-destructively.
//hide
### //suppress <node>
Suppress all <node> in the current WorldEdit region non-destructively.
//suppress Diamond Block
//suppress glass
//suppress mesecons:wire_00000000_off
### //highlight <node>
Highlight <node> in the current WorldEdit region by hiding everything else non-destructively.
//highlight Diamond Block
//highlight glass
//highlight mesecons:wire_00000000_off
### //restore
Restores nodes hidden with WorldEdit in the current WorldEdit region.
//restore
### //save <file>
Save the current WorldEdit region to "(world folder)/schems/<file>.we".
//save some random filename
//save huge_base
### //allocate <file>
Set the region defined by nodes from "(world folder)/schems/<file>.we" as the current WorldEdit region.
//allocate some random filename
//allocate huge_base
### //load <file>
Load nodes from "(world folder)/schems/<file>.we" with position 1 of the current WorldEdit region as the origin.
//load some random filename
//load huge_base
### //lua <code>
Executes <code> as a Lua chunk in the global namespace.
//lua worldedit.pos1["singleplayer"] = {x=0, y=0, z=0}
//lua worldedit.rotate(worldedit.pos1["singleplayer"], worldedit.pos2["singleplayer"], "y", 90)
### //luatransform <code>
Executes <code> as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region.
//luatransform minetest.add_node(pos, {name="default:stone"})
//luatransform if minetest.get_node(pos).name == "air" then minetest.add_node(pos, {name="default:water_source"})
### //mtschemcreate <file>
Save the current WorldEdit region using the Minetest Schematic format to "(world folder)/schems/<file>.mts".
//mtschemcreate some random filename
//mtschemcreate huge_base
### //mtschemplace <file>
Load nodes from "(world folder)/schems/<file>.mts" with position 1 of the current WorldEdit region as the origin.
//mtschemplace some random filename
//mtschemplace huge_base
### //mtschemprob start/finish/get
After using //mtschemprob start all nodes punched will bring up a text field where a probablity can be entered.
This mode can be left with //mtschemprob finish. //mtschemprob get will display the probabilities saved for the nodes.
//mtschemprob get
### //clearobjects
Clears all objects within the WorldEdit region.
//clearobjects

435
ChatCommands.md Normal file
View File

@ -0,0 +1,435 @@
Chat Commands
-------------
For more information, see the [README](README.md).
Many commands also have shorter names that can be typed faster. For example, if we wanted to use `//move ? 5`, we could instead type `//m ? 5`. All shortened names are listed below:
| Short Name | Original Name |
|:-----------|:-------------------|
| `//i` | `//inspect` |
| `//rst` | `//reset` |
| `//mk` | `//mark` |
| `//umk` | `//unmark` |
| `//1` | `//pos1` |
| `//2` | `//pos2` |
| `//fp` | `//fixedpos` |
| `//v` | `//volume` |
| `//s` | `//set` |
| `//r` | `//replace` |
| `//ri` | `//replaceinverse` |
| `//hspr` | `//hollowsphere` |
| `//spr` | `//sphere` |
| `//hdo` | `//hollowdome` |
| `//do` | `//dome` |
| `//hcyl` | `//hollowcylinder` |
| `//cyl` | `//cylinder` |
| `//hpyr` | `//hollowpyramid` |
| `//pyr` | `//pyramid` |
### `//about`
Get information about the mod.
//about
### `//inspect on/off/1/0/true/false/yes/no/enable/disable/<blank>`
Enable or disable node inspection.
//inspect on
//inspect off
//inspect 1
//inspect 0
//inspect true
//inspect false
//inspect yes
//inspect no
//inspect enable
//inspect disable
//inspect
### `//reset`
Reset the region so that it is empty.
//reset
### `//mark`
Show markers at the region positions.
//mark
### `//unmark`
Hide markers if currently shown.
//unmark
### `//pos1`
Set WorldEdit region position 1 to the player's location.
//pos1
### `//pos2`
Set WorldEdit region position 2 to the player's location.
//pos2
### `//p set/set1/set2/get`
Set WorldEdit region, WorldEdit position 1, or WorldEdit position 2 by punching nodes, or display the current WorldEdit region.
//p set
//p set1
//p set2
//p get
### `//fixedpos set1 x y z`
Set a WorldEdit region position to the position at (`<x>`, `<y>`, `<z>`).
//fixedpos set1 0 0 0
//fixedpos set1 -30 5 28
//fixedpos set2 1004 -200 432
### `//volume`
Display the volume of the current WorldEdit region.
//volume
### `//deleteblocks`
Delete the MapBlocks (16x16x16 units) that contain the selected region. This means that mapgen will be invoked for that area. As only whole MapBlocks get removed, the deleted area is usually larger than the selected one. Also, mapgen can trigger mechanisms like mud reflow or cavegen, which affects nodes (up to 112 nodes away) outside the MapBlock, so dont use this near buildings. Note that active entities are not part of a MapBlock and do not get deleted.
//deleteblocks
### `//set <node>`
Set the current WorldEdit region to `<node>`.
//set air
//set cactus
//set Blue Lightstone
//set dirt with grass
### `//mix <node1> ...`
Fill the current WorldEdit region with a random mix of `<node1>`, `...`.
//mix air
//mix cactus stone glass sandstone
//mix Bronze
//mix default:cobble air
### `//replace <search node> <replace node>`
Replace all instances of `<search node>` with `<replace node>` in the current WorldEdit region.
//replace Cobblestone air
//replace lightstone_blue glass
//replace dirt Bronze Block
//replace mesecons:wire_00000000_off flowers:flower_tulip
### `//replaceinverse <search node> <replace node>`
Replace all nodes other than `<search node>` with `<replace node>` in the current WorldEdit region.
//replaceinverse Cobblestone air
//replaceinverse flowers:flower_waterlily glass
//replaceinverse dirt Bronze Block
//replaceinverse mesecons:wire_00000000_off flowers:flower_tulip
### `//hollowsphere <radius> <node>`
Add hollow sphere centered at WorldEdit position 1 with radius `<radius>`, composed of `<node>`.
//hollowsphere 5 Diamond Block
//hollowsphere 12 glass
//hollowsphere 17 mesecons:wire_00000000_off
### `//sphere <radius> <node>`
Add sphere centered at WorldEdit position 1 with radius `<radius>`, composed of `<node>`.
//sphere 5 Diamond Block
//sphere 12 glass
//sphere 17 mesecons:wire_00000000_off
### `//hollowdome <radius> <node>`
Add hollow dome centered at WorldEdit position 1 with radius `<radius>`, composed of `<node>`.
//hollowdome 5 Diamond Block
//hollowdome -12 glass
//hollowdome 17 mesecons:wire_00000000_off
### `//dome <radius> <node>`
Add dome centered at WorldEdit position 1 with radius `<radius>`, composed of `<node>`.
//dome 5 Diamond Block
//dome -12 glass
//dome 17 mesecons:wire_00000000_off
### `//hollowcylinder x/y/z/? <length> <radius> <node>`
Add hollow cylinder at WorldEdit position 1 along the x/y/z/? axis with length `<length>` and radius `<radius>`, composed of `<node>`.
//hollowcylinder x +5 8 Bronze Block
//hollowcylinder y 28 10 glass
//hollowcylinder z -12 3 mesecons:wire_00000000_off
//hollowcylinder ? 2 4 default:stone
### `//cylinder x/y/z/? <length> <radius> <node>`
Add cylinder at WorldEdit position 1 along the x/y/z/? axis with length `<length>` and radius `<radius>`, composed of `<node>`.
//cylinder x +5 8 Bronze Block
//cylinder y 28 10 glass
//cylinder z -12 3 mesecons:wire_00000000_off
//cylinder ? 2 4 default:stone
### `//hollowpyramid x/y/z? <height> <node>`
Add hollow pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height `<height>`, composed of `<node>`.
//hollowpyramid x 8 Diamond Block
//hollowpyramid y -5 glass
//hollowpyramid z 2 mesecons:wire_00000000_off
//hollowpyramid ? 12 mesecons:wire_00000000_off
### `//pyramid x/y/z? <height> <node>`
Add pyramid centered at WorldEdit position 1 along the x/y/z/? axis with height `<height>`, composed of `<node>`.
//pyramid x 8 Diamond Block
//pyramid y -5 glass
//pyramid z 2 mesecons:wire_00000000_off
//pyramid ? 12 mesecons:wire_00000000_off
### `//spiral <length> <height> <spacer> <node>`
Add spiral centered at WorldEdit position 1 with side length `<length>`, height `<height>`, space between walls `<spacer>`, composed of `<node>`.
//spiral 20 5 3 Diamond Block
//spiral 5 2 1 glass
//spiral 7 1 5 mesecons:wire_00000000_off
### `//copy x/y/z/? <amount>`
Copy the current WorldEdit region along the x/y/z/? axis by `<amount>` nodes.
//copy x 15
//copy y -7
//copy z +4
//copy ? 8
### `//move x/y/z/? <amount>`
Move the current WorldEdit positions and region along the x/y/z/? axis by `<amount>` nodes.
//move x 15
//move y -7
//move z +4
//move ? -1
### `//stack x/y/z/? <count>`
Stack the current WorldEdit region along the x/y/z/? axis `<count>` times.
//stack x 3
//stack y -1
//stack z +5
//stack ? 12
### `//stack2 <count> <x> <y> <z>`
Stack the current WorldEdit region `<count>` times by offset `<x>`, `<y>`, `<z>`.
//stack2 5 3 8 2
//stack2 1 -1 -1 -1
### `//scale <factor>`
Scale the current WorldEdit positions and region by a factor of positive integer `<factor>` with position 1 as the origin.
//scale 2
//scale 1
//scale 10
### `//transpose x/y/z/? x/y/z/?`
Transpose the current WorldEdit positions and region along the x/y/z/? and x/y/z/? axes.
//transpose x y
//transpose x z
//transpose y z
//transpose ? y
### `//flip x/y/z/?`
Flip the current WorldEdit region along the x/y/z/? axis.
//flip x
//flip y
//flip z
//flip ?
### `//rotate x/y/z/? <angle>`
Rotate the current WorldEdit positions and region along the x/y/z/? axis by angle `<angle>` (90 degree increment).
//rotate x 90
//rotate y 180
//rotate z 270
//rotate ? -90
### `//orient <angle>`
Rotate oriented nodes in the current WorldEdit region around the Y axis by angle `<angle>` (90 degree increment)
//orient 90
//orient 180
//orient 270
//orient -90
### `//fixlight`
Fixes the lighting in the current WorldEdit region.
//fixlight
### `//drain`
Removes any fluid node within the current WorldEdit region.
//drain
### `//hide`
Hide all nodes in the current WorldEdit region non-destructively.
//hide
### `//suppress <node>`
Suppress all <node> in the current WorldEdit region non-destructively.
//suppress Diamond Block
//suppress glass
//suppress mesecons:wire_00000000_off
### `//highlight <node>`
Highlight <node> in the current WorldEdit region by hiding everything else non-destructively.
//highlight Diamond Block
//highlight glass
//highlight mesecons:wire_00000000_off
### `//restore`
Restores nodes hidden with WorldEdit in the current WorldEdit region.
//restore
### `//save <file>`
Save the current WorldEdit region to "(world folder)/schems/`<file>`.we".
//save some random filename
//save huge_base
### `//allocate <file>`
Set the region defined by nodes from "(world folder)/schems/`<file>`.we" as the current WorldEdit region.
//allocate some random filename
//allocate huge_base
### `//load <file>`
Load nodes from "(world folder)/schems/`<file>`.we" with position 1 of the current WorldEdit region as the origin.
//load some random filename
//load huge_base
### `//lua <code>`
Executes `<code>` as a Lua chunk in the global namespace.
//lua worldedit.pos1["singleplayer"] = {x=0, y=0, z=0}
//lua worldedit.rotate(worldedit.pos1["singleplayer"], worldedit.pos2["singleplayer"], "y", 90)
### `//luatransform <code>`
Executes `<code>` as a Lua chunk in the global namespace with the variable pos available, for each node in the current WorldEdit region.
//luatransform minetest.add_node(pos, {name="default:stone"})
//luatransform if minetest.get_node(pos).name == "air" then minetest.add_node(pos, {name="default:water_source"})
### `//mtschemcreate <file>`
Save the current WorldEdit region using the Minetest Schematic format to "(world folder)/schems/`<file>`.mts".
//mtschemcreate some random filename
//mtschemcreate huge_base
### `//mtschemplace <file>`
Load nodes from "(world folder)/schems/`<file>`.mts" with position 1 of the current WorldEdit region as the origin.
//mtschemplace some random filename
//mtschemplace huge_base
### `//mtschemprob start/finish/get`
After using `//mtschemprob start` all nodes punched will bring up a text field where a probablity can be entered.
This mode can be left with `//mtschemprob finish`. `//mtschemprob get` will display the probabilities saved for the nodes.
//mtschemprob get
### `//clearobjects`
Clears all objects within the WorldEdit region.
//clearobjects
### `//shift x/y/z/?/up/down/left/right/front/back [+|-]<amount>`
Shifts the selection area by `[+|-]<amount>` without touching its contents. The shifting axis can be absolute (`x/y/z`) or
relative (`up/down/left/right/front/back`).
//shift left 5
### `//expand [+|-]x/y/z/?/up/down/left/right/front/back <amount> [reverse-amount]`
Expands the selection by `<amount>` in the selected absolute or relative axis. If specified, the selection can be expanded in the
opposite direction over the same axis by `[reverse-amount]`.
//expand right 7 5
### `//contract [+|-]x/y/z/?/up/down/left/right/front/back <amount> [reverse-amount]`
Contracts the selection by `<amount>` in the selected absolute or relative axis. If specified, the selection can be contracted in the
opposite direction over the same axis by `[reverse-amount]`.
//expand right 7 5
### `//outset [hv] <amount>`
Expands the selection in all directions by `<amount>`. If specified, the selection can be expanded horizontally in the x and z axes `[h]`
or vertically in the y axis `[v]`.
//outset v 5
### `//inset [hv] <amount>`
Contracts the selection in all directions by `<amount>`. If specified, the selection can be contracted horizontally in the x and z axes `[h]`
or vertically in the y axis `[v]`.
//outset v 5

0
LICENSE.txt Normal file → Executable file
View File

100
README.md Normal file → Executable file
View File

@ -1,4 +1,4 @@
WorldEdit v1.0 for MineTest 0.4.8+ WorldEdit v1.1 for Minetest 0.4.8+
================================== ==================================
The ultimate in-game world editing tool for [Minetest](http://minetest.net/)! Tons of functionality to help with building, fixing, and more. The ultimate in-game world editing tool for [Minetest](http://minetest.net/)! Tons of functionality to help with building, fixing, and more.
@ -6,26 +6,55 @@ For more information, see the [forum topic](https://forum.minetest.net/viewtopic
# New users should see the [tutorial](Tutorial.md). # New users should see the [tutorial](Tutorial.md).
![Screenshot](http://i.imgur.com/lwhodrv.png)
Installing
----------
If you are using Windows, consider installing this mod using [MODSTER](https://forum.minetest.net/viewtopic.php?id=6497), a super simple mod installer that will take care of everything for you. If you are using MODSTER, skip directly to step 6 in the instructions below.
There is a nice installation guide over at the [Minetest Wiki](http://wiki.minetest.com/wiki/Installing_mods). Here is a short summary:
1. Download the mod from the [official releases page](https://github.com/Uberi/Minetest-WorldEdit/releases). The download links are labelled "Source Code". If you are using Windows, you will probably want to download the ZIP version.
2. You should have a file named `SOMETHING.zip` or `SOMETHING.tar.gz`.
3. Extract this file using your archiver of choice. If you are using Windows, open the ZIP file and move the folder inside to a safe place outside of the ZIP file.
4. Make sure that you now have a folder with a file named README.md inside it. If you just have another folder inside this folder, use this nested folder instead.
5. Move this folder into the `MINETEST_FOLDER/mods` folder, where `MINETEST_FOLDER` is the folder Minetest is located in.
6. Open Minetest to a world selection screen.
7. Select a world you want to use WorldEdit in by left clicking on it once, and press the **Configure** button.
8. You should have a mod selection screen. Select the one named something like `Minetest-WorldEdit` by left clicking once and press the **Enable MP** button.
9. Press the **Save** button. You can now use WorldEdit in that world. Repeat steps 7 to 9 to enable WorldEdit for other worlds too.
If you are having trouble, try asking for help in the [IRC channel](http://webchat.freenode.net/?channels=#minetest) (faster but may not always have helpers online) or ask on the [forum topic](https://forum.minetest.net/viewtopic.php?id=572) (slower but more likely to get help).
Usage Usage
----- -----
WorldEdit works primarily through chat commands. Depending on your key bindings, you can invoke chat entry with the "t" key, and open the chat console with the "F10" key. WorldEdit works primarily through the WorldEdit GUI and chat commands. Depending on your key bindings, you can invoke chat entry with the "t" key, and open the chat console with the "F10" key.
WorldEdit has a huge potential for abuse by untrusted players. Therefore, users will not be able to use WorldEdit unless they have the "worldedit" privelege. This is available by default in single player, but in multiplayer the permission must be explicitly given by someone with the right credentials, using the follwoing chat command: `/grant <player name> worldedit`. This privelege can later be removed using the following chat command: `/revoke <player name> worldedit`. WorldEdit has a huge potential for abuse by untrusted players. Therefore, users will not be able to use WorldEdit unless they have the `worldedit` privelege. This is available by default in single player, but in multiplayer the permission must be explicitly given by someone with the right credentials, using the follwoing chat command: `/grant <player name> worldedit`. This privelege can later be removed using the following chat command: `/revoke <player name> worldedit`.
Certain functions/commands such as WorldEdit GUI's "Run Lua" function (equivalent to the `//lua` and `//luatransform` chat command) additionally require the `server` privilege. This is because it is extremely dangerous to give access to these commands to untrusted players, since they essentially are able to control the computer the server is running on. Give this privilege only to people you trust with your computer.
For in-game information about these commands, type `/help <command name>` in the chat. For example, to learn more about the `//copy` command, simply type `/help /copy` to display information relevant to copying a region. For in-game information about these commands, type `/help <command name>` in the chat. For example, to learn more about the `//copy` command, simply type `/help /copy` to display information relevant to copying a region.
Chat Commands Interface
------------- ---------
WorldEdit is accessed in-game through an interface. By default, the mod distribution includes a chat interface for this purpose. It is documented in the [Chat Commands Reference](Chat Commands.md). WorldEdit is accessed in-game in two main ways.
If visual manipulation of nodes is desired, the [WorldEdit GUI](https://forum.minetest.net/viewtopic.php?id=3112) mod provides a simple interface with buttons and text entry fields for this purpose. The GUI adds a screen to each player's inventory that gives access to various WorldEdit functions. The [tutorial](Tutorial.md) and the [Chat Commands Reference](ChatCommands.md) may be helpful in learning to use it.
The chat interface adds many chat commands that perform various WorldEdit powered tasks. It is documented in the [Chat Commands Reference](ChatCommands.md).
Compatibility Compatibility
------------- -------------
This mod supports Minetest versions 0.4.8 and newer. Older versions of WorldEdit may work with older versions of Minetest, but are not recommended. This mod supports Minetest versions 0.4.8 and newer. Older versions of WorldEdit may work with older versions of Minetest, but are not recommended or supported.
WorldEdit works quite well with other mods, and does not have any known mod conflicts. WorldEdit works quite well with other mods, and does not have any known mod conflicts.
WorldEdit GUI works with [Unified Inventory](https://forum.minetest.net/viewtopic.php?id=3933) and [Inventory++](https://forum.minetest.net/viewtopic.php?id=6204), but these are not required to use the mod.
If you use any other inventory manager mods, note that they may conflict with the WorldEdit GUI. If this is the case, it may be necessary to disable them.
WorldEdit API WorldEdit API
------------- -------------
WorldEdit exposes all significant functionality in a simple Lua interface. Adding WorldEdit to the file "depends.txt" in your mod gives you access to all of the `worldedit` functions. The API is useful for tasks such as high-performance node manipulation, alternative interfaces, and map creation. WorldEdit exposes all significant functionality in a simple Lua interface. Adding WorldEdit to the file "depends.txt" in your mod gives you access to all of the `worldedit` functions. The API is useful for tasks such as high-performance node manipulation, alternative interfaces, and map creation.
@ -40,11 +69,13 @@ This API is documented in the [WorldEdit API Reference](WorldEdit API.md).
Axes Axes
---- ----
The coordinate system is the same as that used by MineTest; Y is upwards, X is perpendicular, and Z is parallel. The coordinate system is the same as that used by Minetest; positive Y is upwards, positive X is rightwards, and positive Z is forwards, if a player is facing North (positive Z axis).
When an axis is specified in a WorldEdit command, it is specified as one of the following values: x, y, z, or ?. When an axis is specified in a WorldEdit chat command, it is specified as one of the following values: `x`, `y`, `z`, or `?`.
The value ? represents the axis the player is currently facing. If the player is facing more than one axis, the axis the player face direction is closest to will be used. In the GUI, there is a dropdown menu for this purpose. The "Look direction" option has the same effect as `?` does in chat commands.
The value `?` represents the axis the player is currently facing. If the player is facing more than one axis, the axis the player face direction is closest to will be used.
Nodes Nodes
----- -----
@ -78,27 +109,46 @@ WorldEdit supports two different types of schematics.
The first is the WorldEdit Schematic format, with the file extension ".we", and in some older versions, ".wem". There have been several previous versions of the WorldEdit Schematic format, but WorldEdit is capable of loading any past versions, and will always support them - there is no need to worry about schematics becoming obselete. The first is the WorldEdit Schematic format, with the file extension ".we", and in some older versions, ".wem". There have been several previous versions of the WorldEdit Schematic format, but WorldEdit is capable of loading any past versions, and will always support them - there is no need to worry about schematics becoming obselete.
The current version of the WorldEdit Schematic format, internally known as version 4, is essentially an array of node data tables in Lua 5.2 table syntax. Specifically: As of version 5, WorldEdit schematics include a header. The header is seperated from the content by a colon (`:`). It contains fields seperated by commas (`,`). Currently only one field is used, which contains the version in ASCII decimal.
return { The current version of the WorldEdit Schematic format is essentially an array of node data tables in Lua 5.1 table syntax preceded by a header.
{ Specifically it looks like this:
["y"] = <y-axis coordinate>,
["x"] = <x-axis coordinate>,
["name"] = <node name>,
["z"] = <z-axis coordinate>,
["meta"] = <metadata table>,
["param2"] = <param2 value>,
["param1"] = <y-axis coordinate>,
},
<...>
}
Value ordering and minor aspects of the syntax, such as trailing commas or newlines, are not guaranteed. 5:return {
{
y = <y-axis coordinate>,
x = <x-axis coordinate>,
z = <z-axis coordinate>,
name = <node name>,
param1 = <param1 value>,
param2 = <param2 value>,
meta = <metadata table>,
},
<...>
}
The ordering of the values and minor aspects of the syntax, such as trailing commas or newlines, are not guaranteed to stay the same in future versions.
The WorldEdit Schematic format is accessed via the WorldEdit API, or WorldEdit serialization chat commands such as `//serialize` and `//deserialize`. The WorldEdit Schematic format is accessed via the WorldEdit API, or WorldEdit serialization chat commands such as `//serialize` and `//deserialize`.
The second is the Minetest Schematic format (MTS). The details of this format may be found in the Minetest documentation and are out of the scope of this document. Access to this format is done via specialized MTS commands such as `//mtschemcreate` and `//mtschemplace`. The second is the Minetest Schematic format (MTS). The details of this format may be found in the Minetest documentation and are out of the scope of this document. Access to this format is done via specialized MTS commands such as `//mtschemcreate` and `//mtschemplace`.
Authors
-------
WorldEdit would not be possible without the contributions of many developers and designers. Below, they are listed alphabetically:
cheapie
cornernote
cyisfor
electricface
kaeza
khonkhortisan
sfan5
ShadowNinja
spillz
Uberi/Temperest
License License
------- -------
Copyright 2013 sfan5, Anthony Zhang (Uberi/Temperest), and Brett O'Donnell (cornernote). Copyright 2013 sfan5, Anthony Zhang (Uberi/Temperest), and Brett O'Donnell (cornernote).

71
Tutorial.md Normal file → Executable file
View File

@ -9,27 +9,50 @@ Let's start with a few assumptions:
* You have WorldEdit installed as a mod. * You have WorldEdit installed as a mod.
* If using Windows, [MODSTER](https://forum.minetest.net/viewtopic.php?pid=101463) makes installing mods totally painless. * If using Windows, [MODSTER](https://forum.minetest.net/viewtopic.php?pid=101463) makes installing mods totally painless.
* Simply download the file, extract the archive, and move it to the correct mod folder for Minetest. * Simply download the file, extract the archive, and move it to the correct mod folder for Minetest.
* See the installation instructions in [README](README.md) if you need more details.
* You are familiar with the basics of the game. * You are familiar with the basics of the game.
* How to walk, jump, and climb. * How to walk, jump, and climb.
* How to dig, place, and punch blocks. * How to dig, place, and punch blocks.
* How to type into the chat and read text from it. * One of the following:
* How to type into the chat and read text from it.
* How to open the inventory screen and press buttons on it.
Overview Overview
-------- --------
WorldEdit has a "region", which is simply a cuboid area defined by two markers, both of which the player can move around. Every player can have their own region with their own two markers. WorldEdit has a "region", which is simply a cuboid area defined by two markers, both of which the player can move around. Every player can have their own region with their own two markers.
WorldEdit chat commands can work inside the region selected, or around the first marker. WorldEdit GUI buttons and chat commands generally work inside the region selected, or around the first marker.
If you are using the chat commands, follow the steps under **Chat Commands**. If you are using the WorldEdit GUI, follow the steps under **WorldEdit GUI**.
Step 1: Selecting a region Step 1: Selecting a region
-------------------------- --------------------------
### Chat Commands
In the chat prompt, enter `//p set`. In the chat, you are prompted to punch two nodes to set the positions of the two markers. In the chat prompt, enter `//p set`. In the chat, you are prompted to punch two nodes to set the positions of the two markers.
Punch a nearby node. Be careful of breakable ones such as torches. A black cube reading "1" will appear around the node. This is the marker for WorldEdit position 1. Punch a nearby node. Be careful of breakable ones such as torches. A black cube reading "1" will appear around the node. This is the marker for WorldEdit position 1.
Walk away from the node you just punched. Now, punch another node. A black cube reading "2" will appear around the node. This is the marker for WorldEdit position 2. Walk away from the node you just punched. Now, punch another node. A black cube reading "2" will appear around the node. This is the marker for WorldEdit position 2.
### WorldEdit GUI
Open the main WorldEdit GUI from your inventory screen. The icon looks like a globe with a red dot in the center.
Press the "Get/Set Positions" button. On the new screen, press the "Set Position 1" button. The inventory screen should close.
Punch a nearby node. Be careful of breakable ones such as torches. A black cube reading "1" will appear around the node. This is the marker for WorldEdit position 1.
Walk away from the node you just punched. Open your inventory again. It should be on the same page as it was before.
Press the "Set Position 2" button. The inventory screen should close.
Now, punch another node. A black cube reading "2" will appear around the node. This is the marker for WorldEdit position 2.
Step 2: Region commands Step 2: Region commands
----------------------- -----------------------
### Chat Commands
In the chat prompt, enter `//set mese`. In the chat, you will see a message showing the number of nodes set after a small delay. In the chat prompt, enter `//set mese`. In the chat, you will see a message showing the number of nodes set after a small delay.
Look at the place between the two markers: it is now filled with MESE blocks! Look at the place between the two markers: it is now filled with MESE blocks!
@ -38,18 +61,60 @@ The `//set <node>` command fills the region with whatever node you want. It is a
Now, try a few different variations, such as `//set torch`, `//set cobble`, and `//set water`. Now, try a few different variations, such as `//set torch`, `//set cobble`, and `//set water`.
### WorldEdit GUI
Open the main WorldEdit GUI from your inventory screen.
Press the "Set Nodes" button. You should see a new screen with various options for setting nodes.
Enter "mese" in the "Name" field. Press Search if you would like to see what the node you just entered looks like.
Press the "Set Nodes" button on this screen. In the chat, you will see a message showing the number of nodes set after a small delay.
Look at the place between the two markers: it is now filled with MESE blocks!
The "Set Nodes" function fills the region with whatever node you want. It is a region-oriented command, which means it works inside the WorldEdit region only.
Now, try a few different variations on the node name, such as "torch", "cobble", and "water".
Step 3: Position commands Step 3: Position commands
------------------------- -------------------------
### Chat Commands
In the chat prompt, enter `//hollowdome 30 glass`. In the chat, you will see a message showing the number of nodes set after a small delay. In the chat prompt, enter `//hollowdome 30 glass`. In the chat, you will see a message showing the number of nodes set after a small delay.
Look around marker 1: it is now surrounded by a hollow glass dome! Look around marker 1: it is now surrounded by a hollow glass dome!
The `//hollowdome <radius> <node>` command creates a hollow dome centered around marker 1, made of any node you want. It is a position-oriented command, which means it works around marker 1 and can go outside the WorldEdit region. The `//hollowdome <radius> <node>` command creates a hollow dome centered around marker 1, made of any node you want. It is a position-oriented command, which means it works around marker 1 and can go outside the WorldEdit region.
### WorldEdit GUI
Open the main WorldEdit GUI from your inventory screen.
Press the "Sphere/Dome" button. You should see a new screen with various options for making spheres or domes.
Enter "glass" in the "Name" field. Press Search if you would like to see what the node you just entered looks like.
Enter "30" in the "Radius" field.
Press the "Hollow Dome" button on this screen. In the chat, you will see a message showing the number of nodes added after a small delay.
Look around marker 1: it is now surrounded by a hollow glass dome!
The "Hollow Dome" function creates a hollow dome centered around marker 1, made of any node you want. It is a position-oriented command, which means it works around marker 1 and can go outside the WorldEdit region.
Step 4: Other commands Step 4: Other commands
---------------------- ----------------------
There are many more commands than what is shown here. See the [Chat Commands Reference](Chat Commands.md) for a detailed list of them, along with descriptions and examples for every single one. ### Chat Commands
There are many more commands than what is shown here. See the [Chat Commands Reference](ChatCommands.md) for a detailed list of them, along with descriptions and examples for every single one.
If you're in-game and forgot how a command works, just use the `/help <command name>` command, without the first forward slash. For example, to see some information about the `//set <node>` command mentioned earlier, simply use `/help /set`. If you're in-game and forgot how a command works, just use the `/help <command name>` command, without the first forward slash. For example, to see some information about the `//set <node>` command mentioned earlier, simply use `/help /set`.
A very useful command to check out is the `//save <schematic>` command, which can save everything inside the WorldEdit region to a file, stored on the computer hosting the server (the player's computer, in single player mode). You can then later use `//load <schematic>` to load the data in a file into a world, even another world on another computer. A very useful command to check out is the `//save <schematic>` command, which can save everything inside the WorldEdit region to a file, stored on the computer hosting the server (the player's computer, in single player mode). You can then later use `//load <schematic>` to load the data in a file into a world, even another world on another computer.
### WorldEdit GUI
This only scratches the surface of what WorldEdit is capable of. Most of the functions in the WorldEdit GUI correspond to chat commands, and so the [Chat Commands Reference](ChatCommands.md) may be useful if you get stuck.
It is helpful to explore the various buttons in the interface and check out what they do. Learning the chat command interface is also useful if you use WorldEdit intensively - an experienced chat command user can usually work faster than an experienced WorldEdit GUI user.

View File

@ -6,13 +6,24 @@ If needed, individual modules such as visualization.lua can be removed without a
For more information, see the [README](README.md). For more information, see the [README](README.md).
General
-------
### value = worldedit.version
Contains the current version of WorldEdit in a table of the form `{major=MAJOR_INTEGER, minor=MINOR_INTEGER}`, where `MAJOR_INTEGER` is the major version (the number before the period) as an integer, and `MINOR_INTEGER` is the minor version (the number after the period) as an integer. This is intended for version checking purposes.
### value = worldedit.version_string
Contains the current version of WorldEdit in the form of a string `"MAJOR_INTEGER.MINOR_INTEGER"`, where `MAJOR_INTEGER` is the major version (the number before the period) as an integer, and `MINOR_INTEGER` is the minor version (the number after the period) as an integer. This is intended for display purposes.
Manipulations Manipulations
------------- -------------
Contained in manipulations.lua, this module allows several node operations to be applied over a region. Contained in manipulations.lua, this module allows several node operations to be applied over a region.
### count = worldedit.set(pos1, pos2, nodename) ### count = worldedit.set(pos1, pos2, node_name)
Sets a region defined by positions `pos1` and `pos2` to `nodename`. To clear to region, use "air" as the value of `nodename`. Sets a region defined by positions `pos1` and `pos2` to `node_name`. To clear a region, use "air" as the value of `node_name`.
Returns the number of nodes set. Returns the number of nodes set.
@ -34,6 +45,12 @@ Copies the region defined by positions `pos1` and `pos2` along the `axis` axis (
Returns the number of nodes copied. Returns the number of nodes copied.
### count = worldedit.copy2(pos1, pos2, off)
Copies the region defined by positions `pos1` and `pos2` by the offset vector `off`.
Returns the number of nodes copied.
### count = worldedit.move(pos1, pos2, axis, amount) ### count = worldedit.move(pos1, pos2, axis, amount)
Moves the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes. Moves the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes.
@ -46,11 +63,17 @@ Duplicates the region defined by positions `pos1` and `pos2` along the `axis` ax
Returns the number of nodes stacked. Returns the number of nodes stacked.
### count, newpos1, newpos2 = worldedit.scale(pos1, pos2, factor) ### count = worldedit.stack2(pos1, pos2, direction, amount)
Scales the region defined by positions `pos1` and `pos2` by an factor of positive integer `factor` with `pos1` as the origin. Duplicates the region defined by positions `pos1` and `pos2` `amount` times with offset vector `direction`.
Returns the number of nodes scaled, the new scaled position 1, and the new scaled position 2. Returns the number of nodes stacked.
### count, newpos1, newpos2 = worldedit.stretch(pos1, pos2, stretchx, stretchy, stretchz)
Stretches the region defined by positions `pos1` and `pos2` by an factor of positive integers `stretchx`, `stretchy`. and `stretchz` along the X, Y, and Z axes, respectively, with `pos1` as the origin.
Returns the number of nodes stretched, the new scaled position 1, and the new scaled position 2.
### count, newpos1, newpos2 = worldedit.transpose(pos1, pos2, axis1, axis2) ### count, newpos1, newpos2 = worldedit.transpose(pos1, pos2, axis1, axis2)
@ -92,51 +115,33 @@ Primitives
---------- ----------
Contained in primitives.lua, this module allows the creation of several geometric primitives. Contained in primitives.lua, this module allows the creation of several geometric primitives.
### count = worldedit.hollow_sphere(pos, radius, nodename) ### count = worldedit.sphere(pos, radius, node_name, hollow)
Adds a hollow sphere centered at `pos` with radius `radius`, composed of `nodename`. Adds a sphere centered at `pos` with radius `radius`, composed of `node_name`.
Returns the number of nodes added. Returns the number of nodes added.
### count = worldedit.sphere(pos, radius, nodename) ### count = worldedit.dome(pos, radius, node_name, hollow)
Adds a sphere centered at `pos` with radius `radius`, composed of `nodename`. Adds a dome centered at `pos` with radius `radius`, composed of `node_name`.
Returns the number of nodes added. Returns the number of nodes added.
### count = worldedit.hollow_dome(pos, radius, nodename) ### count = worldedit.cylinder(pos, axis, length, radius, node_name, hollow)
Adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`. Adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `node_name`.
Returns the number of nodes added. Returns the number of nodes added.
### count = worldedit.dome(pos, radius, nodename) ### count = worldedit.pyramid(pos, axis, height, node_name, hollow)
Adds a dome centered at `pos` with radius `radius`, composed of `nodename`. Adds a pyramid centered at `pos` along the `axis` axis ("x" or "y" or "z") with height `height`, composed of `node_name`.
Returns the number of nodes added. Returns the number of nodes added.
### count = worldedit.hollow_cylinder(pos, axis, length, radius, nodename) ### count = worldedit.spiral(pos, length, height, spacer, node_name)
Adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`. Adds a spiral centered at `pos` with side length `length`, height `height`, space between walls `spacer`, composed of `node_name`.
Returns the number of nodes added.
### count = worldedit.cylinder(pos, axis, length, radius, nodename)
Adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`.
Returns the number of nodes added.
### count = worldedit.pyramid(pos, axis, height, nodename)
Adds a pyramid centered at `pos` along the `axis` axis ("x" or "y" or "z") with height `height`.
Returns the number of nodes added.
### count = worldedit.spiral(pos, length, height, spacer, nodename)
Adds a spiral centered at `pos` with side length `length`, height `height`, space between walls `spacer`, composed of `nodename`.
Returns the number of nodes added. Returns the number of nodes added.
@ -156,15 +161,15 @@ Hides all nodes in a region defined by positions `pos1` and `pos2` by non-destru
Returns the number of nodes hidden. Returns the number of nodes hidden.
### count = worldedit.suppress(pos1, pos2, nodename) ### count = worldedit.suppress(pos1, pos2, node_name)
Suppresses all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes. Suppresses all instances of `node_name` in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes.
Returns the number of nodes suppressed. Returns the number of nodes suppressed.
### count = worldedit.highlight(pos1, pos2, nodename) ### count = worldedit.highlight(pos1, pos2, node_name)
Highlights all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively hiding all other nodes. Highlights all instances of `node_name` in a region defined by positions `pos1` and `pos2` by non-destructively hiding all other nodes.
Returns the number of nodes found. Returns the number of nodes found.
@ -178,29 +183,30 @@ Serialization
------------- -------------
Contained in serialization.lua, this module allows regions of nodes to be serialized and deserialized to formats suitable for use outside MineTest. Contained in serialization.lua, this module allows regions of nodes to be serialized and deserialized to formats suitable for use outside MineTest.
### version = worldedit.valueversion(value) ### version, extra_fields, content = worldedit.read_header(value)
Determines the version of serialized data `value`. Reads the header from serialized data `value`.
Returns the version as a positive integer or 0 for unknown versions. Returns the version as a positive integer (nil for unknown versions),
extra header fields (nil if not supported), and the content after the header.
### data, count = worldedit.serialize(pos1, pos2) ### data, count = worldedit.serialize(pos1, pos2)
Converts the region defined by positions `pos1` and `pos2` into a single string. Converts the region defined by positions `pos1` and `pos2` into a single string.
Returns the serialized data and the number of nodes serialized. Returns the serialized data and the number of nodes serialized, or nil.
### pos1, pos2, count = worldedit.allocate(originpos, value) ### pos1, pos2, count = worldedit.allocate(origin_pos, value)
Determines the volume the nodes represented by string `value` would occupy if deserialized at `originpos`. Determines the volume the nodes represented by string `value` would occupy if deserialized at `origin_pos`.
Returns the two corner positions and the number of nodes. Returns the two corner positions and the number of nodes, or nil.
### count = worldedit.deserialize(originpos, value) ### count = worldedit.deserialize(origin_pos, value)
Loads the nodes represented by string `value` at position `originpos`. Loads the nodes represented by string `value` at position `origin_pos`.
Returns the number of nodes deserialized. Returns the number of nodes deserialized or nil.
Code Code
---- ----

12
config.ld Executable file
View File

@ -0,0 +1,12 @@
project = "WorldEdit"
title = "WorldEdit API Documentation"
description = "Minetest mod to mass-modify nodes"
format = "markdown"
file = {"worldedit"}
topics = {
"README.md",
"Tutorial.md",
"ChatCommands.md",
"LICENSE.txt"
}

0
modpack.txt Normal file → Executable file
View File

50
worldedit/code.lua Normal file → Executable file
View File

@ -1,32 +1,35 @@
worldedit = worldedit or {} --- Lua code execution functions.
local minetest = minetest --local copy of global -- @module worldedit.code
--executes `code` as a Lua chunk in the global namespace, returning an error if the code fails or nil otherwise --- Executes `code` as a Lua chunk in the global namespace.
worldedit.lua = function(code) -- @return An error message if the code fails, or nil on success.
local operation, message = loadstring(code) function worldedit.lua(code)
if operation == nil then --code parsing failed local func, err = loadstring(code)
return message if not func then -- Syntax error
return err
end end
local status, message = pcall(operation) local good, err = pcall(func)
if status == nil then --operation failed if not good then -- Runtime error
return message return err
end end
return nil return nil
end end
--executes `code` as a Lua chunk in the global namespace with the variable pos available, for each node in a region defined by positions `pos1` and `pos2`, returning an error if the code fails or nil otherwise
worldedit.luatransform = function(pos1, pos2, code)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local factory, message = loadstring("return function(pos) " .. code .. " end") --- Executes `code` as a Lua chunk in the global namespace with the variable
if factory == nil then --code parsing failed -- pos available, for each node in a region defined by positions `pos1` and
return message -- `pos2`.
-- @return An error message if the code fails, or nil on success.
function worldedit.luatransform(pos1, pos2, code)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local factory, err = loadstring("return function(pos) " .. code .. " end")
if not factory then -- Syntax error
return err
end end
local operation = factory() local func = factory()
--make area stay loaded worldedit.keep_loaded(pos1, pos2)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do while pos.x <= pos2.x do
@ -34,9 +37,9 @@ worldedit.luatransform = function(pos1, pos2, code)
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local status, message = pcall(operation, pos) local good, err = pcall(func, pos)
if status == nil then --operation failed if not good then -- Runtime error
return message return err
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
end end
@ -46,3 +49,4 @@ worldedit.luatransform = function(pos1, pos2, code)
end end
return nil return nil
end end

114
worldedit/common.lua Executable file
View File

@ -0,0 +1,114 @@
--- Common functions [INTERNAL]. All of these functions are internal!
-- @module worldedit.common
--- Copies and modifies positions `pos1` and `pos2` so that each component of
-- `pos1` is less than or equal to the corresponding component of `pos2`.
-- Returns the new positions.
function worldedit.sort_pos(pos1, pos2)
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
if pos1.x > pos2.x then
pos2.x, pos1.x = pos1.x, pos2.x
end
if pos1.y > pos2.y then
pos2.y, pos1.y = pos1.y, pos2.y
end
if pos1.z > pos2.z then
pos2.z, pos1.z = pos1.z, pos2.z
end
return pos1, pos2
end
--- Determines the volume of the region defined by positions `pos1` and `pos2`.
-- @return The volume.
function worldedit.volume(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
return (pos2.x - pos1.x + 1) *
(pos2.y - pos1.y + 1) *
(pos2.z - pos1.z + 1)
end
--- Gets other axes given an axis.
-- @raise Axis must be x, y, or z!
function worldedit.get_axis_others(axis)
if axis == "x" then
return "y", "z"
elseif axis == "y" then
return "x", "z"
elseif axis == "z" then
return "x", "y"
else
error("Axis must be x, y, or z!")
end
end
function worldedit.keep_loaded(pos1, pos2)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
end
local mh = {}
worldedit.manip_helpers = mh
--- Generates an empty VoxelManip data table for an area.
-- @return The empty data table.
function mh.get_empty_data(area)
-- Fill emerged area with ignore so that blocks in the area that are
-- only partially modified aren't overwriten.
local data = {}
local c_ignore = minetest.get_content_id("ignore")
for i = 1, worldedit.volume(area.MinEdge, area.MaxEdge) do
data[i] = c_ignore
end
return data
end
function mh.init(pos1, pos2)
local manip = minetest.get_voxel_manip()
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
return manip, area
end
function mh.init_radius(pos, radius)
local pos1 = vector.subtract(pos, radius)
local pos2 = vector.add(pos, radius)
return mh.init(pos1, pos2)
end
function mh.init_axis_radius(base_pos, axis, radius)
return mh.init_axis_radius_length(base_pos, axis, radius, radius)
end
function mh.init_axis_radius_length(base_pos, axis, radius, length)
local other1, other2 = worldedit.get_axis_others(axis)
local pos1 = {
[axis] = base_pos[axis],
[other1] = base_pos[other1] - radius,
[other2] = base_pos[other2] - radius
}
local pos2 = {
[axis] = base_pos[axis] + length,
[other1] = base_pos[other1] + radius,
[other2] = base_pos[other2] + radius
}
return mh.init(pos1, pos2)
end
function mh.finish(manip, data)
-- Update map
manip:set_data(data)
manip:write_to_map()
manip:update_map()
end

62
worldedit/compatibility.lua Normal file → Executable file
View File

@ -1,9 +1,21 @@
worldedit = worldedit or {} --- Compatibility functions.
local minetest = minetest --local copy of global -- @module worldedit.compatibility
local function deprecated(new_func)
local info = debug.getinfo(1, "n")
local msg = "worldedit." .. info.name .. "() is deprecated."
if new_func then
msg = msg .. " Use worldedit." .. new_func .. "() instead."
end
minetest.log("deprecated", msg)
end
worldedit.allocate_old = worldedit.allocate worldedit.allocate_old = worldedit.allocate
worldedit.deserialize_old = worldedit.deserialize worldedit.deserialize_old = worldedit.deserialize
worldedit.metasave = function(pos1, pos2, filename)
function worldedit.metasave(pos1, pos2, filename)
deprecated("save")
local file, err = io.open(filename, "wb") local file, err = io.open(filename, "wb")
if err then return 0 end if err then return 0 end
local data, count = worldedit.serialize(pos1, pos2) local data, count = worldedit.serialize(pos1, pos2)
@ -11,10 +23,52 @@ worldedit.metasave = function(pos1, pos2, filename)
file:close() file:close()
return count return count
end end
worldedit.metaload = function(originpos, filename)
function worldedit.metaload(originpos, filename)
deprecated("load")
filename = minetest.get_worldpath() .. "/schems/" .. file .. ".wem" filename = minetest.get_worldpath() .. "/schems/" .. file .. ".wem"
local file, err = io.open(filename, "wb") local file, err = io.open(filename, "wb")
if err then return 0 end if err then return 0 end
local data = file:read("*a") local data = file:read("*a")
return worldedit.deserialize(originpos, data) return worldedit.deserialize(originpos, data)
end end
function worldedit.scale(pos1, pos2, factor)
deprecated("stretch")
return worldedit.stretch(pos1, pos2, factor, factor, factor)
end
function worldedit.valueversion(value)
deprecated("read_header")
local version = worldedit.read_header(value)
if not version or version > worldedit.LATEST_SERIALIZATION_VERSION then
return 0
end
return version
end
function worldedit.replaceinverse(pos1, pos2, search_node, replace_node)
deprecated("replace")
return worldedit.replace(pos1, pos2, search_node, replace_node, true)
end
function worldedit.clearobjects(...)
deprecated("clear_objects")
return worldedit.clear_objects(...)
end
function worldedit.hollow_sphere(pos, radius, node_name)
deprecated("sphere")
return worldedit.sphere(pos, radius, node_name, true)
end
function worldedit.hollow_dome(pos, radius, node_name)
deprecated("dome")
return worldedit.dome(pos, radius, node_name, true)
end
function worldedit.hollow_cylinder(pos, axis, length, radius, node_name)
deprecated("cylinder")
return worldedit.cylinder(pos, axis, length, radius, node_name, true)
end

258
worldedit/cuboid.lua Normal file
View File

@ -0,0 +1,258 @@
-- Expands or contracts the cuboid in all axes by amount (positive or negative)
worldedit.cuboid_volumetric_expand = function(name, amount)
local pos1 = worldedit.pos1[name]
local pos2 = worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
return false, "Undefined cuboid"
end
local delta1 = vector.new()
local delta2 = vector.new()
local delta_dir1
local delta_dir2
delta1 = vector.add(delta1, amount)
delta2 = vector.add(delta2, amount)
delta_dir1, delta_dir2 = worldedit.get_expansion_directions(pos1, pos2)
delta1 = vector.multiply(delta1, delta_dir1)
delta2 = vector.multiply(delta2, delta_dir2)
worldedit.pos1[name] = vector.add(pos1, delta1)
worldedit.pos2[name] = vector.add(pos2, delta2)
return true
end
-- Expands or contracts the cuboid in a single axis by amount (positive or negative)
worldedit.cuboid_linear_expand = function(name, axis, direction, amount)
local pos1 = worldedit.pos1[name]
local pos2 = worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
return false, "undefined cuboid"
end
if direction ~= 1 and direction ~= -1 then
return false, "invalid marker"
end
local marker = worldedit.marker_get_closest_to_axis(name, axis, direction)
local deltavect = vector.new()
if axis == 'x' then
deltavect.x = amount * direction
elseif axis == 'y' then
deltavect.y = amount * direction
elseif axis == 'z' then
deltavect.z = amount * direction
else
return false, "invalid axis"
end
worldedit.marker_move(name, marker, deltavect)
return true
end
-- Shifts the cuboid by '+-amount' in axis 'axis'
worldedit.cuboid_shift = function(name, axis, amount)
local pos1 = worldedit.pos1[name]
local pos2 = worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
return false, "undefined cuboid"
end
if axis == 'x' then
worldedit.pos1[name].x = pos1.x + amount
worldedit.pos2[name].x = pos2.x + amount
elseif axis == 'y' then
worldedit.pos1[name].y = pos1.y + amount
worldedit.pos2[name].y = pos2.y + amount
elseif axis == 'z' then
worldedit.pos1[name].z = pos1.z + amount
worldedit.pos2[name].z = pos2.z + amount
else
return false, "invalid axis"
end
return true
end
-- Moves the location of a single marker by adding deltavector
worldedit.marker_move = function(name, marker, deltavector)
if marker ~= 1 and marker ~= 2 then
return false
end
if marker == 1 then
local pos = worldedit.pos1[name]
worldedit.pos1[name] = vector.add(deltavector, pos)
else
local pos = worldedit.pos2[name]
worldedit.pos2[name] = vector.add(deltavector, pos)
end
return true
end
-- Updates the location ingame of the markers
worldedit.marker_update = function(name, marker)
if marker == nil then
worldedit.mark_pos1(name)
worldedit.mark_pos2(name)
elseif marker == 1 then
worldedit.mark_pos1(name)
elseif marker == 2 then
worldedit.mark_pos2(name)
else
minetest.debug(
"worldedit: Invalid execution of function update_markers")
end
end
-- Returns two vectors with the directions for volumetric expansion
worldedit.get_expansion_directions = function(mark1, mark2)
if mark1 == nil or mark2 == nil then
return
end
local dir1 = vector.new()
local dir2 = vector.new()
if mark1.x < mark2.x then
dir1.x = -1
dir2.x = 1
else
dir1.x = 1
dir2.x = -1
end
if mark1.y < mark2.y then
dir1.y = -1
dir2.y = 1
else
dir1.y = 1
dir2.y = -1
end
if mark1.z < mark2.z then
dir1.z = -1
dir2.z = 1
else
dir1.z = 1
dir2.z = -1
end
return dir1, dir2
end
-- Return the marker that is closest to the player
worldedit.marker_get_closest_to_player = function(name)
local playerpos = minetest.get_player_by_name(name):getpos()
local dist1 = vector.distance(playerpos, worldedit.pos1[name])
local dist2 = vector.distance(playerpos, worldedit.pos2[name])
if dist1 < dist2 then
return 1
else
return 2
end
end
-- Returns the closest marker to the specified axis and direction
worldedit.marker_get_closest_to_axis = function(name, axis, direction)
local pos1 = vector.new()
local pos2 = vector.new()
if direction ~= 1 and direction ~= -1 then
return nil
end
if axis == 'x' then
pos1.x = worldedit.pos1[name].x * direction
pos2.x = worldedit.pos2[name].x * direction
if pos1.x > pos2.x then
return 1
else
return 2
end
elseif axis == 'y' then
pos1.y = worldedit.pos1[name].y * direction
pos2.y = worldedit.pos2[name].y * direction
if pos1.y > pos2.y then
return 1
else
return 2
end
elseif axis == 'z' then
pos1.z = worldedit.pos1[name].z * direction
pos2.z = worldedit.pos2[name].z * direction
if pos1.z > pos2.z then
return 1
else
return 2
end
else
return nil
end
end
-- Translates up, down, left, right, front, back to their corresponding axes and
-- directions according to faced direction
worldedit.translate_direction = function(name, direction)
local axis, dir = worldedit.player_axis(name)
local resaxis, resdir
if direction == "up" then
return 'y', 1
end
if direction == "down" then
return 'y', -1
end
if direction == "front" then
if axis == "y" then
resaxis = nil
resdir = nil
else
resaxis = axis
resdir = dir
end
end
if direction == "back" then
if axis == "y" then
resaxis = nil
resdir = nil
else
resaxis = axis
resdir = -dir
end
end
if direction == "left" then
if axis == 'x' then
resaxis = 'z'
resdir = dir
elseif axis == 'z' then
resaxis = 'x'
resdir = -dir
end
end
if direction == "right" then
if axis == 'x' then
resaxis = 'z'
resdir = -dir
elseif axis == 'z' then
resaxis = 'x'
resdir = dir
end
end
return resaxis, resdir
end

50
worldedit/init.lua Normal file → Executable file
View File

@ -1,19 +1,45 @@
assert(minetest.get_voxel_manip, string.rep(">", 300) .. "HEY YOU! YES, YOU OVER THERE. THIS VERSION OF WORLDEDIT REQUIRES MINETEST 0.4.8 OR LATER! YOU HAVE AN OLD VERSION." .. string.rep("<", 300)) --- Worldedit.
-- @module worldedit
-- @release 1.1
-- @copyright 2013 sfan5, Anthony Zhang (Uberi/Temperest), and Brett O'Donnell (cornernote).
-- @license GNU Affero General Public License version 3 (AGPLv3)
-- @author sfan5
-- @author Anthony Zang (Uberi/Temperest)
-- @author Bret O'Donnel (cornernote)
-- @author ShadowNinja
worldedit = {}
worldedit.version = {1, 1, major=1, minor=1}
worldedit.version_string = table.concat(worldedit.version, ".")
if not minetest.get_voxel_manip then
local err_msg = "This version of WorldEdit requires Minetest 0.4.8 or later! You have an old version."
minetest.log("error", string.rep("#", 128))
minetest.log("error", err_msg)
minetest.log("error", string.rep("#", 128))
error(err_msg)
end
local path = minetest.get_modpath(minetest.get_current_modname()) local path = minetest.get_modpath(minetest.get_current_modname())
local loadmodule = function(path) local function load_module(path)
local file = io.open(path) local file = io.open(path, "r")
if not file then if not file then return end
return
end
file:close() file:close()
return dofile(path) return dofile(path)
end end
loadmodule(path .. "/manipulations.lua") dofile(path .. "/common.lua")
loadmodule(path .. "/primitives.lua") load_module(path .. "/manipulations.lua")
loadmodule(path .. "/visualization.lua") load_module(path .. "/primitives.lua")
loadmodule(path .. "/serialization.lua") load_module(path .. "/visualization.lua")
loadmodule(path .. "/code.lua") load_module(path .. "/serialization.lua")
loadmodule(path .. "/compatibility.lua") load_module(path .. "/code.lua")
load_module(path .. "/compatibility.lua")
load_module(path .. "/cuboid.lua")
if minetest.setting_getbool("log_mods") then
print("[WorldEdit] Loaded!")
end

View File

@ -1,191 +1,138 @@
worldedit = worldedit or {} --- Generic node manipulations.
local minetest = minetest --local copy of global -- @module worldedit.manipulations
--modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions local mh = worldedit.manip_helpers
worldedit.sort_pos = function(pos1, pos2)
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
if pos1.x > pos2.x then
pos2.x, pos1.x = pos1.x, pos2.x
end
if pos1.y > pos2.y then
pos2.y, pos1.y = pos1.y, pos2.y
end
if pos1.z > pos2.z then
pos2.z, pos1.z = pos1.z, pos2.z
end
return pos1, pos2
end
--determines the volume of the region defined by positions `pos1` and `pos2`, returning the volume
worldedit.volume = function(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
return (pos2.x - pos1.x + 1) * (pos2.y - pos1.y + 1) * (pos2.z - pos1.z + 1)
end
--sets a region defined by positions `pos1` and `pos2` to `nodename`, returning the number of nodes filled --- Sets a region to `node_names`.
worldedit.set = function(pos1, pos2, nodename) -- @param pos1
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) -- @param pos2
-- @param node_names Node name or list of node names.
-- @return The number of nodes set.
function worldedit.set(pos1, pos2, node_names)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--set up voxel manipulator local manip, area = mh.init(pos1, pos2)
local manip = minetest.get_voxel_manip() local data = mh.get_empty_data(area)
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
--fill emerged area with ignore if type(node_names) == "string" then -- Only one type of node
local nodes = {} local id = minetest.get_content_id(node_names)
local ignore = minetest.get_content_id("ignore") -- Fill area with node
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do for i in area:iterp(pos1, pos2) do
nodes[i] = ignore data[i] = id
end
else -- Several types of nodes specified
local node_ids = {}
for i, v in ipairs(node_names) do
node_ids[i] = minetest.get_content_id(v)
end
-- Fill area randomly with nodes
local id_count, rand = #node_ids, math.random
for i in area:iterp(pos1, pos2) do
data[i] = node_ids[rand(id_count)]
end
end end
--fill selected area with node mh.finish(manip, data)
local node_id = minetest.get_content_id(nodename)
for i in area:iterp(pos1, pos2) do
nodes[i] = node_id
end
--update map nodes
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return worldedit.volume(pos1, pos2) return worldedit.volume(pos1, pos2)
end end
--replaces all instances of `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced
worldedit.replace = function(pos1, pos2, searchnode, replacenode) --- Replaces all instances of `search_node` with `replace_node` in a region.
-- When `inverse` is `true`, replaces all instances that are NOT `search_node`.
-- @return The number of nodes replaced.
function worldedit.replace(pos1, pos2, search_node, replace_node, inverse)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--set up voxel manipulator local manip, area = mh.init(pos1, pos2)
local manip = minetest.get_voxel_manip() local data = manip:get_data()
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2}) local search_id = minetest.get_content_id(search_node)
local replace_id = minetest.get_content_id(replace_node)
local nodes = manip:get_data()
local searchnode_id = minetest.get_content_id(searchnode)
local replacenode_id = minetest.get_content_id(replacenode)
local count = 0 local count = 0
for i in area:iterp(pos1, pos2) do --replace searchnode with replacenode
if nodes[i] == searchnode_id then
nodes[i] = replacenode_id
count = count + 1
end
end
--update map nodes --- TODO: This could be shortened by checking `inverse` in the loop,
manip:set_data(nodes) -- but that would have a speed penalty. Is the penalty big enough
manip:write_to_map() -- to matter?
manip:update_map() if not inverse then
for i in area:iterp(pos1, pos2) do
return count if data[i] == search_id then
end data[i] = replace_id
count = count + 1
--replaces all nodes other than `searchnode` with `replacenode` in a region defined by positions `pos1` and `pos2`, returning the number of nodes replaced end
worldedit.replaceinverse = function(pos1, pos2, searchnode, replacenode) end
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) else
for i in area:iterp(pos1, pos2) do
--set up voxel manipulator if data[i] ~= search_id then
local manip = minetest.get_voxel_manip() data[i] = replace_id
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2) count = count + 1
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
local nodes = manip:get_data()
local searchnode_id = minetest.get_content_id(searchnode)
local replacenode_id = minetest.get_content_id(replacenode)
local count = 0
for i in area:iterp(pos1, pos2) do --replace anything that is not searchnode with replacenode
if nodes[i] ~= searchnode_id then
nodes[i] = replacenode_id
count = count + 1
end
end
--update map nodes
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return count
end
worldedit.copy = function(pos1, pos2, axis, amount)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
if amount == 0 then
return
end
local other1, other2
if axis == "x" then
other1, other2 = "y", "z"
elseif axis == "y" then
other1, other2 = "x", "z"
else --axis == "z"
other1, other2 = "x", "y"
end
--make area stay loaded
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
--prepare slice along axis
local extent = {
[axis] = 1,
[other1]=pos2[other1] - pos1[other1] + 1,
[other2]=pos2[other2] - pos1[other2] + 1,
}
local nodes = {}
local schematic = {size=extent, data=nodes}
local currentpos = {x=pos1.x, y=pos1.y, z=pos1.z}
local stride = {x=1, y=extent.x, z=extent.x * extent.y}
local get_node = minetest.get_node
for index1 = 1, extent[axis] do --go through each slice
--copy slice into schematic
local newindex1 = (index1 + offset[axis]) * stride[axis] + 1 --offset contributed by axis plus 1 to make it 1-indexed
for index2 = 1, extent[other1] do
local newindex2 = newindex1 + (index2 + offset[other1]) * stride[other1]
for index3 = 1, extent[other2] do
local i = newindex2 + (index3 + offset[other2]) * stride[other2]
nodes[i] = get_node(pos)
end end
end end
--copy schematic to target
currentpos[axis] = currentpos[axis] + amount
place_schematic(currentpos, schematic)
--wip: copy meta
currentpos[axis] = currentpos[axis] + 1
end end
return worldedit.volume(pos1, pos2)
mh.finish(manip, data)
return count
end end
--copies the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes, returning the number of nodes copied
worldedit.copy = function(pos1, pos2, axis, amount) --- Duplicates a region `amount` times with offset vector `direction`.
-- Stacking is spread across server steps, one copy per step.
-- @return The number of nodes stacked.
function worldedit.stack2(pos1, pos2, direction, amount, finished)
local i = 0
local translated = {x=0, y=0, z=0}
local function next_one()
if i < amount then
i = i + 1
translated.x = translated.x + direction.x
translated.y = translated.y + direction.y
translated.z = translated.z + direction.z
worldedit.copy2(pos1, pos2, translated)
minetest.after(0, next_one)
else
if finished then
finished()
end
end
end
next_one()
return worldedit.volume(pos1, pos2) * amount
end
--- Copies a region along `axis` by `amount` nodes.
-- @param pos1
-- @param pos2
-- @param axis Axis ("x", "y", or "z")
-- @param amount
-- @return The number of nodes copied.
function worldedit.copy(pos1, pos2, axis, amount)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--make area stay loaded worldedit.keep_loaded(pos1, pos2)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local get_node, get_meta, set_node = minetest.get_node,
minetest.get_meta, minetest.set_node
-- Copy things backwards when negative to avoid corruption.
-- FIXME: Lots of code duplication here.
if amount < 0 then if amount < 0 then
local pos = {x=pos1.x, y=0, z=0} local pos = {}
pos.x = pos1.x
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local node = get_node(pos) --obtain current node local node = get_node(pos) -- Obtain current node
local meta = get_meta(pos):to_table() --get meta of current node local meta = get_meta(pos):to_table() -- Get meta of current node
local value = pos[axis] --store current position local value = pos[axis] -- Store current position
pos[axis] = value + amount --move along axis pos[axis] = value + amount -- Move along axis
add_node(pos, node) --copy node to new position set_node(pos, node) -- Copy node to new position
get_meta(pos):from_table(meta) --set metadata of new node get_meta(pos):from_table(meta) -- Set metadata of new node
pos[axis] = value --restore old position pos[axis] = value -- Restore old position
pos.z = pos.z + 1 pos.z = pos.z + 1
end end
pos.y = pos.y + 1 pos.y = pos.y + 1
@ -193,19 +140,20 @@ worldedit.copy = function(pos1, pos2, axis, amount)
pos.x = pos.x + 1 pos.x = pos.x + 1
end end
else else
local pos = {x=pos2.x, y=0, z=0} local pos = {}
pos.x = pos2.x
while pos.x >= pos1.x do while pos.x >= pos1.x do
pos.y = pos2.y pos.y = pos2.y
while pos.y >= pos1.y do while pos.y >= pos1.y do
pos.z = pos2.z pos.z = pos2.z
while pos.z >= pos1.z do while pos.z >= pos1.z do
local node = get_node(pos) --obtain current node local node = get_node(pos) -- Obtain current node
local meta = get_meta(pos):to_table() --get meta of current node local meta = get_meta(pos):to_table() -- Get meta of current node
local value = pos[axis] --store current position local value = pos[axis] -- Store current position
pos[axis] = value + amount --move along axis pos[axis] = value + amount -- Move along axis
add_node(pos, node) --copy node to new position set_node(pos, node) -- Copy node to new position
get_meta(pos):from_table(meta) --set metadata of new node get_meta(pos):from_table(meta) -- Set metadata of new node
pos[axis] = value --restore old position pos[axis] = value -- Restore old position
pos.z = pos.z - 1 pos.z = pos.z - 1
end end
pos.y = pos.y - 1 pos.y = pos.y - 1
@ -216,31 +164,70 @@ worldedit.copy = function(pos1, pos2, axis, amount)
return worldedit.volume(pos1, pos2) return worldedit.volume(pos1, pos2)
end end
--moves the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") by `amount` nodes, returning the number of nodes moved --- Copies a region by offset vector `off`.
worldedit.move = function(pos1, pos2, axis, amount) -- @param pos1
-- @param pos2
-- @param off
-- @return The number of nodes copied.
function worldedit.copy2(pos1, pos2, off)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--make area stay loaded worldedit.keep_loaded(pos1, pos2)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
--wip: move slice by slice using schematic method in the move axis and transfer metadata in separate loop (and if the amount is greater than the length in the axis, copy whole thing at a time and erase original after, using schematic method) local get_node, get_meta, set_node = minetest.get_node,
local get_node, get_meta, add_node, remove_node = minetest.get_node, minetest.get_meta, minetest.add_node, minetest.remove_node minetest.get_meta, minetest.set_node
local pos = {}
pos.x = pos2.x
while pos.x >= pos1.x do
pos.y = pos2.y
while pos.y >= pos1.y do
pos.z = pos2.z
while pos.z >= pos1.z do
local node = get_node(pos) -- Obtain current node
local meta = get_meta(pos):to_table() -- Get meta of current node
local newpos = vector.add(pos, off) -- Calculate new position
set_node(newpos, node) -- Copy node to new position
get_meta(newpos):from_table(meta) -- Set metadata of new node
pos.z = pos.z - 1
end
pos.y = pos.y - 1
end
pos.x = pos.x - 1
end
return worldedit.volume(pos1, pos2)
end
--- Moves a region along `axis` by `amount` nodes.
-- @return The number of nodes moved.
function worldedit.move(pos1, pos2, axis, amount)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
worldedit.keep_loaded(pos1, pos2)
--- TODO: Move slice by slice using schematic method in the move axis
-- and transfer metadata in separate loop (and if the amount is
-- greater than the length in the axis, copy whole thing at a time and
-- erase original after, using schematic method).
local get_node, get_meta, set_node, remove_node = minetest.get_node,
minetest.get_meta, minetest.set_node, minetest.remove_node
-- Copy things backwards when negative to avoid corruption.
--- FIXME: Lots of code duplication here.
if amount < 0 then if amount < 0 then
local pos = {x=pos1.x, y=0, z=0} local pos = {}
pos.x = pos1.x
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local node = get_node(pos) --obtain current node local node = get_node(pos) -- Obtain current node
local meta = get_meta(pos):to_table() --get metadata of current node local meta = get_meta(pos):to_table() -- Get metadata of current node
remove_node(pos) remove_node(pos) -- Remove current node
local value = pos[axis] --store current position local value = pos[axis] -- Store current position
pos[axis] = value + amount --move along axis pos[axis] = value + amount -- Move along axis
add_node(pos, node) --move node to new position set_node(pos, node) -- Move node to new position
get_meta(pos):from_table(meta) --set metadata of new node get_meta(pos):from_table(meta) -- Set metadata of new node
pos[axis] = value --restore old position pos[axis] = value -- Restore old position
pos.z = pos.z + 1 pos.z = pos.z + 1
end end
pos.y = pos.y + 1 pos.y = pos.y + 1
@ -248,20 +235,21 @@ worldedit.move = function(pos1, pos2, axis, amount)
pos.x = pos.x + 1 pos.x = pos.x + 1
end end
else else
local pos = {x=pos2.x, y=0, z=0} local pos = {}
pos.x = pos2.x
while pos.x >= pos1.x do while pos.x >= pos1.x do
pos.y = pos2.y pos.y = pos2.y
while pos.y >= pos1.y do while pos.y >= pos1.y do
pos.z = pos2.z pos.z = pos2.z
while pos.z >= pos1.z do while pos.z >= pos1.z do
local node = get_node(pos) --obtain current node local node = get_node(pos) -- Obtain current node
local meta = get_meta(pos):to_table() --get metadata of current node local meta = get_meta(pos):to_table() -- Get metadata of current node
remove_node(pos) remove_node(pos) -- Remove current node
local value = pos[axis] --store current position local value = pos[axis] -- Store current position
pos[axis] = value + amount --move along axis pos[axis] = value + amount -- Move along axis
add_node(pos, node) --move node to new position set_node(pos, node) -- Move node to new position
get_meta(pos):from_table(meta) --set metadata of new node get_meta(pos):from_table(meta) -- Set metadata of new node
pos[axis] = value --restore old position pos[axis] = value -- Restore old position
pos.z = pos.z - 1 pos.z = pos.z - 1
end end
pos.y = pos.y - 1 pos.y = pos.y - 1
@ -272,8 +260,15 @@ worldedit.move = function(pos1, pos2, axis, amount)
return worldedit.volume(pos1, pos2) return worldedit.volume(pos1, pos2)
end end
--duplicates the region defined by positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z") `count` times, returning the number of nodes stacked
worldedit.stack = function(pos1, pos2, axis, count) --- Duplicates a region along `axis` `amount` times.
-- Stacking is spread across server steps, one copy per step.
-- @param pos1
-- @param pos2
-- @param axis Axis direction, "x", "y", or "z".
-- @param count
-- @return The number of nodes stacked.
function worldedit.stack(pos1, pos2, axis, count)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local length = pos2[axis] - pos1[axis] + 1 local length = pos2[axis] - pos1[axis] + 1
if count < 0 then if count < 0 then
@ -282,61 +277,86 @@ worldedit.stack = function(pos1, pos2, axis, count)
end end
local amount = 0 local amount = 0
local copy = worldedit.copy local copy = worldedit.copy
for i = 1, count do local i = 1
amount = amount + length function next_one()
copy(pos1, pos2, axis, amount) if i <= count then
i = i + 1
amount = amount + length
copy(pos1, pos2, axis, amount)
minetest.after(0, next_one)
end
end end
next_one()
return worldedit.volume(pos1, pos2) * count return worldedit.volume(pos1, pos2) * count
end end
--scales the region defined by positions `pos1` and `pos2` by an factor of positive integer `factor` with `pos1` as the origin, returning the number of nodes scaled, the new scaled position 1, and the new scaled position 2
worldedit.scale = function(pos1, pos2, factor) --- Stretches a region by a factor of positive integers along the X, Y, and Z
-- axes, respectively, with `pos1` as the origin.
-- @param pos1
-- @param pos2
-- @param stretch_x Amount to stretch along X axis.
-- @param stretch_y Amount to stretch along Y axis.
-- @param stretch_z Amount to stretch along Z axis.
-- @return The number of nodes scaled.
-- @return The new scaled position 1.
-- @return The new scaled position 2.
function worldedit.stretch(pos1, pos2, stretch_x, stretch_y, stretch_z)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--prepare schematic of large node -- Prepare schematic of large node
local get_node, get_meta, place_schematic = minetest.get_node, minetest.get_meta, minetest.place_schematic local get_node, get_meta, place_schematic = minetest.get_node,
local placeholder_node = {name="", param1=0, param2=0} minetest.get_meta, minetest.place_schematic
local placeholder_node = {name="", param1=255, param2=0}
local nodes = {} local nodes = {}
for i = 1, factor ^ 3 do for i = 1, stretch_x * stretch_y * stretch_z do
nodes[i] = placeholder_node nodes[i] = placeholder_node
end end
local schematic = {size={x=factor, y=factor, z=factor}, data=nodes} local schematic = {size={x=stretch_x, y=stretch_y, z=stretch_z}, data=nodes}
local size = factor - 1 local size_x, size_y, size_z = stretch_x - 1, stretch_y - 1, stretch_z - 1
--make area stay loaded local new_pos2 = {
local manip = minetest.get_voxel_manip() x = pos1.x + (pos2.x - pos1.x) * stretch_x + size_x,
local new_pos2 = {x=pos1.x + (pos2.x - pos1.x) * factor + size, y=pos1.y + (pos2.y - pos1.y) * factor + size, z=pos1.z + (pos2.z - pos1.z) * factor + size} y = pos1.y + (pos2.y - pos1.y) * stretch_y + size_y,
manip:read_from_map(pos1, new_pos2) z = pos1.z + (pos2.z - pos1.z) * stretch_z + size_z,
}
worldedit.keep_loaded(pos1, new_pos2)
local pos = {x=pos2.x, y=0, z=0} local pos = {x=pos2.x, y=0, z=0}
local bigpos = {x=0, y=0, z=0} local big_pos = {x=0, y=0, z=0}
while pos.x >= pos1.x do while pos.x >= pos1.x do
pos.y = pos2.y pos.y = pos2.y
while pos.y >= pos1.y do while pos.y >= pos1.y do
pos.z = pos2.z pos.z = pos2.z
while pos.z >= pos1.z do while pos.z >= pos1.z do
local node = get_node(pos) --obtain current node local node = get_node(pos) -- Get current node
local meta = get_meta(pos):to_table() --get meta of current node local meta = get_meta(pos):to_table() -- Get meta of current node
local value = pos[axis] --store current position -- Calculate far corner of the big node
local posx, posy, posz = pos1.x + (pos.x - pos1.x) * factor, pos1.y + (pos.y - pos1.y) * factor, pos1.z + (pos.z - pos1.z) * factor local pos_x = pos1.x + (pos.x - pos1.x) * stretch_x
local pos_y = pos1.y + (pos.y - pos1.y) * stretch_y
local pos_z = pos1.z + (pos.z - pos1.z) * stretch_z
--create large node -- Create large node
placeholder_node.name = node.name placeholder_node.name = node.name
placeholder_node.param1, placeholder_node.param2 = node.param1, node.param2 placeholder_node.param2 = node.param2
bigpos.x, bigpos.y, bigpos.z = posx, posy, posz big_pos.x, big_pos.y, big_pos.z = pos_x, pos_y, pos_z
place_schematic(bigpos, schematic) place_schematic(big_pos, schematic)
--fill in large node meta -- Fill in large node meta
if next(meta.fields) ~= nil and next(meta.inventory) ~= nil then --node has meta fields if next(meta.fields) ~= nil or next(meta.inventory) ~= nil then
for x = 0, size do -- Node has meta fields
for y = 0, size do for x = 0, size_x do
for z = 0, size do for y = 0, size_y do
bigpos.x, bigpos.y, bigpos.z = posx + x, posy + y, posz + z for z = 0, size_z do
get_meta(bigpos):from_table(meta) --set metadata of new node big_pos.x = pos_x + x
end big_pos.y = pos_y + y
end big_pos.z = pos_z + z
-- Set metadata of new node
get_meta(big_pos):from_table(meta)
end
end
end end
end end
pos.z = pos.z - 1 pos.z = pos.z - 1
@ -345,11 +365,15 @@ worldedit.scale = function(pos1, pos2, factor)
end end
pos.x = pos.x - 1 pos.x = pos.x - 1
end end
return worldedit.volume(pos1, pos2) * (factor ^ 3), pos1, new_pos2 return worldedit.volume(pos1, pos2) * stretch_x * stretch_y * stretch_z, pos1, new_pos2
end end
--transposes a region defined by the positions `pos1` and `pos2` between the `axis1` and `axis2` axes, returning the number of nodes transposed, the new transposed position 1, and the new transposed position 2
worldedit.transpose = function(pos1, pos2, axis1, axis2) --- Transposes a region between two axes.
-- @return The number of nodes transposed.
-- @return The new transposed position 1.
-- @return The new transposed position 2.
function worldedit.transpose(pos1, pos2, axis1, axis2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local compare local compare
@ -365,37 +389,36 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2)
end end
end end
--calculate the new position 2 after transposition -- Calculate the new position 2 after transposition
local new_pos2 = {x=pos2.x, y=pos2.y, z=pos2.z} local new_pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
new_pos2[axis1] = pos1[axis1] + extent2 new_pos2[axis1] = pos1[axis1] + extent2
new_pos2[axis2] = pos1[axis2] + extent1 new_pos2[axis2] = pos1[axis2] + extent1
--make area stay loaded local upper_bound = {x=pos2.x, y=pos2.y, z=pos2.z}
local manip = minetest.get_voxel_manip() if upper_bound[axis1] < new_pos2[axis1] then upper_bound[axis1] = new_pos2[axis1] end
local upperbound = {x=pos2.x, y=pos2.y, z=pos2.z} if upper_bound[axis2] < new_pos2[axis2] then upper_bound[axis2] = new_pos2[axis2] end
if upperbound[axis1] < new_pos2[axis1] then upperbound[axis1] = new_pos2[axis1] end worldedit.keep_loaded(pos1, upper_bound)
if upperbound[axis2] < new_pos2[axis2] then upperbound[axis2] = new_pos2[axis2] end
manip:read_from_map(pos1, upperbound)
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local get_node, get_meta, set_node = minetest.get_node,
minetest.get_meta, minetest.set_node
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2] local extent1, extent2 = pos[axis1] - pos1[axis1], pos[axis2] - pos1[axis2]
if compare(extent1, extent2) then --transpose only if below the diagonal if compare(extent1, extent2) then -- Transpose only if below the diagonal
local node1 = get_node(pos) local node1 = get_node(pos)
local meta1 = get_meta(pos):to_table() local meta1 = get_meta(pos):to_table()
local value1, value2 = pos[axis1], pos[axis2] --save position values local value1, value2 = pos[axis1], pos[axis2] -- Save position values
pos[axis1], pos[axis2] = pos1[axis1] + extent2, pos1[axis2] + extent1 --swap axis extents pos[axis1], pos[axis2] = pos1[axis1] + extent2, pos1[axis2] + extent1 -- Swap axis extents
local node2 = get_node(pos) local node2 = get_node(pos)
local meta2 = get_meta(pos):to_table() local meta2 = get_meta(pos):to_table()
add_node(pos, node1) set_node(pos, node1)
get_meta(pos):from_table(meta1) get_meta(pos):from_table(meta1)
pos[axis1], pos[axis2] = value1, value2 --restore position values pos[axis1], pos[axis2] = value1, value2 -- Restore position values
add_node(pos, node2) set_node(pos, node2)
get_meta(pos):from_table(meta2) get_meta(pos):from_table(meta2)
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
@ -407,19 +430,20 @@ worldedit.transpose = function(pos1, pos2, axis1, axis2)
return worldedit.volume(pos1, pos2), pos1, new_pos2 return worldedit.volume(pos1, pos2), pos1, new_pos2
end end
--flips a region defined by the positions `pos1` and `pos2` along the `axis` axis ("x" or "y" or "z"), returning the number of nodes flipped
worldedit.flip = function(pos1, pos2, axis) --- Flips a region along `axis`.
-- @return The number of nodes flipped.
function worldedit.flip(pos1, pos2, axis)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--make area stay loaded worldedit.keep_loaded(pos1, pos2)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
--wip: flip the region slice by slice along the flip axis using schematic method --- TODO: Flip the region slice by slice along the flip axis using schematic method.
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
local start = pos1[axis] + pos2[axis] local start = pos1[axis] + pos2[axis]
pos2[axis] = pos1[axis] + math.floor((pos2[axis] - pos1[axis]) / 2) pos2[axis] = pos1[axis] + math.floor((pos2[axis] - pos1[axis]) / 2)
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local get_node, get_meta, set_node = minetest.get_node,
minetest.get_meta, minetest.set_node
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
@ -427,14 +451,14 @@ worldedit.flip = function(pos1, pos2, axis)
while pos.z <= pos2.z do while pos.z <= pos2.z do
local node1 = get_node(pos) local node1 = get_node(pos)
local meta1 = get_meta(pos):to_table() local meta1 = get_meta(pos):to_table()
local value = pos[axis] local value = pos[axis] -- Save position
pos[axis] = start - value pos[axis] = start - value -- Shift position
local node2 = get_node(pos) local node2 = get_node(pos)
local meta2 = get_meta(pos):to_table() local meta2 = get_meta(pos):to_table()
add_node(pos, node1) set_node(pos, node1)
get_meta(pos):from_table(meta1) get_meta(pos):from_table(meta1)
pos[axis] = value pos[axis] = value -- Restore position
add_node(pos, node2) set_node(pos, node2)
get_meta(pos):from_table(meta2) get_meta(pos):from_table(meta2)
pos.z = pos.z + 1 pos.z = pos.z + 1
end end
@ -445,63 +469,74 @@ worldedit.flip = function(pos1, pos2, axis)
return worldedit.volume(pos1, pos2) return worldedit.volume(pos1, pos2)
end end
--rotates a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise around axis `axis` (90 degree increment), returning the number of nodes rotated
worldedit.rotate = function(pos1, pos2, axis, angle) --- Rotates a region clockwise around an axis.
-- @param pos1
-- @param pos2
-- @param axis Axis ("x", "y", or "z").
-- @param angle Angle in degrees (90 degree increments only).
-- @return The number of nodes rotated.
-- @return The new first position.
-- @return The new second position.
function worldedit.rotate(pos1, pos2, axis, angle)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local axis1, axis2 local other1, other2 = worldedit.get_axis_others(axis)
if axis == "x" then
axis1, axis2 = "z", "y"
elseif axis == "y" then
axis1, axis2 = "x", "z"
else --axis == "z"
axis1, axis2 = "y", "x"
end
angle = angle % 360 angle = angle % 360
local count local count
if angle == 90 then if angle == 90 then
worldedit.flip(pos1, pos2, axis1) worldedit.flip(pos1, pos2, other1)
count, pos1, pos2 = worldedit.transpose(pos1, pos2, axis1, axis2) count, pos1, pos2 = worldedit.transpose(pos1, pos2, other1, other2)
elseif angle == 180 then elseif angle == 180 then
worldedit.flip(pos1, pos2, axis1) worldedit.flip(pos1, pos2, other1)
count = worldedit.flip(pos1, pos2, axis2) count = worldedit.flip(pos1, pos2, other2)
elseif angle == 270 then elseif angle == 270 then
worldedit.flip(pos1, pos2, axis2) worldedit.flip(pos1, pos2, other2)
count, pos1, pos2 = worldedit.transpose(pos1, pos2, axis1, axis2) count, pos1, pos2 = worldedit.transpose(pos1, pos2, other1, other2)
else
error("Only 90 degree increments are supported!")
end end
return count, pos1, pos2 return count, pos1, pos2
end end
--rotates all oriented nodes in a region defined by the positions `pos1` and `pos2` by `angle` degrees clockwise (90 degree increment) around the Y axis, returning the number of nodes oriented
worldedit.orient = function(pos1, pos2, angle) --wip: support 6D facedir rotation along arbitrary axis --- Rotates all oriented nodes in a region clockwise around the Y axis.
-- @param pos1
-- @param pos2
-- @param angle Angle in degrees (90 degree increments only).
-- @return The number of nodes oriented.
-- TODO: Support 6D facedir rotation along arbitrary axis.
function worldedit.orient(pos1, pos2, angle)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local registered_nodes = minetest.registered_nodes local registered_nodes = minetest.registered_nodes
local wallmounted = { local wallmounted = {
[90]={[0]=0, [1]=1, [2]=5, [3]=4, [4]=2, [5]=3}, [90] = {[0]=0, 1, 5, 4, 2, 3},
[180]={[0]=0, [1]=1, [2]=3, [3]=2, [4]=5, [5]=4}, [180] = {[0]=0, 1, 3, 2, 5, 4},
[270]={[0]=0, [1]=1, [2]=4, [3]=5, [4]=3, [5]=2} [270] = {[0]=0, 1, 4, 5, 3, 2}
} }
local facedir = { local facedir = {
[90]={[0]=1, [1]=2, [2]=3, [3]=0}, [90] = {[0]=1, 2, 3, 0},
[180]={[0]=2, [1]=3, [2]=0, [3]=1}, [180] = {[0]=2, 3, 0, 1},
[270]={[0]=3, [1]=0, [2]=1, [3]=2} [270] = {[0]=3, 0, 1, 2}
} }
angle = angle % 360 angle = angle % 360
if angle == 0 then if angle == 0 then
return 0 return 0
end end
if angle % 90 ~= 0 then
error("Only 90 degree increments are supported!")
end
local wallmounted_substitution = wallmounted[angle] local wallmounted_substitution = wallmounted[angle]
local facedir_substitution = facedir[angle] local facedir_substitution = facedir[angle]
--make area stay loaded worldedit.keep_loaded(pos1, pos2)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local count = 0 local count = 0
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local set_node, get_node, get_meta, swap_node = minetest.set_node,
minetest.get_node, minetest.get_meta, minetest.swap_node
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
@ -514,13 +549,13 @@ worldedit.orient = function(pos1, pos2, angle) --wip: support 6D facedir rotatio
if def.paramtype2 == "wallmounted" then if def.paramtype2 == "wallmounted" then
node.param2 = wallmounted_substitution[node.param2] node.param2 = wallmounted_substitution[node.param2]
local meta = get_meta(pos):to_table() local meta = get_meta(pos):to_table()
add_node(pos, node) set_node(pos, node)
get_meta(pos):from_table(meta) get_meta(pos):from_table(meta)
count = count + 1 count = count + 1
elseif def.paramtype2 == "facedir" then elseif def.paramtype2 == "facedir" then
node.param2 = facedir_substitution[node.param2] node.param2 = facedir_substitution[node.param2]
local meta = get_meta(pos):to_table() local meta = get_meta(pos):to_table()
add_node(pos, node) set_node(pos, node)
get_meta(pos):from_table(meta) get_meta(pos):from_table(meta)
count = count + 1 count = count + 1
end end
@ -534,42 +569,53 @@ worldedit.orient = function(pos1, pos2, angle) --wip: support 6D facedir rotatio
return count return count
end end
--fixes the lighting in a region defined by positions `pos1` and `pos2`, returning the number of nodes updated
worldedit.fixlight = function(pos1, pos2) --- Attempts to fix the lighting in a region.
-- @return The number of nodes updated.
function worldedit.fixlight(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--make area stay loaded local vmanip = minetest.get_voxel_manip(pos1, pos2)
local manip = minetest.get_voxel_manip() vmanip:write_to_map()
manip:read_from_map(pos1, pos2) vmanip:update_map() -- this updates the lighting
local nodes = minetest.find_nodes_in_area(pos1, pos2, "air") return worldedit.volume(pos1, pos2)
local dig_node = minetest.dig_node
for _, pos in ipairs(nodes) do
dig_node(pos)
end
return #nodes
end end
--clears all objects in a region defined by the positions `pos1` and `pos2`, returning the number of objects cleared
worldedit.clearobjects = function(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
--make area stay loaded --- Clears all objects in a region.
local manip = minetest.get_voxel_manip() -- @return The number of objects cleared.
manip:read_from_map(pos1, pos2) function worldedit.clear_objects(pos1, pos2)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local pos1x, pos1y, pos1z = pos1.x, pos1.y, pos1.z worldedit.keep_loaded(pos1, pos2)
local pos2x, pos2y, pos2z = pos2.x + 1, pos2.y + 1, pos2.z + 1
local center = {x=(pos1x + pos2x) / 2, y=(pos1y + pos2y) / 2, z=(pos1z + pos2z) / 2} --center of region -- Offset positions to include full nodes (positions are in the center of nodes)
local radius = ((center.x - pos1x + 0.5) + (center.y - pos1y + 0.5) + (center.z - pos1z + 0.5)) ^ 0.5 --bounding sphere radius local pos1x, pos1y, pos1z = pos1.x - 0.5, pos1.y - 0.5, pos1.z - 0.5
local pos2x, pos2y, pos2z = pos2.x + 0.5, pos2.y + 0.5, pos2.z + 0.5
-- Center of region
local center = {
x = pos1x + ((pos2x - pos1x) / 2),
y = pos1y + ((pos2y - pos1y) / 2),
z = pos1z + ((pos2z - pos1z) / 2)
}
-- Bounding sphere radius
local radius = math.sqrt(
(center.x - pos1x) ^ 2 +
(center.y - pos1y) ^ 2 +
(center.z - pos1z) ^ 2)
local count = 0 local count = 0
for _, obj in pairs(minetest.get_objects_inside_radius(center, radius)) do --all objects in bounding sphere for _, obj in pairs(minetest.get_objects_inside_radius(center, radius)) do
local entity = obj:get_luaentity() local entity = obj:get_luaentity()
if not (entity and entity.name:find("^worldedit:")) then --avoid WorldEdit entities -- Avoid players and WorldEdit entities
if not obj:is_player() and (not entity or
not entity.name:find("^worldedit:")) then
local pos = obj:getpos() local pos = obj:getpos()
if pos.x >= pos1x and pos.x <= pos2x if pos.x >= pos1x and pos.x <= pos2x and
and pos.y >= pos1y and pos.y <= pos2y pos.y >= pos1y and pos.y <= pos2y and
and pos.z >= pos1z and pos.z <= pos2z then --inside region pos.z >= pos1z and pos.z <= pos2z then
-- Inside region
obj:remove() obj:remove()
count = count + 1 count = count + 1
end end
@ -577,3 +623,4 @@ worldedit.clearobjects = function(pos1, pos2)
end end
return count return count
end end

View File

@ -1,478 +1,276 @@
worldedit = worldedit or {} --- Functions for creating primitive shapes.
local minetest = minetest --local copy of global -- @module worldedit.primitives
--adds a hollow sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added local mh = worldedit.manip_helpers
worldedit.hollow_sphere = function(pos, radius, nodename)
--set up voxel manipulator
local manip = minetest.get_voxel_manip()
local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}
local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
--fill emerged area with ignore
local nodes = {}
local ignore = minetest.get_content_id("ignore")
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
nodes[i] = ignore
end
--fill selected area with node --- Adds a sphere of `node_name` centered at `pos`.
local node_id = minetest.get_content_id(nodename) -- @param pos Position to center sphere at.
-- @param radius Sphere radius.
-- @param node_name Name of node to make shere of.
-- @param hollow Whether the sphere should be hollow.
-- @return The number of nodes added.
function worldedit.sphere(pos, radius, node_name, hollow)
local manip, area = mh.init_radius(pos, radius)
local data = mh.get_empty_data(area)
-- Fill selected area with node
local node_id = minetest.get_content_id(node_name)
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
local zstride, ystride = area.zstride, area.ystride local stride_z, stride_y = area.zstride, area.ystride
local count = 0 local count = 0
for z = -radius, radius do for z = -radius, radius do
local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed -- Offset contributed by z plus 1 to make it 1-indexed
local new_z = (z + offset_z) * stride_z + 1
for y = -radius, radius do for y = -radius, radius do
local newy = newz + (y + offsety) * ystride local new_y = new_z + (y + offset_y) * stride_y
for x = -radius, radius do for x = -radius, radius do
local squared = x * x + y * y + z * z local squared = x * x + y * y + z * z
if squared >= min_radius and squared <= max_radius then --position is on surface of sphere if squared <= max_radius and (not hollow or squared >= min_radius) then
local i = newy + (x + offsetx) -- Position is on surface of sphere
nodes[i] = node_id local i = new_y + (x + offset_x)
data[i] = node_id
count = count + 1 count = count + 1
end end
end end
end end
end end
--update map nodes mh.finish(manip, data)
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return count return count
end end
--adds a sphere centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.sphere = function(pos, radius, nodename)
--set up voxel manipulator
local manip = minetest.get_voxel_manip()
local pos1 = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}
local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
--fill emerged area with ignore --- Adds a dome.
local nodes = {} -- @param pos Position to center dome at.
local ignore = minetest.get_content_id("ignore") -- @param radius Dome radius. Negative for concave domes.
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do -- @param node_name Name of node to make dome of.
nodes[i] = ignore -- @param hollow Whether the dome should be hollow.
end -- @return The number of nodes added.
-- TODO: Add axis option.
--fill selected area with node function worldedit.dome(pos, radius, node_name, hollow)
local node_id = minetest.get_content_id(nodename) local min_y, max_y = 0, radius
local max_radius = radius * (radius + 1)
local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
local zstride, ystride = area.zstride, area.ystride
local count = 0
for z = -radius, radius do
local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed
for y = -radius, radius do
local newy = newz + (y + offsety) * ystride
for x = -radius, radius do
if x * x + y * y + z * z <= max_radius then --position is inside sphere
local i = newy + (x + offsetx)
nodes[i] = node_id
count = count + 1
end
end
end
end
--update map nodes
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return count
end
--adds a hollow dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.hollow_dome = function(pos, radius, nodename)
--set up voxel manipulator
local manip = minetest.get_voxel_manip()
local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius}
local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
--fill emerged area with ignore
local nodes = {}
local ignore = minetest.get_content_id("ignore")
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
nodes[i] = ignore
end
local miny, maxy = 0, radius
if radius < 0 then if radius < 0 then
radius = -radius radius = -radius
miny, maxy = -radius, 0 min_y, max_y = -radius, 0
end end
--fill selected area with node local manip, area = mh.init_axis_radius(pos, "y", radius)
local node_id = minetest.get_content_id(nodename) local data = mh.get_empty_data(area)
-- Add dome
local node_id = minetest.get_content_id(node_name)
local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1) local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
local zstride, ystride = area.zstride, area.ystride local stride_z, stride_y = area.zstride, area.ystride
local count = 0 local count = 0
for z = -radius, radius do for z = -radius, radius do
local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed local new_z = (z + offset_z) * stride_z + 1 --offset contributed by z plus 1 to make it 1-indexed
for y = miny, maxy do for y = min_y, max_y do
local newy = newz + (y + offsety) * ystride local new_y = new_z + (y + offset_y) * stride_y
for x = -radius, radius do for x = -radius, radius do
local squared = x * x + y * y + z * z local squared = x * x + y * y + z * z
if squared >= min_radius and squared <= max_radius then --position is on surface of sphere if squared <= max_radius and (not hollow or squared >= min_radius) then
local i = newy + (x + offsetx) -- Position is in dome
nodes[i] = node_id local i = new_y + (x + offset_x)
data[i] = node_id
count = count + 1 count = count + 1
end end
end end
end end
end end
--update map nodes mh.finish(manip, data)
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return count return count
end end
--adds a dome centered at `pos` with radius `radius`, composed of `nodename`, returning the number of nodes added --- Adds a cylinder.
worldedit.dome = function(pos, radius, nodename) -- @param pos Position to center base of cylinder at.
--set up voxel manipulator -- @param axis Axis ("x", "y", or "z")
local manip = minetest.get_voxel_manip() -- @param length Cylinder length.
local pos1 = {x=pos.x - radius, y=pos.y, z=pos.z - radius} -- @param radius Cylinder radius.
local pos2 = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius} -- @param node_name Name of node to make cylinder of.
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2) -- @param hollow Whether the cylinder should be hollow.
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2}) -- @return The number of nodes added.
function worldedit.cylinder(pos, axis, length, radius, node_name, hollow)
local other1, other2 = worldedit.get_axis_others(axis)
--fill emerged area with ignore -- Handle negative lengths
local nodes = {} local current_pos = {x=pos.x, y=pos.y, z=pos.z}
local ignore = minetest.get_content_id("ignore")
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
nodes[i] = ignore
end
local miny, maxy = 0, radius
if radius < 0 then
radius = -radius
miny, maxy = -radius, 0
end
--fill selected area with node
local node_id = minetest.get_content_id(nodename)
local max_radius = radius * (radius + 1)
local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z
local zstride, ystride = area.zstride, area.ystride
local count = 0
for z = -radius, radius do
local newz = (z + offsetz) * zstride + 1 --offset contributed by z plus 1 to make it 1-indexed
for y = miny, maxy do
local newy = newz + (y + offsety) * ystride
for x = -radius, radius do
if x * x + y * y + z * z <= max_radius then --position is inside sphere
local i = newy + (x + offsetx)
nodes[i] = node_id
count = count + 1
end
end
end
end
--update map nodes
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return count
end
--adds a hollow cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.hollow_cylinder = function(pos, axis, length, radius, nodename) --wip: rewrite this using voxelmanip
local other1, other2
if axis == "x" then
other1, other2 = "y", "z"
elseif axis == "y" then
other1, other2 = "x", "z"
else --axis == "z"
other1, other2 = "x", "y"
end
--handle negative lengths
local currentpos = {x=pos.x, y=pos.y, z=pos.z}
if length < 0 then if length < 0 then
length = -length length = -length
currentpos[axis] = currentpos[axis] - length current_pos[axis] = current_pos[axis] - length
end end
--make area stay loaded -- Set up voxel manipulator
local manip = minetest.get_voxel_manip() local manip, area = mh.init_axis_radius_length(current_pos, axis, radius, length)
local pos1 = { local data = mh.get_empty_data(area)
[axis]=currentpos[axis],
[other1]=currentpos[other1] - radius,
[other2]=currentpos[other2] - radius
}
local pos2 = {
[axis]=currentpos[axis] + length - 1,
[other1]=currentpos[other1] + radius,
[other2]=currentpos[other2] + radius
}
manip:read_from_map(pos1, pos2)
--create schematic for single node column along the axis -- Add cylinder
local node = {name=nodename, param1=0, param2=0} local node_id = minetest.get_content_id(node_name)
local nodes = {} local min_radius, max_radius = radius * (radius - 1), radius * (radius + 1)
for i = 1, length do
nodes[i] = node
end
local schematic = {size={[axis]=length, [other1]=1, [other2]=1}, data=nodes}
--add columns in a circle around axis to form cylinder
local place_schematic = minetest.place_schematic
local count = 0
local offset1, offset2 = 0, radius
local delta = -radius
while offset1 <= offset2 do
--add node at each octant
local first1, first2 = pos[other1] + offset1, pos[other1] - offset1
local second1, second2 = pos[other2] + offset2, pos[other2] - offset2
currentpos[other1], currentpos[other2] = first1, second1
place_schematic(currentpos, schematic) --octant 1
currentpos[other1] = first2
place_schematic(currentpos, schematic) --octant 4
currentpos[other2] = second2
place_schematic(currentpos, schematic) --octant 5
currentpos[other1] = first1
place_schematic(currentpos, schematic) --octant 8
local first1, first2 = pos[other1] + offset2, pos[other1] - offset2
local second1, second2 = pos[other2] + offset1, pos[other2] - offset1
currentpos[other1], currentpos[other2] = first1, second1
place_schematic(currentpos, schematic) --octant 2
currentpos[other1] = first2
place_schematic(currentpos, schematic) --octant 3
currentpos[other2] = second2
place_schematic(currentpos, schematic) --octant 6
currentpos[other1] = first1
place_schematic(currentpos, schematic) --octant 7
count = count + 8 --wip: broken because sometimes currentpos is repeated
--move to next location
delta = delta + (offset1 * 2) + 1
if delta >= 0 then
offset2 = offset2 - 1
delta = delta - (offset2 * 2)
end
offset1 = offset1 + 1
end
count = count * length --apply the length to the number of nodes
return count
end
--adds a cylinder at `pos` along the `axis` axis ("x" or "y" or "z") with length `length` and radius `radius`, composed of `nodename`, returning the number of nodes added
worldedit.cylinder = function(pos, axis, length, radius, nodename)
local other1, other2
if axis == "x" then
other1, other2 = "y", "z"
elseif axis == "y" then
other1, other2 = "x", "z"
else --axis == "z"
other1, other2 = "x", "y"
end
--handle negative lengths
local currentpos = {x=pos.x, y=pos.y, z=pos.z}
if length < 0 then
length = -length
currentpos[axis] = currentpos[axis] - length
end
--set up voxel manipulator
local manip = minetest.get_voxel_manip()
local pos1 = {
[axis]=currentpos[axis],
[other1]=currentpos[other1] - radius,
[other2]=currentpos[other2] - radius
}
local pos2 = {
[axis]=currentpos[axis] + length - 1,
[other1]=currentpos[other1] + radius,
[other2]=currentpos[other2] + radius
}
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
--fill emerged area with ignore
local nodes = {}
local ignore = minetest.get_content_id("ignore")
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
nodes[i] = ignore
end
--fill selected area with node
local node_id = minetest.get_content_id(nodename)
local max_radius = radius * (radius + 1)
local stride = {x=1, y=area.ystride, z=area.zstride} local stride = {x=1, y=area.ystride, z=area.zstride}
local offset = {x=currentpos.x - emerged_pos1.x, y=currentpos.y - emerged_pos1.y, z=currentpos.z - emerged_pos1.z} local offset = {
x = current_pos.x - area.MinEdge.x,
y = current_pos.y - area.MinEdge.y,
z = current_pos.z - area.MinEdge.z,
}
local min_slice, max_slice = offset[axis], offset[axis] + length - 1 local min_slice, max_slice = offset[axis], offset[axis] + length - 1
local count = 0 local count = 0
for index2 = -radius, radius do for index2 = -radius, radius do
local newindex2 = (index2 + offset[other1]) * stride[other1] + 1 --offset contributed by other axis 1 plus 1 to make it 1-indexed -- Offset contributed by other axis 1 plus 1 to make it 1-indexed
local new_index2 = (index2 + offset[other1]) * stride[other1] + 1
for index3 = -radius, radius do for index3 = -radius, radius do
local newindex3 = newindex2 + (index3 + offset[other2]) * stride[other2] local new_index3 = new_index2 + (index3 + offset[other2]) * stride[other2]
if index2 * index2 + index3 * index3 <= max_radius then local squared = index2 * index2 + index3 * index3
for index1 = min_slice, max_slice do --add column along axis if squared <= max_radius and (not hollow or squared >= min_radius) then
local i = newindex3 + index1 * stride[axis] -- Position is in cylinder
nodes[i] = node_id -- Add column along axis
for index1 = min_slice, max_slice do
local vi = new_index3 + index1 * stride[axis]
data[vi] = node_id
end end
count = count + length count = count + length
end end
end end
end end
--update map nodes mh.finish(manip, data)
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return count return count
end end
--adds a pyramid centered at `pos` with height `height`, composed of `nodename`, returning the number of nodes added
worldedit.pyramid = function(pos, axis, height, nodename)
local other1, other2
if axis == "x" then
other1, other2 = "y", "z"
elseif axis == "y" then
other1, other2 = "x", "z"
else --axis == "z"
other1, other2 = "x", "y"
end
local pos1 = {x=pos.x - height, y=pos.y - height, z=pos.z - height} --- Adds a pyramid.
local pos2 = {x=pos.x + height, y=pos.y + height, z=pos.z + height} -- @param pos Position to center base of pyramid at.
-- @param axis Axis ("x", "y", or "z")
-- @param height Pyramid height.
-- @param node_name Name of node to make pyramid of.
-- @param hollow Whether the pyramid should be hollow.
-- @return The number of nodes added.
function worldedit.pyramid(pos, axis, height, node_name, hollow)
local other1, other2 = worldedit.get_axis_others(axis)
--handle inverted pyramids -- Set up voxel manipulator
local startaxis, endaxis, step local manip, area = mh.init_axis_radius(pos, axis,
height >= 0 and height or -height)
local data = mh.get_empty_data(area)
-- Handle inverted pyramids
local start_axis, end_axis, step
if height > 0 then if height > 0 then
height = height - 1 height = height - 1
step = 1 step = 1
pos1[axis] = pos[axis] --upper half of box
else else
height = height + 1 height = height + 1
step = -1 step = -1
pos2[axis] = pos[axis] --lower half of box
end end
--set up voxel manipulator -- Add pyramid
local manip = minetest.get_voxel_manip() local node_id = minetest.get_content_id(node_name)
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
--fill emerged area with ignore
local nodes = {}
local ignore = minetest.get_content_id("ignore")
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
nodes[i] = ignore
end
--fill selected area with node
local node_id = minetest.get_content_id(nodename)
local stride = {x=1, y=area.ystride, z=area.zstride} local stride = {x=1, y=area.ystride, z=area.zstride}
local offset = {x=pos.x - emerged_pos1.x, y=pos.y - emerged_pos1.y, z=pos.z - emerged_pos1.z} local offset = {
local size = height * step x = pos.x - area.MinEdge.x,
y = pos.y - area.MinEdge.y,
z = pos.z - area.MinEdge.z,
}
local size = math.abs(height * step)
local count = 0 local count = 0
for index1 = 0, height, step do --go through each level of the pyramid -- For each level of the pyramid
local newindex1 = (index1 + offset[axis]) * stride[axis] + 1 --offset contributed by axis plus 1 to make it 1-indexed for index1 = 0, height, step do
-- Offset contributed by axis plus 1 to make it 1-indexed
local new_index1 = (index1 + offset[axis]) * stride[axis] + 1
for index2 = -size, size do for index2 = -size, size do
local newindex2 = newindex1 + (index2 + offset[other1]) * stride[other1] local new_index2 = new_index1 + (index2 + offset[other1]) * stride[other1]
for index3 = -size, size do for index3 = -size, size do
local i = newindex2 + (index3 + offset[other2]) * stride[other2] local i = new_index2 + (index3 + offset[other2]) * stride[other2]
nodes[i] = node_id if (not hollow or size - math.abs(index2) < 2 or size - math.abs(index3) < 2) then
data[i] = node_id
count = count + 1
end
end end
end end
count = count + (size * 2 + 1) ^ 2
size = size - 1 size = size - 1
end end
--update map nodes mh.finish(manip, data)
manip:set_data(nodes)
manip:write_to_map()
manip:update_map()
return count return count
end end
--adds a spiral centered at `pos` with side length `length`, height `height`, space between walls `spacer`, composed of `nodename`, returning the number of nodes added --- Adds a spiral.
worldedit.spiral = function(pos, length, height, spacer, nodename) -- @param pos Position to center spiral at.
-- @param length Spral length.
-- @param height Spiral height.
-- @param spacer Space between walls.
-- @param node_name Name of node to make spiral of.
-- @return Number of nodes added.
-- TODO: Add axis option.
function worldedit.spiral(pos, length, height, spacer, node_name)
local extent = math.ceil(length / 2) local extent = math.ceil(length / 2)
local pos1 = {x=pos.x - extent, y=pos.y, z=pos.z - extent}
local pos2 = {x=pos.x + extent, y=pos.y + height, z=pos.z + extent}
--set up voxel manipulator local manip, area = mh.init_axis_radius_length(pos, "y", extent, height)
local manip = minetest.get_voxel_manip() local data = mh.get_empty_data(area)
local emerged_pos1, emerged_pos2 = manip:read_from_map(pos1, pos2)
local area = VoxelArea:new({MinEdge=emerged_pos1, MaxEdge=emerged_pos2})
--fill emerged area with ignore -- Set up variables
local nodes = {} local node_id = minetest.get_content_id(node_name)
local ignore = minetest.get_content_id("ignore")
for i = 1, worldedit.volume(emerged_pos1, emerged_pos2) do
nodes[i] = ignore
end
--
local node_id = minetest.get_content_id(nodename)
local stride = {x=1, y=area.ystride, z=area.zstride} local stride = {x=1, y=area.ystride, z=area.zstride}
local offsetx, offsety, offsetz = pos.x - emerged_pos1.x, pos.y - emerged_pos1.y, pos.z - emerged_pos1.z local offset_x, offset_y, offset_z = pos.x - area.MinEdge.x, pos.y - area.MinEdge.y, pos.z - area.MinEdge.z
local i = offsetz * stride.z + offsety * stride.y + offsetx + 1 local i = offset_z * stride.z + offset_y * stride.y + offset_x + 1
--add first column -- Add first column
local count = height
local column = i local column = i
for y = 1, height do for y = 1, height do
nodes[column] = node_id data[column] = node_id
column = column + stride.y column = column + stride.y
end end
--add spiral segments -- Add spiral segments
local axis, other = "x", "z" local stride_axis, stride_other = stride.x, stride.z
local sign = 1 local sign = -1
local count = height local segment_length = 0
for segment = 1, length / spacer - 1 do --go through each segment except the last spacer = spacer + 1
for index = 1, segment * spacer do --fill segment -- Go through each segment except the last
i = i + stride[axis] * sign for segment = 1, math.floor(length / spacer) * 2 do
-- Change sign and length every other turn starting with the first
if segment % 2 == 1 then
sign = -sign
segment_length = segment_length + spacer
end
-- Fill segment
for index = 1, segment_length do
-- Move along the direction of the segment
i = i + stride_axis * sign
local column = i local column = i
for y = 1, height do --add column -- Add column
nodes[column] = node_id for y = 1, height do
data[column] = node_id
column = column + stride.y column = column + stride.y
end end
count = count + height
end
axis, other = other, axis --swap axes
if segment % 2 == 1 then --change sign every other turn
sign = -sign
end end
count = count + segment_length * height
stride_axis, stride_other = stride_other, stride_axis -- Swap axes
end end
--add shorter final segment -- Add shorter final segment
for index = 1, (math.floor(length / spacer) - 2) * spacer do sign = -sign
i = i + stride[axis] * sign for index = 1, segment_length do
i = i + stride_axis * sign
local column = i local column = i
for y = 1, height do --add column -- Add column
nodes[column] = node_id for y = 1, height do
data[column] = node_id
column = column + stride.y column = column + stride.y
end end
count = count + height
end end
print(minetest.serialize(nodes)) count = count + segment_length * height
--update map nodes
manip:set_data(nodes) mh.finish(manip, data)
manip:write_to_map()
manip:update_map()
return count return count
end end

View File

@ -1,44 +1,61 @@
worldedit = worldedit or {} --- Schematic serialization and deserialiation.
local minetest = minetest --local copy of global -- @module worldedit.serialization
--modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions worldedit.LATEST_SERIALIZATION_VERSION = 5
worldedit.sort_pos = function(pos1, pos2) local LATEST_SERIALIZATION_HEADER = worldedit.LATEST_SERIALIZATION_VERSION .. ":"
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
if pos1.x > pos2.x then
pos2.x, pos1.x = pos1.x, pos2.x
end
if pos1.y > pos2.y then
pos2.y, pos1.y = pos1.y, pos2.y
end
if pos1.z > pos2.z then
pos2.z, pos1.z = pos1.z, pos2.z
end
return pos1, pos2
end
--determines the version of serialized data `value`, returning the version as a positive integer or 0 for unknown versions
worldedit.valueversion = function(value) --[[
if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then --previous list format Serialization version history:
return 3 1: Original format. Serialized Lua table with a weird linked format...
2: Position and node seperated into sub-tables in fields `1` and `2`.
3: List of nodes, one per line, with fields seperated by spaces.
Format: <X> <Y> <Z> <Name> <Param1> <Param2>
4: Serialized Lua table containing a list of nodes with `x`, `y`, `z`,
`name`, `param1`, `param2`, and `meta` fields.
5: Added header and made `param1`, `param2`, and `meta` fields optional.
Header format: <Version>,<ExtraHeaderField1>,...:<Content>
--]]
--- Reads the header of serialized data.
-- @param value Serialized WorldEdit data.
-- @return The version as a positive natural number, or 0 for unknown versions.
-- @return Extra header fields as a list of strings, or nil if not supported.
-- @return Content (data after header).
function worldedit.read_header(value)
if value:find("^[0-9]+[%-:]") then
local header_end = value:find(":", 1, true)
local header = value:sub(1, header_end - 1):split(",")
local version = tonumber(header[1])
table.remove(header, 1)
local content = value:sub(header_end + 1)
return version, header, content
end
-- Old versions that didn't include a header with a version number
if value:find("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)") and not value:find("%{") then -- List format
return 3, nil, value
elseif value:find("^[^\"']+%{%d+%}") then elseif value:find("^[^\"']+%{%d+%}") then
if value:find("%[\"meta\"%]") then --previous meta flat table format if value:find("%[\"meta\"%]") then -- Meta flat table format
return 2 return 2, nil, value
end end
return 1 --original flat table format return 1, nil, value -- Flat table format
elseif value:find("%{") then --current nested table format elseif value:find("%{") then -- Raw nested table format
return 4 return 4, nil, value
end end
return 0 --unknown format return nil
end end
--converts the region defined by positions `pos1` and `pos2` into a single string, returning the serialized data and the number of nodes serialized
worldedit.serialize = function(pos1, pos2)
--make area stay loaded
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) --- Converts the region defined by positions `pos1` and `pos2`
-- into a single string.
-- @return The serialized data.
-- @return The number of nodes serialized.
function worldedit.serialize(pos1, pos2)
pos1, pos2 = worldedit.sort_pos(pos1, pos2)
worldedit.keep_loaded(pos1, pos2)
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
local count = 0 local count = 0
local result = {} local result = {}
@ -53,21 +70,29 @@ worldedit.serialize = function(pos1, pos2)
count = count + 1 count = count + 1
local meta = get_meta(pos):to_table() local meta = get_meta(pos):to_table()
--convert metadata itemstacks to itemstrings local meta_empty = true
-- Convert metadata item stacks to item strings
for name, inventory in pairs(meta.inventory) do for name, inventory in pairs(meta.inventory) do
for index, stack in ipairs(inventory) do for index, stack in ipairs(inventory) do
meta_empty = false
inventory[index] = stack.to_string and stack:to_string() or stack inventory[index] = stack.to_string and stack:to_string() or stack
end end
end end
for k in pairs(meta) do
if k ~= "inventory" then
meta_empty = false
break
end
end
result[count] = { result[count] = {
x = pos.x - pos1.x, x = pos.x - pos1.x,
y = pos.y - pos1.y, y = pos.y - pos1.y,
z = pos.z - pos1.z, z = pos.z - pos1.z,
name = node.name, name = node.name,
param1 = node.param1, param1 = node.param1 ~= 0 and node.param1 or nil,
param2 = node.param2, param2 = node.param2 ~= 0 and node.param2 or nil,
meta = meta, meta = not meta_empty and meta or nil,
} }
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
@ -76,198 +101,139 @@ worldedit.serialize = function(pos1, pos2)
end end
pos.x = pos.x + 1 pos.x = pos.x + 1
end end
result = minetest.serialize(result) --convert entries to a string -- Serialize entries
return result, count result = minetest.serialize(result)
return LATEST_SERIALIZATION_HEADER .. result, count
end end
--determines the volume the nodes represented by string `value` would occupy if deserialized at `originpos`, returning the two corner positions and the number of nodes
--contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible) --- Loads the schematic in `value` into a node list in the latest format.
worldedit.allocate = function(originpos, value) -- Contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile)
-- by ChillCode, available under the MIT license.
-- @return A node list in the latest format, or nil on failure.
local function load_schematic(value)
local version, header, content = worldedit.read_header(value)
local nodes = {}
if version == 1 or version == 2 then -- Original flat table format
local tables = minetest.deserialize(content)
if not tables then return nil end
-- Transform the node table into an array of nodes
for i = 1, #tables do
for j, v in pairs(tables[i]) do
if type(v) == "table" then
tables[i][j] = tables[v[1]]
end
end
end
nodes = tables[1]
if version == 1 then --original flat table format
for i, entry in ipairs(nodes) do
local pos = entry[1]
entry.x, entry.y, entry.z = pos.x, pos.y, pos.z
entry[1] = nil
local node = entry[2]
entry.name, entry.param1, entry.param2 = node.name, node.param1, node.param2
entry[2] = nil
end
end
elseif version == 3 then -- List format
for x, y, z, name, param1, param2 in content:gmatch(
"([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+" ..
"([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do
param1, param2 = tonumber(param1), tonumber(param2)
table.insert(nodes, {
x = tonumber(x),
y = tonumber(y),
z = tonumber(z),
name = name,
param1 = param1 ~= 0 and param1 or nil,
param2 = param2 ~= 0 and param2 or nil,
})
end
elseif version == 4 or version == 5 then -- Nested table format
if not jit then
-- This is broken for larger tables in the current version of LuaJIT
nodes = minetest.deserialize(content)
else
-- XXX: This is a filthy hack that works surprisingly well - in LuaJIT, `minetest.deserialize` will fail due to the register limit
nodes = {}
content = content:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1) -- remove the starting and ending values to leave only the node data
local escaped = content:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
local startpos, startpos1, endpos = 1, 1
while true do -- go through each individual node entry (except the last)
startpos, endpos = escaped:find("},%s*{", startpos)
if not startpos then
break
end
local current = content:sub(startpos1, startpos)
local entry = minetest.deserialize("return " .. current)
table.insert(nodes, entry)
startpos, startpos1 = endpos, endpos
end
local entry = minetest.deserialize("return " .. content:sub(startpos1)) -- process the last entry
table.insert(nodes, entry)
end
else
return nil
end
return nodes
end
--- Determines the volume the nodes represented by string `value` would occupy
-- if deserialized at `origin_pos`.
-- @return Low corner position.
-- @return High corner position.
-- @return The number of nodes.
function worldedit.allocate(origin_pos, value)
local nodes = load_schematic(value)
if not nodes then return nil end
return worldedit.allocate_with_nodes(origin_pos, nodes)
end
-- Internal
function worldedit.allocate_with_nodes(origin_pos, nodes)
local huge = math.huge local huge = math.huge
local pos1x, pos1y, pos1z = huge, huge, huge local pos1x, pos1y, pos1z = huge, huge, huge
local pos2x, pos2y, pos2z = -huge, -huge, -huge local pos2x, pos2y, pos2z = -huge, -huge, -huge
local originx, originy, originz = originpos.x, originpos.y, originpos.z local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
local count = 0 for i, entry in ipairs(nodes) do
local version = worldedit.valueversion(value) local x, y, z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
if version == 1 or version == 2 then --flat table format if x < pos1x then pos1x = x end
--obtain the node table if y < pos1y then pos1y = y end
local get_tables = loadstring(value) if z < pos1z then pos1z = z end
if get_tables then --error loading value if x > pos2x then pos2x = x end
return originpos, originpos, count if y > pos2y then pos2y = y end
end if z > pos2z then pos2z = z end
local tables = get_tables()
--transform the node table into an array of nodes
for i = 1, #tables do
for j, v in pairs(tables[i]) do
if type(v) == "table" then
tables[i][j] = tables[v[1]]
end
end
end
local nodes = tables[1]
--check the node array
count = #nodes
if version == 1 then --original flat table format
for index = 1, count do
local entry = nodes[index]
local pos = entry[1]
local x, y, z = originx - pos.x, originy - pos.y, originz - pos.z
if x < pos1x then pos1x = x end
if y < pos1y then pos1y = y end
if z < pos1z then pos1z = z end
if x > pos2x then pos2x = x end
if y > pos2y then pos2y = y end
if z > pos2z then pos2z = z end
end
else --previous meta flat table format
for index = 1, count do
local entry = nodes[index]
local x, y, z = originx - entry.x, originy - entry.y, originz - entry.z
if x < pos1x then pos1x = x end
if y < pos1y then pos1y = y end
if z < pos1z then pos1z = z end
if x > pos2x then pos2x = x end
if y > pos2y then pos2y = y end
if z > pos2z then pos2z = z end
end
end
elseif version == 3 then --previous list format
for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries
x, y, z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)
if x < pos1x then pos1x = x end
if y < pos1y then pos1y = y end
if z < pos1z then pos1z = z end
if x > pos2x then pos2x = x end
if y > pos2y then pos2y = y end
if z > pos2z then pos2z = z end
count = count + 1
end
elseif version == 4 then --current nested table format
--wip: this is a filthy hack that works surprisingly well
value = value:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1)
local escaped = value:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
local startpos, startpos1, endpos = 1, 1
local nodes = {}
while true do
startpos, endpos = escaped:find("},%s*{", startpos)
if not startpos then
break
end
local current = value:sub(startpos1, startpos)
table.insert(nodes, minetest.deserialize("return " .. current))
startpos, startpos1 = endpos, endpos
end
table.insert(nodes, minetest.deserialize("return " .. value:sub(startpos1)))
--local nodes = minetest.deserialize(value) --wip: this is broken for larger tables in the current version of LuaJIT
count = #nodes
for index = 1, count do
local entry = nodes[index]
x, y, z = originx + entry.x, originy + entry.y, originz + entry.z
if x < pos1x then pos1x = x end
if y < pos1y then pos1y = y end
if z < pos1z then pos1z = z end
if x > pos2x then pos2x = x end
if y > pos2y then pos2y = y end
if z > pos2z then pos2z = z end
end
end end
local pos1 = {x=pos1x, y=pos1y, z=pos1z} local pos1 = {x=pos1x, y=pos1y, z=pos1z}
local pos2 = {x=pos2x, y=pos2y, z=pos2z} local pos2 = {x=pos2x, y=pos2y, z=pos2z}
return pos1, pos2, count return pos1, pos2, #nodes
end end
--loads the nodes represented by string `value` at position `originpos`, returning the number of nodes deserialized
--contains code based on [table.save/table.load](http://lua-users.org/wiki/SaveTableToFile) by ChillCode, available under the MIT license (GPL compatible)
worldedit.deserialize = function(originpos, value)
--make area stay loaded --wip: not very performant
local pos1, pos2 = worldedit.allocate(originpos, value)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local originx, originy, originz = originpos.x, originpos.y, originpos.z --- Loads the nodes represented by string `value` at position `origin_pos`.
-- @return The number of nodes deserialized.
function worldedit.deserialize(origin_pos, value)
local nodes = load_schematic(value)
if not nodes then return nil end
local pos1, pos2 = worldedit.allocate_with_nodes(origin_pos, nodes)
worldedit.keep_loaded(pos1, pos2)
local origin_x, origin_y, origin_z = origin_pos.x, origin_pos.y, origin_pos.z
local count = 0 local count = 0
local add_node, get_meta = minetest.add_node, minetest.get_meta local add_node, get_meta = minetest.add_node, minetest.get_meta
local version = worldedit.valueversion(value) for i, entry in ipairs(nodes) do
if version == 1 or version == 2 then --original flat table format entry.x, entry.y, entry.z = origin_x + entry.x, origin_y + entry.y, origin_z + entry.z
--obtain the node table -- Entry acts as both position and node
local get_tables = loadstring(value) add_node(entry, entry)
if not get_tables then --error loading value if entry.meta then
return count
end
local tables = get_tables()
--transform the node table into an array of nodes
for i = 1, #tables do
for j, v in pairs(tables[i]) do
if type(v) == "table" then
tables[i][j] = tables[v[1]]
end
end
end
local nodes = tables[1]
--load the node array
count = #nodes
if version == 1 then --original flat table format
for index = 1, count do
local entry = nodes[index]
local pos = entry[1]
pos.x, pos.y, pos.z = originx - pos.x, originy - pos.y, originz - pos.z
add_node(pos, entry[2])
end
else --previous meta flat table format
for index = 1, #nodes do
local entry = nodes[index]
entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z
add_node(entry, entry) --entry acts both as position and as node
get_meta(entry):from_table(entry.meta)
end
end
elseif version == 3 then --previous list format
local pos = {x=0, y=0, z=0}
local node = {name="", param1=0, param2=0}
for x, y, z, name, param1, param2 in value:gmatch("([+-]?%d+)%s+([+-]?%d+)%s+([+-]?%d+)%s+([^%s]+)%s+(%d+)%s+(%d+)[^\r\n]*[\r\n]*") do --match node entries
pos.x, pos.y, pos.z = originx + tonumber(x), originy + tonumber(y), originz + tonumber(z)
node.name, node.param1, node.param2 = name, param1, param2
add_node(pos, node)
count = count + 1
end
elseif version == 4 then --current nested table format
--wip: this is a filthy hack that works surprisingly well
value = value:gsub("return%s*{", "", 1):gsub("}%s*$", "", 1)
local escaped = value:gsub("\\\\", "@@"):gsub("\\\"", "@@"):gsub("(\"[^\"]*\")", function(s) return string.rep("@", #s) end)
local startpos, startpos1, endpos = 1, 1
local nodes = {}
while true do
startpos, endpos = escaped:find("},%s*{", startpos)
if not startpos then
break
end
local current = value:sub(startpos1, startpos)
table.insert(nodes, minetest.deserialize("return " .. current))
startpos, startpos1 = endpos, endpos
end
table.insert(nodes, minetest.deserialize("return " .. value:sub(startpos1)))
--local nodes = minetest.deserialize(value) --wip: this is broken for larger tables in the current version of LuaJIT
--load the nodes
count = #nodes
for index = 1, count do
local entry = nodes[index]
entry.x, entry.y, entry.z = originx + entry.x, originy + entry.y, originz + entry.z
add_node(entry, entry) --entry acts both as position and as node
end
--load the metadata
for index = 1, count do
local entry = nodes[index]
get_meta(entry):from_table(entry.meta) get_meta(entry):from_table(entry.meta)
end end
end end
return count return #nodes
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 B

145
worldedit/visualization.lua Normal file → Executable file
View File

@ -1,57 +1,38 @@
worldedit = worldedit or {} --- Functions for visibly hiding nodes
local minetest = minetest --local copy of global -- @module worldedit.visualization
--modifies positions `pos1` and `pos2` so that each component of `pos1` is less than or equal to its corresponding conent of `pos2`, returning two new positions
worldedit.sort_pos = function(pos1, pos2)
pos1 = {x=pos1.x, y=pos1.y, z=pos1.z}
pos2 = {x=pos2.x, y=pos2.y, z=pos2.z}
if pos1.x > pos2.x then
pos2.x, pos1.x = pos1.x, pos2.x
end
if pos1.y > pos2.y then
pos2.y, pos1.y = pos1.y, pos2.y
end
if pos1.z > pos2.z then
pos2.z, pos1.z = pos1.z, pos2.z
end
return pos1, pos2
end
--determines the volume of the region defined by positions `pos1` and `pos2`, returning the volume
worldedit.volume = function(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
return (pos2.x - pos1.x + 1) * (pos2.y - pos1.y + 1) * (pos2.z - pos1.z + 1)
end
minetest.register_node("worldedit:placeholder", { minetest.register_node("worldedit:placeholder", {
drawtype = "airlike", drawtype = "airlike",
paramtype = "light", paramtype = "light",
sunlight_propagates = true, sunlight_propagates = true,
diggable = false, diggable = false,
walkable = false,
groups = {not_in_creative_inventory=1}, groups = {not_in_creative_inventory=1},
}) })
--hides all nodes in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes, returning the number of nodes hidden --- Hides all nodes in a region defined by positions `pos1` and `pos2` by
worldedit.hide = function(pos1, pos2) -- non-destructively replacing them with invisible nodes.
--make area stay loaded -- @return The number of nodes hidden.
local manip = minetest.get_voxel_manip() function worldedit.hide(pos1, pos2)
manip:read_from_map(pos1, pos2) pos1, pos2 = worldedit.sort_pos(pos1, pos2)
worldedit.keep_loaded(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local get_node, get_meta, swap_node = minetest.get_node,
minetest.get_meta, minetest.swap_node
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
while pos.y <= pos2.y do while pos.y <= pos2.y do
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local node = get_node(pos) local node = get_node(pos)
if node.name ~= "worldedit:placeholder" then if node.name ~= "air" and node.name ~= "worldedit:placeholder" then
local data = get_meta(pos):to_table() --obtain metadata of original node -- Save the node's original name
data.fields.worldedit_placeholder = node.name --add the node's name get_meta(pos):set_string("worldedit_placeholder", node.name)
node.name = "worldedit:placeholder" --set node name -- Swap in placeholder node
add_node(pos, node) --add placeholder node node.name = "worldedit:placeholder"
get_meta(pos):from_table(data) --set placeholder metadata to the original node's metadata swap_node(pos, node)
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
end end
@ -62,40 +43,44 @@ worldedit.hide = function(pos1, pos2)
return worldedit.volume(pos1, pos2) return worldedit.volume(pos1, pos2)
end end
--suppresses all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively replacing them with invisible nodes, returning the number of nodes suppressed --- Suppresses all instances of `node_name` in a region defined by positions
worldedit.suppress = function(pos1, pos2, nodename) -- `pos1` and `pos2` by non-destructively replacing them with invisible nodes.
--ignore placeholder supression -- @return The number of nodes suppressed.
if nodename == "worldedit:placeholder" then function worldedit.suppress(pos1, pos2, node_name)
-- Ignore placeholder supression
if node_name == "worldedit:placeholder" then
return 0 return 0
end end
--make area stay loaded pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) worldedit.keep_loaded(pos1, pos2)
local nodes = minetest.find_nodes_in_area(pos1, pos2, nodename)
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local nodes = minetest.find_nodes_in_area(pos1, pos2, node_name)
local get_node, get_meta, swap_node = minetest.get_node,
minetest.get_meta, minetest.swap_node
for _, pos in ipairs(nodes) do for _, pos in ipairs(nodes) do
local node = get_node(pos) local node = get_node(pos)
local data = get_meta(pos):to_table() --obtain metadata of original node -- Save the node's original name
data.fields.worldedit_placeholder = node.name --add the node's name get_meta(pos):set_string("worldedit_placeholder", node.name)
node.name = "worldedit:placeholder" --set node name -- Swap in placeholder node
add_node(pos, node) --add placeholder node node.name = "worldedit:placeholder"
get_meta(pos):from_table(data) --set placeholder metadata to the original node's metadata swap_node(pos, node)
end end
return #nodes return #nodes
end end
--highlights all instances of `nodename` in a region defined by positions `pos1` and `pos2` by non-destructively hiding all other nodes, returning the number of nodes found --- Highlights all instances of `node_name` in a region defined by positions
worldedit.highlight = function(pos1, pos2, nodename) -- `pos1` and `pos2` by non-destructively hiding all other nodes.
--make area stay loaded -- @return The number of nodes found.
local manip = minetest.get_voxel_manip() function worldedit.highlight(pos1, pos2, node_name)
manip:read_from_map(pos1, pos2) pos1, pos2 = worldedit.sort_pos(pos1, pos2)
worldedit.keep_loaded(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local pos = {x=pos1.x, y=0, z=0} local pos = {x=pos1.x, y=0, z=0}
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local get_node, get_meta, swap_node = minetest.get_node,
minetest.get_meta, minetest.swap_node
local count = 0 local count = 0
while pos.x <= pos2.x do while pos.x <= pos2.x do
pos.y = pos1.y pos.y = pos1.y
@ -103,14 +88,14 @@ worldedit.highlight = function(pos1, pos2, nodename)
pos.z = pos1.z pos.z = pos1.z
while pos.z <= pos2.z do while pos.z <= pos2.z do
local node = get_node(pos) local node = get_node(pos)
if node.name == nodename then --node found if node.name == node_name then -- Node found
count = count + 1 count = count + 1
elseif node.name ~= "worldedit:placeholder" then --hide other nodes elseif node.name ~= "worldedit:placeholder" then -- Hide other nodes
local data = get_meta(pos):to_table() --obtain metadata of original node -- Save the node's original name
data.fields.worldedit_placeholder = node.name --add the node's name get_meta(pos):set_string("worldedit_placeholder", node.name)
node.name = "worldedit:placeholder" --set node name -- Swap in placeholder node
add_node(pos, node) --add placeholder node node.name = "worldedit:placeholder"
get_meta(pos):from_table(data) --set placeholder metadata to the original node's metadata swap_node(pos, node)
end end
pos.z = pos.z + 1 pos.z = pos.z + 1
end end
@ -121,22 +106,26 @@ worldedit.highlight = function(pos1, pos2, nodename)
return count return count
end end
--restores all nodes hidden with WorldEdit functions in a region defined by positions `pos1` and `pos2`, returning the number of nodes restored -- Restores all nodes hidden with WorldEdit functions in a region defined
worldedit.restore = function(pos1, pos2) -- by positions `pos1` and `pos2`.
--make area stay loaded -- @return The number of nodes restored.
local manip = minetest.get_voxel_manip() function worldedit.restore(pos1, pos2)
manip:read_from_map(pos1, pos2)
local pos1, pos2 = worldedit.sort_pos(pos1, pos2) local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
worldedit.keep_loaded(pos1, pos2)
local nodes = minetest.find_nodes_in_area(pos1, pos2, "worldedit:placeholder") local nodes = minetest.find_nodes_in_area(pos1, pos2, "worldedit:placeholder")
local get_node, get_meta, add_node = minetest.get_node, minetest.get_meta, minetest.add_node local get_node, get_meta, swap_node = minetest.get_node,
minetest.get_meta, minetest.swap_node
for _, pos in ipairs(nodes) do for _, pos in ipairs(nodes) do
local node = get_node(pos) local node = get_node(pos)
local data = get_meta(pos):to_table() --obtain node metadata local meta = get_meta(pos)
node.name = data.fields.worldedit_placeholder --set node name local data = meta:to_table()
data.fields.worldedit_placeholder = nil --delete old nodename node.name = data.fields.worldedit_placeholder
add_node(pos, node) --add original node data.fields.worldedit_placeholder = nil
get_meta(pos):from_table(data) --set original node metadata meta:from_table(data)
swap_node(pos, node)
end end
return #nodes return #nodes
end end

1
worldedit_commands/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*~

View File

@ -0,0 +1,240 @@
minetest.register_chatcommand("/outset", {
params = "[h|v] <amount>",
description = "outset the selection",
privs = {worldedit=true},
func = function(name, param)
local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
if find == nil then
return false, "invalid usage: " .. param
end
local pos1 = worldedit.pos1[name]
local pos2 = worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
return false,
"Undefined region. Region must be defined beforehand."
end
local hv_test = dir:find("[^hv]+")
if hv_test ~= nil then
return false, "Invalid direction."
end
if dir == "" or dir == "hv" or dir == "vh" then
assert(worldedit.cuboid_volumetric_expand(name, amount))
elseif dir == "h" then
assert(worldedit.cuboid_linear_expand(name, 'x', 1, amount))
assert(worldedit.cuboid_linear_expand(name, 'x', -1, amount))
assert(worldedit.cuboid_linear_expand(name, 'z', 1, amount))
assert(worldedit.cuboid_linear_expand(name, 'z', -1, amount))
elseif dir == "v" then
assert(worldedit.cuboid_linear_expand(name, 'y', 1, amount))
assert(worldedit.cuboid_linear_expand(name, 'y', -1, amount))
else
return false, "Invalid number of arguments"
end
worldedit.marker_update(name)
return true, "Region outset by " .. amount .. " blocks"
end,
}
)
minetest.register_chatcommand("/inset", {
params = "[h|v] <amount>",
description = "inset the selection",
privs = {worldedit=true},
func = function(name, param)
local find, _, dir, amount = param:find("(%a*)%s*([+-]?%d+)")
if find == nil then
return false, "invalid usage: " .. param
end
local pos1 = worldedit.pos1[name]
local pos2 = worldedit.pos2[name]
if pos1 == nil or pos2 == nil then
return false,
"Undefined region. Region must be defined beforehand."
end
local hv_test = dir:find("[^hv]+")
if hv_test ~= nil then
return false, "Invalid direction."
end
if dir == "" or dir == "vh" or dir == "hv" then
assert(worldedit.cuboid_volumetric_expand(name, -amount))
elseif dir == "h" then
assert(worldedit.cuboid_linear_expand(name, 'x', 1, -amount))
assert(worldedit.cuboid_linear_expand(name, 'x', -1, -amount))
assert(worldedit.cuboid_linear_expand(name, 'z', 1, -amount))
assert(worldedit.cuboid_linear_expand(name, 'z', -1, -amount))
elseif dir == "v" then
assert(worldedit.cuboid_linear_expand(name, 'y', 1, -amount))
assert(worldedit.cuboid_linear_expand(name, 'y', -1, -amount))
else
return false, "Invalid number of arguments"
end
worldedit.marker_update(name)
return true, "Region inset by " .. amount .. " blocks"
end,
}
)
minetest.register_chatcommand("/shift", {
params = "[x|y|z|?|up|down|left|right|front|back] [+|-]<amount>",
description = "Moves the selection region. Does not move contents.",
privs = {worldedit=true},
func = function(name, param)
local pos1 = worldedit.pos1[name]
local pos2 = worldedit.pos2[name]
local find, _, direction, amount = param:find("([%?%l]+)%s*([+-]?%d+)")
if find == nil then
worldedit.player_notify(name, "invalid usage: " .. param)
return
end
if pos1 == nil or pos2 == nil then
worldedit.player_notify(name,
"Undefined region. Region must be defined beforehand.")
return
end
local axis, dir
if direction == "x" or direction == "y" or direction == "z" then
axis, dir = direction, 1
elseif direction == "?" then
axis, dir = worldedit.player_axis(name)
else
axis, dir = worldedit.translate_direction(name, direction)
end
if axis == nil or dir == nil then
return false, "Invalid if looking straight up or down"
end
assert(worldedit.cuboid_shift(name, axis, amount * dir))
worldedit.marker_update(name)
return true, "Region shifted by " .. amount .. " nodes"
end,
}
)
minetest.register_chatcommand("/expand", {
params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
description = "expand the selection in one or two directions at once",
privs = {worldedit=true},
func = function(name, param)
local find, _, sign, direction, amount,
rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
if find == nil then
worldedit.player_notify(name, "invalid use: " .. param)
return
end
if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
worldedit.player_notify(name,
"Undefined region. Region must be defined beforehand.")
return
end
local absolute = direction:find("[xyz?]")
local dir, axis
if rev_amount == "" then
rev_amount = 0
end
if absolute == nil then
axis, dir = worldedit.translate_direction(name, direction)
if axis == nil or dir == nil then
return false, "Invalid if looking straight up or down"
end
else
if direction == "?" then
axis, dir = worldedit.player_axis(name)
else
axis = direction
dir = 1
end
end
if sign == "-" then
dir = -dir
end
worldedit.cuboid_linear_expand(name, axis, dir, amount)
worldedit.cuboid_linear_expand(name, axis, -dir, rev_amount)
worldedit.marker_update(name)
return true, "Region expanded by " .. (amount + rev_amount) .. " nodes"
end,
}
)
minetest.register_chatcommand("/contract", {
params = "[+|-]<x|y|z|?|up|down|left|right|front|back> <amount> [reverse-amount]",
description = "contract the selection in one or two directions at once",
privs = {worldedit=true},
func = function(name, param)
local find, _, sign, direction, amount,
rev_amount = param:find("([+-]?)([%?%l]+)%s*(%d+)%s*(%d*)")
if find == nil then
worldedit.player_notify(name, "invalid use: " .. param)
return
end
if worldedit.pos1[name] == nil or worldedit.pos2[name] == nil then
worldedit.player_notify(name,
"Undefined region. Region must be defined beforehand.")
return
end
local absolute = direction:find("[xyz?]")
local dir, axis
if rev_amount == "" then
rev_amount = 0
end
if absolute == nil then
axis, dir = worldedit.translate_direction(name, direction)
if axis == nil or dir == nil then
return false, "Invalid if looking straight up or down"
end
else
if direction == "?" then
axis, dir = worldedit.player_axis(name)
else
axis = direction
dir = 1
end
end
if sign == "-" then
dir = -dir
end
worldedit.cuboid_linear_expand(name, axis, dir, -amount)
worldedit.cuboid_linear_expand(name, axis, -dir, -rev_amount)
worldedit.marker_update(name)
return true, "Region contracted by " .. (amount + rev_amount) .. " nodes"
end,
}
)

0
worldedit_commands/depends.txt Normal file → Executable file
View File

File diff suppressed because it is too large Load Diff

View File

@ -1,25 +1,6 @@
worldedit.marker1 = {} worldedit.marker1 = {}
worldedit.marker2 = {} worldedit.marker2 = {}
worldedit.marker = {} worldedit.marker_region = {}
--wip: use this as a huge entity to make a full worldedit region box
minetest.register_entity(":worldedit:region_cube", {
initial_properties = {
visual = "upright_sprite",
visual_size = {x=1.1, y=1.1},
textures = {"worldedit_pos1.png"},
visual_size = {x=10, y=10},
physical = false,
},
on_step = function(self, dtime)
if self.active == nil then
self.object:remove()
end
end,
on_punch = function(self, hitter)
--wip: remove the entire region marker
end,
})
--marks worldedit region position 1 --marks worldedit region position 1
worldedit.mark_pos1 = function(name) worldedit.mark_pos1 = function(name)
@ -37,11 +18,11 @@ worldedit.mark_pos1 = function(name)
if pos1 ~= nil then if pos1 ~= nil then
--add marker --add marker
worldedit.marker1[name] = minetest.add_entity(pos1, "worldedit:pos1") worldedit.marker1[name] = minetest.add_entity(pos1, "worldedit:pos1")
worldedit.marker1[name]:get_luaentity().active = true if worldedit.marker1[name] ~= nil then
if pos2 ~= nil then --region defined worldedit.marker1[name]:get_luaentity().player_name = name
worldedit.mark_region(pos1, pos2)
end end
end end
worldedit.mark_region(name)
end end
--marks worldedit region position 2 --marks worldedit region position 2
@ -60,23 +41,73 @@ worldedit.mark_pos2 = function(name)
if pos2 ~= nil then if pos2 ~= nil then
--add marker --add marker
worldedit.marker2[name] = minetest.add_entity(pos2, "worldedit:pos2") worldedit.marker2[name] = minetest.add_entity(pos2, "worldedit:pos2")
worldedit.marker2[name]:get_luaentity().active = true if worldedit.marker2[name] ~= nil then
if pos1 ~= nil then --region defined worldedit.marker2[name]:get_luaentity().player_name = name
worldedit.mark_region(pos1, pos2)
end end
end end
worldedit.mark_region(name)
end end
worldedit.mark_region = function(pos1, pos2) worldedit.mark_region = function(name)
--make area stay loaded local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
if worldedit.marker[name] ~= nil then --marker already exists if worldedit.marker_region[name] ~= nil then --marker already exists
--wip: remove markers --wip: make the area stay loaded somehow
for _, entity in ipairs(worldedit.marker_region[name]) do
entity:remove()
end
worldedit.marker_region[name] = nil
end end
if pos1 ~= nil and pos2 ~= nil then if pos1 ~= nil and pos2 ~= nil then
--wip: place markers local pos1, pos2 = worldedit.sort_pos(pos1, pos2)
local vec = vector.subtract(pos2, pos1)
local maxside = math.max(vec.x, math.max(vec.y, vec.z))
local limit = tonumber(minetest.setting_get("active_object_send_range_blocks")) * 16
if maxside > limit * 1.5 then
-- The client likely won't be able to see the plane markers as intended anyway,
-- thus don't place them and also don't load the area into memory
return
end
local thickness = 0.2
local sizex, sizey, sizez = (1 + pos2.x - pos1.x) / 2, (1 + pos2.y - pos1.y) / 2, (1 + pos2.z - pos1.z) / 2
--make area stay loaded
local manip = minetest.get_voxel_manip()
manip:read_from_map(pos1, pos2)
local markers = {}
--XY plane markers
for _, z in ipairs({pos1.z - 0.5, pos2.z + 0.5}) do
local marker = minetest.add_entity({x=pos1.x + sizex - 0.5, y=pos1.y + sizey - 0.5, z=z}, "worldedit:region_cube")
if marker ~= nil then
marker:set_properties({
visual_size={x=sizex * 2, y=sizey * 2},
collisionbox = {-sizex, -sizey, -thickness, sizex, sizey, thickness},
})
marker:get_luaentity().player_name = name
table.insert(markers, marker)
end
end
--YZ plane markers
for _, x in ipairs({pos1.x - 0.5, pos2.x + 0.5}) do
local marker = minetest.add_entity({x=x, y=pos1.y + sizey - 0.5, z=pos1.z + sizez - 0.5}, "worldedit:region_cube")
if marker ~= nil then
marker:set_properties({
visual_size={x=sizez * 2, y=sizey * 2},
collisionbox = {-thickness, -sizey, -sizez, thickness, sizey, sizez},
})
marker:setyaw(math.pi / 2)
marker:get_luaentity().player_name = name
table.insert(markers, marker)
end
end
worldedit.marker_region[name] = markers
end end
end end
@ -91,14 +122,13 @@ minetest.register_entity(":worldedit:pos1", {
physical = false, physical = false,
}, },
on_step = function(self, dtime) on_step = function(self, dtime)
if self.active == nil then if worldedit.marker1[self.player_name] == nil then
self.object:remove() self.object:remove()
end end
end, end,
on_punch = function(self, hitter) on_punch = function(self, hitter)
self.object:remove() self.object:remove()
local name = hitter:get_player_name() worldedit.marker1[self.player_name] = nil
worldedit.marker1[name] = nil
end, end,
}) })
@ -113,13 +143,39 @@ minetest.register_entity(":worldedit:pos2", {
physical = false, physical = false,
}, },
on_step = function(self, dtime) on_step = function(self, dtime)
if self.active == nil then if worldedit.marker2[self.player_name] == nil then
self.object:remove() self.object:remove()
end end
end, end,
on_punch = function(self, hitter) on_punch = function(self, hitter)
self.object:remove() self.object:remove()
local name = hitter:get_player_name() worldedit.marker2[self.player_name] = nil
worldedit.marker2[name] = nil
end, end,
}) })
minetest.register_entity(":worldedit:region_cube", {
initial_properties = {
visual = "upright_sprite",
visual_size = {x=1.1, y=1.1},
textures = {"worldedit_cube.png"},
visual_size = {x=10, y=10},
physical = false,
},
on_step = function(self, dtime)
if worldedit.marker_region[self.player_name] == nil then
self.object:remove()
return
end
end,
on_punch = function(self, hitter)
local markers = worldedit.marker_region[self.player_name]
if not markers then
return
end
for _, entity in ipairs(markers) do
entity:remove()
end
worldedit.marker_region[self.player_name] = nil
end,
})

View File

@ -0,0 +1,65 @@
local safe_region_callback = {}
local safe_region_param = {}
local function check_region(name, param)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name] --obtain positions
if pos1 == nil or pos2 == nil then
worldedit.player_notify(name, "no region selected")
return nil
end
return worldedit.volume(pos1, pos2)
end
--`callback` is a callback to run when the user confirms
--`nodes_needed` is a function accepting `param`, `pos1`, and `pos2` to calculate the number of nodes needed
local function safe_region(callback, nodes_needed)
--default node volume calculation
nodes_needed = nodes_needed or check_region
return function(name, param)
--check if the operation applies to a safe number of nodes
local count = nodes_needed(name, param)
if count == nil then return end --invalid command
if count < 10000 then
return callback(name, param)
end
--save callback to call later
safe_region_callback[name], safe_region_param[name] = callback, param
worldedit.player_notify(name, "WARNING: this operation could affect up to " .. count .. " nodes; type //y to continue or //n to cancel")
end
end
local function reset_pending(name)
safe_region_callback[name], safe_region_param[name] = nil, nil
end
minetest.register_chatcommand("/y", {
params = "",
description = "Confirm a pending operation",
func = function(name)
local callback, param = safe_region_callback[name], safe_region_param[name]
if not callback then
worldedit.player_notify(name, "no operation pending")
return
end
safe_region_callback[name], safe_region_param[name] = nil, nil --reset pending operation
callback(name, param)
end,
})
minetest.register_chatcommand("/n", {
params = "",
description = "Confirm a pending operation",
func = function(name)
if not safe_region_callback[name] then
worldedit.player_notify(name, "no operation pending")
return
end
safe_region_callback[name], safe_region_param[name] = nil, nil
end,
})
return safe_region, check_region, reset_pending

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 B

View File

@ -0,0 +1,24 @@
minetest.register_tool(":worldedit:wand", {
description = "WorldEdit Wand tool, Left-click to set 1st position, right-click to set 2nd",
inventory_image = "worldedit_wand.png",
stack_max = 1, -- there is no need to have more than one
liquids_pointable = true, -- ground with only water on can be selected as well
on_use = function(itemstack, placer, pointed_thing)
if placer ~= nil and pointed_thing ~= nil and pointed_thing.type == "node" then
local name = placer:get_player_name()
worldedit.pos1[name] = pointed_thing.under
worldedit.mark_pos1(name)
end
return itemstack -- nothing consumed, nothing changed
end,
on_place = function(itemstack, placer, pointed_thing) -- Left Click
if placer ~= nil and pointed_thing ~= nil and pointed_thing.type == "node" then
local name = placer:get_player_name()
worldedit.pos2[name] = pointed_thing.under
worldedit.mark_pos2(name)
end
return itemstack -- nothing consumed, nothing changed
end,
})

View File

@ -0,0 +1,6 @@
worldedit
worldedit_commands
unified_inventory?
inventory_plus?
sfinv?
creative?

768
worldedit_gui/functionality.lua Executable file
View File

@ -0,0 +1,768 @@
--saved state for each player
local gui_nodename1 = {} --mapping of player names to node names (arbitrary strings may also appear as values)
local gui_nodename2 = {} --mapping of player names to node names (arbitrary strings may also appear as values)
local gui_axis1 = {} --mapping of player names to axes (one of 1, 2, 3, or 4, representing the axes in the `axis_indices` table below)
local gui_axis2 = {} --mapping of player names to axes (one of 1, 2, 3, or 4, representing the axes in the `axis_indices` table below)
local gui_distance1 = {} --mapping of player names to a distance (arbitrary strings may also appear as values)
local gui_distance2 = {} --mapping of player names to a distance (arbitrary strings may also appear as values)
local gui_distance3 = {} --mapping of player names to a distance (arbitrary strings may also appear as values)
local gui_count1 = {} --mapping of player names to a quantity (arbitrary strings may also appear as values)
local gui_count2 = {} --mapping of player names to a quantity (arbitrary strings may also appear as values)
local gui_count3 = {} --mapping of player names to a quantity (arbitrary strings may also appear as values)
local gui_angle = {} --mapping of player names to an angle (one of 90, 180, 270, representing the angle in degrees clockwise)
local gui_filename = {} --mapping of player names to file names (arbitrary strings may also appear as values)
local gui_formspec = {} --mapping of player names to formspecs
local gui_code = {} --mapping of player names to formspecs
--set default values
setmetatable(gui_nodename1, {__index = function() return "Cobblestone" end})
setmetatable(gui_nodename2, {__index = function() return "Stone" end})
setmetatable(gui_axis1, {__index = function() return 4 end})
setmetatable(gui_axis2, {__index = function() return 1 end})
setmetatable(gui_distance1, {__index = function() return "10" end})
setmetatable(gui_distance2, {__index = function() return "5" end})
setmetatable(gui_distance3, {__index = function() return "2" end})
setmetatable(gui_count1, {__index = function() return "3" end})
setmetatable(gui_count2, {__index = function() return "6" end})
setmetatable(gui_count3, {__index = function() return "4" end})
setmetatable(gui_angle, {__index = function() return 90 end})
setmetatable(gui_filename, {__index = function() return "building" end})
setmetatable(gui_formspec, {__index = function() return "size[5,5]\nlabel[0,0;Hello, world!]" end})
setmetatable(gui_code, {__index = function() return "minetest.chat_send_player(\"singleplayer\", \"Hello, world!\")" end})
local axis_indices = {["X axis"]=1, ["Y axis"]=2, ["Z axis"]=3, ["Look direction"]=4}
local axis_values = {"x", "y", "z", "?"}
setmetatable(axis_indices, {__index = function () return 4 end})
setmetatable(axis_values, {__index = function () return "?" end})
local angle_indices = {["90 degrees"]=1, ["180 degrees"]=2, ["270 degrees"]=3}
local angle_values = {90, 180, 270}
setmetatable(angle_indices, {__index = function () return 1 end})
setmetatable(angle_values, {__index = function () return 90 end})
-- given multiple sets of privileges, produces a single set of privs that would have the same effect as requiring all of them at the same time
local combine_privs = function(...)
local result = {}
for i, privs in ipairs({...}) do
for name, value in pairs(privs) do
if result[name] ~= nil and result[name] ~= value then --the priv must be both true and false, which can never happen
return {__fake_priv_that_nobody_has__=true} --privilege table that can never be satisfied
end
result[name] = value
end
end
return result
end
-- display node (or unknown_node image otherwise) at specified pos in formspec
local formspec_node = function(pos, nodename)
return nodename and string.format("item_image[%s;1,1;%s]", pos, nodename)
or string.format("image[%s;1,1;worldedit_gui_unknown.png]", pos)
end
-- two further priv helpers
local function we_privs(command)
return minetest.chatcommands["/" .. command].privs
end
local function combine_we_privs(list)
local args = {}
for _, t in ipairs(list) do
table.insert(args, we_privs(t))
end
return combine_privs(unpack(args))
end
worldedit.register_gui_function("worldedit_gui_about", {
name = "About",
privs = {interact=true},
on_select = function(name)
minetest.chatcommands["/about"].func(name, "")
end,
})
worldedit.register_gui_function("worldedit_gui_inspect", {
name = "Toggle Inspect",
privs = we_privs("inspect"),
on_select = function(name)
minetest.chatcommands["/inspect"].func(name, worldedit.inspect[name] and "disable" or "enable")
end,
})
worldedit.register_gui_function("worldedit_gui_region", {
name = "Get/Set Region",
privs = combine_we_privs({"p", "pos1", "pos2", "reset", "mark", "unmark", "volume", "fixedpos"}),
get_formspec = function(name)
local pos1, pos2 = worldedit.pos1[name], worldedit.pos2[name]
return "size[9,7]" .. worldedit.get_formspec_header("worldedit_gui_region") ..
"button_exit[0,1;3,0.8;worldedit_gui_p_get;Get Positions]" ..
"button_exit[3,1;3,0.8;worldedit_gui_p_set1;Choose Position 1]" ..
"button_exit[6,1;3,0.8;worldedit_gui_p_set2;Choose Position 2]" ..
"button_exit[0,2;3,0.8;worldedit_gui_pos1;Position 1 Here]" ..
"button_exit[3,2;3,0.8;worldedit_gui_pos2;Position 2 Here]" ..
"button_exit[6,2;3,0.8;worldedit_gui_reset;Reset Region]" ..
"button_exit[0,3;3,0.8;worldedit_gui_mark;Mark Region]" ..
"button_exit[3,3;3,0.8;worldedit_gui_unmark;Unmark Region]" ..
"button_exit[6,3;3,0.8;worldedit_gui_volume;Region Volume]" ..
"label[0,4.7;Position 1]" ..
string.format("field[2,5;1.5,0.8;worldedit_gui_fixedpos_pos1x;X ;%s]", pos1 and pos1.x or "") ..
string.format("field[3.5,5;1.5,0.8;worldedit_gui_fixedpos_pos1y;Y ;%s]", pos1 and pos1.y or "") ..
string.format("field[5,5;1.5,0.8;worldedit_gui_fixedpos_pos1z;Z ;%s]", pos1 and pos1.z or "") ..
"button_exit[6.5,4.68;2.5,0.8;worldedit_gui_fixedpos_pos1_submit;Set Position 1]" ..
"label[0,6.2;Position 2]" ..
string.format("field[2,6.5;1.5,0.8;worldedit_gui_fixedpos_pos2x;X ;%s]", pos2 and pos2.x or "") ..
string.format("field[3.5,6.5;1.5,0.8;worldedit_gui_fixedpos_pos2y;Y ;%s]", pos2 and pos2.y or "") ..
string.format("field[5,6.5;1.5,0.8;worldedit_gui_fixedpos_pos2z;Z ;%s]", pos2 and pos2.z or "") ..
"button_exit[6.5,6.18;2.5,0.8;worldedit_gui_fixedpos_pos2_submit;Set Position 2]"
end,
})
worldedit.register_gui_handler("worldedit_gui_region", function(name, fields)
if fields.worldedit_gui_p_get then
minetest.chatcommands["/p"].func(name, "get")
return true
elseif fields.worldedit_gui_p_set1 then
minetest.chatcommands["/p"].func(name, "set1")
return true
elseif fields.worldedit_gui_p_set2 then
minetest.chatcommands["/p"].func(name, "set2")
return true
elseif fields.worldedit_gui_pos1 then
minetest.chatcommands["/pos1"].func(name, "")
worldedit.show_page(name, "worldedit_gui_region")
return true
elseif fields.worldedit_gui_pos2 then
minetest.chatcommands["/pos2"].func(name, "")
worldedit.show_page(name, "worldedit_gui_region")
return true
elseif fields.worldedit_gui_reset then
minetest.chatcommands["/reset"].func(name, "")
worldedit.show_page(name, "worldedit_gui_region")
return true
elseif fields.worldedit_gui_mark then
minetest.chatcommands["/mark"].func(name, "")
worldedit.show_page(name, "worldedit_gui_region")
return true
elseif fields.worldedit_gui_unmark then
minetest.chatcommands["/unmark"].func(name, "")
worldedit.show_page(name, "worldedit_gui_region")
return true
elseif fields.worldedit_gui_volume then
minetest.chatcommands["/volume"].func(name, "")
worldedit.show_page(name, "worldedit_gui_region")
return true
elseif fields.worldedit_gui_fixedpos_pos1_submit then
minetest.chatcommands["/fixedpos"].func(name, string.format("set1 %s %s %s",
tostring(fields.worldedit_gui_fixedpos_pos1x),
tostring(fields.worldedit_gui_fixedpos_pos1y),
tostring(fields.worldedit_gui_fixedpos_pos1z)))
worldedit.show_page(name, "worldedit_gui_region")
return true
elseif fields.worldedit_gui_fixedpos_pos2_submit then
minetest.chatcommands["/fixedpos"].func(name, string.format("set2 %s %s %s",
tostring(fields.worldedit_gui_fixedpos_pos2x),
tostring(fields.worldedit_gui_fixedpos_pos2y),
tostring(fields.worldedit_gui_fixedpos_pos2z)))
worldedit.show_page(name, "worldedit_gui_region")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_set", {
name = "Set Nodes",
privs = we_privs("set"),
get_formspec = function(name)
local node = gui_nodename1[name]
local nodename = worldedit.normalize_nodename(node)
return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_set") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_set_node;Name;%s]", minetest.formspec_escape(node)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_set_search;Search]" ..
formspec_node("5.5,1.1", nodename) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_set_submit;Set Nodes]"
end,
})
worldedit.register_gui_handler("worldedit_gui_set", function(name, fields)
if fields.worldedit_gui_set_search or fields.worldedit_gui_set_submit then
gui_nodename1[name] = tostring(fields.worldedit_gui_set_node)
worldedit.show_page(name, "worldedit_gui_set")
if fields.worldedit_gui_set_submit then
local n = worldedit.normalize_nodename(gui_nodename1[name])
if n then
minetest.chatcommands["/set"].func(name, n)
end
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_replace", {
name = "Replace Nodes",
privs = combine_we_privs({"replace", "replaceinverse"}),
get_formspec = function(name)
local search, replace = gui_nodename1[name], gui_nodename2[name]
local search_nodename, replace_nodename = worldedit.normalize_nodename(search), worldedit.normalize_nodename(replace)
return "size[6.5,4]" .. worldedit.get_formspec_header("worldedit_gui_replace") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_replace_search;Name;%s]", minetest.formspec_escape(search)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_replace_search_search;Search]" ..
formspec_node("5.5,1.1", search_nodename) ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_replace_replace;Name;%s]", minetest.formspec_escape(replace)) ..
"button[4,2.18;1.5,0.8;worldedit_gui_replace_replace_search;Search]" ..
formspec_node("5.5,2.1", replace_nodename) ..
"button_exit[0,3.5;3,0.8;worldedit_gui_replace_submit;Replace Nodes]" ..
"button_exit[3.5,3.5;3,0.8;worldedit_gui_replace_submit_inverse;Replace Inverse]"
end,
})
worldedit.register_gui_handler("worldedit_gui_replace", function(name, fields)
if fields.worldedit_gui_replace_search_search or fields.worldedit_gui_replace_replace_search
or fields.worldedit_gui_replace_submit or fields.worldedit_gui_replace_submit_inverse then
gui_nodename1[name] = tostring(fields.worldedit_gui_replace_search)
gui_nodename2[name] = tostring(fields.worldedit_gui_replace_replace)
worldedit.show_page(name, "worldedit_gui_replace")
local submit = nil
if fields.worldedit_gui_replace_submit then
submit = "replace"
elseif fields.worldedit_gui_replace_submit_inverse then
submit = "replaceinverse"
end
if submit then
local n1 = worldedit.normalize_nodename(gui_nodename1[name])
local n2 = worldedit.normalize_nodename(gui_nodename2[name])
if n1 and n2 then
minetest.chatcommands["/"..submit].func(name, string.format("%s %s", n1, n2))
end
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_sphere_dome", {
name = "Sphere/Dome",
privs = combine_we_privs({"hollowsphere", "sphere", "hollowdome", "dome"}),
get_formspec = function(name)
local node, radius = gui_nodename1[name], gui_distance2[name]
local nodename = worldedit.normalize_nodename(node)
return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_sphere_dome") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_sphere_dome_node;Name;%s]", minetest.formspec_escape(node)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_sphere_dome_search;Search]" ..
formspec_node("5.5,1.1", nodename) ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_sphere_dome_radius;Radius;%s]", minetest.formspec_escape(radius)) ..
"button_exit[0,3.5;3,0.8;worldedit_gui_sphere_dome_submit_hollow;Hollow Sphere]" ..
"button_exit[3.5,3.5;3,0.8;worldedit_gui_sphere_dome_submit_solid;Solid Sphere]" ..
"button_exit[0,4.5;3,0.8;worldedit_gui_sphere_dome_submit_hollow_dome;Hollow Dome]" ..
"button_exit[3.5,4.5;3,0.8;worldedit_gui_sphere_dome_submit_solid_dome;Solid Dome]"
end,
})
worldedit.register_gui_handler("worldedit_gui_sphere_dome", function(name, fields)
if fields.worldedit_gui_sphere_dome_search
or fields.worldedit_gui_sphere_dome_submit_hollow or fields.worldedit_gui_sphere_dome_submit_solid
or fields.worldedit_gui_sphere_dome_submit_hollow_dome or fields.worldedit_gui_sphere_dome_submit_solid_dome then
gui_nodename1[name] = tostring(fields.worldedit_gui_sphere_dome_node)
gui_distance2[name] = tostring(fields.worldedit_gui_sphere_dome_radius)
worldedit.show_page(name, "worldedit_gui_sphere_dome")
local submit = nil
if fields.worldedit_gui_sphere_dome_submit_hollow then
submit = "hollowsphere"
elseif fields.worldedit_gui_sphere_dome_submit_solid then
submit = "sphere"
elseif fields.worldedit_gui_sphere_dome_submit_hollow_dome then
submit = "hollowdome"
elseif fields.worldedit_gui_sphere_dome_submit_solid_dome then
submit = "dome"
end
if submit then
local n = worldedit.normalize_nodename(gui_nodename1[name])
if n then
minetest.chatcommands["/"..submit].func(name, string.format("%s %s", gui_distance2[name], n))
end
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_cylinder", {
name = "Cylinder",
privs = combine_we_privs({"hollowcylinder", "cylinder"}),
get_formspec = function(name)
local node, axis, length, radius = gui_nodename1[name], gui_axis1[name], gui_distance1[name], gui_distance2[name]
local nodename = worldedit.normalize_nodename(node)
return "size[6.5,5]" .. worldedit.get_formspec_header("worldedit_gui_cylinder") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_cylinder_node;Name;%s]", minetest.formspec_escape(node)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_cylinder_search;Search]" ..
formspec_node("5.5,1.1", nodename) ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_cylinder_length;Length;%s]", minetest.formspec_escape(length)) ..
string.format("dropdown[4,2.18;2.5;worldedit_gui_cylinder_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
string.format("field[0.5,3.5;4,0.8;worldedit_gui_cylinder_radius;Radius;%s]", minetest.formspec_escape(radius)) ..
"button_exit[0,4.5;3,0.8;worldedit_gui_cylinder_submit_hollow;Hollow Cylinder]" ..
"button_exit[3.5,4.5;3,0.8;worldedit_gui_cylinder_submit_solid;Solid Cylinder]"
end,
})
worldedit.register_gui_handler("worldedit_gui_cylinder", function(name, fields)
if fields.worldedit_gui_cylinder_search
or fields.worldedit_gui_cylinder_submit_hollow or fields.worldedit_gui_cylinder_submit_solid then
gui_nodename1[name] = tostring(fields.worldedit_gui_cylinder_node)
gui_axis1[name] = axis_indices[fields.worldedit_gui_cylinder_axis]
gui_distance1[name] = tostring(fields.worldedit_gui_cylinder_length)
gui_distance2[name] = tostring(fields.worldedit_gui_cylinder_radius)
worldedit.show_page(name, "worldedit_gui_cylinder")
local submit = nil
if fields.worldedit_gui_cylinder_submit_hollow then
submit = "hollowcylinder"
elseif fields.worldedit_gui_cylinder_submit_solid then
submit = "cylinder"
end
if submit then
local n = worldedit.normalize_nodename(gui_nodename1[name])
if n then
minetest.chatcommands["/"..submit].func(name, string.format("%s %s %s %s", axis_values[gui_axis1[name]], gui_distance1[name], gui_distance2[name], n))
end
end
return true
end
if fields.worldedit_gui_cylinder_axis then
gui_axis1[name] = axis_indices[fields.worldedit_gui_cylinder_axis]
worldedit.show_page(name, "worldedit_gui_cylinder")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_pyramid", {
name = "Pyramid",
privs = we_privs("pyramid"),
get_formspec = function(name)
local node, axis, length = gui_nodename1[name], gui_axis1[name], gui_distance1[name]
local nodename = worldedit.normalize_nodename(node)
return "size[6.5,4]" .. worldedit.get_formspec_header("worldedit_gui_pyramid") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_pyramid_node;Name;%s]", minetest.formspec_escape(node)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_pyramid_search;Search]" ..
formspec_node("5.5,1.1", nodename) ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_pyramid_length;Length;%s]", minetest.formspec_escape(length)) ..
string.format("dropdown[4,2.18;2.5;worldedit_gui_pyramid_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
"button_exit[0,3.5;3,0.8;worldedit_gui_pyramid_submit_hollow;Hollow Pyramid]" ..
"button_exit[3.5,3.5;3,0.8;worldedit_gui_pyramid_submit_solid;Solid Pyramid]"
end,
})
worldedit.register_gui_handler("worldedit_gui_pyramid", function(name, fields)
if fields.worldedit_gui_pyramid_search or fields.worldedit_gui_pyramid_submit_solid or fields.worldedit_gui_pyramid_submit_hollow or fields.worldedit_gui_pyramid_axis then
gui_nodename1[name] = tostring(fields.worldedit_gui_pyramid_node)
gui_axis1[name] = axis_indices[fields.worldedit_gui_pyramid_axis]
gui_distance1[name] = tostring(fields.worldedit_gui_pyramid_length)
worldedit.show_page(name, "worldedit_gui_pyramid")
local submit = nil
if fields.worldedit_gui_pyramid_submit_solid then
submit = "pyramid"
elseif fields.worldedit_gui_pyramid_submit_hollow then
submit = "hollowpyramid"
end
if submit then
local n = worldedit.normalize_nodename(gui_nodename1[name])
if n then
minetest.chatcommands["/"..submit].func(name, string.format("%s %s %s", axis_values[gui_axis1[name]], gui_distance1[name], n))
end
end
return true
end
if fields.worldedit_gui_pyramid_axis then
gui_axis1[name] = axis_indices[fields.worldedit_gui_pyramid_axis]
worldedit.show_page(name, "worldedit_gui_pyramid")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_spiral", {
name = "Spiral",
privs = we_privs("spiral"),
get_formspec = function(name)
local node, length, height, space = gui_nodename1[name], gui_distance1[name], gui_distance2[name], gui_distance3[name]
local nodename = worldedit.normalize_nodename(node)
return "size[6.5,6]" .. worldedit.get_formspec_header("worldedit_gui_spiral") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_spiral_node;Name;%s]", minetest.formspec_escape(node)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_spiral_search;Search]" ..
formspec_node("5.5,1.1", nodename) ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_spiral_length;Side Length;%s]", minetest.formspec_escape(length)) ..
string.format("field[0.5,3.5;4,0.8;worldedit_gui_spiral_height;Height;%s]", minetest.formspec_escape(height)) ..
string.format("field[0.5,4.5;4,0.8;worldedit_gui_spiral_space;Wall Spacing;%s]", minetest.formspec_escape(space)) ..
"button_exit[0,5.5;3,0.8;worldedit_gui_spiral_submit;Spiral]"
end,
})
worldedit.register_gui_handler("worldedit_gui_spiral", function(name, fields)
if fields.worldedit_gui_spiral_search or fields.worldedit_gui_spiral_submit then
gui_nodename1[name] = fields.worldedit_gui_spiral_node
gui_distance1[name] = tostring(fields.worldedit_gui_spiral_length)
gui_distance2[name] = tostring(fields.worldedit_gui_spiral_height)
gui_distance3[name] = tostring(fields.worldedit_gui_spiral_space)
worldedit.show_page(name, "worldedit_gui_spiral")
if fields.worldedit_gui_spiral_submit then
local n = worldedit.normalize_nodename(gui_nodename1[name])
if n then
minetest.chatcommands["/spiral"].func(name, string.format("%s %s %s %s", gui_distance1[name], gui_distance2[name], gui_distance3[name], n))
end
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_copy_move", {
name = "Copy/Move",
privs = combine_we_privs({"copy", "move"}),
get_formspec = function(name)
local axis = gui_axis1[name] or 4
local amount = gui_distance1[name] or "10"
return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_copy_move") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_copy_move_amount;Amount;%s]", minetest.formspec_escape(amount)) ..
string.format("dropdown[4,1.18;2.5;worldedit_gui_copy_move_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_copy_move_copy;Copy Region]" ..
"button_exit[3.5,2.5;3,0.8;worldedit_gui_copy_move_move;Move Region]"
end,
})
worldedit.register_gui_handler("worldedit_gui_copy_move", function(name, fields)
if fields.worldedit_gui_copy_move_copy or fields.worldedit_gui_copy_move_move then
gui_axis1[name] = axis_indices[fields.worldedit_gui_copy_move_axis] or 4
gui_distance1[name] = tostring(fields.worldedit_gui_copy_move_amount)
worldedit.show_page(name, "worldedit_gui_copy_move")
if fields.worldedit_gui_copy_move_copy then
minetest.chatcommands["/copy"].func(name, string.format("%s %s", axis_values[gui_axis1[name]], gui_distance1[name]))
else --fields.worldedit_gui_copy_move_move
minetest.chatcommands["/move"].func(name, string.format("%s %s", axis_values[gui_axis1[name]], gui_distance1[name]))
end
return true
end
if fields.worldedit_gui_copy_move_axis then
gui_axis1[name] = axis_indices[fields.worldedit_gui_copy_move_axis] or 4
worldedit.show_page(name, "worldedit_gui_copy_move")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_stack", {
name = "Stack",
privs = we_privs("stack"),
get_formspec = function(name)
local axis, count = gui_axis1[name], gui_count1[name]
return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_stack") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_stack_count;Count;%s]", minetest.formspec_escape(count)) ..
string.format("dropdown[4,1.18;2.5;worldedit_gui_stack_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_stack_submit;Stack]"
end,
})
worldedit.register_gui_handler("worldedit_gui_stack", function(name, fields)
if fields.worldedit_gui_stack_submit then
gui_axis1[name] = axis_indices[fields.worldedit_gui_stack_axis]
gui_count1[name] = tostring(fields.worldedit_gui_stack_count)
worldedit.show_page(name, "worldedit_gui_stack")
minetest.chatcommands["/stack"].func(name, string.format("%s %s", axis_values[gui_axis1[name]], gui_count1[name]))
return true
end
if fields.worldedit_gui_stack_axis then
gui_axis1[name] = axis_indices[fields.worldedit_gui_stack_axis]
worldedit.show_page(name, "worldedit_gui_stack")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_stretch", {
name = "Stretch",
privs = we_privs("stretch"),
get_formspec = function(name)
local stretchx, stretchy, stretchz = gui_count1[name], gui_count2[name], gui_count3[name]
return "size[5,5]" .. worldedit.get_formspec_header("worldedit_gui_stretch") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_stretch_x;Stretch X;%s]", minetest.formspec_escape(stretchx)) ..
string.format("field[0.5,2.5;4,0.8;worldedit_gui_stretch_y;Stretch Y;%s]", minetest.formspec_escape(stretchy)) ..
string.format("field[0.5,3.5;4,0.8;worldedit_gui_stretch_z;Stretch Z;%s]", minetest.formspec_escape(stretchz)) ..
"button_exit[0,4.5;3,0.8;worldedit_gui_stretch_submit;Stretch]"
end,
})
worldedit.register_gui_handler("worldedit_gui_stretch", function(name, fields)
if fields.worldedit_gui_stretch_submit then
gui_count1[name] = tostring(fields.worldedit_gui_stretch_x)
gui_count2[name] = tostring(fields.worldedit_gui_stretch_y)
gui_count3[name] = tostring(fields.worldedit_gui_stretch_z)
worldedit.show_page(name, "worldedit_gui_stretch")
minetest.chatcommands["/stretch"].func(name, string.format("%s %s %s", gui_count1[name], gui_count2[name], gui_count3[name]))
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_transpose", {
name = "Transpose",
privs = we_privs("transpose"),
get_formspec = function(name)
local axis1, axis2 = gui_axis1[name], gui_axis2[name]
return "size[5.5,3]" .. worldedit.get_formspec_header("worldedit_gui_transpose") ..
string.format("dropdown[0,1;2.5;worldedit_gui_transpose_axis1;X axis,Y axis,Z axis,Look direction;%d]", axis1) ..
string.format("dropdown[3,1;2.5;worldedit_gui_transpose_axis2;X axis,Y axis,Z axis,Look direction;%d]", axis2) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_transpose_submit;Transpose]"
end,
})
worldedit.register_gui_handler("worldedit_gui_transpose", function(name, fields)
if fields.worldedit_gui_transpose_submit then
gui_axis1[name] = axis_indices[fields.worldedit_gui_transpose_axis1]
worldedit.show_page(name, "worldedit_gui_transpose")
minetest.chatcommands["/transpose"].func(name, string.format("%s %s", axis_values[gui_axis1[name]], axis_values[gui_axis2[name]]))
return true
end
if fields.worldedit_gui_transpose_axis1 then
gui_axis1[name] = axis_indices[fields.worldedit_gui_transpose_axis1]
worldedit.show_page(name, "worldedit_gui_transpose")
return true
end
if fields.worldedit_gui_transpose_axis2 then
gui_axis2[name] = axis_indices[fields.worldedit_gui_transpose_axis2]
worldedit.show_page(name, "worldedit_gui_transpose")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_flip", {
name = "Flip",
privs = we_privs("flip"),
get_formspec = function(name)
local axis = gui_axis1[name]
return "size[5,3]" .. worldedit.get_formspec_header("worldedit_gui_flip") ..
string.format("dropdown[0,1;2.5;worldedit_gui_flip_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_flip_submit;Flip]"
end,
})
worldedit.register_gui_handler("worldedit_gui_flip", function(name, fields)
if fields.worldedit_gui_flip_submit then
gui_axis1[name] = axis_indices[fields.worldedit_gui_flip_axis]
worldedit.show_page(name, "worldedit_gui_flip")
minetest.chatcommands["/flip"].func(name, axis_values[gui_axis1[name]])
return true
end
if fields.worldedit_gui_flip_axis then
gui_axis1[name] = axis_indices[fields.worldedit_gui_flip_axis]
worldedit.show_page(name, "worldedit_gui_flip")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_rotate", {
name = "Rotate",
privs = we_privs("rotate"),
get_formspec = function(name)
local axis, angle = gui_axis1[name], gui_angle[name]
return "size[5.5,3]" .. worldedit.get_formspec_header("worldedit_gui_rotate") ..
string.format("dropdown[0,1;2.5;worldedit_gui_rotate_angle;90 degrees,180 degrees,270 degrees;%s]", angle) ..
string.format("dropdown[3,1;2.5;worldedit_gui_rotate_axis;X axis,Y axis,Z axis,Look direction;%d]", axis) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_rotate_submit;Rotate]"
end,
})
worldedit.register_gui_handler("worldedit_gui_rotate", function(name, fields)
if fields.worldedit_gui_rotate_submit then
gui_axis1[name] = axis_indices[fields.worldedit_gui_rotate_axis]
gui_angle[name] = angle_indices[fields.worldedit_gui_rotate_angle]
worldedit.show_page(name, "worldedit_gui_rotate")
minetest.chatcommands["/rotate"].func(name, string.format("%s %s", axis_values[gui_axis1[name]], angle_values[gui_angle[name]]))
return true
end
if fields.worldedit_gui_rotate_axis then
gui_axis1[name] = axis_indices[fields.worldedit_gui_rotate_axis]
worldedit.show_page(name, "worldedit_gui_rotate")
return true
end
if fields.worldedit_gui_rotate_angle then
gui_angle[name] = angle_indices[fields.worldedit_gui_rotate_angle]
worldedit.show_page(name, "worldedit_gui_rotate")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_orient", {
name = "Orient",
privs = we_privs("orient"),
get_formspec = function(name)
local angle = gui_angle[name]
return "size[5,3]" .. worldedit.get_formspec_header("worldedit_gui_orient") ..
string.format("dropdown[0,1;2.5;worldedit_gui_orient_angle;90 degrees,180 degrees,270 degrees;%s]", angle) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_orient_submit;Orient]"
end,
})
worldedit.register_gui_handler("worldedit_gui_orient", function(name, fields)
if fields.worldedit_gui_orient_submit then
gui_angle[name] = angle_indices[fields.worldedit_gui_orient_angle]
worldedit.show_page(name, "worldedit_gui_orient")
minetest.chatcommands["/orient"].func(name, tostring(angle_values[gui_angle[name]]))
return true
end
if fields.worldedit_gui_orient_angle then
gui_angle[name] = angle_indices[fields.worldedit_gui_orient_angle]
worldedit.show_page(name, "worldedit_gui_orient")
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_fixlight", {
name = "Fix Lighting",
privs = we_privs("fixlight"),
on_select = function(name)
minetest.chatcommands["/fixlight"].func(name, "")
end,
})
worldedit.register_gui_function("worldedit_gui_hide", {
name = "Hide Region",
privs = we_privs("hide"),
on_select = function(name)
minetest.chatcommands["/hide"].func(name, "")
end,
})
worldedit.register_gui_function("worldedit_gui_suppress", {
name = "Suppress Nodes",
privs = we_privs("suppress"),
get_formspec = function(name)
local node = gui_nodename1[name]
local nodename = worldedit.normalize_nodename(node)
return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_suppress") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_suppress_node;Name;%s]", minetest.formspec_escape(node)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_suppress_search;Search]" ..
formspec_node("5.5,1.1", nodename) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_suppress_submit;Suppress Nodes]"
end,
})
worldedit.register_gui_handler("worldedit_gui_suppress", function(name, fields)
if fields.worldedit_gui_suppress_search or fields.worldedit_gui_suppress_submit then
gui_nodename1[name] = tostring(fields.worldedit_gui_suppress_node)
worldedit.show_page(name, "worldedit_gui_suppress")
if fields.worldedit_gui_suppress_submit then
local n = worldedit.normalize_nodename(gui_nodename1[name])
if n then
minetest.chatcommands["/suppress"].func(name, n)
end
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_highlight", {
name = "Highlight Nodes",
privs = we_privs("highlight"),
get_formspec = function(name)
local node = gui_nodename1[name]
local nodename = worldedit.normalize_nodename(node)
return "size[6.5,3]" .. worldedit.get_formspec_header("worldedit_gui_highlight") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_highlight_node;Name;%s]", minetest.formspec_escape(node)) ..
"button[4,1.18;1.5,0.8;worldedit_gui_highlight_search;Search]" ..
formspec_node("5.5,1.1", nodename) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_highlight_submit;Highlight Nodes]"
end,
})
worldedit.register_gui_handler("worldedit_gui_highlight", function(name, fields)
if fields.worldedit_gui_highlight_search or fields.worldedit_gui_highlight_submit then
gui_nodename1[name] = tostring(fields.worldedit_gui_highlight_node)
worldedit.show_page(name, "worldedit_gui_highlight")
if fields.worldedit_gui_highlight_submit then
local n = worldedit.normalize_nodename(gui_nodename1[name])
if n then
minetest.chatcommands["/highlight"].func(name, n)
end
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_restore", {
name = "Restore Region",
privs = we_privs("restore"),
on_select = function(name)
minetest.chatcommands["/restore"].func(name, "")
end,
})
worldedit.register_gui_function("worldedit_gui_save_load", {
name = "Save/Load",
privs = combine_we_privs({"save", "allocate", "load"}),
get_formspec = function(name)
local filename = gui_filename[name]
return "size[6,4]" .. worldedit.get_formspec_header("worldedit_gui_save_load") ..
string.format("field[0.5,1.5;4,0.8;worldedit_gui_save_filename;Filename;%s]", minetest.formspec_escape(filename)) ..
"button_exit[0,2.5;3,0.8;worldedit_gui_save_load_submit_save;Save]" ..
"button_exit[3,2.5;3,0.8;worldedit_gui_save_load_submit_allocate;Allocate]" ..
"button_exit[0,3.5;3,0.8;worldedit_gui_save_load_submit_load;Load]"
end,
})
worldedit.register_gui_handler("worldedit_gui_save_load", function(name, fields)
if fields.worldedit_gui_save_load_submit_save or fields.worldedit_gui_save_load_submit_allocate or fields.worldedit_gui_save_load_submit_load then
gui_filename[name] = tostring(fields.worldedit_gui_save_filename)
worldedit.show_page(name, "worldedit_gui_save_load")
if fields.worldedit_gui_save_load_submit_save then
minetest.chatcommands["/save"].func(name, gui_filename[name])
elseif fields.worldedit_gui_save_load_submit_allocate then
minetest.chatcommands["/allocate"].func(name, gui_filename[name])
else --fields.worldedit_gui_save_load_submit_load
minetest.chatcommands["/load"].func(name, gui_filename[name])
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_lua", {
name = "Run Lua", privs = minetest.chatcommands["/clearobjects"].privs,
privs = we_privs("lua"),
get_formspec = function(name)
local code = gui_code[name]
return "size[8,6.5]" .. worldedit.get_formspec_header("worldedit_gui_lua") ..
string.format("textarea[0.5,1;7.5,5.5;worldedit_gui_lua_code;Lua Code;%s]", minetest.formspec_escape(code)) ..
"button_exit[0,6;3,0.8;worldedit_gui_lua_run;Run Lua]" ..
"button_exit[5,6;3,0.8;worldedit_gui_lua_transform;Lua Transform]"
end,
})
worldedit.register_gui_handler("worldedit_gui_lua", function(name, fields)
if fields.worldedit_gui_lua_run or fields.worldedit_gui_lua_transform then
gui_code[name] = fields.worldedit_gui_lua_code
worldedit.show_page(name, "worldedit_gui_lua")
if fields.worldedit_gui_lua_run then
minetest.chatcommands["/lua"].func(name, gui_code[name])
else --fields.worldedit_gui_lua_transform
minetest.chatcommands["/luatransform"].func(name, gui_code[name])
end
return true
end
return false
end)
worldedit.register_gui_function("worldedit_gui_clearobjects", {
name = "Clear Objects",
privs = we_privs("clearobjects"),
on_select = function(name)
minetest.chatcommands["/clearobjects"].func(name, "")
end,
})

298
worldedit_gui/init.lua Executable file
View File

@ -0,0 +1,298 @@
worldedit = worldedit or {}
--[[
Example:
worldedit.register_gui_function("worldedit_gui_hollow_cylinder", {
name = "Make Hollow Cylinder",
privs = {worldedit=true},
get_formspec = function(name) return "some formspec here" end,
on_select = function(name) print(name .. " clicked the button!") end,
})
Use `nil` for the `options` parameter to unregister the function associated with the given identifier.
Use `nil` for the `get_formspec` field to denote that the function does not have its own screen.
The `privs` field may not be `nil`.
If the identifier is already registered to another function, it will be replaced by the new one.
The `on_select` function must not call `worldedit.show_page`
]]
worldedit.pages = {} --mapping of identifiers to options
local identifiers = {} --ordered list of identifiers
worldedit.register_gui_function = function(identifier, options)
if options.privs == nil or next(options.privs) == nil then
error("privs unset")
end
worldedit.pages[identifier] = options
table.insert(identifiers, identifier)
end
--[[
Example:
worldedit.register_gui_handler("worldedit_gui_hollow_cylinder", function(name, fields)
print(minetest.serialize(fields))
end)
]]
worldedit.register_gui_handler = function(identifier, handler)
local enabled = true
minetest.register_on_player_receive_fields(function(player, formname, fields)
if not enabled then return false end
enabled = false
minetest.after(0.2, function() enabled = true end)
local name = player:get_player_name()
--ensure the player has permission to perform the action
local entry = worldedit.pages[identifier]
if entry and minetest.check_player_privs(name, entry.privs) then
return handler(name, fields)
end
return false
end)
end
worldedit.get_formspec_header = function(identifier)
local entry = worldedit.pages[identifier] or {}
return "button[0,0;2,0.5;worldedit_gui;Back]" ..
string.format("label[2,0;WorldEdit GUI > %s]", entry.name or "")
end
local get_formspec = function(name, identifier)
if worldedit.pages[identifier] then
return worldedit.pages[identifier].get_formspec(name)
end
return worldedit.pages["worldedit_gui"].get_formspec(name) --default to showing main page if an unknown page is given
end
--implement worldedit.show_page(name, page) in different ways depending on the available APIs
if rawget(_G, "unified_inventory") then --unified inventory installed
local old_func = worldedit.register_gui_function
worldedit.register_gui_function = function(identifier, options)
old_func(identifier, options)
unified_inventory.register_page(identifier, {get_formspec=function(player) return {formspec=options.get_formspec(player:get_player_name())} end})
end
unified_inventory.register_button("worldedit_gui", {
type = "image",
image = "inventory_plus_worldedit_gui.png",
tooltip = "Worldedit GUI",
show_with = "worldedit", --Modiff MFF (Crabman 30/06/2015)
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if fields.worldedit_gui then --main page
worldedit.show_page(name, "worldedit_gui")
return true
elseif fields.worldedit_gui_exit then --return to original page
local player = minetest.get_player_by_name(name)
if player then
unified_inventory.set_inventory_formspec(player, "craft")
end
return true
end
return false
end)
worldedit.show_page = function(name, page)
local player = minetest.get_player_by_name(name)
if player then
player:set_inventory_formspec(get_formspec(name, page))
end
end
elseif rawget(_G, "inventory_plus") then --inventory++ installed
minetest.register_on_joinplayer(function(player)
local can_worldedit = minetest.check_player_privs(player:get_player_name(), {worldedit=true})
if can_worldedit then
inventory_plus.register_button(player, "worldedit_gui", "WorldEdit")
end
end)
--show the form when the button is pressed and hide it when done
local gui_player_formspecs = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if fields.worldedit_gui then --main page
gui_player_formspecs[name] = player:get_inventory_formspec()
worldedit.show_page(name, "worldedit_gui")
return true
elseif fields.worldedit_gui_exit then --return to original page
if gui_player_formspecs[name] then
inventory_plus.set_inventory_formspec(player, inventory_plus.get_formspec(player, "main"))
end
return true
end
return false
end)
worldedit.show_page = function(name, page)
local player = minetest.get_player_by_name(name)
if player then
inventory_plus.set_inventory_formspec(player, get_formspec(name, page))
end
end
elseif rawget(_G, "sfinv") then --sfinv installed (part of minetest_game since 0.4.15)
assert(sfinv.enabled)
local orig_get = sfinv.pages["sfinv:crafting"].get
sfinv.override_page("sfinv:crafting", {
get = function(self, player, context)
local can_worldedit = minetest.check_player_privs(player, {worldedit=true})
local fs = orig_get(self, player, context)
return fs .. (can_worldedit and "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]" or "")
end
})
--show the form when the button is pressed and hide it when done
minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.worldedit_gui then --main page
worldedit.show_page(player:get_player_name(), "worldedit_gui")
return true
elseif fields.worldedit_gui_exit then --return to original page
sfinv.set_page(player, "sfinv:crafting")
return true
end
return false
end)
worldedit.show_page = function(name, page)
local player = minetest.get_player_by_name(name)
if player then
player:set_inventory_formspec(get_formspec(name, page))
end
end
else --fallback button
-- FIXME: this is a huge clusterfuck and the back button is broken
local player_formspecs = {}
local update_main_formspec = function(name)
local formspec = player_formspecs[name]
if not formspec then
return
end
local player = minetest.get_player_by_name(name)
if not player then --this is in case the player signs off while the media is loading
return
end
if (minetest.check_player_privs(name, {creative=true}) or
minetest.setting_getbool("creative_mode")) and
creative then --creative is active, add button to modified formspec
local creative_formspec = player:get_inventory_formspec()
local tab_id = tonumber(creative_formspec:match("tabheader%[.-;(%d+)%;"))
if tab_id == 1 then
formspec = creative_formspec ..
"image_button[0,1;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]"
elseif not tab_id then
formspec = creative_formspec ..
"image_button[6,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]"
else
return
end
else
formspec = formspec .. "image_button[0,0;1,1;inventory_plus_worldedit_gui.png;worldedit_gui;]"
end
player:set_inventory_formspec(formspec)
end
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
minetest.after(1, function()
if minetest.get_player_by_name(name) then --ensure the player is still signed in
player_formspecs[name] = player:get_inventory_formspec()
minetest.after(0.01, function()
update_main_formspec(name)
end)
end
end)
end)
minetest.register_on_leaveplayer(function(player)
player_formspecs[player:get_player_name()] = nil
end)
local gui_player_formspecs = {}
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if fields.worldedit_gui then --main page
gui_player_formspecs[name] = player:get_inventory_formspec()
worldedit.show_page(name, "worldedit_gui")
return true
elseif fields.worldedit_gui_exit then --return to original page
if gui_player_formspecs[name] then
player:set_inventory_formspec(gui_player_formspecs[name])
end
return true
else --deal with creative_inventory setting the formspec on every single message
minetest.after(0.01,function()
update_main_formspec(name)
end)
return false --continue processing in creative inventory
end
end)
worldedit.show_page = function(name, page)
local player = minetest.get_player_by_name(name)
if player then
player:set_inventory_formspec(get_formspec(name, page))
end
end
end
worldedit.register_gui_function("worldedit_gui", {
name = "WorldEdit GUI",
privs = {interact=true},
get_formspec = function(name)
--create a form with all the buttons arranged in a grid
local buttons, x, y, index = {}, 0, 1, 0
local width, height = 3, 0.8
local columns = 5
for i, identifier in pairs(identifiers) do
if identifier ~= "worldedit_gui" then
local entry = worldedit.pages[identifier]
table.insert(buttons, string.format((entry.get_formspec and "button" or "button_exit") ..
"[%g,%g;%g,%g;%s;%s]", x, y, width, height, identifier, minetest.formspec_escape(entry.name)))
index, x = index + 1, x + width
if index == columns then --row is full
x, y = 0, y + height
index = 0
end
end
end
if index == 0 then --empty row
y = y - height
end
return string.format("size[%g,%g]", math.max(columns * width, 5), math.max(y + 0.5, 3)) ..
"button[0,0;2,0.5;worldedit_gui_exit;Back]" ..
"label[2,0;WorldEdit GUI]" ..
table.concat(buttons)
end,
})
worldedit.register_gui_handler("worldedit_gui", function(name, fields)
for identifier, entry in pairs(worldedit.pages) do --check for WorldEdit GUI main formspec button selection
if fields[identifier] and identifier ~= "worldedit_gui" then
--ensure player has permission to perform action
local has_privs, missing_privs = minetest.check_player_privs(name, entry.privs)
if not has_privs then
worldedit.player_notify(name, "you are not allowed to use this function (missing privileges: " .. table.concat(missing_privs, ", ") .. ")")
return false
end
if entry.on_select then
entry.on_select(name)
end
if entry.get_formspec then
worldedit.show_page(name, identifier)
end
return true
end
end
return false
end)
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/functionality.lua")

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 597 B

1
worldedit_infinity/depends.txt Executable file
View File

@ -0,0 +1 @@
worldedit?

103
worldedit_infinity/init.lua Executable file
View File

@ -0,0 +1,103 @@
worldedit = rawget(_G, "worldedit") or {}
local minetest = minetest --local copy of global
local get_pointed = function(pos, nearest, distance)
if distance > 100 then
return false
end
--check for collision with node
local nodename = minetest.get_node(pos).name
if nodename ~= "air"
and nodename ~= "default:water_source"
and nodename ~= "default:water_flowing" then
if nodename ~= "ignore" then
return nearest
end
return false
end
end
local use = function(itemstack, user, pointed_thing)
if pointed_thing.type == "nothing" then --pointing at nothing
local placepos = worldedit.raytrace(user:getpos(), user:get_look_dir(), get_pointed)
if placepos then --extended reach
pointed_thing.type = "node"
pointed_thing.under = nil --wip
pointed_thing.above = nil --wip
end
end
return minetest.item_place_node(itemstack, user, pointed_thing)
end
--
worldedit.raytrace = function(pos, dir, callback)
local base = {x=math.floor(pos.x), y=math.floor(pos.y), z=math.floor(pos.z)}
local stepx, stepy, stepz = 0, 0, 0
local componentx, componenty, componentz = 0, 0, 0
local intersectx, intersecty, intersectz = 0, 0, 0
if dir.x == 0 then
intersectx = math.huge
elseif dir.x > 0 then
stepx = 1
componentx = 1 / dir.x
intersectx = ((base.x - pos.x) + 1) * componentx
else
stepx = -1
componentx = 1 / -dir.x
intersectx = (pos.x - base.x) * componentx
end
if dir.y == 0 then
intersecty = math.huge
elseif dir.y > 0 then
stepy = 1
componenty = 1 / dir.y
intersecty = ((base.y - pos.y) + 1) * componenty
else
stepy = -1
componenty = 1 / -dir.y
intersecty = (pos.y - base.y) * componenty
end
if dir.z == 0 then
intersectz = math.huge
elseif dir.z > 0 then
stepz = 1
componentz = 1 / dir.z
intersectz = ((base.z - pos.z) + 1) * componentz
else
stepz = -1
componentz = 1 / -dir.z
intersectz = (pos.z - base.z) * componentz
end
local distance = 0
local nearest = {x=base.x, y=base.y, z=base.z}
while true do
local values = {callback(base, nearest, distance)}
if #values > 0 then
return unpack(values)
end
nearest.x, nearest.y, nearest.z = base.x, base.y, base.z
if intersectx < intersecty then
if intersectx < intersectz then
base.x = base.x + stepx
distance = intersectx
intersectx = intersectx + componentx
else
base.z = base.z + stepz
distance = intersectz
intersectz = intersectz + componentz
end
elseif intersecty < intersectz then
base.y = base.y + stepy
distance = intersecty
intersecty = intersecty + componenty
else
base.z = base.z + stepz
distance = intersectz
intersectz = intersectz + componentz
end
end
end

1
worldedit_limited/depends.txt Executable file
View File

@ -0,0 +1 @@
worldedit

120
worldedit_limited/init.lua Executable file
View File

@ -0,0 +1,120 @@
do return end
do
local MAX_VOLUME = 30 * 30 * 30
local we = worldedit
local volume = we.volume
local safewrap = function(func)
return function(pos1, pos2, ...)
if validbox(pos1, pos2) then
return func(pos1, pos2, ...)
end
return 0, pos1, pos2
end
end
local validbox = function(pos1, pos2)
tpos1, tpos2 = we.sort_pos(pos1, pos2)
if volume(tpos1, tpos2) > MAX_VOLUME then
return false
end
--check for ownership of area if ownership mod is installed
if owner_defs then
local inside = false
for _, def in pairs(owner_defs) do
--sort positions
local tdef = {x1=def.x1, x2 = def.x2, y1=def.y1, y2=def.y2, z1=def.z1, z2=def.z2}
if tdef.x1 > tdef.x2 then
tdef.x1, tdef.x2 = tdef.x2, tdef.x1
end
if tdef.y1 > tdef.y2 then
tdef.y1, tdef.y2 = tdef.y2, tdef.y1
end
if tdef.z1 > tdef.z2 then
tdef.z1, tdef.z2 = tdef.z2, tdef.z1
end
--check ownership
if tpos1.x >= tdef.x1 and tpos1.x <= tdef.x2
and tpos2.x >= tdef.x1 and tpos2.x <= tdef.x2
and tpos1.y >= tdef.y1 and tpos1.y <= tdef.y2
and tpos2.y >= tdef.y1 and tpos2.y <= tdef.y2
and tpos1.z >= tdef.z1 and tpos1.z <= tdef.z2
and tpos2.z >= tdef.z1 and tpos2.z <= tdef.z2
and name == def.owner then --wip: name isn't available here
inside = true
break
end
end
if not inside then
return false
end
end
return true
end
worldedit = {
sort_pos = we.sort_pos,
set = safewrap(we.set),
replace = safewrap(we.replace),
replaceinverse = safewrap(we.replaceinverse),
copy = function(pos1, pos2, axis, amount)
tpos1, tpos2 = we.sort_pos(pos1, pos2)
tpos1[axis] = tpos1[axis] + amount
tpos2[axis] = tpos2[axis] + amount
if validbox(pos1, pos2) and validbox(tpos1, tpos2) then
we.copy(pos1, pos2, axis, amount)
else
return 0
end
end,
move = function(pos1, pos2, axis, amount)
tpos1, tpos2 = we.sort_pos(pos1, pos2)
tpos1[axis] = tpos1[axis] + amount
tpos2[axis] = tpos2[axis] + amount
if validbox(pos1, pos2) and validbox(tpos1, tpos2) then
we.move(pos1, pos2, axis, amount)
else
return 0
end
end,
stack = function(pos1, pos2, axis, count)
tpos1, tpos2 = we.sort_pos(pos1, pos2)
local length = (tpos2[axis] - tpos1[axis] + 1) * count
if count < 0 then
tpos1[axis] = tpos1[axis] + length
else
tpos2[axis] = tpos2[axis] + length
end
if validbox(tpos1, tpos2) then
we.stack(pos1, pos2, axis, amount)
else
return 0
end
end,
--wip: add transpose, rotate safely
flip = safewrap(we.flip),
orient = safewrap(we.orient),
fixlight = safewrap(we.fixlight),
--wip: add primitives here
volume = we.volume,
hide = safewrap(we.hide),
suppress = safewrap(we.suppress),
highlight = safewrap(we.highlight),
restore = safewrap(we.restore),
serialize = safewrap(we.serialize),
allocate = we.allocate,
deserialize = function(originpos, value)
local tpos1, tpos2 = we.allocate(originpos, value)
if validbox(tpos1, tpos2) then
we.deserialize(originpos, value)
else
return 0
end
end,
}
end

View File

@ -0,0 +1 @@
worldedit_commands

View File

@ -0,0 +1,51 @@
--provides shorter names for the commands in `worldedit_commands`
--returns true if command could not be aliased, false otherwise
worldedit.alias_chatcommand = function(alias, original_command)
if not minetest.chatcommands[original_command] then
minetest.log("error", "worldedit_shortcommands: original command " .. original_command .. " does not exist")
return true
end
if minetest.chatcommands[alias] then
minetest.log("error", "worldedit_shortcommands: alias " .. alias .. " already exists")
return true
end
minetest.register_chatcommand(alias, minetest.chatcommands[original_command])
return false
end
worldedit.alias_chatcommand("/i", "/inspect")
worldedit.alias_chatcommand("/rst", "/reset")
worldedit.alias_chatcommand("/mk", "/mark")
worldedit.alias_chatcommand("/umk", "/unmark")
worldedit.alias_chatcommand("/1", "/pos1")
worldedit.alias_chatcommand("/2", "/pos2")
worldedit.alias_chatcommand("/fp", "/fixedpos")
worldedit.alias_chatcommand("/v", "/volume")
worldedit.alias_chatcommand("/s", "/set")
worldedit.alias_chatcommand("/r", "/replace")
worldedit.alias_chatcommand("/ri", "/replaceinverse")
worldedit.alias_chatcommand("/hspr", "/hollowsphere")
worldedit.alias_chatcommand("/spr", "/sphere")
worldedit.alias_chatcommand("/hdo", "/hollowdome")
worldedit.alias_chatcommand("/do", "/dome")
worldedit.alias_chatcommand("/hcyl", "/hollowcylinder")
worldedit.alias_chatcommand("/cyl", "/cylinder")
worldedit.alias_chatcommand("/hpyr", "/hollowpyramid")
worldedit.alias_chatcommand("/pyr", "/pyramid")
worldedit.alias_chatcommand("/spl", "/spiral")
worldedit.alias_chatcommand("/m", "/move")
worldedit.alias_chatcommand("/c", "/copy")
worldedit.alias_chatcommand("/stk", "/stack")
worldedit.alias_chatcommand("/sch", "/stretch")
worldedit.alias_chatcommand("/tps", "/transpose")
worldedit.alias_chatcommand("/fl", "/flip")
worldedit.alias_chatcommand("/rot", "/rotate")
worldedit.alias_chatcommand("/ort", "/orient")
worldedit.alias_chatcommand("/hi", "/hide")
worldedit.alias_chatcommand("/sup", "/suppress")
worldedit.alias_chatcommand("/hlt", "/highlight")
worldedit.alias_chatcommand("/rsr", "/restore")
worldedit.alias_chatcommand("/l", "/lua")
worldedit.alias_chatcommand("/lt", "/luatransform")
worldedit.alias_chatcommand("/clro", "/clearobjects")