File: soldier.lua

package info (click to toggle)
ufoai 2.5-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 82,120 kB
  • sloc: cpp: 225,232; python: 5,111; ansic: 4,133; php: 2,209; perl: 1,931; sh: 1,505; xml: 1,115; makefile: 406; sed: 11
file content (96 lines) | stat: -rw-r--r-- 1,679 bytes parent folder | download | duplicates (6)
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
--[[
	AI Entry point.
--]]
function think()
	local alien = ai.see("all", "alien")[1]
	local civ = ai.see("all", "civilian")[1]
	local phalanx = ai.see("all", "phalanx")[1]

	if alien then
		local alienDist = ai.distance(alien)
		-- check danger close
		if alienDist < 128 then
			if phalanx then
				local phalanxDist = ai.distance(phalanx)

				-- check alien closer than phalanx
				if alienDist < phalanxDist then
					engage( alien[1] )
				else
					if ai.isinjured() then
						hide(alien:team())
					else
						herd(phalanx)
					end
				end
			else
				engage( alien[1] )
			end
		else
			if phalanx then
				local phalanxDist = ai.distance(phalanx)
				-- check alien closer than phalanx
				if alienDist < phalanxDist then
					hide(alien:team())
				else
					if civ then
						local civDist = ai.distance(civ)

						-- check phalanx closer than civilian
						if phalanxDist < civDist then
							herd(phalanx)
						else
							herd(civ)
						end
					else
						herd(phalanx)
					end
				end
			else
				if civ then
					local civDist = ai.distance(civ)

					-- check alien closer than civilian
					if alienDist < civDist then
						engage( alien[1] )
					else
						herd(civ)
					end
				else
					hide(alien:team())
				end
			end
		end
	else
		if civ then
			herd(civ)
		else
			if phalanx then
				herd(phalanx)
			else
				engage( alien[1] )
			end
		end
	end
end

--[[
	Tries to move to herd position
--]]
function herd(target)
	local pos = ai.positionherd(target)
	if pos then
		pos:goto()
	end
end

--[[
	Tries to move to hide position
--]]
function hide(team)
	local pos = ai.positionhide(team)
	if pos then
		pos:goto()
	end
	ai.crouch(false)
end