Scripts/hyperradio.js |
"use strict";
this.name = "hyperradio";
this.author = "Svengali";
this.copyright = "(C)2008-2013, License:CC-by-nc-sa-3.0";
this.description = "Musicplayer. Expandable by musicpacks.";
this.version = "1.26.1";
/* *** CHANGE ONLY THIS PART - Add new or override the following entries to use it as playList *** */
this.hyperradio_playListOriginal = [
"hyperradio-exampleA.ogg",
"hyperradio-exampleB.ogg"
];
/* *** END OF CHANGES *** */
this.stationList = ['hyperradio_playListOriginal'];
this.stationDesc = ['GalaxyRadio 1.26.1'];
this.startUp = function()
{
delete this.startUp;
if(!worldScripts.Cabal_Common_Functions || typeof(worldScripts.Cabal_Common_Functions.Cabal_Common)==='undefined'){this.killSelf(" -> Cabal_Common_Library is missing."); return;}
this.helper = new worldScripts.Cabal_Common_Functions.Cabal_Common();
if(this.helper.internalVersion<15){this.killSelf(" -> Cabal_Common_Functions is too old."); return;}
var requires = ["snoopers","2.4"];
this.news = 0;
this.vNews = 31;
this.showNews = false;
if(this.helper.oxpVersionTest(this.name,requires)){
this.showNews = true;
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);
}
} else log(this.name,this.name+" - deactivating all specials");
/* Via OXPConfig2 configurable settings */
this.logging = false;
this.mergePacks = true;
this.podMusic = true;
this.onlyMain = false;
// Other settings
this.canPlay = false; // false if EQ not installed, docked, EQ damaged, in interstallar space or tempDisabled
this.tempDisabled = false; // used to temp disable the HyperRadio, reset on witchspacing
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;
this.current = 'start'; // current song
this.next = 'start'; // next selected song
this.currentStation = ''; // musicpack or main
this.currentList = []; // playlist
this.selectable = [[],[],[],[],[],[],[],[]];
this.cleanMVs();
delete this.killSelf;
};
this.killSelf = function(desc)
{
if(desc!==1){
player.consoleMessage(this.name+" - Check your Latest.log.",10);
log(this.name,this.name+" - Shutting down."+desc);
}
for(var prop in this){if(prop!=='name' && prop!=='version' && prop!=='stationList') delete this[prop];}
this.deactivated = true;
return;
};
this.playerWillSaveGame = function()
{
missionVariables.hyperradio_last = this.hyperradioDuration;
if(this.news) missionVariables.hyperradio_news = this.news+'|'+this.sysA+'|'+this.sysB;
this.saved = true;
};
this.cleanMVs = function()
{
missionVariables.hyperradio_last = null;
missionVariables.hyperradio_news = null;
delete this.saved;
return;
};
this.distribution = function(P)
{
var K = Math.random()%1;
for(var j=0;j<P.length;j++){
if(P[j]>K){
return(j);
}
}
return(0);
};
this.doInit = function()
{
this.stations = this.stationList.length;
if(this.logging) log(this.name,this.name+': Stations found:'+this.stations);
if(this.stations>1){
if(this.mergePacks){
var lname = "",outL = this.stationList.length,inL = 0;
while(outL--){
inL = worldScripts.hyperradio[this.stationList[outL]].length;
while(inL--){
lname = this.stationList[outL];
this.hyperradio_playListOriginal.push(worldScripts.hyperradio[lname][inL]);
}
if(outL===1) break;
}
this.hyperradio_playListOriginal = this.helper.arrRemoveByValue(this.hyperradio_playListOriginal,'hyperradio-exampleA.ogg');
this.hyperradio_playListOriginal = this.helper.arrRemoveByValue(this.hyperradio_playListOriginal,'hyperradio-exampleB.ogg');
if(this.logging) log(this.name,this.name+': Main Playlist contains '+this.hyperradio_playListOriginal.length+' songs. Examples files removed.');
}
for(var de=1;de<this.stations;de++){
var sDesc = this.stationList[de];
var test = sDesc.replace(/(^hyperradio_|playList|[0-9])/g,'');
test += " "+this.helper.randSpan(100,999)+"."+this.helper.rand(99);
this.stationDesc.push(test);
if(this.logging) log(this.name,"Desc: "+test);
}
var Given = [],j,k,A = [],Probs = [],S = "",Q = 1,Total = 8;
for(var c=0;c<this.stations;c++){Given.push(1);}
for(j=Given.length-1;j>=0;j--){A[j] = 0; Probs[j] = Q; Q -= Given[j]/Total;}
for(j=0;j<64;j++){k = this.distribution(Probs); S += k; A[k]++;}
var g=8,ii,c=Given.length-1;
while(g--){
ii =Given.length;
if(c>0) this.selectable[g].push(c);
c--;
while(ii--){
if((S[g+ii]&ii)) this.selectable[g].push((S[g+ii]&ii));
else this.selectable[g].push(0);
}
this.selectable[g] = this.helper.arrUnique(this.selectable[g]);
}
if(this.logging){for(var o=0;o<8;o++){log(this.name,this.selectable[o]);}}
} else this.selectable = [[0],[0],[0],[0],[0],[0],[0],[0]];
delete this.doInit;
delete this.helper;
delete this.distribution;
return;
};
this.playerBoughtEquipment = function(equipmentKey)
{
switch(equipmentKey){
case 'EQ_HYPER_RADIO':
this.hyperRadio = true;
this.hyperradioDuration = Math.floor(20+clock.days);
EquipmentInfo.infoForKey('EQ_HYPER_RADIO_ADD').effectiveTechLevel = 7;
break;
case 'EQ_HYPER_RADIO_ADD':
this.hyperRadio = true;
this.hyperradioDuration += 20;
player.ship.removeEquipment('EQ_HYPER_RADIO_ADD');
missionVariables.TL_FOR_EQ_HYPER_RADIO_ADD = null;
break;
case 'EQ_HYPER_RADIO_PIRATE':
player.ship.removeEquipment('EQ_HYPER_RADIO_PIRATE');
this.hyperRadio = true;
player.ship.awardEquipment('EQ_HYPER_RADIO');
missionVariables.TL_FOR_EQ_HYPER_RADIO_PIRATE = null;
this.hyperradioDuration += Math.ceil(20/Math.log(system.government+2.1));
this.news = 0;
break;
}
};
this.equipmentDamaged = this.equipmentDestroyed = function(equipmentKey)
{
if(equipmentKey==='EQ_HYPER_RADIO'){
this.canPlay = false;
this.failPlay(1);
}
};
this.shipWillDockWithStation = function()
{
this.failPlay();
this.canPlay = false;
this.CheckHyperradioLicence();
};
this.shipWillLaunchFromStation = function()
{
missionVariables.TL_FOR_EQ_HYPER_RADIO_ADD = null;
if(player.ship.equipmentStatus('EQ_HYPER_RADIO')==='EQUIPMENT_OK'){
if(system.isInterstellarSpace || this.tempDisabled){
this.canPlay = false;
return;
}
this.hyperRadio = true;
this.SelectHyperradioStation();
}
};
this.shipLaunchedFromStation = function()
{
if(this.hyperRadio && !system.isInterstellarSpace){
this.current = 'start';
this.next = 'start';
if(!this.tempDisabled) this.canPlay = true;
if(this.logging) log(this.name,this.name+': Duration:'+this.hyperradioDuration+' tmp:'+this.tempDisabled);
}
};
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.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(!missionVariables.TL_FOR_EQ_HYPER_RADIO_PIRATE && (system.ID===a || system.ID===b)){
EquipmentInfo.infoForKey('EQ_HYPER_RADIO_PIRATE').effectiveTechLevel = 1;
this.news = 0;
return;
} else missionVariables.TL_FOR_EQ_HYPER_RADIO_PIRATE = null;
} else if(this.news===3){
this.news = 0;
return;
}
}
if(this.news) return;
if((clock.days&31)===this.vNews){
missionVariables.TL_FOR_EQ_HYPER_RADIO_PIRATE = null;
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:'[HYPERRADIO_NEWSA]',Pic:'hyperradio_news.png',Music:'snoopers_gcnews.ogg',Model:'snoopers_gcnewsA1',Pos:[57,36,170]};
} else if(a>0.8){
this.news = 3;
obj = {Message:'[HYPERRADIO_NEWSD]',Agency:1};
} else {
this.news = 4;
obj = {Message:'[HYPERRADIO_NEWSE] '+this.sysA+' and '+this.sysB+' [HYPERRADIO_NEWSE2]',Agency:3};
}
} else {
if(a>0.7){
if(a>0.9){
this.news = 1;
obj = {Message:'[HYPERRADIO_NEWSB]',Pic:'hyperradio_news.png',Music:'snoopers_gcnews.ogg',Model:'snoopers_gcnewsA1',Pos:[57,36,170]};
} else {
this.news = 4;
obj = {Message:'[HYPERRADIO_NEWSE] '+this.sysA+' and '+this.sysB+' [HYPERRADIO_NEWSE2]',Agency:3};
}
} else {
this.news = 5;
obj = {Message:'[HYPERRADIO_NEWSC] '+this.sysA+' and '+this.sysB+' in this sector. [HYPERRADIO_NEWSC2]',Agency:2};
}
}
if(this.logging) log(this.name,this.name+': New event:'+this.news);
obj.ID = this.name;
var b = worldScripts.snoopers.insertNews(obj);
if(b) this.news = 0;
else this.vNews ^= this.news+15;
}
}
};
this.alertConditionChanged = function(condition)
{
if(this.doInit) this.doInit();
if(this.hyperRadio){
if(condition===3) this.failPlay();
else if(!player.ship.docked && !system.isInterstellarSpace && !this.tempDisabled) this.canPlay = true;
}
};
this.shipLaunchedEscapePod = function()
{
this.failPlay();
if(this.podMusic) Sound.playMusic('hyperradio-escapepod.ogg');
};
this.SelectHyperradioStation = function(next)
{
if(system.isInterstellarSpace){
player.consoleMessage("HyperRadio deactivated. Could not transfer data.",8);
return;
}
if(this.onlyMain || this.stations<2) this.stationSelect = 0;
else {
var currentAvail = this.selectable[system.government];
if(this.logging && !next) log(this.name,"Subset:"+currentAvail);
if(next){
this.cycle++;
if(this.cycle>currentAvail.length-1) this.cycle = 0;
this.stationSelect = currentAvail[this.cycle];
} else {
this.stationSelect = currentAvail[Math.floor(Math.random()*currentAvail.length)];
this.cycle = this.stationSelect;
}
}
if(next) player.consoleMessage("Channel : "+this.stationDesc[this.stationSelect]);
if(this.logging) log(this.name,"Selected: "+this.stationSelect);
return;
};
this.GetHyperradioSongName = function()
{
var listName = this.stationList[this.stationSelect];
var i = worldScripts.hyperradio[listName].length;
if(listName===this.currentStation){
if(!this.currentList.length){
while(i--) this.currentList.push(worldScripts.hyperradio[listName][i]);
}
} else {
this.currentStation = listName;
this.currentList = [];
while(i--) this.currentList.push(worldScripts.hyperradio[listName][i]);
}
if(this.currentList.length) this.currentList.sort(function(){return 0.5-Math.random();});
var songName = this.currentList[0];
if(!listName || !songName || !songName.match('.ogg')){
player.consoleMessage("HyperRadio: NO VALID ENTRY FOUND! See Logfile for Details.",8);
log(this.name,this.name+': WARNING. NO VALID ENTRY IN PLAYLIST FOUND. Error in:'+listName+' '+songName+' Station:'+this.stationSelect);
return(false);
}
this.next = songName;
this.currentList.shift();
if(this.logging) log(this.name,this.name+': Station:'+listName+' Song:'+songName+' remaining #'+this.currentList.length+' in this list.');
return(true);
};
this.CheckHyperradioLicence = function()
{
if(this.hyperRadio && clock.days>=this.hyperradioDuration){
player.ship.removeEquipment('EQ_HYPER_RADIO');
this.hyperRadio = false;
this.canPlay = false;
this.hyperradioDuration = 0;
player.consoleMessage("Your HyperRadio license expired. Buy a new one at your local dealer.",6);
return(false);
}
if(system.isInterstellarSpace) return(false);
if(player.ship.equipmentStatus('EQ_HYPER_RADIO')==='EQUIPMENT_DAMAGED'){
this.canPlay = false;
player.consoleMessage("Can't connect to HyperRadio station. Device damaged.",6);
return(false);
}
if(player.ship.equipmentStatus('EQ_HYPER_RADIO')==='EQUIPMENT_OK') this.canPlay = true;
return(true);
};
this.guiScreenChanged = function()
{
if(this.doInit) this.doInit();
if(this.saved) this.cleanMVs();
};
this.startMusic = function()
{
if(player.ship.status==='STATUS_IN_FLIGHT'){
if(this.tempDisabled){
player.consoleMessage("HyperRadio signal interrupted.");
this.failPlay();
} else if(this.hyperRadio && this.CheckHyperradioLicence() && this.canPlay){
if(this.current!=='start') Sound.stopMusic(this.current);
if(!this.GetHyperradioSongName() || this.next===this.stationList[this.stationSelect]){
this.canPlay = false;
return;
} else {
this.current = this.next;
player.consoleMessage("Connection to "+this.stationDesc[this.stationSelect]+" established. Days left: "+(this.hyperradioDuration-clock.days));
Sound.playMusic(this.current);
}
}
}
return;
};
this.shipDied = function()
{
this.failPlay(1);
};
this.failPlay = function(mode)
{
if(this.current!=='start'){
Sound.stopMusic(this.current);
if(mode) Sound.playMusic('hyperradio-fail.ogg');
}
return;
};
// Callback from Snoopers
this.newsDisplayed = function()
{
switch(this.news){
case 3: this.tempDisabled = true; break;
case 4: break;
default: this.news = 0; break;}
};
// OXPConfig2 settings
this.oxpcSettings = {
Info: {Name:this.name,Display:"Hyperradio",EarlyCall:true,EarlySet:true,
InfoB:"If musicpacks are installed channel lists are generated. Switching is done via primed EQ activation key. mergePacks removes the example files, onlyMain uses only the main playlist. Changes to mergePack needs restart to take effect."},
Bool0: {Name:"logging",Def:false,Desc:"Logging functions."},
Bool1: {Name:"mergePacks",Def:true,Desc:"Copy Packs in mainlist."},
Bool2: {Name:"podMusic",Def:true,Desc:"Escapepod music."},
Bool3: {Name:"onlyMain",Def:false,Desc:"Use only mainlist."}
};
|