1
/*jslint indent: 4, maxlen: 120, maxerr: 50, white: true, es5: true, undef: true, regexp: true, newcap: true */
2
/*jshint es5: true, undef: true, eqnull: true, noempty: true, eqeqeq: true, boss: true, loopfunc: true, laxbreak: true,
3
strict: true, curly: true */
4
/*global worldScripts, log, Timer, addFrameCallback, removeFrameCallback, isValidFrameCallback, Vector3D */
6
/* Jaguar Company Base Buoy
8
* Copyright © 2012 Richard Thomas Harrison (Tricky)
10
* This work is licensed under the Creative Commons
11
* Attribution-Noncommercial-Share Alike 3.0 Unported License.
13
* To view a copy of this license, visit
14
* http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter
15
* to Creative Commons, 171 Second Street, Suite 300, San Francisco,
16
* California, 94105, USA.
18
* Ship related functions for the base buoy.
24
/* Standard public variables for OXP scripts. */
25
this.name = "jaguar_company_base_buoy.js";
26
this.author = "Tricky";
27
this.copyright = "© 2012 Richard Thomas Harrison (Tricky)";
28
this.license = "CC BY-NC-SA 3.0";
29
this.description = "Ship script for the Jaguar Company Base Buoy.";
32
/* Private variable. */
35
/* Ship event callbacks. */
37
/* Initialise various variables on ship birth. */
38
this.shipSpawned = function () {
39
/* No longer needed after setting up. */
40
delete this.shipSpawned;
42
/* Initialise the p_buoy variable object.
43
* Encapsulates all private global data.
46
/* Cache the world scripts. */
47
mainScript : worldScripts["Jaguar Company"],
48
attackersScript : worldScripts["Jaguar Company Attackers"],
49
/* Local copies of the logging variables. */
50
logging : worldScripts["Jaguar Company"].$logging,
51
logExtra : worldScripts["Jaguar Company"].$logExtra
54
/* Register this buoy as a friendly. */
55
p_base.attackersScript.$addFriendly(this.ship);
56
/* Wait 5 seconds then find the witchpoint. */
57
p_buoy.nextTarget = "WITCHPOINT";
58
this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 5);
61
/* Base buoy was removed by script. */
62
this.shipRemoved = function (suppressDeathEvent) {
63
if (suppressDeathEvent) {
67
/* Reset the script check. */
68
worldScripts["Jaguar Company"].$buoyOK = false;
69
/* Force a launch of a new buoy. */
70
worldScripts["Jaguar Company"].$buoyLaunched = false;
73
/* The base buoy has just become invalid. */
74
this.entityDestroyed = function () {
75
/* Reset the script check. */
76
worldScripts["Jaguar Company"].$buoyOK = false;
77
/* Force a launch of a new buoy. */
78
worldScripts["Jaguar Company"].$buoyLaunched = false;
79
/* Stop and remove the frame callback and timer. */
80
this.$removeBuoyTimer();
81
this.$removeBuoyFCB();
84
/* Stop and remove the timer. */
85
this.$removeBuoyTimer = function () {
86
if (this.$buoyTimerReference) {
87
if (this.$buoyTimerReference.isRunning) {
88
this.$buoyTimerReference.stop();
91
delete this.$buoyTimerReference;
95
/* Stop and remove the frame callback. */
96
this.$removeBuoyFCB = function () {
97
/* Turn the flashers on. */
98
this.ship.lightsActive = true;
100
if (this.$buoyFCBReference) {
101
if (isValidFrameCallback(this.$buoyFCBReference)) {
102
removeFrameCallback(this.$buoyFCBReference);
105
delete this.$buoyFCBReference;
109
/* Point the dish at Jaguar Company Patrol. */
110
this.$findJaguarCompanyPatrol = function () {
116
/* Search for the patrol ships. */
117
patrolShips = system.shipsWithPrimaryRole("jaguar_company_patrol");
119
if (!patrolShips.length) {
120
/* We are on our own. Point the dish at the witchpoint. */
121
return p_buoy.mainScript.$witchpointBuoy.position;
124
/* Cache the length. */
125
patrolShipsLength = patrolShips.length;
127
/* Work out the midpoint position of all the patrol ships. */
128
midpointPosition = new Vector3D(0, 0, 0);
130
for (patrolShipsCounter = 0; patrolShipsCounter < patrolShipsLength; patrolShipsCounter += 1) {
131
midpointPosition = midpointPosition.add(patrolShips[patrolShipsCounter].position);
134
midpointPosition.x /= patrolShipsLength;
135
midpointPosition.y /= patrolShipsLength;
136
midpointPosition.z /= patrolShipsLength;
138
return midpointPosition;
141
this.$buoyTimer = function () {
142
var buoy = this.ship,
146
if (p_buoy.nextTarget === "JAGUAR_COMPANY_PATROL") {
147
/* Find the position of then patrol ships. */
148
position = this.$findJaguarCompanyPatrol();
149
} else if (p_buoy.nextTarget === "PLANET") {
150
this.$removeBuoyTimer();
151
/* Wait 30 seconds then track Jaguar Company Patrol every 1 minute. */
152
p_buoy.nextTarget = "JAGUAR_COMPANY_PATROL";
153
this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 30, 60);
154
/* Find the position of the main planet. */
155
position = system.mainPlanet.position;
157
this.$removeBuoyTimer();
159
if (system.isInterstellarSpace) {
160
/* Wait 30 seconds then track Jaguar Company Patrol every 1 minute. */
161
p_buoy.nextTarget = "JAGUAR_COMPANY_PATROL";
162
this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 30, 60);
164
/* Wait 30 seconds then find the planet. */
165
p_buoy.nextTarget = "PLANET";
166
this.$buoyTimerReference = new Timer(this, this.$buoyTimer, 30);
169
/* Find the position of the witchpoint. */
170
position = p_buoy.mainScript.$witchpointBuoy.position;
173
/* Vector pointing towards the target. */
174
vector = position.subtract(buoy.position).direction();
175
/* Angle to the target from current heading. */
176
p_buoy.finalAngle = buoy.heading.angleTo(vector);
178
if (p_buoy.finalAngle < 0.087266462599716478846184538424431) {
179
/* Already pointing in the rough direction of the target.
180
* Looking for a difference of greater than 5 degrees.
185
/* Cross vector for rotate. */
186
p_buoy.cross = buoy.heading.cross(vector).direction();
187
/* Starting angle. */
189
/* Should take about 5 seconds (at 60 FPS). */
190
p_buoy.deltaAngle = p_buoy.finalAngle / 300;
192
if (p_buoy.logging && p_buoy.logExtra) {
193
log(this.name, "$buoyTimer::Buoy tracking target...");
196
/* Use a frame callback to do this smoothly. */
197
this.$buoyFCBReference = addFrameCallback(this.$buoyFCB.bind(this));
200
/* Frame callback to slowly rotate the buoy towards Jaguar Company Patrol.
203
* delta - amount of game clock time past since the last frame.
205
this.$buoyFCB = function (delta) {
206
var buoy = this.ship;
208
if (!buoy || !buoy.isValid) {
209
/* Buoy can be invalid for 1 frame. */
210
this.$removeBuoyTimer();
211
this.$removeBuoyFCB();
217
/* Do nothing if paused. */
221
if (p_buoy.angle >= p_buoy.finalAngle) {
222
/* Reached the desired orientation. */
223
this.$removeBuoyFCB();
228
/* Rotate in delta angle steps. */
229
buoy.orientation = buoy.orientation.rotate(p_buoy.cross, -p_buoy.deltaAngle);
230
/* Update the current angle. */
231
p_buoy.angle += p_buoy.deltaAngle;