Skip to content

Commit

Permalink
V2
Browse files Browse the repository at this point in the history
Add docs inside of scripts
2 new nodes - WFC Data Creator, WFC Map Generator
Moved Scenes and Scripts to demo folder
  • Loading branch information
pietru2004 committed Nov 18, 2022
1 parent 0bded7a commit 564fa95
Show file tree
Hide file tree
Showing 19 changed files with 790 additions and 49 deletions.
11 changes: 11 additions & 0 deletions addons/WFC_Based_Gen/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Main Screen Plugin Demo

This plugin demo shows how to make a main screen plugin.
The main screen appears as a button next to the "2D", "3D", "Script", and
"AssetLib" buttons. It also shows up when a Node it `handles` is selected.

For more information, see this documentation article:
https://docs.godotengine.org/en/latest/tutorials/plugins/editor/making_main_screen_plugins.html

If you would like to see a more complete example of what main screen plugins
are capable of, check out the [2.5D demo project](../../../misc/2.5d).
5 changes: 0 additions & 5 deletions addons/WFC_Based_Gen/main_panel.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,21 @@ render_target_update_mode = 3

[node name="MainModel" type="MeshInstance" parent="ViewHolder/Viewport"]
mesh = SubResource( 1 )
material/0 = null

[node name="X" type="MeshInstance" parent="ViewHolder/Viewport"]
transform = Transform( -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0, 1, 3, 0, 0 )
material_override = SubResource( 4 )
mesh = SubResource( 5 )
material/0 = null

[node name="Y" type="MeshInstance" parent="ViewHolder/Viewport"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3, 0 )
material_override = SubResource( 6 )
mesh = SubResource( 5 )
material/0 = null

[node name="Z" type="MeshInstance" parent="ViewHolder/Viewport"]
transform = Transform( 1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 3 )
material_override = SubResource( 7 )
mesh = SubResource( 5 )
material/0 = null

[node name="Model_x_plus" type="MeshInstance" parent="ViewHolder/Viewport"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 2, 0, 0 )
Expand Down Expand Up @@ -428,7 +424,6 @@ text = "<<"
anchor_right = 0.062
anchor_bottom = 0.04
margin_right = 0.511997
margin_bottom = -1.90735e-06
text = "File"
items = [ "Open", null, 0, false, false, 0, 0, null, "", false, "Save", null, 0, false, false, 1, 0, null, "", false, "Save As", null, 0, false, false, 2, 0, null, "", false ]

Expand Down
27 changes: 27 additions & 0 deletions addons/WFC_Based_Gen/nodes/FullyRuledGridMap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@ tool
extends GridMap
class_name FullyRuledGridMap

##
## Fully Ruled GridMap Generator script
##
## @desc:
## This Generator uses Data set created using WFC Rule Creator panel.
## This Generator creates Mesh Library during generation.
##
## Generation is WFC Ruled -> adds all xz positions to list then selects
## randomly tile to set that has lowest possible number of solutions.
##
## If allow_above_start_height is true generator will add tile above after
## item in cell in setted
##
## If allow_under_start_height is true generator will add tile above after
## item in cell in setted
##
## Use regen() function to completly regenerate map with recreating MeshLib.
##
## Set regen_lock variable to disallow run of regen() function.
## (It is binded to press_to_regen button)
##
## Use generate() function to generate new map.
##
## Use save_data() function to retrieve data of current cell placement.
## Use load_data(data) function to load data of saved cell placement.
##

export(String) var TilesDataPath
export(int) var map_size = 10
export(int) var map_start_height = 0
Expand Down
21 changes: 21 additions & 0 deletions addons/WFC_Based_Gen/nodes/RuledLinearGridMap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,27 @@ tool
extends GridMap
class_name RuledLinearGridMap

##
## Ruled Linear GridMap Generator script
##
## @desc:
## This Generator uses Data set created using WFC Rule Creator panel.
## This Generator creates Mesh Library during generation.
##
## Generation is LINEAR -> each z pos on each x pos on each y pos
##
## Use regen() function to completly regenerate map with recreating MeshLib.
##
## Set regen_lock variable to disallow run of regen() function.
## (It is binded to press_to_regen button)
##
## Use generate() function to generate new map.
##
## Use save_data() function to retrieve data of current cell placement.
## Use load_data(data) function to load data of saved cell placement.
##


export(String) var TilesDataPath
export(int) var map_size = 10
export(bool) var regen_lock = true
Expand Down
19 changes: 19 additions & 0 deletions addons/WFC_Based_Gen/nodes/RuledRandomVerticalGridMap.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ export(int) var map_max_height = 6
export(bool) var regen_lock = true
export(bool) var press_to_regen setget regen

