-
Notifications
You must be signed in to change notification settings - Fork 0
/
JobManager.gd
111 lines (90 loc) · 2.5 KB
/
JobManager.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
extends Node
var jobs = [
preload("res://Job1.tscn"), preload("res://Job1_2.tscn"),
preload("res://Job2.tscn"),
preload("res://Job3.tscn"), preload("res://Job3_2.tscn"), preload("res://Job3_3.tscn"),
preload("res://Job4.tscn")
]
var max_jobs = 7
func get_job(number) -> Node:
if number > max_jobs or number < 1:
print('invalid job number')
return null
var job_instance = jobs[number - 1].instance()
return job_instance
func num_jobs() -> int:
return max_jobs
func get_next_job() -> int:
return int(min(get_node("/root/GlobalData").max_job_completed + 1, max_jobs))
func check_job_completion(job_num : int, map : TileMap) -> bool:
var JOB_WIDTH = 19
var MAX_TILE = 40
var first_x = -1
var first_y = -1
var first_tile = false
var last_x = -1
var last_y = -1
var job_instance = get_job(job_num)
if not job_instance:
return false
var goal_map : TileMap = job_instance.get_goal()
if not goal_map:
print("failed to get goal map for job " + str(job_num))
job_instance.print_tree_pretty()
return false
var max_j = -1
for ty in range(JOB_WIDTH):
for tx in range(JOB_WIDTH):
var this_tile = goal_map.get_cell(tx, ty)
if this_tile > 0:
if first_x < 0:
first_x = tx
first_y = ty
first_tile = this_tile
last_x = tx
last_y = ty
max_j = max(max_j, tx)
var width = max_j - first_x + 1
var verified = false
var search_origin_x = -1
var search_origin_y = -1
var searching = false
var return_coord = false
var tx = 0
var ty = 0
var safety = 200000
while ty <= MAX_TILE:
if verified:
break
tx = 0
while tx <= MAX_TILE:
safety -= 1
if safety < 0:
return false
var this_tile = map.get_cell(tx, ty)
if searching:
var x_offset = tx + (first_x - search_origin_x)
var y_offset = ty + (first_y - search_origin_y)
var that_tile = goal_map.get_cell(x_offset, y_offset)
if that_tile < 1 or that_tile == this_tile:
if x_offset == last_x and y_offset == last_y:
verified = true
break
if that_tile < 1 and this_tile == first_tile and not return_coord:
return_coord = Vector2(tx, ty)
else:
searching = false
if return_coord:
ty = return_coord.y
tx = return_coord.x - 1
continue
if not searching and this_tile == first_tile and not (tx + width > MAX_TILE):
search_origin_x = tx
search_origin_y = ty
if first_x == last_x and first_y == last_y: # job is only 1 tile
verified = true
break
searching = true
tx += 1
ty += 1
return verified