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 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
GameVar("gv_Deployment", false)
GameVar("gv_DeploymentStarted", false)
GameVar("gv_DeploymentDir", false)
MapVar("gv_Redeployment", false)
MapVar("RedeploymentThread", false)

DefineClass.DeploymentMarker = {
	__parents = {"GridMarker"},
	
	properties = {
		{ category = "Grid Marker", id = "Type", name = "Type", editor = "dropdownlist", items = {"DeployArea"}, default = "DeployArea", no_edit = true },
		{ category = "Marker", id = "Reachable", name = "Reachable only", editor = "bool", default = false, help = "Area of marker includes only tiles reachable from marker position, not the entire rectangle"},
		{ category = "Marker", id = "GroundVisuals", name = "Ground Visuals", editor = "bool", default = true, help = "Show ground mesh on the marker area"},
		{ category = "Trigger Logic", id = "Trigger", name = "Trigger", editor = "dropdownlist", items = { "always", }, default = "always", no_edit = true},
		{ category = "Trigger Logic", id = "TriggerEffects", name = "Effects", editor = "nested_list", base_class = "Effect", default = false, no_edit = true},
		{ category = "Deployment", id = "AlternateEntrance", name = "Alternate Entrance", editor = "bool", default = false},
	},
}

function DeploymentMarker:Init()
	self:UpdateVisuals(self.Type, true)
end 

function DeploymentMarker:TriggerThreadProc()
end

function DeploymentMarker:IsAreaVisible()
	return IsEditorActive() or gv_DeploymentStarted and self:IsMarkerEnabled()
end

local deploy_types = {"Entrance", "Defender", "DefenderPriority", "DeployArea"}
function IsDeployMarker(marker)
	return not not table.find(deploy_types, marker.Type)
end

