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 system, log, worldScripts */
6
/* Jaguar Company Asteroid
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 asteroids cluttering the space around the base.
24
/* Standard public variables for OXP scripts. */
25
this.name = "jaguar_company_asteroid.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 asteroids cluttering the space around the base.";
32
/* Ship event callbacks. */
34
/* Move the asteroid to a safe distance from the base launch corridor on birth. */
35
this.shipSpawned = function () {
36
var mainScript = worldScripts["Jaguar Company"],
38
base = mainScript.$jaguarCompanyBase,
39
/* Increased to 12km for the first try. */
43
totalDistanceMoved = 0,
50
/* No longer needed after setting up. */
51
delete this.shipSpawned;
53
if (!base || !base.isValid ||
54
(!asteroid.hasRole("jaguar_company_asteroid") && !asteroid.hasRole("jaguar_company_boulder"))) {
55
/* Not an asteroid spawned by the base. */
59
/* Stop warnings about anonymous local functions within loops.
60
* Used by 'system.filteredEntities'. Returns true for any valid entity.
62
function $validEntity(entity) {
63
return (entity && entity.isValid);
66
/* Don't drift. Just leave it rotating. */
67
asteroid.velocity = new Vector3D(0, 0, 0);
68
/* Work out if it is near to the base during spawning. */
69
distance = asteroid.position.distanceTo(base.position);
71
if (distance < safeDistance) {
72
direction = asteroid.position.subtract(base.position).direction();
74
/* Check the launch corridor. (0.86 from src/Core/Entities/DockEntity.m) */
75
if (direction.dot(base.heading) > 0.86) {
76
/* Asteroids added too close to the base can block launches.
77
* Move them to a safe distance (+/- 500m) from the base.
78
* Safe distance is altered if there is another entity nearby.
80
newDistance = oldDistance = distance;
84
/* Increase the safe distance by 1km. */
86
/* Work out a new distance (varied by +/- 500m). */
87
totalDistanceMoved += distance = (safeDistance - newDistance) + (500 - (Math.random() * 1000));
88
/* Move the asteroid. */
89
asteroid.position = asteroid.position.add(direction.multiply(distance));
90
/* New distance from the base launch corridor. */
91
newDistance = oldDistance + totalDistanceMoved;
92
/* Search for any entity intersecting this asteroid (plus 500m) at the new distance. */
93
entities = system.filteredEntities(this, $validEntity, asteroid, asteroid.collisionRadius + 500);
94
/* An empty array is what we are looking for. */
95
ok = !entities.length;
98
if (mainScript.$logging && mainScript.$logExtra) {
99
log(this.name, "shipSpawned::Moving " + asteroid.displayName + " to " + asteroid.position + "\n" +
100
"* Moved: " + asteroidMoved + " times\n" +
101
"* Safe distance: " + safeDistance + "\n" +
102
"* Old distance: " + oldDistance + "\n" +
103
"* Total distance moved: " + totalDistanceMoved + "\n" +
104
"* New distance: " + newDistance);