randomize_gridmap.gd 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. @tool
  2. extends GridMap
  3. var valid_gridmap = false
  4. var null_or_not_gridmap = false
  5. var meshlib_mismatch = false
  6. var is_self = false
  7. var interchangable_tiles_rotatable: Array
  8. var interchangable_tiles_fixed: Array
  9. var rotation_values = [0, 10, 16, 22]
  10. enum ROTATION_METHODS {RESET, KEEP_ORIGINAL, RANDOM}
  11. @export var target_gridmap: NodePath
  12. @export var randomize = false: set = set_randomize
  13. @export var rotation_method: ROTATION_METHODS = ROTATION_METHODS.RANDOM
  14. @onready var target_gridmap_node = get_node(target_gridmap)
  15. func _ready() -> void:
  16. if not Engine.is_editor_hint():
  17. set_process(false)
  18. set_physics_process(false)
  19. func _process(delta: float) -> void:
  20. if not Engine.is_editor_hint() or randomize == false:
  21. return
  22. valid_gridmap = is_gridmap_valid()
  23. if !valid_gridmap:
  24. return
  25. generate_randomisation_rules()
  26. randomize_target_gridmap()
  27. randomize = false
  28. func set_randomize(value: bool):
  29. randomize = value
  30. if not Engine.is_editor_hint():
  31. return
  32. set_process(value)
  33. func is_gridmap_valid():
  34. if not Engine.is_editor_hint():
  35. set_process(false)
  36. return
  37. if target_gridmap_node != null and target_gridmap_node.is_class("GridMap"):
  38. if target_gridmap_node == self:
  39. is_self = true
  40. elif target_gridmap_node.mesh_library != mesh_library:
  41. print ("meshlib mismatch")
  42. meshlib_mismatch = true
  43. else:
  44. print ("valid gridmap given")
  45. return true
  46. else:
  47. print ("null or not a gridmap given")
  48. null_or_not_gridmap = true
  49. return false
  50. func generate_randomisation_rules():
  51. var current_row = -99
  52. var temp_array: Array
  53. var used_cells = get_used_cells()
  54. var cell_loop_index = 0
  55. var cell_meshlib_index
  56. var row_changes = false
  57. var last_index_reached = false
  58. interchangable_tiles_rotatable.clear()
  59. interchangable_tiles_fixed.clear()
  60. used_cells.sort_custom(sort_by_z_position)
  61. #print (used_cells)
  62. if !valid_gridmap:
  63. return
  64. for cell in used_cells:
  65. current_row = cell.z
  66. if used_cells.size() > cell_loop_index + 1:
  67. if used_cells[cell_loop_index].z != used_cells[cell_loop_index + 1].z:
  68. row_changes = true
  69. else:
  70. row_changes = false
  71. cell_loop_index += 1
  72. else:
  73. row_changes = true
  74. last_index_reached = true
  75. cell_meshlib_index = get_cell_item(cell)
  76. temp_array.append(cell_meshlib_index)
  77. if row_changes == true:
  78. if current_row < 0:
  79. interchangable_tiles_rotatable.push_back(temp_array.duplicate(false))
  80. else:
  81. interchangable_tiles_fixed.push_back(temp_array.duplicate(false))
  82. temp_array.clear()
  83. if last_index_reached == true:
  84. print ("rotatable: " + str(interchangable_tiles_rotatable))
  85. print ("fixed: " + str(interchangable_tiles_fixed))
  86. func set_random_gridmap_tile(target_index, refmap_subarray, cell, _random_rotation = false):
  87. var random_pick_ref_index = randi_range(0, refmap_subarray.size() - 1)
  88. var random_pick_meshlib_index = refmap_subarray[random_pick_ref_index]
  89. if _random_rotation:
  90. if rotation_method == ROTATION_METHODS.KEEP_ORIGINAL:
  91. target_gridmap_node.set_cell_item(cell, random_pick_meshlib_index, target_gridmap_node.get_cell_item_orientation(cell))
  92. elif rotation_method == ROTATION_METHODS.RESET:
  93. target_gridmap_node.set_cell_item(cell, random_pick_meshlib_index)
  94. else:
  95. target_gridmap_node.set_cell_item(cell, random_pick_meshlib_index, rotation_values[randi_range(0,3)])
  96. else:
  97. target_gridmap_node.set_cell_item(cell, random_pick_meshlib_index, target_gridmap_node.get_cell_item_orientation(cell))
  98. func randomize_target_gridmap():
  99. print ("gridmap randomize started")
  100. var index_in_refmap_found = false
  101. var sub_array_with_index = null
  102. for cell in target_gridmap_node.get_used_cells():
  103. var target_meshlib_index = target_gridmap_node.get_cell_item(cell)
  104. for refmap_subarray in interchangable_tiles_rotatable:
  105. if refmap_subarray.has(target_meshlib_index):
  106. set_random_gridmap_tile(target_meshlib_index, refmap_subarray, cell, true)
  107. for refmap_subarray in interchangable_tiles_fixed:
  108. if refmap_subarray.has(target_meshlib_index):
  109. set_random_gridmap_tile(target_meshlib_index, refmap_subarray, cell)
  110. print ("gridmap randomize finished")
  111. func sort_by_z_position(a: Vector3, b: Vector3):
  112. if a.z < b.z:
  113. return true
  114. return false