Skip to content

ds_wfc_gen

Kamran Wali edited this page Jul 21, 2024 · 17 revisions

Inherits: DS_BaseGen

Inherited By: ds_wfc_base_continuous, ds_wfc_gen_thread

Base class for all Wave Function Collapse generator objects.

Description

ds_wfc_gen is the parent/base class for all the stage generator scripts that uses Wave Function Collapse for generating stages.

Properties

Type Name Default Value
DS_WFC_Data _data
bool _is_lock_tiles true
int _nuke_range 3
int _nuke_limit 3
int _loop_limit -1
bool _is_debug false
bool _is_debug_tile_index false
bool _is_debug_tile_type false
bool _is_debug_tile_rot false
bool _is_debug_tile_coord false
bool _is_debug_self_print false
int _index_start_tile 0
DS_Tile[] _tiles_open
DS_Tile[] _tiles_closed
DS_Tile[] _tiles_search_open
DS_Tile[] _tiles_search_closed
int _c_loop 0
float _debug_time 1.0
float _debug_total_time 0.0
int _debug_nuke_counter 0
bool _is_processing false

Methods

Return Type Name
void add_tile_to_process( DS_Tile tile )
int get_edge_opposite_index( int index, int size )
void process_main( bool is_search )
void start_debug_timer()
float get_debug_timer()
void set_processing( bool is_enabled )
void reset_fail_safe()
void reset_gen()
void reset()
void _process_grid( bool is_search )
void _reprocess_tile()
void _add_failed_tiles()
int[] _get_rules_temp_tile( DS_Tile tile, int edge, int type )
int[] _get_rules( DS_Tile tile )
void _process_tile( DS_Tile tile, int[] rules )
bool _is_found_type( DS_Tile tile, int type, int rot )
bool _is_valid_combination( int tile_rot, int tile_type, int edge_tile_type, int edge_tile_rot, int edge_index )
void _nuke( DS_Tile tile, int cur, int counter, int ignore_edge )
DS_Tile _find_nearest_none_processed_tile( DS_Tile tile )
bool _is_tile_processed(DS_Tile tile)
void _total_successful_tiles()
int[] _convert_start_array()

Property Descriptions

DS_WFC_Data _data

The data file containing the Wave Function Collapse rules. Also this data is used for generating the stages.


bool _is_lock_tiles = true

This flag determines if to lock the tiles once the WFC generator is done processing. True means the tiles will be locked once processing is done, false otherwise. Locking the tiles are mostly needed for expand and continuous WFC generators because that will make sure that once a new process has been started then the already processed tiles will NOT be processed again.


int _nuke_range = 3

The range of the nuke process. Minimum value is 1.

Note: See the _nuke() method for further details.


int _nuke_limit = -1

The number of nukes available for the current WFC generator. Once the limit threshold has been crossed the generator will stop processing and thus the generator will fail to create a stage. This is needed to make sure during the debug/testing phase that the generator does NOT run in an endless loop. This also acts as a fail safe for the generator. Once a good rule set is found then the _nuke_limit can be disabled which is optional.

When _nuke_limit is -1 it means _nuke_limit has been disabled. _nuke_limit above 1 means the number of nukes available.


int _loop_limit = -1

The number of loops available for the current WFC generator. Once the limit threshold has been crossed the generator will stop processing and this the generator will fail to create a stage. This is needed to make sure during the debug/testing phase that the generator does NOT run in an endless loop. This also acts as a fail safe for the generator. Once a good rule set is found then the _loop_limit can be disabled which is optional.

A single loop is completed when the continuous while loop in the method process_main has been completed once.

When _loop_limit is -1 it means _loop_limit has been disabled. _loop_limit above 1 means the number of nukes available.


bool _is_debug = false

This method enables/disables the debug print once the generator is done processing.


bool _is_debug_tile_index = false

If this flag is enabled then the tile index in the grid will be printed once the generator is done processing.

Note: _is_debug MUST be enabled as well for this to be printed.


bool _is_debug_tile_type = false

If this flag is enabled then the tile types in the grid will be printed once the generator is done processing.

Note: _is_debug MUST be enabled as well for this to be printed.


bool _is_debug_tile_rot = false

If this flag is enabled then the tile rotations in the grid will be printed once the generator is done processing.

Note: _is_debug MUST be enabled as well for this to be printed.


bool _is_debug_tile_coord = false

If this flag is enabled then the tile coordinates in the grid will be printed once the generator is done processing.

Note: _is_debug MUST be enabled as well for this to be printed.


bool _is_debug_self_print = false

If this flag is enabled then the scripts _to_string() method will be called once the generator is done processing.

Note: _is_debug MUST be enabled as well for this to be printed.


int _index_start_tile = 0

The first tile in the grid that will be processed by the WFC generator, ds_wfc_gen.

Note: If at least one tile is added to _start_tiles then the 0th index from the array will be the first tile to be processed. Otherwise a random tile will be selected by ds_wfc_gen from the grid to be the first tile to be processed.


DS_Tile[] _tiles_open

An array of tiles that are yet to be processed by ds_wfc_gen.


DS_Tile[] _tiles_closed

An array of tiles that has already been processed by ds_wfc_gen.


DS_Tile[] _tiles_search_open

An array of tiles that are yet to be searched by ds_wfc_gen.


DS_Tile[] _tiles_search_closed

An array of tiles that have already been searched by ds_wfc_gen.


int _c_loop = 0

The current number of loops the generator has gone through. This will later be needed to show in the debug print if _is_debug has been enabled.


float _debug_time = 1.0

For storing the generator starting time that will later be needed to calculate the total processing time, see _debug_total_time for more details.


float _debug_total_time = 0.0

The total processing time for the generator.


int _debug_nuke_counter = 0

Keeping track of the total number of nukes that has been fired by the ds_wfc_gen. This will later be needed to show in the debug print if _is_debug has been enabled.


bool _is_processing = false

This flag represents the processing status of the generator, ds_wfc_gen. True means the generator is still processing, false otherwise.


Method Descriptions

void add_tile_to_process ( DS_Tile tile )

This method adds the given tile to the array _tiles_open to be processed by the generator, ds_wfc_gen.


int get_edge_opposite_index ( int index, int size )

This method gets the opposite index of the given edge index.

If index 0, UP, is given then the returned opposite index will be 3, BOTTOM. If index 1, NORTH, is given then the returned opposite index will be 4, SOUTH. If index 2, EAST, is given then the returned opposite index will be 5, WEST. If index 3, BOTTOM, is given then the returned opposite index will be 0, UP. If index 4, SOUTH, is given then the returned opposite index will be 1, NORTH. If index 5, WEST, is given then the returned opposite index will be 2, EAST.


void process_main ( bool is_search )

This is the main process of the generator, ds_wfc_gen, which does all the processing for the Wave Function Collapse. This method is called from the setup().

Note: To start the processing please call the method setup() as this method will do handle some prerequisites before calling the process_main() method. Just calling process_main() method will give weird results.


void start_debug_timer () const

This method starts the debug timer when the generator process has been started for the first time. It will reset once the generator has been resetted and setup() method has been called again.


float get_debug_timer () const

This method gets the total processing time of the generator when this method was called.


void set_processing ( bool is_enabled )

This method updates the processing status, _is_processing, of the generator. True will enable the processing of the generator, false otherwise.


void reset_fail_safe ()

This method will reset the fail safe properties. The fail properties are _c_loop and _debug_nuke_counter


void reset_gen ()

This method ONLY resets the generator and calls the method reset_fail_safe() as well. This reset is mainly used by the expand and continuous WFC generators. This makes sure that the generator can be reused again without compromising the current state of the grid.


void reset ()

This method does a hard reset by resetting the grid and calls the methods reset_gen() and reset_fail_safe() for resetting. This reset is mainly used for resetting everything and reusing the entire grid and generator again for WFC.


void _process_grid ( bool is_search )

This method processes the grid. It first searches for the first tile with the lowest entropy and starts processing it.

if the bool is_search is true then once a tile has been finished processing then it's unprocessed neighbouring tiles will be added to the _tiles_open array for processing.


void _reprocess_tile ()

This method reprocesses an error tile. Basically if any tiles are found to break the rules then this method will fix it.


void _add_failed_tiles ()

This method adds all the failed tiles back to the array _tiles_open to be processed again. It finds a failed tile by going through all the tiles in the grid and checking if the tile has a type value of -1. Tile type value of -1 means either that tile has failed or has NOT been processed.


int[] _get_rules_temp_tile ( DS_Tile tile, int edge, int type )

This method gets all the available rules for the given tile by searching through all the edges. The given edge will be the only edge that will have a temp value. The reason for setting one edge value is because for the possiblity that the edge may have that type in future. This will help in reducing errors while making the stage.

The tile parameter's rules are found for all the edges. That is all the rules from all the edges.

The edge parameter is the edge which will have a temp type which will be used to get all the edge rules for the tile. If an edge value is given that is NOT within 0 to 5 then the temp edge will be ignored.

The type parameter is the temp type for the given edge of the give tile which will be used to get all the edge rules for the tile.


int[] _get_rules ( DS_Tile tile )

This method gets all the available rules for the given tile by searching through all the edges.


void _process_tile ( DS_Tile tile, int[] rules )

This method processes the given tile. It randomly selects a rule from the given rules array and checks if the rule fits the tile. If it fits then processing the tile is done if NOT then the currently selected rule is discarded and another randomly rule is select. If NO rules found for the tile then it will have a tile type of -1 which means the tile process has failed.


bool _is_found_type ( DS_Tile tile, int type, int rot )

This method checks if the given tile with the given type and given rotation, rot, fits in the grid. If it does NOT fit then the tile will be rotated 90 degrees clockwise. The range of rotation is from 0 to 270. If NO rotation fits the given tile then it's type will be set as -1.

When the method returns true it means the tile fits in the current state of the grid, false means otherwise.


bool _is_valid_combination ( int tile_rot, int tile_type, int edge_tile_type, int edge_tile_rot, int edge_index )

This method checks if the given tile combinations are valid which means it does NOT fall in the invalid combo rules.

The tile_rot parameter is the rotation value of the given tile that needs to be checked.

The tile_type parameter is the tile type of the given tile that needs to be checked.

The edge_tile_type parameter is the tile type of the given neighbour that needs to be checked.

The edge_tile_rot parameter is the rotation value of the given neighbour that needs to be checked.

The edge_index parameter is the edge/neighbour of the given tile that needs to be checked.

When the method returns true it means the combination is valide, false otherwise.


void _nuke ( DS_Tile tile, int cur, int counter, int ignore_edge)

This method nukes the tiles in the grid. This means that the nuked tiles are resetted back to default, unprocessed, value. This method uses recursion to find the tiles to nuke.

This method is needed when the generator, ds_wfc_gen, can NOT fit a tile in the grid any longer. This then allows to free up the tiles in that failed area for the generator to work again in that area. This gives the generator the possibility to generate a different result in that area. The range of the nuke can be set by changing the _nuke_range property.

The nuke process starts from the given tile parameter. The process also ignores tiles that that are fixed and the given ignore_edge neighbour of the given tile. The nuked process fans out like a star with 4 ends, see the Fig 1 below to get an idea how the shape of the nuked process looks like.

A processed tile that has been nuked will be removed from the array _tiles_closed and will be open for processing again.

Nuke_Shape
Fig 1: Nuked Process Shape

DS_Tile _find_nearest_none_processed_tile ( DS_Tile tile)

This method finds the nearest unprocessed tile to a processed tile from the given tile.


bool _is_tile_processed ( DS_Tile tile )

Thie method checks if the give tile has been processed or NOT.

When the method returns true it means the tile has been processed, false otherwise.


void _total_successful_tiles ()

This method gets the total successful tiles. It gets the result by going through all the tiles in the grid. The tiles with type that are NOT -1 are counted as a successful tile


int[] _convert_start_array ()

This method converts the array DS_TileInfo[] _start_tiles into array int[].


Clone this wiki locally