##
## Ruled Random Vertical GridMap Generator script
##
## @desc:
## This Generator uses Data set created using WFC Rule Creator panel.
## This Generator creates Mesh Library during generation.
##
## Generation is RANDOM -> each xz pos selected randomly on each y pos
##
## Use regen() function to completly regenerate map with recreating MeshLib.
##
## Set regen_lock variable to disallow run of regen() function.
## (It is binded to press_to_regen button)
##
## Use generate() function to generate new map.
##
## Use save_data() function to retrieve data of current cell placement.
## Use load_data(data) function to load data of saved cell placement.
##

var tiles_list := []
var tiles_data := {}
Expand Down
98 changes: 98 additions & 0 deletions addons/WFC_Based_Gen/nodes/WFCDataCreator.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
tool
extends GridMap
class_name WFCDataCreator

##
## Wave Function Colapse Data Creator script
##
## @desc:
## This Scripts creates data set used in WFCMapGenerator.
## Required - existing mesh Library and painted rules
## Side Note: Data Saver ignores fields where
##
## The tile_duplicates allows the tile to be added multiple times,
## which allows how often should tile be placed in sittuation,
## this might require more rules in extreme situations.
##
## Pressing click_to_export exports data set to selected file path in json format.
##

export(String) var export_file_path := "res://data/tile_data.json"
export(bool) var tile_duplicates := false
export(bool) var click_to_export := false setget run_export


# Called when the node enters the scene tree for the first time.
func _ready():
if !Engine.editor_hint:
queue_free()

func run_export(value):
var cells = get_used_cells()

var used_ids := []
var cells_data := {}

for cell in cells:
var cell_data = get_cell_data(cell)
var ncell := {}

cells_data=add_key_if_not_exist(cells_data,cell_data["id"],cell_data["orientation"])

ncell=get_cell_data(cell+Vector3(1,0,0))
cells_data=add_neighbours(cells_data,cell_data,ncell,"x_plus")
ncell=get_cell_data(cell+Vector3(-1,0,0))
cells_data=add_neighbours(cells_data,cell_data,ncell,"x_minus")

ncell=get_cell_data(cell+Vector3(0,1,0))
cells_data=add_neighbours(cells_data,cell_data,ncell,"y_plus")
ncell=get_cell_data(cell+Vector3(0,-1,0))
cells_data=add_neighbours(cells_data,cell_data,ncell,"y_minus")

ncell=get_cell_data(cell+Vector3(0,0,1))
cells_data=add_neighbours(cells_data,cell_data,ncell,"z_plus")
ncell=get_cell_data(cell+Vector3(0,0,-1))
cells_data=add_neighbours(cells_data,cell_data,ncell,"z_minus")

if !used_ids.has(cell_data["id"]):
used_ids.append(cell_data["id"])

save_file(used_ids,cells_data)

func get_cell_data(cell)->Dictionary:
var id = get_cell_item(cell.x,cell.y,cell.z)
var orientation = get_cell_item_orientation(cell.x,cell.y,cell.z)
return {"id":id,"orientation":orientation,"cell":cell}

func add_key_if_not_exist(data:Dictionary,key,orientation:int)->Dictionary:
if !data.has(str(key)+":"+str(orientation)):
var tile_name = mesh_library.get_item_name(key)
data[str(key)+":"+str(orientation)]={
"tile_name":tile_name,
"orientation":orientation,
"x_plus":[],
"x_minus":[],
"y_plus":[],
"y_minus":[],
"z_plus":[],
"z_minus":[],
}
return data

func add_neighbours(cells:Dictionary,key_cell:Dictionary,ncell:Dictionary,side:String)->Dictionary:
if ncell["id"]>=0:
var key_m := str(key_cell["id"])+":"+str(key_cell["orientation"])
var key_n := str(ncell["id"])+":"+str(ncell["orientation"])
if !cells[key_m][side].has(key_n) or tile_duplicates:
cells[key_m][side].append(key_n)
return cells

func save_file(used_ids,data):
if export_file_path.length()>0:
if !export_file_path.ends_with(".json"):
export_file_path=export_file_path+".json"
var file := File.new()
file.open(export_file_path,File.WRITE)
file.store_line(to_json({"used_ids":used_ids,"wfc_data":data}))
file.close()
print_debug("File Saved in "+export_file_path)
Loading

0 comments on commit 564fa95

Please sign in to comment.