Skip to content

Instantly share code, notes, and snippets.

@Plasmarobo
Created April 7, 2020 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plasmarobo/ffde74831ad3cd461b0e4258e04dcdd9 to your computer and use it in GitHub Desktop.
Save Plasmarobo/ffde74831ad3cd461b0e4258e04dcdd9 to your computer and use it in GitHub Desktop.
A quick n dirty untested port to js of TurnipPrices.cpp
function SeededRandom()
{
var srandom = {
context: [0,0,0,0]
};
return srandom;
}
SeededRandom.prototype.Seed(seed)
{
if (typeof variable !== 'undefined') {
seed = 42069;
}
srandom.context.push(0x6C078965 * (seed ^ (seed >> 30)) + 1);
srandom.context.push(0x6C078965 * (context[0] ^ (context[0] >> 30)) + 2);
srandom.context.push(0x6C078965 * (context[1] ^ (context[1] >> 30)) + 3);
srandom.context.push(0x6C078965 * (context[2] ^ (context[2] >> 30)) + 4);
}
SeededRandom.prototype.FromArray = function(seed1, seed2, seed3, seed4)
{
if ((seed1 | seed2 | seed3 | seed4) == 0) // seeds must not be all zero.
{
seed1 = 1;
seed2 = 0x6C078967;
seed3 = 0x714ACB41;
seed4 = 0x48077044;
}
this.context[0] = seed1;
this.context[1] = seed2;
this.context[2] = seed3;
this.context[3] = seed4;
}
SeededRandom.prototype.GetU32 = function()
{
this.context[0] ^ (this.context[0] << 11);
this.context[0] = this.context[1];
this.context[1] = this.context[2];
this.context[2] = this.context[3];
this.context[3] = n ^ (n >> 8) ^ this.context[3] ^ (this.context[3] >> 19);
return this.context[3];
}
SeededRandom.prototype.GetU64 = function()
{
var n1 = this.context[0] ^ (this.context[0] << 11);
var n2 = this.context[1];
var n3 = n1 ^ (n1 >> 8) ^ this.context[3];
this.context[0] = this.context[2];
this.context[1] = this.context[3];
this.context[2] = n3 ^ (this.context[3] >> 19);
this.context[3] = n2 ^ (n2 << 11) ^ ((n2 ^ (n2 << 11)) >> 8) ^ this.context[2] ^ (n3 >> 19);
return (this.context[2] << 32) | this.context[3];
}
SeededRandom.prototype.RandBool = function()
{
return this.GetU32() & 0x80000000;
}
SeededRandom.prototype.RandInt = function(max, min)
{
return ((this.GetU32() * (max - min + 1)) >> 32) + min;
}
SeededRandom.prototype.RandFloat = function(a, b)
{
var val = 0x3F800000 | (this.GetU32() >> 9);
return a + ((val - 1.0) * (b - a));
}
function TurnipPrices(seed)
{
var prices = {
basePrice: 0,
sellPrices: [0,0,0,0,0,0,0,0,0,0,0,0,0,0],
whatPattern: 0,
tmp40: 0,
rng: new SeededRandom()
};
prices.rng.Seed(seed);
return prices;
}
TurnipPrices.prototype.calculate()
{
this.basePrice = this.rng.RandInt(90, 110);
var chance = this.rng.RandInt(0, 99);
var nextPattern;
if (this.whatPattern >= 4)
{
nextPattern = 2;
}
else
{
switch (this.whatPattern)
{
case 0:
if (chance < 20)
{
nextPattern = 0;
}
else if (chance < 50)
{
nextPattern = 1;
}
else if (chance < 65)
{
nextPattern = 2;
}
else
{
nextPattern = 3;
}
break;
case 1:
if (chance < 50)
{
nextPattern = 0;
}
else if (chance < 55)
{
nextPattern = 1;
}
else if (chance < 75)
{
nextPattern = 2;
}
else
{
nextPattern = 3;
}
break;
case 2:
if (chance < 25)
{
nextPattern = 0;
}
else if (chance < 70)
{
nextPattern = 1;
}
else if (chance < 75)
{
nextPattern = 2;
}
else
{
nextPattern = 3;
}
break;
case 3:
if (chance < 45)
{
nextPattern = 0;
}
else if (chance < 70)
{
nextPattern = 1;
}
else if (chance < 85)
{
nextPattern = 2;
}
else
{
nextPattern = 3;
}
break;
}
}
this.whatPattern = nextPattern;
for (var i = 2; i < 14; i++)
{
this.sellPrices[i] = 0;
}
this.sellPrices[0] = this.basePrice;
this.sellPrices[1] = this.basePrice;
var work, decPhaseLen1, decPhaseLen2, peakStart;
var hiPhaseLen1, hiPhaseLen2and3, hiPhaseLen3;
var rate;
switch (this.whatPattern)
{
case 0:
// PATTERN 0: high, decreasing, high, decreasing, high
work = 2;
decPhaseLen1 = this.rng.RandBool() ? 3 : 2;
decPhaseLen2 = 5 - decPhaseLen1;
hiPhaseLen1 = this.rng.RandInt(0, 6);
hiPhaseLen2and3 = 7 - hiPhaseLen1;
hiPhaseLen3 = this.rng.RandInt(0, hiPhaseLen2and3 - 1);
// high phase 1
for (var i = 0; i < hiPhaseLen1; i++)
{
this.sellPrices[work++] = ceil(this.rng.RandFloat(0.9, 1.4) * this.basePrice);
}
// decreasing phase 1
rate = this.rng.RandFloat(0.8, 0.6);
for (var i = 0; i < decPhaseLen1; i++)
{
this.sellPrices[work++] = ceil(rate * this.basePrice);
rate -= 0.04;
rate -= this.rng.RandFloat(0, 0.06);
}
// high phase 2
for (var i = 0; i < (hiPhaseLen2and3 - hiPhaseLen3); i++)
{
this.sellPrices[work++] = ceil(this.rng.RandFloat(0.9, 1.4) * this.basePrice);
}
// decreasing phase 2
rate = this.rng.RandFloat(0.8, 0.6);
for (var i = 0; i < decPhaseLen2; i++)
{
this.sellPrices[work++] = ceil(rate * this.basePrice);
rate -= 0.04;
rate -= this.rng.RandFloat(0, 0.06);
}
// high phase 3
for (var i = 0; i < hiPhaseLen3; i++)
{
this.sellPrices[work++] = ceil(this.rng.RandFloat(0.9, 1.4) * this.basePrice);
}
break;
case 1:
// PATTERN 1: decreasing middle, high spike, random low
peakStart = this.rng.RandInt(3, 9);
rate = this.rng.RandFloat(0.9, 0.85);
for (work = 2; work < peakStart; work++)
{
this.sellPrices[work] = ceil(rate * this.basePrice);
rate -= 0.03;
rate -= this.rng.RandFloat(0, 0.02);
}
this.sellPrices[work++] = ceil(this.rng.RandFloat(0.9, 1.4) * this.basePrice);
this.sellPrices[work++] = ceil(this.rng.RandFloat(1.4, 2.0) * this.basePrice);
this.sellPrices[work++] = ceil(this.rng.RandFloat(2.0, 6.0) * this.basePrice);
this.sellPrices[work++] = ceil(this.rng.RandFloat(1.4, 2.0) * this.basePrice);
this.sellPrices[work++] = ceil(this.rng.RandFloat(0.9, 1.4) * this.basePrice);
for (; work < 14; work++)
{
this.sellPrices[work] = ceil(this.rng.RandFloat(0.4, 0.9) * this.basePrice);
}
break;
case 2:
// PATTERN 2: consistently decreasing
rate = 0.9;
rate -= this.rng.RandFloat(0, 0.05);
for (work = 2; work < 14; work++)
{
this.sellPrices[work] = ceil(rate * this.basePrice);
rate -= 0.03;
rate -= this.rng.RandFloat(0, 0.02);
}
break;
case 3:
// PATTERN 3: decreasing, spike, decreasing
peakStart = this.rng.RandInt(2, 9);
// decreasing phase before the peak
rate = this.rng.RandFloat(0.9, 0.4);
for (work = 2; work < peakStart; work++)
{
this.sellPrices[work] = ceil(rate * this.basePrice);
rate -= 0.03;
rate -= this.rng.RandFloat(0, 0.02);
}
this.sellPrices[work++] = ceil(this.rng.RandFloat(0.9, 1.4) * this.basePrice);
this.sellPrices[work++] = ceil(this.rng.RandFloat(0.9, 1.4) * this.basePrice);
rate = this.rng.RandFloat(1.4, 2.0);
this.sellPrices[work++] = ceil(this.rng.RandFloat(1.4, rate) * this.basePrice) - 1;
this.sellPrices[work++] = ceil(rate * basePrice);
this.sellPrices[work++] = ceil(this.rng.RandFloat(1.4, rate) * this.basePrice) - 1;
// decreasing phase after the peak
if (work < 14)
{
rate = this.rng.RandFloat(0.9, 0.4);
for (; work < 14; work++)
{
this.sellPrices[work] = ceil(rate * this.basePrice);
rate -= 0.03;
rate -= this.rng.RandFloat(0, 0.02);
}
}
break;
}
this.sellPrices[0] = 0;
this.sellPrices[1] = 0;
}
function main(pattern, seed)
{
if (typeof seed === "undefined")
{
seed = 42069;
}
var turnips = new TurnipPrices(parseInt(seed));
turnips.whatPattern = pattern;
turnips.calculate();
const zeroPad = (n, p) => String(n).padStart(p, '0');
console.log("Pattern {0}".format(turnips.whatPattern));
console.log("Sun Mon Tue Wed Thu Fri Sat");
console.log("{0} {1} {2} {3} {4} {5} {6}".format(
zeroPad(turnips.basePrice, 3),
zeroPad(turnips.sellPrices[2], 3),
zeroPad(turnips.sellPrices[4], 3),
zeroPad(turnips.sellPrices[6], 3),
zeroPad(turnips.sellPrices[8], 3),
zeroPad(turnips.sellPrices[10], 3),
zeroPad(turnips.sellPrices[12], 3)));
console.log(" {0} {1} {2} {3} {4} {5}".format(
zeroPad(turnips.sellPrices[3], 3),
zeroPad(turnips.sellPrices[5], 3),
zeroPad(turnips.sellPrices[7], 3),
zeroPad(turnips.sellPrices[9], 3),
zeroPad(turnips.sellPrices[11], 3),
zeroPad(turnips.sellPrices[13], 3)));
}
//main(pattern, seed);
main(0, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment