mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-12-22 22:22:26 +01:00
Added arrays to Netscript. Fixed bug with Hacknet Nodes
This commit is contained in:
parent
6582026557
commit
60207b8e04
@ -74,12 +74,12 @@ HacknetNode.prototype.calculateLevelUpgradeCost = function(levels=1) {
|
|||||||
HacknetNode.prototype.purchaseLevelUpgrade = function(levels=1) {
|
HacknetNode.prototype.purchaseLevelUpgrade = function(levels=1) {
|
||||||
var cost = this.calculateLevelUpgradeCost(levels);
|
var cost = this.calculateLevelUpgradeCost(levels);
|
||||||
if (isNaN(cost)) {return false;}
|
if (isNaN(cost)) {return false;}
|
||||||
if (cost > Player.money) {return false;}
|
|
||||||
Player.loseMoney(cost);
|
|
||||||
if (this.level + levels > CONSTANTS.HacknetNodeMaxLevel) {
|
if (this.level + levels > CONSTANTS.HacknetNodeMaxLevel) {
|
||||||
var diff = Math.max(0, CONSTANTS.HacknetNodeMaxLevel - this.level);
|
var diff = Math.max(0, CONSTANTS.HacknetNodeMaxLevel - this.level);
|
||||||
return this.purchaseLevelUpgrade(diff);
|
return this.purchaseLevelUpgrade(diff);
|
||||||
}
|
}
|
||||||
|
if (cost > Player.money) {return false;}
|
||||||
|
Player.loseMoney(cost);
|
||||||
this.level += levels;
|
this.level += levels;
|
||||||
this.updateMoneyGainRate();
|
this.updateMoneyGainRate();
|
||||||
return true;
|
return true;
|
||||||
|
@ -28,7 +28,6 @@ Environment.prototype = {
|
|||||||
if (name in this.vars) {
|
if (name in this.vars) {
|
||||||
return this.vars[name];
|
return this.vars[name];
|
||||||
}
|
}
|
||||||
console.log("here");
|
|
||||||
throw new Error("Undefined variable " + name);
|
throw new Error("Undefined variable " + name);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -28,11 +28,46 @@ function evaluate(exp, workerScript) {
|
|||||||
reject(e);
|
reject(e);
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
} else if (exp.value == "array") {
|
||||||
|
//A raw array. This will be called under something like this:
|
||||||
|
// x = Array[1, 2, 3];
|
||||||
|
if (exp.array && exp.array instanceof Array) {
|
||||||
|
resolve(exp.array);
|
||||||
|
} else {
|
||||||
|
reject(makeRuntimeRejectMsg(workerScript, "Invalid array instantiation"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
resolve(env.get(exp.value));
|
var res = env.get(exp.value);
|
||||||
|
console.log(res);
|
||||||
|
if (exp.index) {
|
||||||
|
//If theres an index field, then this variable is supposed to be an array
|
||||||
|
//and the user needs to be indexing it
|
||||||
|
if (res.constructor === Array || res instanceof Array) {
|
||||||
|
//Do array stuff here
|
||||||
|
var iPromise = evaluate(exp.index.value, workerScript);
|
||||||
|
iPromise.then(function(i) {
|
||||||
|
console.log("Index resolved with value: " + i);
|
||||||
|
if (i >= res.length || i < 0) {
|
||||||
|
return reject(makeRuntimeRejectMsg(workerScript, "Out of bounds: Invalid index in [] operator"));
|
||||||
|
} else {
|
||||||
|
//Evaluate here
|
||||||
|
return evaluate(res[i], workerScript);
|
||||||
|
}
|
||||||
|
}).then(function(res) {
|
||||||
|
resolve(res);
|
||||||
|
}).catch(function(e) {
|
||||||
|
reject(e);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
reject(makeRuntimeRejectMsg(workerScript, "Trying to access a non-array variable using the [] operator"));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
resolve(res);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw new Error("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString());
|
reject("|" + workerScript.serverIp + "|" + workerScript.name + "|" + e.toString());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
//Can currently only assign to "var"s
|
//Can currently only assign to "var"s
|
||||||
@ -236,6 +271,7 @@ function evaluate(exp, workerScript) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var runScriptPromise = runScriptFromScript(scriptServer, scriptname, workerScript);
|
var runScriptPromise = runScriptFromScript(scriptServer, scriptname, workerScript);
|
||||||
|
return runScriptPromise;
|
||||||
}).then(function(res) {
|
}).then(function(res) {
|
||||||
resolve(res);
|
resolve(res);
|
||||||
}).catch(function(e) {
|
}).catch(function(e) {
|
||||||
|
@ -211,17 +211,27 @@ function Parser(input) {
|
|||||||
value: "hacknetnodes",
|
value: "hacknetnodes",
|
||||||
index: index,
|
index: index,
|
||||||
op: op,
|
op: op,
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
unexpected();
|
unexpected();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parse_array() {
|
||||||
|
//Declaring a new array with Array[1,2,3]
|
||||||
|
var array = delimited("[", "]", ",", parse_expression);
|
||||||
|
return {type: "var",
|
||||||
|
value: "array",
|
||||||
|
array: array
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function parse_arrayindex() {
|
function parse_arrayindex() {
|
||||||
var index = delimited("[", "]", ";", parse_expression);
|
var index = delimited("[", "]", ";", parse_expression);
|
||||||
var val = 0;
|
var val = 0;
|
||||||
if (index.length == 1 && (index[0].type == "num" || index[0].type == "var")) {
|
if (index.length == 1 && (index[0].type == "num" || index[0].type == "var")) {
|
||||||
val = index[0];
|
val = index[0];
|
||||||
} else {
|
} else {
|
||||||
|
val = 0;
|
||||||
console.log("WARNING: Extra indices passed in")
|
console.log("WARNING: Extra indices passed in")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,12 +263,21 @@ function Parser(input) {
|
|||||||
if (is_kw("if")) return parse_if();
|
if (is_kw("if")) return parse_if();
|
||||||
if (is_kw("for")) return parse_for();
|
if (is_kw("for")) return parse_for();
|
||||||
if (is_kw("while")) return parse_while();
|
if (is_kw("while")) return parse_while();
|
||||||
//if (is_kw("hacknetnodes")) return parse_hacknetnodes();
|
|
||||||
//Note, let for loops be function calls (call node types)
|
|
||||||
if (is_kw("true") || is_kw("false")) return parse_bool();
|
if (is_kw("true") || is_kw("false")) return parse_bool();
|
||||||
|
|
||||||
var tok = input.next();
|
var tok = input.next();
|
||||||
if (tok.type == "var" && tok.value == "hacknetnodes") return parse_hacknetnodes();
|
if (tok.type == "var" && tok.value == "hacknetnodes") return parse_hacknetnodes();
|
||||||
|
if (tok.type == "var" && tok.value == "Array") return parse_array();
|
||||||
|
if (tok.type == "var" && is_punc("[")) {
|
||||||
|
//Returns a variable node except with an extra "index" field so
|
||||||
|
//we can identify it as an index
|
||||||
|
var index = parse_arrayindex();
|
||||||
|
if (index.type != "index") {
|
||||||
|
unexpected();
|
||||||
|
}
|
||||||
|
tok.index = index;
|
||||||
|
return tok;
|
||||||
|
}
|
||||||
if (tok.type == "var" || tok.type == "num" || tok.type == "str")
|
if (tok.type == "var" || tok.type == "num" || tok.type == "str")
|
||||||
return tok;
|
return tok;
|
||||||
unexpected();
|
unexpected();
|
||||||
|
@ -31,7 +31,7 @@ function runScriptsLoop() {
|
|||||||
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) {
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) {
|
||||||
try {
|
try {
|
||||||
var ast = Parser(Tokenizer(InputStream(workerScripts[i].code)));
|
var ast = Parser(Tokenizer(InputStream(workerScripts[i].code)));
|
||||||
//console.log(ast);
|
console.log(ast);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Error parsing script: " + workerScripts[i].name);
|
console.log("Error parsing script: " + workerScripts[i].name);
|
||||||
dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":<br>" + e);
|
dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":<br>" + e);
|
||||||
|
Loading…
Reference in New Issue
Block a user