/
CrimeTracking.lua
124 lines (95 loc) · 3.05 KB
/
CrimeTracking.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
-- Copyright © 2008-2016 Pioneer Developers. See AUTHORS.txt for details
-- Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
local Event = import("Event")
local Game = import("Game")
local Engine = import("Engine")
local Timer = import("Timer")
local Serializer = import("Serializer")
local Legal = import("Legal")
-- Fine at which police will launch and hunt donwn outlaw player
local maxFineTolerated = 300
-- store which station sent them out
local policeDispatched = false
-- check if we should dispatch police, or call them back
local function doLawAndOrder ()
if Game.player.flightState == "HYPERSPACE" then return end
local crimes, fine = Game.player:GetCrimeOutstanding()
if not policeDispatched then
if fine > maxFineTolerated and
Game.player.flightState == "FLYING" and
Engine.rand:Integer(0,1) > Game.system.lawlessness then
local station = Game.player:FindNearestTo("SPACESTATION")
if station.lawEnforcedRange >= station:DistanceTo(Game.player) then
station:LaunchPolice(Game.player)
policeDispatched = station
end
return
end
end
-- if police are out flying about, check if they should land
-- Note: If player docks police will land.
if policeDispatched then
if fine < maxFineTolerated or
Game.player.flightState == "DOCKED" or Game.player.flightState == "DOCKING" then
policeDispatched:LandPolice()
policeDispatched = false
end
end
end
local loaded_data
local onGameStart = function ()
if (loaded_data) then
policeDispatched = loaded_data.policeDispatched
end
loaded_data = nil
Timer:CallEvery(5, doLawAndOrder)
end
local serialize = function ()
local data = {
policeDispatched = policeDispatched,
}
return data
end
local unserialize = function (data)
loaded_data = data
end
local onJettison = function(ship, cargo)
if ship:IsPlayer() then
if cargo.price <= 0 or not Game.system:IsCommodityLegal(cargo) then
Legal:notifyOfCrime(ship,"DUMPING")
end
end
end
local onShipHit = function(ship, attacker)
if attacker and attacker:IsPlayer() then
Legal:notifyOfCrime(ship,"PIRACY")
end
end
local onShipDestroyed = function(ship, attacker)
-- Note: crash issue #887, this _should_ no longer trigger crash.
-- Also, attacker can be a body, which does not have an IsPlayer()
if attacker and attacker:isa("Ship") and attacker:IsPlayer() then
Legal:notifyOfCrime(ship,"MURDER")
end
end
local onShipFiring = function(ship)
if ship:IsPlayer() then
Legal:notifyOfCrime(ship,"WEAPONS_DISCHARGE")
end
end
local onLeaveSystem = function(ship)
if not ship:IsPlayer() then return end
-- if we leave the system, the space station object will be invalid
policeDispatched = nil
end
local onGameEnd = function ()
policeDispatched = nil
end
Event.Register("onShipHit", onShipHit)
Event.Register("onShipDestroyed", onShipDestroyed)
Event.Register("onShipFiring", onShipFiring)
Event.Register("onJettison", onJettison)
Event.Register("onGameStart", onGameStart)
Event.Register("onGameEnd", onGameEnd)
Event.Register("onLeaveSystem", onLeaveSystem)
Serializer:Register("CrimeTracking", serialize, unserialize)