Config/script.js |
"use strict";
this.name = "oreProcessor";
this.author = "Eric, spara";
this.copyright = "Creative Commons: attribution, non-commercial, sharealike.";
this.description = "Ore processor main";
//2.2.1 Gem-Stones bug fix for Oolite 1.81
//2.2 scanner is now a separate equipment/upgrade. Failure chance is removed.
//2.1.1 added support for manifest_mfd
//2.1 added support for griff's additional shipset splinters
//2.1 added the possibility to hardwire the processor to work without activation
this.splinter = new Array(); // array that will hold a list of splinter records
this.startUp = function () {
this.oreTimer = new Timer(this, this.processOre, 5, 0.5);
this.oreTimer.stop();
this.$scanTimer = new Timer(this, this.$scanForMinerals, 1, 1);
this.$scanTimer.stop();
this.mySound = new SoundSource;
this.$scanSound = new SoundSource;
this.mySound.sound = "op_process.ogg";
this.mySound.loop = false;
}
this.processOre = function () {
//remove lost splinters from the array and stop processing if we run out splinters
while (this.splinter[0].status !== "STATUS_IN_HOLD") {
//remove dumped splinter from process array
this.splinter.shift();
//stop processing if out of splinters
if (this.splinter.length === 0) {
this.oreTimer.stop();
this.mySound.stop();
return;
}
}
// busy processing splinter
if (parseInt(this.splinter[0].$oreProcessor.count) > 0 && player.ship.energy > 6.4) {
player.consoleMessage("Processing ore.");
this.splinter[0].$oreProcessor.count--;
player.ship.energy -= 6.4;
}
else {
if (this.toggle) this.toggle=false; else this.toggle = true; // to avoid identical message supression.
if (parseInt(this.splinter[0].$oreProcessor.count) === 0) {
//process splinter
if (this.splinter[0].status === "STATUS_IN_HOLD") {
player.consoleMessage(expandDescription(this.splinter[0].$oreProcessor.message)+(this.toggle?"":" "));
//1.81 fix
if (this.splinter[0].$oreProcessor.type === "Gem-Stones" && oolite.compareVersion("1.80") === -1)
this.splinter[0].setCargo("gem_stones", this.splinter[0].$oreProcessor.quantity);
else
this.splinter[0].setCargo(this.splinter[0].$oreProcessor.type.toLowerCase(), this.splinter[0].$oreProcessor.quantity);
this.splinter[0].displayName = "Processed Splinter ("+this.splinter[0].$oreProcessor.type+")";
this.splinter.shift(); // remove the processed splinter from the unprocessed splinter array.
//notify manifest_mfd about content cargo change
if (worldScripts["manifest_mfd"])
worldScripts["manifest_mfd"].notifyCargoChange();
}
}
else {
//stop processing if out of energy
player.consoleMessage("Not enough energy, processing aborted!!");
this.oreTimer.stop();
this.mySound.stop();
return;
}
// restart the sound for next splinter
if (this.splinter.length > 0) this.mySound.play(10);
else {
//last splinter processed, lets stop
this.oreTimer.stop();
this.mySound.stop();
}
}
}
//Add contents to splinters
this.$addValuablesToSplinter = function(ship)
{
//Check if the splinter has already been handled
if (!ship.$oreProcessor) {
var type, quantity, message;
//handle custom splinters
if (ship.scriptInfo.cargotype) {
type = ship.scriptInfo.cargotype;
quantity = parseInt(ship.scriptInfo.quantity);
message = ship.scriptInfo.message;
}
//handle regular splinters
else {
var randomOre = Math.floor(992 * Math.random()); //0 - 991
if (randomOre < 300) {
type = "Alloys";
quantity = 1;
message = "@1 ton of Alloys extracted.";
}
else if (randomOre < 315) {
type = "Gem-Stones";
quantity = -5;
message = "Extracted @1 gr of Gems from the splinter!!";
}
else if (randomOre < 330) {
type = "Gem-Stones";
quantity = -7;
message = "Extracted @1 gr of Gems from the splinter!!";
}
else if (randomOre < 346) {
type = "Gem-Stones";
quantity = -10;
message = "Extracted @1 gr of Gems from the splinter!!";
}
else if (randomOre < 386) {
type = "Gold";
quantity = -2;
message = "@1 kilogram of Gold extracted.";
}
else if (randomOre < 396) {
type = "Gold";
quantity = -3;
message = "@1 kilogram of Gold extracted.";
}
else if (randomOre < 406) {
type = "Gold";
quantity = -6;
message = "@1 kilogram of Gold extracted.";
}
else if (randomOre < 421) {
type = "Platinum";
quantity = -2;
message = "@1 kilogram of Platinum extracted.";
}
else if (randomOre < 444) {
type = "Platinum";
quantity = -3;
message = "@1 kilogram of Platinum extracted.";
}
else if (randomOre < 452) {
type = "Platinum";
quantity = -6;
message = "@1 kilogram of Platinum extracted.";
}
else if (randomOre < 592) {
type = "Radioactives";
quantity = 1;
message = "@1 ton of Radioactives extracted.";
}
else {
type = "Minerals";
quantity = 1;
message = "@1 ton of Common Minerals.";
}
}
if (quantity < 0)
quantity= Math.ceil(Math.random()*(-quantity)); // only negative values are randomised.
if (quantity > 1) {
message = message.replace("gr ", "grs ");
message = message.replace("kilogram ", "kilograms ")
}
ship.$oreProcessor = { message: message.replace("@1", quantity),
type: type,
quantity: quantity,
//randomize hardness of the splinter
count: 5 + Math.random() * 15};
}
}
//Add scooped splinter to splinter array.
this.shipScoopedOther = function(whom) {
//Check that the scooped item is a splinter, contains minerals and is not scripted
if ((whom.primaryRole !== "splinter" && whom.primaryRole !== "griff_splinter") || whom.commodity !== "minerals" || whom.script.name !== "oolite-default-ship-script") return;
//Check that the splinter is not already added to prevent double booking. This happens when scooping, dumping, scooping...
for (var i = 0; i < this.splinter.length; i++) {
if (this.splinter[i] === whom)
this.splinter.splice(i, 1);
}
//Add valuables to the scooped splinter
this.$addValuablesToSplinter(whom);
//Add unprocessed scooped splinter to the list of unprocessed splinters
if (whom.$oreProcessor.count > 0) this.splinter.push(whom);
//Automagically start processing if the processor has been hardwired
if (player.ship.equipmentStatus("EQ_HARDWIRE_ORE_PROCESSOR") === "EQUIPMENT_OK" && player.ship.equipmentStatus("EQ_ORE_PROCESSOR") === "EQUIPMENT_OK" && this.splinter.length > 0 && !this.oreTimer.isRunning) {
this.$extract();
}
}
//Activate/deactivate the ore processor
this.$extract = function() {
//Start processing
if (!this.oreTimer.isRunning) {
if (player.ship.energy > 6.4) {
var process = false;
//check that there is a splinter to process in the hold. If unprocessed splinter has been dumped or it is destroyed, it cannot be processed, thus the record is removed.
for (var i = this.splinter.length; i > 0; i--) {
if (this.splinter[i-1].status === "STATUS_IN_HOLD") {
process = true;
break;
}
else this.splinter.pop();
}
//found something to process, let's go
if (process) {
this.oreTimer.start();
if (!this.mySound.isPlaying) this.mySound.play(10);
}
//nothing to process, let's not go
else player.consoleMessage("No unprocessed splinters to process.");
}
else player.consoleMessage("Not enough energy to start processing.");
}
//Stop processing
else {
this.oreTimer.stop();
this.mySound.stop();
player.consoleMessage("Processing aborted.");
}
}
//Activate scan
this.$activateScan = function() {
if (!this.$scanTimer.isRunning) {
if (player.ship.energy > 3.2) {
this.$pass = 0;
this.$originalTarget = player.ship.target;
//randomize the time that a scan will take.
//this.$scanRounds = 5 + Math.round(Math.random() * 5);
this.$scanTimer = new Timer(this, this.$scanForMinerals, 0);
}
else player.consoleMessage("Not enough energy to start scanning.");
}
else {
this.$scanTimer.stop();
this.$scanSound.stop();
player.consoleMessage("Scanning aborted.");
}
}
//The actual scanning function
this.$scanForMinerals = function() {
var target = player.ship.target;
//notify sound
this.$scanSound.sound = "[@boop]";
//No target
if (!target) {
this.$scanSound.play();
player.consoleMessage("No target to scan.");
return;
}
//Target switched while scanning
if (this.$originalTarget !== target) {
this.$scanSound.play();
player.consoleMessage("Target lost, scan aborted");
return;
}
//Target is not a splinter
if (target.primaryRole !== "splinter" && target.primaryRole !== "griff_splinter") {
this.$scanSound.play();
player.consoleMessage("Target is not a splinter.");
return;
}
//Target is too far
if (player.ship.position.distanceTo(target) > 2500) {
this.$scanSound.play();
player.consoleMessage("Target is too far for scanning.");
return;
}
//Valuables are already detected
if (target.displayName.indexOf("(") !== -1) {
this.$scanSound.play();
player.consoleMessage("Minerals already detected.");
return;
}
//If enough energy, scan
if (player.ship.energy > 3.2) {
this.$scanSound.sound = "op_scan.ogg";
this.$scanSound.play();
if (this.$pass < 9/*this.$scanRounds*/) {
player.consoleMessage("Scanning for minerals.");
player.ship.energy -= 3.2;
this.$pass++;
this.$scanTimer = new Timer(this, this.$scanForMinerals, 0.5);
return;
}
else {
this.$addValuablesToSplinter(target);
if (target.$oreProcessor.type !== "Minerals") {
this.$addValuablesToSplinter(target);
player.consoleMessage(target.$oreProcessor.type+" detected.");
target.displayName = target.name+" ("+target.$oreProcessor.type+")";
return;
}
}
}
//Energy low, abort
else {
this.$scanSound.play();
player.consoleMessage("Not enough energy, scan aborted.");
return;
}
//No valuables found
player.consoleMessage("No valuable minerals detected.");
target.displayName = target.name+" (Common Minerals)";
}
this.playerBoughtEquipment = function(equipment) {
if (equipment === "EQ_UNDO_HARDWIRE_ORE_PROCESSOR") {
player.ship.removeEquipment("EQ_HARDWIRE_ORE_PROCESSOR");
player.ship.removeEquipment("EQ_UNDO_HARDWIRE_ORE_PROCESSOR");
}
}
|