| Scripts/hyperradio2.js |
"use strict";
this.name = "hyperradio2";
this.author = "Svengali";
this.copyright = "(C)2008-2025, License:CC-by-nc-sa-3.0";
this.description = "Musicplayer. Expandable by musicpacks.";
this.version = "2.0";
this.currentChannel = expandDescription("[hyper_off]");
this.currentChannelIndex = -1;
this.savedChannelIndex = 0;
this.allowPirateRadio = false;
this.allowLicenseExtension = false;
this.canPlay = false;
this.licenseLength = 60; // increase this value to make subscription last longer
// note, though, that you should also change the description in equipment.plist to match.
this.logging = false;
this.showNews = true;
this.news = 0;
this.vNews = 31;
this.testing = false;
this.tempDisabled = false;
this.startUp = function () {
delete this.startUp;
this.libMain = worldScripts.Lib_Main._lib;
this.libMusic = worldScripts.Lib_Music;
this.libMusic._addCallback("new_song", this.name, "$updateMFD");
this.hyperRadio = false; // true if installed and license is valid
if (missionVariables.hyperradio_last) {
this.hyperradioDuration = parseInt(missionVariables.hyperradio_last, null);
} else this.hyperradioDuration = 0;
if (missionVariables.hyperradio_news) {
var tmp = missionVariables.hyperradio_news.split("|");
this.news = parseInt(tmp[0], null);
this.sysA = tmp[1];
this.sysB = tmp[2];
this.vNews = 15 + parseInt(this.news, null);
}
}
this.startUpComplete = function () {
this.libMusic._addChannel({
name: "GalaxyRadio 2.0", radio: true, sounds: [
{ snd: "hyperradio-exampleA.ogg", dur: 28 },
{ snd: "hyperradio-exampleB.ogg", dur: 13 }
]
});
// add link in hud selector
var h = worldScripts.hudselector;
if (h) h.$HUDSelectorAddMFD(this.name, this.name, expandDescription("[hyperradio_mfd_name]"));
// add link in auto-prime equipment
if (worldScripts.AutoPrimeEquipment)
worldScripts.AutoPrimeEquipment.$addConfig("HyperRadioMFD", "EQ_HYPER_RADIO");
}
this.playerWillSaveGame = function () {
missionVariables.hyperradio_last = this.hyperradioDuration;
missionVariables.hyperradio_licenseLength = this.licenseLength;
if (this.news) missionVariables.hyperradio_news = this.news + "|" + this.sysA + "|" + this.sysB;
this.saved = true;
}
this.shipLaunchedFromStation = function () {
if (this.CheckHyperradioLicense() == true) {
this.checkSwitch = new Timer(this, this.switchTimer.bind(this), 1, 1);
}
}
this.shipDockedWithStation = this.shipDied = function () {
if (this.checkSwitch && this.checkSwitch.isRunning) this.checkSwitch.stop();
this.CheckHyperradioLicense();
// turn off radio when docking
if (this.currentChannelIndex >= 0) {
this.SelectHyperradioStation(0);
this.canPlay = false;
this.hyperRadio = false;
}
}
this.shipWillLaunchFromStation = function () {
this.allowLicenseExtension = false;
if (player.ship.equipmentStatus("EQ_HYPER_RADIO") === "EQUIPMENT_OK") {
if (system.isInterstellarSpace || this.tempDisabled) {
this.canPlay = false;
return;
}
this.canPlay = true;
this.hyperRadio = true;
this.SelectHyperradioStation();
this.$updateMFD();
}
}
this.playerBoughtEquipment = function (equipmentKey) {
if (equipmentKey == "EQ_HYPER_RADIO") {
this.hyperRadio = true;
this.hyperradioDuration = Math.floor(this.licenseLength + clock.days);
this.allowLicenseExtension = true;
return;
}
if (equipmentKey == "EQ_HYPER_RADIO_ADD") {
this.hyperRadio = true;
this.hyperradioDuration += this.licenseLength;
player.ship.removeEquipment("EQ_HYPER_RADIO_ADD");
this.allowLicenseExtension = false;
return;
}
if (equipmentKey == "EQ_HYPER_RADIO_PIRATE") {
this.hyperRadio = true;
player.ship.removeEquipment("EQ_HYPER_RADIO_PIRATE");
player.ship.awardEquipment("EQ_HYPER_RADIO");
this.allowPirateRadio = false;
this.hyperradioDuration += Math.ceil(this.licenseLength / Math.log(system.government + 2.1));
this.news = 0;
return;
}
}
this.equipmentDamaged = function (equipmentKey) {
if (equipmentKey === "EQ_HYPER_RADIO") {
this.canPlay = false;
this.failPlay(1);
}
}
this.shipExitedWitchspace = function () {
if (this.logging) log(this.name, this.name + ": clock:" + (clock.days & 31) + " vNews:" + this.vNews);
//this.tempDisabled = false;
if (this.hyperRadio) {
if (system.isInterstellarSpace) {
this.currentChannel = "Off";
this.currentChannelIndex = -1;
this.libMusic.$E0 = 0;
this.libMusic._buildList();
this.failPlay(1);
player.consoleMessage("HyperRadio deactivated. Could not transfer data.", 8);
this.canPlay = false;
return;
} //else this.SelectHyperradioStation();
this.canPlay = true;
}
if (!this.showNews || system.isInterstellarSpace) return;
if (this.news) {
if (this.news === 4) {
var a = System.systemIDForName(this.sysA);
var b = System.systemIDForName(this.sysB);
if (!this.allowPirateRadio && (system.ID === a || system.ID === b)) {
this.allowPirateRadio = true;
this.news = 0;
return;
} else this.allowPirateRadio = false;
} else if (this.news === 3) {
this.news = 0;
return;
}
}
if (this.news) return;
if ((clock.days & 31) === this.vNews || this.testing == true) {
this.allowPirateRadio = false;
var a = Math.random();
if (a > 0.5) {
this.sysA = System.systemNameForID(Math.floor(Math.random() * 255));
this.sysB = System.systemNameForID(Math.floor(Math.random() * 255));
var obj;
if (this.hyperRadio) {
if (a > 0.9) {
this.news = 2;
obj = { Message: "[HYPERRADIO2_NEWSA]", DKey: 1, Pic: "hyperradio_news.png", Music: "GNN.ogg", Model: "snoopers_gcnewsA1", Pos: [57, 36, 170] };
} else if (a > 0.8) {
this.news = 3;
obj = { Message: "[HYPERRADIO2_NEWSD]", DKey: 1, Agency: 1 };
} else {
this.news = 4;
obj = { Message: "[HYPERRADIO2_NEWSE] " + this.sysA + " and " + this.sysB + " [HYPERRADIO2_NEWSE2]", DKey: 1, Agency: 3 };
}
} else {
if (a > 0.7) {
if (a > 0.9) {
this.news = 1;
obj = { Message: "[HYPERRADIO2_NEWSB]", Pic: "hyperradio_news.png", Music: "GNN.ogg", Model: "snoopers_gcnewsA1", DKey: 1, Pos: [57, 36, 170] };
} else {
this.news = 4;
obj = { Message: "[HYPERRADIO2_NEWSE] " + this.sysA + " and " + this.sysB + " [HYPERRADIO2_NEWSE2]", DKey: 1, Agency: 3 };
}
} else {
this.news = 5;
obj = { Message: "[HYPERRADIO2_NEWSC] " + this.sysA + " and " + this.sysB + " in this sector. [HYPERRADIO2_NEWSC2]", DKey: 1, Agency: 2 };
}
}
if (this.logging) log(this.name, this.name + ": New event:" + this.news);
obj.ID = this.name;
obj.CB = this.name + ".newsDisplayed";
var b = worldScripts.GNN._insertNews(obj);
if (b) this.news = 0;
else this.vNews ^= this.news + 15;
}
}
}
this.newsDisplayed = function () {
switch (this.news) {
case 3: this.tempDisabled = true; break;
case 4: break;
default: this.news = 0; break;
}
}
this.failPlay = function (mode) {
if (this.libMusic.$dub && this.libMusic.$dub.channel == this.currentChannel) {
Sound.stopMusic(this.libMusic.$dub.cur);
if (mode) Sound.playMusic("hyperradio-fail.ogg");
}
}
this.CheckHyperradioLicense = function () {
if (this.hyperRadio && clock.days >= this.hyperradioDuration) {
player.ship.removeEquipment("EQ_HYPER_RADIO");
this.canPlay = false;
this.hyperradioDuration = 0;
player.consoleMessage(expandDescription("[hyper_expired]"), 6);
return false;
}
if (system.isInterstellarSpace) return (false);
if (player.ship.equipmentStatus("EQ_HYPER_RADIO") === "EQUIPMENT_DAMAGED") {
this.canPlay = false;
player.consoleMessage(expandDescription("[hyper_damaged]"), 6);
return false;
}
if (player.ship.equipmentStatus("EQ_HYPER_RADIO") === "EQUIPMENT_OK") this.canPlay = true;
return true;
}
this.SelectHyperradioStation = function (next) {
if (this.doInit) this.doInit();
if (this.canPlay == false) {
player.consoleMessage(expandDescription("[hyper_error]"), 6);
return;
}
if (next && this.currentChannelIndex == -1) {
player.consoleMessage(expandDescription("[hyper_channels_off]"));
return;
}
if (next == 0) {
if (this.currentChannelIndex == -1) {
this.currentChannelIndex = this.savedChannelIndex;
} else {
this.savedChannelIndex = this.currentChannelIndex;
this.currentChannelIndex = -1;
}
} else {
if (next > 0) this.currentChannelIndex += 1;
if (this.currentChannelIndex >= (this.stations - 1)) this.currentChannelIndex = 0;
}
if (this.currentChannelIndex == -1) {
this.currentChannel = expandDescription("[hyper_off]");
if (worldScripts.Lib_Music.$dub.cur != null) Sound.stopMusic(worldScripts.Lib_Music.$dub.cur);
this.turnOnEventMusic();
} else {
this.savedChannelIndex = this.currentChannelIndex;
this.currentChannel = this.libMusic.$radios[this.currentChannelIndex];
this.turnOffEventMusic();
}
if (this.currentChannelIndex >= 0) player.consoleMessage(expandDescription("[hyper_new_channel]", { name: this.currentChannel }));
else player.consoleMessage(expandDescription("[hyper_radio_off]"));
if (this.currentChannelIndex == -1) {
this.libMusic.$E0 = 0;
} else {
this.libMusic.$E0 = this.masking[this.currentChannelIndex];
}
this.libMusic._buildList();
this.libMusic._resetTimer(0);
}
this.turnOffEventMusic = function() {
var g = this.libMusic.$groupIDs;
for (var i = 0; i < g.length; i++) {
if (this.libMusic.$act[g[i]] == 1) this.libMusic._toggleGroup(g[i]);
}
}
this.turnOnEventMusic = function() {
var g = this.libMusic.$groupIDs;
for (var i = 0; i < g.length; i++) {
if (this.libMusic.$act[g[i]] == 0) this.libMusic._toggleGroup(g[i]);
}
}
this.doInit = function () {
this.stations = this.libMusic.$radios.length;
if (this.logging) log(this.name, this.name + ": Stations found:" + this.stations);
this.masking = [];
var initial = 1;
for (var i = 0; i < this.stations; i++) {
this.masking.push(initial);
initial *= 2;
}
delete this.doInit;
}
this.switchTimer = function switchTimer() {
if (this.doInit) return; // if we haven't selected a station, just return
// if the radio has switched to something other than generic (eg "fight"),
// and the hand property is no longer "radio" switch to that radio channel
if (this.libMusic.$dub.radio != "generic" && this.libMusic.$dub.hand != "radio") {
this.turnOnEventMusic();
this.libMusic._resetTimer(0);
return;
}
// if the radio has switched to generic, and we have previously selected a channel and it's not active
// switch to that channel
if (this.libMusic.$dub.radio == "generic" && this.currentChannel != expandDescription("[hyper_off]") && this.libMusic.$dub.hand != "radio") {
this.turnOffEventMusic();
this.libMusic._resetTimer(0);
}
}
this.$hyperRadioMFD = function() {
var p = player.ship;
var data = worldScripts.Lib_Music.$dub;
var queue = data.queue;
var index = -1;
var list = "";
if (data.cur && data.idx >= 0) {
var count = 0;
for (var i = data.idx; i < queue.length; i++) {
list += (list == "" ? "" : "\n") + this.$padTextRight((i == data.idx ? "> " : "") + queue[i].snd.substring(0, queue[i].snd.length - 4), 15);
count += 1;
if (count == 9) break;
}
if (count < 9) {
for (var i = 0; i < data.idx; i++) {
list += "\n" + this.$padTextRight(queue[i].snd.substring(0, queue[i].snd.length - 4), 15);
count += 1;
if (count == 9) break;
}
}
for (var i = 0; i < queue.length; i++) {
}
}
var channel = "";
if (data.channel != "RadioPlaylist") {
channel = "Off";
} else {
if (data.idx >= 0) {
channel = queue[data.idx].act;
}
}
var text = this.$padTextRight(expandDescription("[hypermfd_title]", {channel: channel}), 15) + "\n" + list;
// send output to MFD
p.setMultiFunctionText("HyperRadioMFD", text, false);
}
this.$updateMFD = function() {
if (player.ship.docked) return;
this.$hyperRadioMFD();
}
//-------------------------------------------------------------------------------------------------------------
// appends space to currentText to the specified length in 'em'
this.$padTextRight = function $padTextRight(currentText, desiredLength, leftSwitch) {
if (currentText == null) currentText = "";
var space = " ";
var hairSpace = String.fromCharCode(31);
var ellip = "…";
var currentLength = defaultFont.measureString(currentText.replace(/%%/g, "%"));
var hairSpaceLength = defaultFont.measureString(hairSpace);
var spaceLength = defaultFont.measureString(space);
// calculate number needed to fill remaining length
var spacesNeeded = parseInt(Math.floor((desiredLength - currentLength) / spaceLength));
if (spacesNeeded < 0) {
spacesNeeded = 0;
} else {
if (!leftSwitch || leftSwitch === false) {
currentText += new Array(spacesNeeded).join(space);
} else {
currentText = new Array(spacesNeeded).join(space) + currentText;
}
currentLength = defaultFont.measureString(currentText.replace(/%%/g, "%"));
}
var padsNeeded = Math.floor((desiredLength - currentLength) / hairSpaceLength);
if (padsNeeded < 1) {
// text is too long for column, so start pulling characters off
var tmp = currentText;
do {
tmp = tmp.substring(0, tmp.length - 2) + ellip;
if (tmp === ellip) break;
} while (defaultFont.measureString(tmp.replace(/%%/g, "%")) > desiredLength);
currentLength = defaultFont.measureString(tmp.replace(/%%/g, "%"));
padsNeeded = Math.floor((desiredLength - currentLength) / hairSpaceLength);
currentText = tmp;
}
// quick way of generating a repeated string of that number
if (!leftSwitch || leftSwitch === false) {
return currentText + new Array(padsNeeded).join(hairSpace);
} else {
return new Array(padsNeeded).join(hairSpace) + currentText;
}
} |