Make islands have limited money, limit amount of price changes each day.

This commit is contained in:
Robbie Antenesse 2016-04-29 12:25:35 -06:00
parent 62524d8693
commit 17227e054a
2 changed files with 65 additions and 24 deletions

View File

@ -87,6 +87,7 @@ G.economy = { // Aww yea, supply and demand.
40, 20, 50, 80, 40, 20, 50, 80,
65, 20, 20, 30, 65, 20, 20, 30,
30, 60, 45, 70], 30, 60, 45, 70],
maxPriceChange: 10,
cargoSold: [0, 0, 0, 0, // The more you sell, the lower the price gets cargoSold: [0, 0, 0, 0, // The more you sell, the lower the price gets
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,

View File

@ -26,19 +26,34 @@ var pr_island = OS.P.Add("Island", {
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0], 0, 0, 0, 0],
money: 0,
inventory: [0, 0, 0, 0, inventory: [0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0], 0, 0, 0, 0],
storageSpace: [],
storageSpace: [0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0],
innPriceDifference: 0, innPriceDifference: 0,
innStays: 0, innStays: 0,
priceDifferences: [], priceDifferences: [0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0],
itemsSold: [0, 0, 0, 0, // The more you sell, the lower the price gets itemsSold: [0, 0, 0, 0, // The more you sell, the lower the price gets
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0], 0, 0, 0, 0],
itemsBought: [0, 0, 0, 0, // The more you buy, the higher the price gets itemsBought: [0, 0, 0, 0, // The more you buy, the higher the price gets
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0],
storageFee: 10,
storageFeeMultiplier: 0.1,
storage: [0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0] 0, 0, 0, 0]
@ -60,8 +75,7 @@ pr_island.SetUp = function () {
if (this.storageSpace[i] > 20) { if (this.storageSpace[i] > 20) {
this.inventory[i] = Math.round(this.storageSpace[i] - Math.randomRange(0, 10)); this.inventory[i] = Math.round(this.storageSpace[i] - Math.randomRange(0, 10));
} } else if (Math.randomRange(0, 100) < 25) {
if (Math.randomRange(0, 100) < 25) {
this.inventory[i] = Math.round(Math.randomRange(0, this.storageSpace[i])); this.inventory[i] = Math.round(Math.randomRange(0, this.storageSpace[i]));
} }
} }
@ -72,25 +86,44 @@ pr_island.SetUp = function () {
if (this.CheckInventory().length < 4) { if (this.CheckInventory().length < 4) {
this.SetUp(); this.SetUp();
} else {
for (var i = 0; i < 16; i++) {
// Generate the amount of money the shop has based on what it has.
this.money += (G.economy.cargoItemWorth[i] + this.priceDifferences[i]) * this.inventory[i];
}
// Minimum amount of money is 100.
if (this.money < 100) this.money = 100;
} }
} }
pr_island.AdjustPrices = function () { pr_island.AdjustPrices = function () {
for (var i = 0; i < 16; i++) { for (var i = 0; i < 16; i++) {
this.priceDifferences[i] = 0; // Save the previous price difference so the change doesn't get above a certain thereshold.
var previousPrice = newPriceDifference = this.priceDifferences[i];
if (this.inventory[i] > (this.storageSpace[i] / 2)) { if (this.inventory[i] > (this.storageSpace[i] / 2)) {
this.priceDifferences[i] += -Math.round(this.inventory[i] * Math.randomRange(1, 3)); newPriceDifference -= Math.round(this.inventory[i] * Math.randomRange(1, 3));
} else if (this.inventory[i] < (this.storageSpace[i] / 4)) { }
this.priceDifferences[i] += Math.round((this.storageSpace[i] - this.inventory[i]) * Math.randomRange(1, 3)); else if (this.inventory[i] < (this.storageSpace[i] / 4)) {
} else { newPriceDifference += Math.round((this.storageSpace[i] - this.inventory[i]) * Math.randomRange(1, 3));
this.priceDifferences[i] += Math.round(Math.randomRange(-2, 2)); }
else {
newPriceDifference += Math.round(Math.randomRange(-2, 2));
} }
this.priceDifferences[i] = Math.round(this.priceDifferences[i] * ((this.itemsBought[i] + 1) / (this.itemsSold[i] + 1))); newPriceDifference = Math.round(newPriceDifference * ((this.itemsBought[i] + 1) / (this.itemsSold[i] + 1)));
if (G.economy.cargoItemWorth[i] + this.priceDifferences[i] < 0) { if (Math.abs(newPriceDifference) > Math.abs(previousPrice) + G.economy.maxPriceChange) {
this.priceDifferences[i] = -G.economy.cargoItemWorth[i] + 1; // Prevent price from changing more than the limit.
newPriceDifference = previousPrice + ((previousPrice < 0) ? -G.economy.maxPriceChange : G.economy.maxPriceChange);
} }
if (G.economy.cargoItemWorth[i] + newPriceDifference < 0) {
newPriceDifference = -G.economy.cargoItemWorth[i] + 1;
}
this.priceDifferences[i] = newPriceDifference;
} }
var priceDifferencesOrdered = this.priceDifferences.slice().sort(sortNumber); var priceDifferencesOrdered = this.priceDifferences.slice().sort(sortNumber);
@ -100,8 +133,11 @@ pr_island.AdjustPrices = function () {
pr_island.SimulateTrade = function () { pr_island.SimulateTrade = function () {
// This will be run on a timer that runs when not trading. // This will be run on a timer that runs when not trading.
for (var i = 0; i < 16; i++) { for (var i = 0; i < 16; i++) {
var amount = 0;
if (this.inventory[i] > 0) { if (this.inventory[i] > 0) {
this.inventory[i] += Math.round(Math.randomRange(-this.storageSpace[i], this.storageSpace[i])); amount = Math.round(Math.randomRange(-this.storageSpace[i], this.storageSpace[i]));
this.inventory[i] += amount;
if (this.inventory[i] < 0) { if (this.inventory[i] < 0) {
this.inventory[i] = 0; this.inventory[i] = 0;
} }
@ -110,11 +146,17 @@ pr_island.SimulateTrade = function () {
} }
} else { } else {
if (Math.randomRange(0, 100) < 15) { if (Math.randomRange(0, 100) < 15) {
this.inventory[i] = Math.round(Math.randomRange(0, this.storageSpace[i])); amount = Math.round(Math.randomRange(0, this.storageSpace[i]));
this.inventory[i] = amount;
} }
} }
// If the amount is positive, then subtract half the amount from the money (because they bought stuff)
// Otherwise, add the negative amount (because they sold that much)
this.money += (-amount * (G.economy.cargoItemWorth[i] + this.priceDifferences[i])) * ((amount > 0) ? 0.5 : 1);
} }
if (this.money <= 0) this.money = 100; // If they run out of money, give them some.
this.AdjustPrices(); this.AdjustPrices();
} }
@ -154,17 +196,17 @@ pr_island.TradeWith = function () {
} }
pr_island.CanSellTo = function (itemIndex, price) { pr_island.CanSellTo = function (itemIndex, price) {
if (this.inventory[itemIndex] < this.storageSpace[itemIndex]) { // If there's space in the inventory if (this.inventory[itemIndex] < this.storageSpace[itemIndex] && price < this.money) {
if (this.itemsBoughtToday[itemIndex] < this.storageSpace[itemIndex] * 0.75) { // and if the island hasn't bought more than 75% of its storage space today // If there's space in the inventory and there's enough money.
return true; return true;
} }
}
return false; return false;
} }
pr_island.SellTo = function (itemIndex, price) { pr_island.SellTo = function (itemIndex, price) {
// Play Buy sound. // Play Buy sound.
this.inventory[itemIndex]++; this.inventory[itemIndex]++;
this.itemsBought[itemIndex]++; this.itemsBought[itemIndex]++;
this.money -= price;
G.inventory.cargo[itemIndex]--; G.inventory.cargo[itemIndex]--;
G.inventory.money += price; G.inventory.money += price;
@ -173,17 +215,15 @@ pr_island.SellTo = function (itemIndex, price) {
pr_island.CanBuyFrom = function (itemIndex, price) { pr_island.CanBuyFrom = function (itemIndex, price) {
if (this.inventory[itemIndex] > 0) { // If there's enough of the item if (this.inventory[itemIndex] > 0) { // If there's enough of the item
if (this.itemsSoldToday[itemIndex] < this.storageSpace[itemIndex] * 0.90) { // and if the island hasn't sold more than 90% of its storage space today
return true; return true;
} }
}
return false; return false;
} }
pr_island.BuyFrom = function (itemIndex, price) { pr_island.BuyFrom = function (itemIndex, price) {
// Play Sell sound. // Play Sell sound.
this.inventory[itemIndex]--; this.inventory[itemIndex]--;
this.itemsSold[itemIndex]++; this.itemsSold[itemIndex]++;
this.itemsSoldToday[itemIndex]++; this.money += price;
G.inventory.cargo[itemIndex]++; G.inventory.cargo[itemIndex]++;
G.inventory.money -= price; G.inventory.money -= price;