-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathai_task.lua
More file actions
123 lines (86 loc) · 3.03 KB
/
ai_task.lua
File metadata and controls
123 lines (86 loc) · 3.03 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
-- Serverside only.
if ( CLIENT ) then return end
local setmetatable = setmetatable
--local table = table
local ai = ai
module( "ai_task" )
--[[---------------------------------------------------------
ENUMs for which kind of task it is.
-----------------------------------------------------------]]
local TYPE_ENGINE = 1
local TYPE_FNAME = 2
--[[---------------------------------------------------------
Keep track of created tasks
UNDONE: There's no need for this right now.
-----------------------------------------------------------]]
--local Task_Index = {}
--[[---------------------------------------------------------
Task metatable
-----------------------------------------------------------]]
local Task = {}
Task.__index = Task
function Task:Init()
self.Type = nil
end
--[[---------------------------------------------------------
Creates an engine based task
-----------------------------------------------------------]]
function Task:InitEngine( _taskname_, _taskdata_ )
self.TaskName = _taskname_
self.TaskID = nil
self.TaskData = _taskdata_
self.Type = TYPE_ENGINE
end
--[[---------------------------------------------------------
Creates an engine based task
-----------------------------------------------------------]]
function Task:InitFunctionName( _start, _end, _taskdata_ )
self.StartFunctionName = _start
self.FunctionName = _end
self.TaskData = _taskdata_
self.Type = TYPE_FNAME
end
function Task:IsEngineType()
return ( self.Type == TYPE_ENGINE )
end
function Task:IsFNameType()
return ( self.Type == TYPE_FNAME )
end
function Task:Start( npc )
if ( self:IsFNameType() ) then self:Start_FName( npc ) return end
if ( self:IsEngineType() ) then
if ( !self.TaskID ) then self.TaskID = ai.GetTaskID( self.TaskName ) end
npc:StartEngineTask( self.TaskID, self.TaskData )
end
end
--[[---------------------------------------------------------
Start_FName (called from Task:Start)
-----------------------------------------------------------]]
function Task:Start_FName( npc )
if ( !self.StartFunctionName ) then return end
--if ( !npc[ self.StartFunctionName ] ) then return end
-- Run the start function. Safely.
npc[ self.StartFunctionName ]( npc, self.TaskData )
end
function Task:Run( npc )
if ( self:IsFNameType() ) then self:Run_FName( npc ) return end
if ( self:IsEngineType() ) then
npc:RunEngineTask( self.TaskID, self.TaskData )
end
end
function Task:Run_FName( npc )
if ( !self.FunctionName ) then return end
--if (!npc[ self.StartFunctionName ]) then return end
-- Run the start function. Safely.
npc[ self.FunctionName ]( npc, self.TaskData )
end
--[[---------------------------------------------------------
Create a new empty task (this is ai_task.New )
-----------------------------------------------------------]]
function New()
local pNewTask = {}
setmetatable( pNewTask, Task )
pNewTask:Init()
--table.insert( Task_Index, pNewTask )
return pNewTask
end