2016-10-27 20:26:00 +02:00
|
|
|
//Replaces the character at an index with a new character
|
|
|
|
String.prototype.replaceAt=function(index, character) {
|
|
|
|
return this.substr(0, index) + character + this.substr(index+character.length);
|
|
|
|
}
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
var Engine = {
|
|
|
|
|
|
|
|
//Clickable objects
|
|
|
|
Clickables: {
|
|
|
|
hackButton: null,
|
|
|
|
|
|
|
|
//Load, save, and delete
|
|
|
|
saveButton: null,
|
|
|
|
loadButton: null,
|
|
|
|
deleteButton: null,
|
|
|
|
|
|
|
|
//Main menu buttons
|
|
|
|
terminalMainMenuButton: null,
|
|
|
|
characterMainMenuButton: null,
|
2016-11-24 23:30:33 +01:00
|
|
|
scriptEditorMainMenuButton: null,
|
2016-10-17 10:24:39 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
//Display objects
|
|
|
|
Display: {
|
|
|
|
//Progress bar
|
2016-11-24 23:30:33 +01:00
|
|
|
progress: null,
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
//Display for status text (such as "Saved" or "Loaded")
|
2016-11-24 23:30:33 +01:00
|
|
|
statusText: null,
|
2016-10-17 10:24:39 +02:00
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
hacking_skill: null,
|
|
|
|
|
2016-10-17 10:24:39 +02:00
|
|
|
//Main menu content
|
2016-11-24 23:30:33 +01:00
|
|
|
terminalContent: null,
|
|
|
|
characterContent: null,
|
|
|
|
scriptEditorContent: null,
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
//Character info
|
2016-11-24 23:30:33 +01:00
|
|
|
characterInfo: null,
|
|
|
|
|
|
|
|
//Script editor text
|
|
|
|
scriptEditorText: null,
|
2016-10-17 10:24:39 +02:00
|
|
|
},
|
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
//Current page status
|
|
|
|
Page: {
|
|
|
|
Terminal: "Terminal",
|
|
|
|
CharacterInfo: "CharacterInfo",
|
|
|
|
ScriptEditor: "ScriptEditor",
|
|
|
|
},
|
|
|
|
currentPage: null,
|
|
|
|
|
|
|
|
|
2016-10-17 10:24:39 +02:00
|
|
|
//Time variables (milliseconds unix epoch time)
|
2016-11-21 07:11:14 +01:00
|
|
|
_lastUpdate: new Date().getTime(),
|
2016-10-17 10:24:39 +02:00
|
|
|
_idleSpeed: 200, //Speed (in ms) at which the main loop is updated
|
|
|
|
|
|
|
|
//Save function
|
2016-11-24 23:30:33 +01:00
|
|
|
saveGame: function() {
|
|
|
|
var PlayerSave = JSON.stringify(Player);
|
|
|
|
var ForeignServersSave = JSON.stringify(ForeignServers);
|
|
|
|
//TODO Add factions + companies here when they're done
|
2016-10-17 10:24:39 +02:00
|
|
|
|
2016-11-24 23:30:33 +01:00
|
|
|
window.localStorage.setItem("netburnerPlayerSave", PlayerSave);
|
|
|
|
window.localStorage.setItem("netburnerForeignServersSave", ForeignServersSave)
|
2016-11-24 23:44:48 +01:00
|
|
|
console.log("Game saved to local storage");
|
2016-10-17 10:24:39 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
//Load saved game function
|
|
|
|
loadSave: function() {
|
|
|
|
//Check to see if file exists
|
2016-11-24 23:30:33 +01:00
|
|
|
if (!window.localStorage.getItem("netburnerPlayerSave")) {
|
2016-11-24 23:44:48 +01:00
|
|
|
console.log("No Player save to load");
|
|
|
|
return false;
|
2016-11-24 23:30:33 +01:00
|
|
|
} else if (!window.localStorage.getItem("netburnerForeignServersSave")) {
|
2016-11-24 23:44:48 +01:00
|
|
|
console.log("No ForeignServers save to load");
|
|
|
|
return false;
|
2016-11-24 23:30:33 +01:00
|
|
|
} else {
|
|
|
|
var PlayerSave = window.localStorage.getItem("netburnerPlayerSave");
|
|
|
|
var ForeignServersSave = window.localStorage.getItem("netburnerForeignServersSave");
|
|
|
|
Player = JSON.parse(PlayerSave);
|
|
|
|
ForeignServers = JSON.parse(ForeignServersSave);
|
2016-11-24 23:44:48 +01:00
|
|
|
return true;
|
2016-10-17 10:24:39 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
//Delete saved game function
|
|
|
|
deleteSave: function() {
|
2016-11-24 23:44:48 +01:00
|
|
|
if (!window.localStorage.getItem("netburnerPlayerSave")) {
|
|
|
|
console.log("No Player Save to delete");
|
|
|
|
return false;
|
|
|
|
} else if (!window.localStorage.getItem("netburnerForeignServersSave")) {
|
|
|
|
console.log("No ForeignServers Save to delete");
|
|
|
|
return false;
|
2016-10-17 10:24:39 +02:00
|
|
|
} else {
|
2016-11-24 23:44:48 +01:00
|
|
|
window.localStorage.removeItem("netburnerPlayerSave");
|
|
|
|
window.localStorage.removeItem("netburnerForeignServersSave");
|
|
|
|
console.log("Deleted saves")
|
|
|
|
return true;
|
2016-10-17 10:24:39 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Load content when a main menu button is clicked */
|
|
|
|
loadTerminalContent: function() {
|
|
|
|
Engine.hideAllContent();
|
|
|
|
Engine.Display.terminalContent.style.visibility = "visible";
|
2016-11-24 23:30:33 +01:00
|
|
|
Engine.currentPage = Engine.Page.Terminal;
|
2016-10-17 10:24:39 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
loadCharacterContent: function() {
|
|
|
|
Engine.hideAllContent();
|
|
|
|
Engine.Display.characterContent.style.visibility = "visible";
|
|
|
|
Engine.displayCharacterInfo();
|
2016-11-24 23:30:33 +01:00
|
|
|
Engine.currentPage = Engine.Page.CharacterInfo;
|
2016-10-17 10:24:39 +02:00
|
|
|
},
|
2016-11-24 23:30:33 +01:00
|
|
|
|
|
|
|
loadScriptEditorContent: function(filename = "", code = "") {
|
|
|
|
Engine.hideAllContent();
|
|
|
|
Engine.Display.scriptEditorContent.style.visibility = "visible";
|
|
|
|
if (filename == "") {
|
|
|
|
document.getElementById("script-editor-filename").value = "untitled";
|
|
|
|
} else {
|
|
|
|
document.getElementById("script-editor-filename").value = filename;
|
|
|
|
}
|
|
|
|
document.getElementById("script-editor-text").value = code;
|
|
|
|
Engine.currentPage = Engine.Page.ScriptEditor;
|
|
|
|
},
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
//Helper function that hides all content
|
|
|
|
hideAllContent: function() {
|
|
|
|
Engine.Display.terminalContent.style.visibility = "hidden";
|
|
|
|
Engine.Display.characterContent.style.visibility = "hidden";
|
2016-11-24 23:30:33 +01:00
|
|
|
Engine.Display.scriptEditorContent.style.visibility = "hidden";
|
2016-10-17 10:24:39 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Display character info */
|
|
|
|
displayCharacterInfo: function() {
|
|
|
|
Engine.Display.characterInfo.innerHTML = 'Money: $' + Player.money + '<br><br>' +
|
|
|
|
'Hacking Level: ' + Player.hacking_skill + '<br><br>' +
|
|
|
|
'Strength: ' + Player.strength + '<br><br>' +
|
|
|
|
'Defense: ' + Player.defense + '<br><br>' +
|
|
|
|
'Dexterity: ' + Player.dexterity + '<br><br>' +
|
2016-10-17 23:23:23 +02:00
|
|
|
'Agility: ' + Player.agility + '<br><br>' +
|
2016-11-02 22:45:10 +01:00
|
|
|
'Charisma: ' + Player.charisma + '<br><br>' +
|
2016-11-01 06:30:59 +01:00
|
|
|
'Servers owned: ' + Player.purchasedServers.length + '<br><br>' +
|
|
|
|
'Hacking Experience: ' + Player.hacking_exp + '<br><br>';
|
2016-10-17 10:24:39 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/* Main Event Loop */
|
|
|
|
idleTimer: function() {
|
|
|
|
//Get time difference
|
2016-11-21 07:11:14 +01:00
|
|
|
var _thisUpdate = new Date().getTime();
|
|
|
|
var diff = _thisUpdate - Engine._lastUpdate;
|
2016-11-28 23:02:06 +01:00
|
|
|
|
2016-11-21 07:11:14 +01:00
|
|
|
//Divide this by cycle time to determine how many cycles have elapsed since last update
|
|
|
|
diff = Math.round(diff / Engine._idleSpeed);
|
2016-11-29 23:56:05 +01:00
|
|
|
|
2016-11-21 07:11:14 +01:00
|
|
|
if (diff > 0) {
|
|
|
|
//Update the game engine by the calculated number of cycles
|
|
|
|
Engine.updateGame(diff);
|
|
|
|
Engine._lastUpdate = _thisUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.requestAnimationFrame(Engine.idleTimer);
|
|
|
|
},
|
|
|
|
|
|
|
|
//TODO Account for numCycles in Code, hasn't been done yet
|
|
|
|
updateGame: function(numCycles = 1) {
|
|
|
|
//Manual hack
|
2016-11-01 06:30:59 +01:00
|
|
|
if (Player.startAction == true) {
|
|
|
|
Engine._totalActionTime = Player.actionTime;
|
|
|
|
Engine._actionTimeLeft = Player.actionTime;
|
|
|
|
Engine._actionInProgress = true;
|
|
|
|
Engine._actionProgressBarCount = 1;
|
|
|
|
Engine._actionProgressStr = "[ ]";
|
|
|
|
Engine._actionTimeStr = "Time left: ";
|
|
|
|
Player.startAction = false;
|
2016-10-24 23:16:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Engine.updateHackProgress();
|
2016-11-21 07:11:14 +01:00
|
|
|
},
|
2016-10-17 10:24:39 +02:00
|
|
|
|
2016-10-24 23:16:51 +02:00
|
|
|
/* Calculates the hack progress for a manual (non-scripted) hack and updates the progress bar/time accordingly */
|
2016-11-01 06:30:59 +01:00
|
|
|
_totalActionTime: 0,
|
|
|
|
_actionTimeLeft: 0,
|
|
|
|
_actionTimeStr: "Time left: ",
|
|
|
|
_actionProgressStr: "[ ]",
|
|
|
|
_actionProgressBarCount: 1,
|
|
|
|
_actionInProgress: false,
|
2016-10-24 23:16:51 +02:00
|
|
|
updateHackProgress: function() {
|
2016-11-01 06:30:59 +01:00
|
|
|
if (Engine._actionInProgress == true) {
|
2016-11-24 23:44:48 +01:00
|
|
|
//TODO Do this calculation based on numCycles rather than idle speed
|
2016-11-01 06:30:59 +01:00
|
|
|
Engine._actionTimeLeft -= (Engine._idleSpeed/ 1000); //Substract idle speed (ms)
|
2016-10-24 23:16:51 +02:00
|
|
|
|
|
|
|
//Calculate percent filled
|
2016-11-01 06:30:59 +01:00
|
|
|
var percent = Math.round((1 - Engine._actionTimeLeft / Engine._totalActionTime) * 100);
|
2016-10-24 23:16:51 +02:00
|
|
|
|
|
|
|
//Update progress bar
|
2016-11-01 06:30:59 +01:00
|
|
|
while (Engine._actionProgressBarCount * 2 <= percent) {
|
|
|
|
Engine._actionProgressStr = Engine._actionProgressStr.replaceAt(Engine._actionProgressBarCount, "|");
|
|
|
|
Engine._actionProgressBarCount += 1;
|
2016-10-24 23:16:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Update hack time remaining
|
2016-11-01 06:30:59 +01:00
|
|
|
Engine._actionTimeStr = "Time left: " + Math.max(0, Math.round(Engine._actionTimeLeft)).toString() + "s";
|
|
|
|
document.getElementById("hack-progress").innerHTML = Engine._actionTimeStr;
|
2016-10-27 20:26:00 +02:00
|
|
|
|
|
|
|
//Dynamically update progress bar
|
2016-11-01 06:30:59 +01:00
|
|
|
document.getElementById("hack-progress-bar").innerHTML = Engine._actionProgressStr.replace( / /g, " " );
|
2016-10-24 23:16:51 +02:00
|
|
|
|
|
|
|
//Once percent is 100, the hack is completed
|
2016-10-27 05:34:18 +02:00
|
|
|
if (percent >= 100) {
|
2016-11-01 06:30:59 +01:00
|
|
|
Engine._actionInProgress = false;
|
|
|
|
Terminal.finishAction();
|
2016-10-24 23:16:51 +02:00
|
|
|
}
|
2016-10-27 20:26:00 +02:00
|
|
|
|
2016-10-24 23:16:51 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-10-17 10:24:39 +02:00
|
|
|
/* Initialization */
|
|
|
|
init: function() {
|
2016-11-03 21:43:46 +01:00
|
|
|
//Initialization functions
|
2016-11-24 23:44:48 +01:00
|
|
|
if (Engine.loadSave()) {
|
|
|
|
console.log("Loaded game from save");
|
|
|
|
Companies.init();
|
|
|
|
CompanyPositions.init();
|
|
|
|
} else {
|
|
|
|
//No save found, start new game
|
|
|
|
console.log("Initializing new game");
|
|
|
|
Player.init();
|
|
|
|
ForeignServers.init();
|
|
|
|
Companies.init();
|
|
|
|
CompanyPositions.init();
|
|
|
|
}
|
|
|
|
|
2016-11-28 23:13:13 +01:00
|
|
|
//if (window.Worker) {
|
|
|
|
// Engine._scriptWebWorker = new Worker("netscript/NetscriptWorker.js");
|
|
|
|
//}
|
2016-11-03 21:43:46 +01:00
|
|
|
|
2016-10-17 10:24:39 +02:00
|
|
|
//Load, save, and delete buttons
|
|
|
|
//Engine.Clickables.saveButton = document.getElementById("save");
|
|
|
|
//Engine.Clickables.saveButton.addEventListener("click", function() {
|
|
|
|
// Engine.saveFile();
|
|
|
|
// return false;
|
|
|
|
//});
|
|
|
|
|
|
|
|
//Engine.Clickables.loadButton = document.getElementById("load");
|
|
|
|
//Engine.Clickables.loadButton.addEventListener("click", function() {
|
|
|
|
// Engine.loadSave();
|
|
|
|
// return false;
|
|
|
|
//});
|
|
|
|
|
|
|
|
//Engine.Clickables.deleteButton = document.getElementById("delete");
|
|
|
|
//Engine.Clickables.deleteButton.addEventListener("click", function() {
|
|
|
|
// Engine.deleteSave();
|
|
|
|
// return false;
|
|
|
|
//});
|
|
|
|
|
|
|
|
//Main menu buttons and content
|
|
|
|
Engine.Clickables.terminalMainMenuButton = document.getElementById("terminal-menu-link");
|
|
|
|
Engine.Clickables.terminalMainMenuButton.addEventListener("click", function() {
|
|
|
|
Engine.loadTerminalContent();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
Engine.Clickables.characterMainMenuButton = document.getElementById("character-menu-link");
|
|
|
|
Engine.Clickables.characterMainMenuButton.addEventListener("click", function() {
|
|
|
|
Engine.loadCharacterContent();
|
|
|
|
return false;
|
|
|
|
});
|
2016-11-24 23:30:33 +01:00
|
|
|
|
|
|
|
Engine.Clickables.scriptEditorMainMenuButton = document.getElementById("create-script-menu-link");
|
|
|
|
Engine.Clickables.scriptEditorMainMenuButton.addEventListener("click", function() {
|
|
|
|
Engine.loadScriptEditorContent();
|
|
|
|
return false;
|
|
|
|
});
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
Engine.Display.terminalContent = document.getElementById("terminal-container");
|
2016-11-24 23:30:33 +01:00
|
|
|
Engine.currentPage = Engine.Page.Terminal;
|
2016-10-17 10:24:39 +02:00
|
|
|
Engine.Display.characterContent = document.getElementById("character-container");
|
2016-11-24 23:30:33 +01:00
|
|
|
Engine.Display.characterContent.style.visibility = "hidden";
|
|
|
|
Engine.Display.scriptEditorContent = document.getElementById("script-editor-container");
|
|
|
|
Engine.Display.scriptEditorContent.style.visibility = "hidden";
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
//Character info
|
|
|
|
Engine.Display.characterInfo = document.getElementById("character-info");
|
2016-11-24 23:30:33 +01:00
|
|
|
//Engine.displayCharacterInfo(); - Don't think I need this
|
|
|
|
|
|
|
|
//Script editor
|
|
|
|
Engine.Display.scriptEditorText = document.getElementById("script-editor-text");
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
//Message at the top of terminal
|
2016-10-20 23:11:01 +02:00
|
|
|
postNetburnerText();
|
2016-10-17 10:24:39 +02:00
|
|
|
|
|
|
|
//Run main loop
|
|
|
|
Engine.idleTimer();
|
2016-11-28 23:13:13 +01:00
|
|
|
|
|
|
|
runScriptsLoop();
|
2016-10-17 10:24:39 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onload = function() {
|
|
|
|
Engine.init();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|