~harrison-rt/+junk/jaguar_company

« back to all changes in this revision

Viewing changes to tags/2.3/jaguar_company_2.3.oxp/Scripts/jaguar_company_asteroid.js

  • Committer: Richard Harrison
  • Date: 2013-01-19 23:01:03 UTC
  • Revision ID: harrison.rt@gmail.com-20130119230103-hukuh0h9jorz3hk1
Tags: 2.4
* Bug fix for spawning Jaguar Company. Potential to spawn the base at all times if the Galactic Navy wasn't present.
* Bug fix for checking the safe zone around the base. Wasn't checking to see if it actually existed.
* Main world script saves the last system ID that was visited for interstellar space.
* Timer on start up for the worldscripts has been added to allow each worldscript to be loaded in. Stops a potential dependency error happening.
* Stray commas in the route list arrays removed.
* Welcome code has been cleaned up.
* missiontext.plist and descriptions.plist cleaned up.
* Altered all player consoleMessage's to show messages for the default time limit.
* Changed all player commsMessage's to consoleMessage.
* Changed some of the player consoleMessage's back into commsMessage.
* Only insert news into Snoopers about battle help if more than 10 minutes has passed since the last news item was inserted.
* If you set $alwaysSpawn to true with OXPConfig the base will be spawned if it doesn't exist.
* Patrol ships follow the Galactic Navy.
** If by some freak accident, all of the Galactic Navy is destroyed, the patrol ships will go back to base if it exists. Otherwise they will patrol the witchpoint to planet lane.
* New reputation level. Shows the location of Jaguar Company Bases in the current galaxy.
** Re-implementation of the game random number generators for system.pseudoRandomNumber and system.scrambledPseudoRandomNumber, to allow checking of all the systems in the current galaxy. The system versions only gives back a value for the current system.
** Oolite v1.76.1 or older only show a list of system names. Go to the long range chart then select the status screen (F6, F6, F5). Borrowed some code from Spara's Trophy Collector OXP for this.
** Oolite v1.77 or newer display the locations on the long range chart. Uses the new object method of mission.markSystem so it won't intefere with other OXPs. (Hopefully)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 */
 
5
 
 
6
/* Jaguar Company Asteroid
 
7
 *
 
8
 * Copyright © 2012 Richard Thomas Harrison (Tricky)
 
9
 *
 
10
 * This work is licensed under the Creative Commons
 
11
 * Attribution-Noncommercial-Share Alike 3.0 Unported License.
 
12
 *
 
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.
 
17
 *
 
18
 * Ship related functions for the asteroids cluttering the space around the base.
 
19
 */
 
20
 
 
21
(function () {
 
22
    "use strict";
 
23
 
 
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.";
 
30
    this.version = "1.1";
 
31
 
 
32
    /* Ship event callbacks. */
 
33
 
 
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"],
 
37
        asteroid = this.ship,
 
38
        base = mainScript.$jaguarCompanyBase,
 
39
        /* Increased to 12km for the first try. */
 
40
        safeDistance = 11000,
 
41
        asteroidMoved = 0,
 
42
        distance,
 
43
        totalDistanceMoved = 0,
 
44
        oldDistance,
 
45
        newDistance,
 
46
        direction,
 
47
        entities,
 
48
        ok = false;
 
49
 
 
50
        /* No longer needed after setting up. */
 
51
        delete this.shipSpawned;
 
52
 
 
53
        if (!base || !base.isValid ||
 
54
            (!asteroid.hasRole("jaguar_company_asteroid") && !asteroid.hasRole("jaguar_company_boulder"))) {
 
55
            /* Not an asteroid spawned by the base. */
 
56
            return;
 
57
        }
 
58
 
 
59
        /* Stop warnings about anonymous local functions within loops.
 
60
         * Used by 'system.filteredEntities'. Returns true for any valid entity.
 
61
         */
 
62
        function $validEntity(entity) {
 
63
            return (entity && entity.isValid);
 
64
        }
 
65
 
 
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);
 
70
 
 
71
        if (distance < safeDistance) {
 
72
            direction = asteroid.position.subtract(base.position).direction();
 
73
 
 
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.
 
79
                 */
 
80
                newDistance = oldDistance = distance;
 
81
 
 
82
                while (!ok) {
 
83
                    asteroidMoved += 1;
 
84
                    /* Increase the safe distance by 1km. */
 
85
                    safeDistance += 1000;
 
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;
 
96
                }
 
97
 
 
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);
 
105
                }
 
106
            }
 
107
        }
 
108
    };
 
109
}).call(this);