-
Notifications
You must be signed in to change notification settings - Fork 7
/
defaultscript.lua
135 lines (118 loc) · 3.68 KB
/
defaultscript.lua
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
-- Ici Lynix
Spaceship.TimeSinceStart = 0
Spaceship.CurrentTarget = nil
Spaceship.FriendList = {}
Spaceship.NextBroadcast = 0
local password = "MieuxQueStarCitizen"
function Spaceship:OnStart()
for k,moduleInfo in pairs(self.Core:GetModules()) do
local moduleType = moduleInfo:GetType()
for name,index in pairs(ModuleType) do
if (moduleType == index) then
self[name] = moduleInfo
break
end
end
end
assert(self.Communications)
assert(self.Engine)
assert(self.Navigation)
assert(self.Radar)
assert(self.Weapon)
end
function Spaceship:OnCommunicationReceivedMessages(messages)
for k,message in pairs(messages) do
local friendSignature = message.data:match("^" .. password .. " (-?%d+)$")
if (friendSignature) then
friendSignature = tonumber(friendSignature)
self.FriendList[friendSignature] = true
if (self.CurrentTarget == friendSignature) then
self:SetTarget(nil)
self:FindNewTarget()
end
end
end
end
function Spaceship:OnTick(elapsedTime)
--print("OnTick(" .. elapsedTime .. ")")
self.TimeSinceStart = self.TimeSinceStart + elapsedTime
if (self.TimeSinceStart > self.NextBroadcast) then
self.Communications:BroadcastSphere(500.0, password .. " " .. tostring(self.Core:GetSignature()))
self.NextBroadcast = self.TimeSinceStart + 3
end
if (not self.isFleeing) then
if (self.CurrentTarget) then
local targetInfo = self.Radar:GetTargetInfo(self.CurrentTarget)
if (targetInfo) then
local forwardVec = self.Core:GetRotation() * Vec3.Forward
if (forwardVec:DotProduct(targetInfo.direction) > 0.99) then
self.Weapon:Shoot()
end
else
self:SetTarget(nil)
self:FindNewTarget()
end
else
self:FindNewTarget()
end
end
end
function Spaceship:OnNavigationDestinationReached()
--print("OnNavigationDestinationReached")
if (self.CurrentTarget) then
if (self.isFleeing) then
self.isFleeing = false
if (self.CurrentTarget) then
self.Navigation:FollowTarget(self.CurrentTarget, 60)
end
else
local currentTargetInfo = self.Radar:GetTargetInfo(self.CurrentTarget)
if (currentTargetInfo) then
local rotation = self.Core:GetRotation()
local fleeingPos = self.Core:GetPosition() + rotation * Vec3.Forward * 200 + rotation * Vec3.Up * 50
self.Engine:Impulse(Vec3.Up, 2)
self.isFleeing = true
self.Navigation:MoveToPosition(fleeingPos, 20)
end
end
end
end
function Spaceship:OnRadarNewObjectInRange(objectSignature, objectEmSignature, objectSize, direction, distance)
--print("OnRadarNewObjectInRange(" .. objectSignature .. ", " .. objectSize .. ", " .. tostring(direction) .. ", " .. distance .. ")")
if (objectSignature ~= 0 and not self.FriendList[objectSignature] and objectEmSignature < 100.0) then
if (self.CurrentTarget) then
local targetInfo = self.Radar:GetTargetInfo(self.CurrentTarget)
if (not targetInfo or targetInfo.distance > distance) then
self:SetTarget(objectSignature)
end
else
self:SetTarget(objectSignature)
end
end
end
function Spaceship:SetTarget(signature)
--print("SetTarget(" .. tostring(signature) .. ")")
self.CurrentTarget = signature
self.isFleeing = false
if (signature) then
self.Navigation:FollowTarget(signature, 60)
end
end
function Spaceship:FindNewTarget()
--print("FindNewTarget")
local closestTarget = nil
local closestTargetDist = math.huge
local ourPos = self.Core:GetPosition()
for _,info in pairs(self.Radar:Scan()) do
if (info.emSignature < 100 and info.distance < closestTargetDist and not self.FriendList[info.signature]) then
closestTarget = info.signature
closestTargetDist = info.distance
end
end
if (closestTarget) then
self:SetTarget(closestTarget)
return true
else
return false -- No target in range
end
end