Skip to content

Commit

Permalink
fixed fake slabs loading, fixed an Undo crash
Browse files Browse the repository at this point in the history
  • Loading branch information
rainlizard committed May 28, 2024
1 parent 76ecffc commit 71f2aff
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 31 deletions.
3 changes: 3 additions & 0 deletions Scenes/CfgLoader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends Node
onready var oGame = Nodelist.list["oGame"]
onready var oMessage = Nodelist.list["oMessage"]
onready var oConfigFilesListWindow = Nodelist.list["oConfigFilesListWindow"]
onready var oCustomSlabSystem = Nodelist.list["oCustomSlabSystem"]


# These are dictionaries containing dictionaries.
Expand Down Expand Up @@ -83,6 +84,8 @@ func start(mapPath):
print('Loaded all .cfg and .toml files: ' + str(OS.get_ticks_msec() - CODETIME_LOADCFG_START) + 'ms')
if oConfigFilesListWindow.visible == true:
Utils.popup_centered(oConfigFilesListWindow)

oCustomSlabSystem.load_unearth_custom_slabs_file()

func load_objects_data(path): # 10ms
var objects_cfg = Utils.read_dkcfg_file(path)
Expand Down
8 changes: 3 additions & 5 deletions Scenes/CustomSlabSystem.gd
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ onready var oMessage = Nodelist.list["oMessage"]

var cfg = ConfigFile.new()

func _ready():
load_file()

func load_file():
func load_unearth_custom_slabs_file():
var filePath = Settings.unearthdata.plus_file("custom_slabs.cfg")

var file = File.new()
Expand Down Expand Up @@ -104,9 +101,10 @@ func add_custom_slab(slab_dict):
if slab_dict["floor_data"].size() > 0:
cfg.set_value(section,"floor"+str(i),slab_dict["floor_data"][i])

print("ADDED CUSTOM SLAB ", head_id)

cfg.save(Settings.unearthdata.plus_file("custom_slabs.cfg"))


func attempt_to_remove_custom_slab(header_id):
oPickSlabWindow.set_selection(null)

Expand Down
18 changes: 12 additions & 6 deletions Scenes/OverheadGraphics.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,15 @@ func update_full_overhead_map(threadType):

match threadType:
SINGLE_THREADED:
overhead2d_update_rect_single_threaded(shapePositionArray)
if thread_currently_processing == false:
overhead2d_update_rect_single_threaded(shapePositionArray)
MULTI_THREADED:
mutex.lock()
job_queue.append(shapePositionArray)
job_queue.append(shapePositionArray.duplicate(true)) # This duplicate somehow fixes a crash, or maybe just helps improve a race condition
mutex.unlock()
thread_currently_processing = true
semaphore.post() # Release the semaphore to signal the thread to process the job
print('Overhead graphics done in '+str(OS.get_ticks_msec()-CODETIME_START)+'ms')



# Using a single threaded version for updating partial graphics.
# and a multi-threaded version for updating the entire map's graphics.
func overhead2d_update_rect_single_threaded(shapePositionArray):
Expand All @@ -71,18 +69,23 @@ func overhead2d_update_rect_single_threaded(shapePositionArray):

func multi_threaded():
while true:
semaphore.wait() # Acquire the semaphore before processing
if job_queue.size() == 0:
continue
thread_currently_processing = true
print("graphics multi_threaded start")

mutex.lock()
var shapePositionArray = job_queue.pop_front()
mutex.unlock()

var newPixelData = PoolByteArray()
print("graphics multi_threaded 1")
var resulting_pixel_data = generate_pixel_data(newPixelData, shapePositionArray)
print("graphics multi_threaded 2")
call_deferred("thread_done", resulting_pixel_data)
print("graphics multi_threaded end")


func thread_done(resulting_pixel_data):
if resulting_pixel_data != null:
pixel_data = resulting_pixel_data
Expand All @@ -95,9 +98,11 @@ func thread_done(resulting_pixel_data):
emit_signal("graphics_thread_completed")

func generate_pixel_data(pixData, shapePositionArray):
print("generate_pixel_data START")
var width = M.xSize * 3
var height = M.ySize * 3
pixData.resize(width * height * 3) # Assuming RGB8 format

for pos in shapePositionArray:
var basePosX = pos.x * 3
var basePosY = pos.y * 3
Expand All @@ -112,6 +117,7 @@ func generate_pixel_data(pixData, shapePositionArray):
pixData[pixelIndex] = cubeFace >> 16 & 255
pixData[pixelIndex + 1] = cubeFace >> 8 & 255
pixData[pixelIndex + 2] = cubeFace & 255
print("generate_pixel_data END")
return pixData