function GetAvailableEntranceMarkers(arrival_dir)
	-- Entrance markers are always enabled as mercs can enter from there.
	-- There are quest cases in which we might want to disable them though, which is denoted by gv_Deployment
	local markers 
	if gv_Deployment ~= "custom" then
		markers = MapGetMarkers("Entrance", g_GoingAboveground and "Underground" or arrival_dir, function(marker)
			return marker:IsMarkerEnabled()
		end)
	else
		markers = {}
	end
	if not g_GoingAboveground then
		markers = markers or {}

		-- Add alt entrance deployment markers.
		local additionalEntrances = MapGetMarkers("DeployArea", arrival_dir, function(marker)
			return marker:IsMarkerEnabled()
		end)
		table.iappend(markers, additionalEntrances)

		MapForEach("map", "DeploymentMarker", function (marker, markers)
			if not marker.AlternateEntrance and marker:IsMarkerEnabled() then
				markers[#markers + 1] = marker
			end
		end, markers)
	end
	
	return markers
end

function GetAvailableDeploymentMarkers(some_unit)
	local markers = {}
	some_unit = some_unit or SelectedObj
	if gv_Deployment == "defend" then
		markers = MapGetMarkers("Defender", false, function(m) return m:IsMarkerEnabled() end)
		local player_side = NetPlayerSide()
		local non_blocked_markers = {}
		for _, marker in ipairs(markers) do
			local area = marker:GetAreaBox()
			local blocked = false
			for _, unit in ipairs(g_Units) do
				if SideIsEnemy(player_side, unit.team.side) and not unit:IsDead() and area:Point2DInside(unit) then
					blocked = true
					break
				end
			end
			if not blocked then
				table.insert(non_blocked_markers, marker)
			end
		end
		
		return #non_blocked_markers > 0 and non_blocked_markers or {markers[1]}
	elseif some_unit then
		markers = GetAvailableEntranceMarkers(some_unit.arrival_dir)
	end
	return markers
end

function GetEnemyDeploymentMarkers()
	local markers = {}
	local _, enemy_squads = GetSquadsInSector(gv_CurrentSectorId)
	for _, squad in ipairs(enemy_squads) do
		local dir = squad.units and squad.units[1] and gv_UnitData[squad.units[1]] and gv_UnitData[squad.units[1]].arrival_dir
		if dir then
			local available = GetAvailableEntranceMarkers(dir)
			for _, marker in ipairs(available) do
				table.insert_unique(markers, marker)
			end
		end
	end
	return markers
end

function UpdateAvailableDeploymentMarkers()
	if gv_DeploymentStarted then
		local enemy_markers = GetEnemyDeploymentMarkers()
		local available = GetAvailableDeploymentMarkers()
		MapForEachMarker("GridMarker", nil, function(marker)
			if IsDeployMarker(marker) then
				if not table.find_value(available, marker) then
					marker:HideArea()
					DeleteBadgesFromTargetOfPreset("DeploymentAreaBadge", marker)
				else
					if not TargetHasBadgeOfPreset("DeploymentAreaBadge", marker) then
						CreateBadgeFromPreset("DeploymentAreaBadge", marker)
					end
					if not marker:IsAreaShown() then
						marker:ShowArea()
					end
				end
				if gv_Deployment == "defend" then
					if not table.find_value(enemy_markers, marker) then
						DeleteBadgesFromTargetOfPreset("EnemyDeploymentAreaBadge", marker)
					else
						if not TargetHasBadgeOfPreset("EnemyDeploymentAreaBadge", marker) then
							CreateBadgeFromPreset("EnemyDeploymentAreaBadge", marker)
						end
					end
				end
			end
		end)
	else
		UpdateEntranceAreasVisibility()
		MapForEachMarker("GridMarker", nil, function(marker)
			if marker.Type == "DeployArea" then
				marker:HideArea()
			end
			DeleteBadgesFromTargetOfPreset("DeploymentAreaBadge", marker)
			DeleteBadgesFromTargetOfPreset("EnemyDeploymentAreaBadge", marker)
		end)
	end
end

function IsFirstSquadDeployment(squad_id) -- check all squads if not squad_id is provided
	local team = GetCurrentTeam()
	if team then
		for i, u in ipairs(team.units) do
			if (not squad_id or u.Squad == squad_id) and u:IsLocalPlayerControlled() and IsUnitDeployed(u) then
				return false
			end
		end
	end
	return true
end

function IsDeploymentReady()
	local team = GetCurrentTeam()
	if team then
		for i, u in ipairs(team.units) do
			if not IsUnitDeployed(u) then
				return false
			end
		end
	end
	return true
end

function GetCurrentDeploymentSquadUnits(local_player_controlled_only)
	local units = {}
	local currentSquad = gv_Squads[g_CurrentSquad]
	for i, session_id in ipairs(currentSquad.units) do
		local u = g_Units[session_id]
		if not local_player_controlled_only or u:IsLocalPlayerControlled() then
			units[#units + 1] = u
		end
	end
	return units
end

if FirstLoad then
	DeployButtonVisible = true
end

function HideDeployButton()
	DeployButtonVisible = false
end

function ShowDeployButton()
	DeployButtonVisible = true
end

function ShouldHideDeployButton()
	return not DeployButtonVisible
end

function ShowUnitsOnDeployment(bShow, bLclPlayer)
	if bShow then
		local igi = GetInGameInterfaceModeDlg()
		if not IsKindOf(igi, "IModeDeployment") or not igi.units_deployed then
			igi = false
		end
	
		for _, t in ipairs(g_Teams) do
			if t.side == "player1" or t.side == "player2" then -- show only player units, enemies will be handled in exploration VisibilityThread
				for i, unit in ipairs(t.units) do
					if bLclPlayer == unit:IsLocalPlayerControlled() then
						unit:SetVisible(true)
						
						-- Mark unit as deployed. It should be in a valid deploy position due to LocalDeployUnitsOnMarker in StartDeployment
						if igi then
							igi.units_deployed[unit] = true
							igi.cursor_voxel = false -- Force recalc
						end
					end
				end
			end
		end
	else
		for _, t in ipairs(g_Teams) do
			if t.side == "player1" or t.side == "player2" or t.side == "enemy1" or t.side == "enemy2" then
				for i, unit in ipairs(t.units) do
					unit:SetVisible(false)
				end
			end
		end
	end
	ObjModified("DeployUpdated")
	ObjModified("UpdateTacticalNotification")
end

function SkipDeployment(mode)
	if gv_Deployment then
		return false
	elseif not mode then
		return true
	end
	if g_TestCombat and g_TestCombat.skip_deployment then
		return true
	end	
	-- temp comment out, check SetupDeployOrExploreUI
	--[[if mode == "attack" or mode == "defend" then
		return false
	end]]

	local currentSector = gv_Sectors[gv_CurrentSectorId]
	local conflict = IsConflictMode(gv_CurrentSectorId)

	if not currentSector.enabled_auto_deploy or not conflict then
		return true
	end

	if g_GoingAboveground then
		return true
	end

	return false
end

function SetDeploymentMode(deploy)
	local defend_mode = deploy == "defend" or not deploy and gv_Deployment == "defend"
	gv_Deployment = deploy
	deploy = not not deploy
	local update_visuals = {}
	if defend_mode then
		if deploy then
			MapForEachMarker("GridMarker", nil, function(marker)
				if (marker.Type == "Defender" or marker.Type == "DefenderPriority") and marker:IsMarkerEnabled() then
					table.insert_unique(g_InteractableAreaMarkers, marker)
					update_visuals[#update_visuals + 1] = marker
				end
			end)
		else
			MapForEachMarker("GridMarker", nil, function(marker)
				if (marker.Type == "Defender" or marker.Type == "DefenderPriority") and marker:IsMarkerEnabled() then
					marker:RemoveFloatTxt()
					table.remove_value(g_InteractableAreaMarkers, marker)
					update_visuals[#update_visuals + 1] = marker
				end
			end)
		end
		
	else
		update_visuals = MapGetMarkers("Entrance", g_GoingAboveground and "Underground" or nil)
		table.iappend(update_visuals, MapGetMarkers("DeployArea"))
		if deploy then
			if not g_GoingAboveground then
				MapForEachMarker("GridMarker", nil, function(marker)
					if marker:IsKindOf("DeploymentMarker") then
						table.insert_unique(g_InteractableAreaMarkers, marker)
					end
				end)
			end
		else
			if not g_GoingAboveground then
				MapForEachMarker("GridMarker", nil, function(marker)
					if marker:IsKindOf("DeploymentMarker") then
						marker:RemoveFloatTxt()
						table.remove_value(g_InteractableAreaMarkers, marker)
					end
				end)
			end
		end
	end
	for _, marker in ipairs(update_visuals) do
		marker.Reachable = true--not deploy
		if marker.area_ground_mesh then
			marker.area_ground_mesh:UpdateState()
		end
		marker:UpdateVisuals(deploy and "DeployArea" or marker.Type, "force")
		marker:RecalcAreaPositions()
		if not marker:IsAreaVisible() then
			marker:HideArea()
		end
	end
	if not deploy then
		UpdateAvailableDeploymentMarkers()
		HideTacticalNotification("deployMode")
		Msg("DeploymentModeDone")
	end
	Msg("DeploymentModeSet", deploy)
end

function TFormat.DeployModeNotif(context_obj)
	local non_deployed = 0
	local non_deployed_lcl_player = 0
	local deployed = 0
	local team = GetCurrentTeam()
	local totalUnits = 0
	if team then
		for i, u in ipairs(team.units) do
			if not IsUnitDeployed(u) then
				non_deployed = non_deployed + 1
				if u:IsLocalPlayerControlled() then
					non_deployed_lcl_player = non_deployed_lcl_player + 1
				end
			else
				deployed = deployed + 1
			end
		end
		totalUnits = #team.units
	end
	local sector = gv_Sectors[gv_CurrentSectorId]
	if non_deployed_lcl_player <= 0 and non_deployed > 0 then
		local other_player_info = GetOtherNetPlayerInfo()
		return T{616675804493, "<other_player> Deploying", other_player = Untranslated(other_player_info and other_player_info.name or "N/A")}
	else
		return T{673244787391, "Deploy Merc(s) (<deployed>/<total>)", deployed = deployed, total = totalUnits}
	end
end

function TFormat.IntelForSector(context_obj)
	local sector = gv_Sectors[gv_CurrentSectorId]
	if sector and sector.Intel and not sector.intel_discovered then
		return T(244800584080, "No Intel for this sector")
	end
	return false
end

function GetDeploymentAreaRollover(marker)
	if marker.DeployRolloverText ~= "" then
		return marker.DeployRolloverText
	end

	if marker.Type == "Entrance" then
		if marker:IsInGroup("North") then
			return T(147747736813, "North Deployment Zone")
		elseif marker:IsInGroup("South") then
			return T(565574703512, "South Deployment Zone")
		elseif marker:IsInGroup("East") then
			return T(189571269539, "East Deployment Zone")
		elseif marker:IsInGroup("West") then
			return T(998300938139, "West Deployment Zone")
		end
	end
	return T(419061570457, "Deployment Area")
end

OnMsg.CustomInteractableEffectsDone = UpdateAvailableDeploymentMarkers

function IsUnitSeenByAnyDeploymentMarker(unit, markers)
	markers = markers or GetAvailableDeploymentMarkers()
	-- If the unit can see the marker, we consider it seeing back.
	-- This will cause weather effects and such to apply.
	-- The code below is a circle x rectangle collision.
	local unitSightRadius = unit:GetSightRadius()
	local ux, uy = unit:GetPosXYZ()
	local half_slabsize = const.SlabSizeX / 2
	for i, m in ipairs(markers) do
		local mx, my = m:GetPosXYZ()
		local distX = abs(ux - mx)
		local markerWidth = m.AreaWidth * half_slabsize
		local dx = distX - markerWidth
		if dx <= unitSightRadius then
			local distY = abs(uy - my)
			local markerHeight = m.AreaHeight * half_slabsize
			local dy = distY - markerHeight
			if dy <= unitSightRadius then
				if distX <= markerWidth / 2 or distY <= markerHeight / 2 then
					return true
				end
				-- Pythagoras
				if dx * dx + dy * dy <= unitSightRadius * unitSightRadius then
					return true
				end
			end
		end
	end
	return false
end

function IsStuckedMercPos(unit, pos, pfclass, destinations)
	if not pfclass then
		pfclass = CalcPFClass("player1")
	end
	if not destinations then
		destinations = {}
		local markers = GetAvailableEntranceMarkers(unit.arrival_dir)
		for i, marker in ipairs(markers) do
			local pos = GetPassSlab(marker)
			if pos then
				table.insert(destinations, pos)
			end
		end
	end
	if #destinations == 0 then
		return false
	end
	local has_path, closest_pos = pf.HasPosPath(pos, destinations, pfclass)
	if has_path and table.find(destinations, closest_pos) then
		return false
	end
	return true
end

function HasStuckedMercs()
	local destinations, dummy
	local pfclass = CalcPFClass("player1")
	for _, t in ipairs(g_Teams) do
		if t.side == "player1" or t.side == "player2" then
			for i, unit in ipairs(t.units) do
				if unit:IsLocalPlayerControlled() and unit:IsValidPos() and not unit:IsDead() then
					if not destinations then
						local markers = GetAvailableEntranceMarkers(unit.arrival_dir)
						if not markers then
							return
						end
						destinations = {}
						for i, marker in ipairs(markers) do
							local pos = GetPassSlab(marker)
							if pos then
								table.insert(destinations, pos)
							end
						end
					end
					if #destinations == 0 then
						return
					end
					-- invalidate the path
					local start_pos = unit.traverse_tunnel and unit.traverse_tunnel:GetExit() or GetPassSlab(unit) or unit:GetPos()
					local pfflags = const.pfmImpassableSource
					local has_path, closest_pos = pf.HasPosPath(start_pos, destinations, pfclass, 0, 0, nil, 0, nil, pfflags)
					if not has_path or not table.find(destinations, closest_pos) then
						return true
					end
				end
			end
		end
	end
	DoneObject(dummy)
	return false
end

function RedeploymentCheck()
	local redeploy = false
	if mapdata.GameLogic and Game and not g_Combat then
		if HasStuckedMercs() then
			redeploy = true
		end
	end
	gv_Redeployment = redeploy
	ObjModified("gv_Redeployment")
end

function RedeploymentCheckDelayed()
	if not mapdata.GameLogic or not Game then
		return
	elseif g_Combat then
		return
	elseif IsValidThread(RedeploymentThread) then
		return
	elseif GameState.disable_redeploy_check then
		return
	end
	RedeploymentThread = CreateGameTimeThread(function()
		Sleep(2000)
		RedeploymentCheck()
	end)
end

OnMsg.OnPassabilityChanged = RedeploymentCheckDelayed
OnMsg.CombatEnd = RedeploymentCheckDelayed