Menu

[0a80cc]: / base / ai / civilian.lua  Maximize  Restore  History

Download this file

169 lines (149 with data), 3.8 kB

  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
--[[
Copyright (C) 2002-2014 UFO: Alien Invasion.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
--]]
local ailc = { }
function ailc.tustouse ()
return ai.actor():TU() - 3
end
function ailc.flee()
local flee_pos = ai.positionflee(ailc.tustouse())
if flee_pos then
return flee_pos:go()
end
return false
end
function ailc.hide (team)
local hide_pos = ai.positionhide(team, ailc.tustouse())
if hide_pos then
return hide_pos:go()
end
return false
end
function ailc.herd (targets)
if #targets > 0 then
for i = 1, #targets do
local herd_pos = ai.positionherd(targets[i], ailc.tustouse())
if herd_pos then
return herd_pos:go()
end
end
end
return false
end
function ailc.approach (targets)
for i = 1, #targets do
for j = 1, 2 do
local near_pos = ai.positionapproach(targets[i], ailc.tustouse(), j == 1)
if near_pos then
return near_pos:go()
end
end
end
return false
end
function ailc.route (waypoints)
if #waypoints > 0 then
for i = 1, #waypoints do
local target_pos = ai.positionmission(waypoints[i], ailc.tustouse())
if target_pos then
if target_pos:go() then
ai.setwaypoint(waypoints[i])
return true
end
end
end
-- Can't get to any waypoints, try to approach the nearest one
return ailc.approach(waypoints)
end
end
function ailc.wander ()
local next_pos = ai.positionwander("rand", (ai.actor():TU() + 1) / 6, ai.actor():pos(), ailc.tustouse())
if next_pos then
next_pos:go()
end
end
function ailc.think_nf ()
local aliens = ai.see("sight", "alien")
if #aliens > 0 then
if not ailc.hide("alien") then
ailc.flee()
end
else
local waypoints = ai.waypoints(25, "path")
if #waypoints > 0 then
ailc.route(waypoints)
else
ai.setwaypoint(nil)
local phalanx = ai.see("sight", "phalanx")
if #phalanx > 0 then
ailc.herd(phalanx)
else
local civs = ai.see("sight", "civilian")
if #civs > 0 then
ailc.herd(civs)
else
ailc.wander()
end
end
end
end
aliens = ai.see("sight", "alien")
if #aliens > 0 then
if not ailc.hide("alien") then
ailc.flee()
end
-- Some civ models don't have crouching animations
-- ailc.crouch(true)
end
end
function ailc.think ()
if ai.actor():morale() ~= "normal" then
if not ailc.hide("~civilian") then
ailc.flee()
end
else
if not ai.isfighter() then
ailc.think_nf()
else
-- We don't currently have fight capable civ teams, so no point in implementing this yet
ai.print("Warning: no fight capable lua ai available for civilian ", ailc.actor())
ailc.think_nf()
end
end
end
--[[
Team think function, just run the normal think function, no idea what team tactics would be for civilians
--]]
function ailc.team_think ()
-- Round just started, set up.
if ailc.squad == nil then
ailc.squad = ai.squad()
ailc.actor = 1
-- Run next actor
else
ailc.actor = ailc.actor + 1
end
-- Done with this round.
if ailc.actor > #ailc.squad then
ailc.squad = nil
return false
end
-- Run the think function for this actor
if not ailc.squad[ailc.actor]:isdead() then
ai.select(ailc.squad[ailc.actor])
ailc.think()
end
return true
end
return ailc