Expand Down
1 change: 1 addition & 0 deletions Scenes/Selector.gd
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ onready var oAddCustomSlabWindow = Nodelist.list["oAddCustomSlabWindow"]
onready var oDisplaySlxNumbers = Nodelist.list["oDisplaySlxNumbers"]
onready var oOwnerSelection = Nodelist.list["oOwnerSelection"]
onready var oSlabNameDisplay = Nodelist.list["oSlabNameDisplay"]
onready var oUndoStates = Nodelist.list["oUndoStates"]

onready var TILE_SIZE = Constants.TILE_SIZE
onready var SUBTILE_SIZE = Constants.SUBTILE_SIZE
Expand Down
10 changes: 7 additions & 3 deletions Scenes/ThreadedSaveUndo.gd
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ func run_threaded_undo_save(_userdata):
var compare_state = create_state()

# For some reason TNGFX occasionally breaks
if compare_state.has("TNGFX") and compare_state["TNGFX"] == null:
oMessage.big("Undo state error", "TNGFX buffer broke")
if compare_state.has("TNGFX") == false:
oMessage.big("Undo state error 1", "TNGFX buffer broke")
continue
elif compare_state["TNGFX"] == null:
oMessage.big("Undo state error 2", "TNGFX buffer broke")
continue

if oUndoStates.are_states_equal(consistent_state, compare_state):
break

# The captured state is consistent, save it as the undo state
oUndoStates.call_deferred("on_undo_state_saved", consistent_state)
print("End multi-threaded save undo state: " + str(OS.get_ticks_msec() - CODETIME_START) + "ms")


func create_state():
var new_state = {}
for EXT in oBuffers.FILE_TYPES:
Expand All @@ -42,4 +47,3 @@ func create_state():
continue
new_state[EXT] = oBuffers.get_buffer_for_extension(EXT, oCurrentMap.path)
return new_state

43 changes: 26 additions & 17 deletions Scenes/UndoStates.gd
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ onready var oNewMapWindow = Nodelist.list["oNewMapWindow"]
onready var oEditor = Nodelist.list["oEditor"]
onready var oMenu = Nodelist.list["oMenu"]



var is_saving_state = false
var undo_history = []
var max_undo_states = 256
var performing_undo = false

var undo_save_queued = false

func _input(event):
if event.is_action_pressed("undo"):
perform_undo()
Expand All @@ -28,33 +27,46 @@ func clear_history():
undo_history.clear()
oMenu.update_undo_availability()

is_saving_state = false # To be sure it's executed
call_deferred("attempt_to_save_new_undo_state")


func attempt_to_save_new_undo_state(): # called by oEditor
if is_saving_state == false:
is_saving_state = true
while Input.is_mouse_button_pressed(BUTTON_LEFT) or oLoadingBar.visible == true or oNewMapWindow.currently_creating_new_map == true:
yield(get_tree(), "idle_frame")
undo_save_queued = true


func _process(delta):
if undo_save_queued == true:
set_process(false)
while true:
print("a")
if Input.is_mouse_button_pressed(BUTTON_LEFT) or \
oLoadingBar.visible == true or \
oNewMapWindow.currently_creating_new_map == true or \
performing_undo == true:
print("b")
yield(get_tree(), "idle_frame")
print("c")
else:
print("d")
break
print("oThreadedSaveUndo.semaphore.post()")
oThreadedSaveUndo.semaphore.post()

undo_save_queued = false
set_process(true)


func on_undo_state_saved(new_state):
if undo_history.size() >= 1 and are_states_equal(new_state, undo_history[0]):
#oMessage.quick("Didn't add undo state as it is the same as the previous undo-state")
is_saving_state = false
return
if undo_history.size() >= max_undo_states:
undo_history.pop_back()
undo_history.push_front(new_state)
oMenu.update_undo_availability()

#oMessage.quick("Undo history size: " + str(undo_history.size()) + " (test)")


is_saving_state = false



func perform_undo():
print("perform_undo")
Expand All @@ -66,8 +78,6 @@ func perform_undo():
oMessage.big("Undo state error", "previous_state is not a dictionary")
return



var CODETIME_START = OS.get_ticks_msec()
performing_undo = true

Expand Down Expand Up @@ -98,7 +108,6 @@ func perform_undo():
var IDLE_FRAME_CODETIME_START = OS.get_ticks_msec()
yield(get_tree(), 'idle_frame')
print('Idle frame (after undo): ' + str(OS.get_ticks_msec() - IDLE_FRAME_CODETIME_START) + 'ms')

performing_undo = false


Expand Down
3 changes: 3 additions & 0 deletions Scenes/WriteData.gd
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func write_tng():


func write_tngfx():
print("write_tngfx START")
var buffer = StreamPeerBuffer.new()
var groupNames = {
Things.TYPE.OBJECT: "Object",
Expand Down Expand Up @@ -208,6 +209,8 @@ func write_tngfx():

lines.set(1, "ThingsCount = " + str(entryNumber))
buffer.put_data("\n".join(lines).to_ascii())

print("write_tngfx END")
return buffer


Expand Down

0 comments on commit 71f2aff

Please sign in to comment.