// Preloads
let normalTexture;
let hungryTexture;
let sickTexture;
let sadTexture;
let sleepyTexture;
let sleepingTexture;
let fatTexture;
let rehabTexture;

let deathSadnessTexture;
let deathNormalTexture;

let backgroundTexture;
let backgroundCasinoTexture;

let dealerTexture;
let blackjackTableTexture;

let shopTexture;

// Buttons
let btn_all_in;
let btn_75_percent;
let btn_50_percent;
let btn_25_percent;
let btn_10_percent;

let btn_hit;
let btn_stand;

let btn_go_home;
let btn_go_to_shop;
let btn_go_to_casino;

let btn_sleep;
let btn_rehab;

// Enums
const gameStates = {
  mainGame: "Main Game",
  inShop: "In Shop",
  bettingInCasino: "Betting In Casino",
  playingInCasino: "Playing In Casino",
};

const otterStates = {
  normal: "Normal",
  hungry: "Hungry",
  sleepy: "Sleepy",
  sick: "Sick",
  sad: "Sad",
  fat: "fat",
  sleeping: "sleeping",
  rehab: "rehab",
  deathHunger: "death_hunger",
  deathHealth: "death_health",
  deathSadness: "death_sadness"
}

const times = {
  currentSecond: 0,
  currentMinute: 0,
}

const foodItems = [
  {
    name: "Rotten Fish",
    cost: 5,
    foodValue: 25,
    diseaseValue: -3,
    funValue: -1,
  },
  {
    name: "Fish",
    cost: 10,
    foodValue: 50,
    diseaseValue: 0,
    funValue: 0,
  },
  {
    name: "Sushi",
    cost: 30,
    foodValue: 75,
    diseaseValue: 1,
    funValue: 5,
  },
  {
    name: "Small Medicine",
    cost: 5,
    foodValue: 0,
    diseaseValue: 10,
    funValue: -2,
  },
  {
    name: "Medium Medicine",
    cost: 15,
    foodValue: 0,
    diseaseValue: 40,
    funValue: 0,
  },
  {
    name: "Large Medicine",
    cost: 20,
    foodValue: 0,
    diseaseValue: 65,
    funValue: 0,
  },
]

// Consts
const canvasXSize = 400;
const canvasYSize = 400;

// Stats
let gameState = gameStates.mainGame;
let otterState = otterStates.normal;

let is_dead = false;

let petName = null;
let currentSprite;

let hunger = 100;
let energy = 100;
let happyness = 100;
let health = 100;
let addiction = 0;

let cash = 0;

const foodDecay = 0.26;
const energyDecay = 0.25;
const funDecay = 0.1;
const healthDecay = 0.05;

let foodDecayBonus = 0;
let energyDecayBonus = 0;
let funDecayBonus = 0;
let healthDecayBonus = 0;

let lastSecond = 0;
let lastMinute = 0;

let currentBet = 0;
const totalCasinoButtons = 10;
let currentHand = 10;
let dealerHand = 10;

// Date
let currentTime = Date.now()
let lastSavedDate = null;

// arrays
let casinoButtons = [];
let shopButtons = [];
let possibleLeftoverActions = [
  "Did a whole bunch of nothing.",
  "Ate a rock.",
  "Drew himself in the sand.",
  "Thought about gambling.",
  "Floated in the water.",
  "Contemplated his life choices.",
  "Did a sick flip.",
  "Flipped somebody off.",
  "Drank water.",
  "Fought a crab.",
  "Tried escaping from you.",
  "Tried to call animal protective services.",
  "Became FBI most wanted... in his dreams.",
  "Made a spotify playlist.",
  "Watched OnlyOtters.",
  "Hit his head.",
  "Met an illegal fish dealer.",
  "Found a $100 bill... but ate it.",
  "Found a girlfriend... but got dumped.",
  "Fell off a rock, but remained unscathed.",
  "Decided to prank his friends.",
  "Almost drowned.",
  "Tried to compete in Eurovision... but didn't get accepted.",
  "Entered a game jam.",
  "Ran out of stuff to do."
];

function setup() {
  frameRate(30);
  imageMode(CENTER);
  createCanvas(canvasXSize, canvasYSize);

  // Maak casino buttons
  for (let i = 1; i < totalCasinoButtons + 1; i++) {
    let btn = createButton(i);
    btn.size(canvasXSize / totalCasinoButtons, canvasYSize / totalCasinoButtons);
    btn.position(i * canvasXSize / totalCasinoButtons - canvasXSize / totalCasinoButtons, 300);
    btn.mousePressed(() => _set_bet(i));
    casinoButtons.push(btn);
  }

  // Maak shop buttons
  for (let i = 0; i < foodItems.length; i++) {
    let btn = createButton(foodItems[i].name + " ($" + foodItems[i].cost + ")");
    btn.size(canvasXSize / foodItems.length, canvasYSize / foodItems.length);
    btn.position(i * canvasXSize / foodItems.length, 300);
    btn.mousePressed(() => buy_and_use_food(i));
    btn.hide();
    shopButtons.push(btn);
  }

  // Maak casino buttons

  btn_75_percent = createButton("75%");
  btn_50_percent = createButton("50%");
  btn_25_percent = createButton("25%");
  btn_10_percent = createButton("10%");
  btn_all_in = createButton("ALL IN");
  btn_75_percent.size(50, 50);;
  btn_50_percent.size(50, 50);
  btn_25_percent.size(50, 50);
  btn_10_percent.size(50, 50);
  btn_all_in.size(50, 50);
  btn_10_percent.position(75, 350);
  btn_25_percent.position(125, 350);
  btn_50_percent.position(175, 350);
  btn_75_percent.position(225, 350)
  btn_all_in.position(275, 350);

  btn_50_percent.mousePressed(() => _on_percent_clicked(2));
  btn_75_percent.mousePressed(() => _on_percent_clicked(100 / 75));
  btn_25_percent.mousePressed(() => _on_percent_clicked(4));
  btn_10_percent.mousePressed(() => _on_percent_clicked(10));
  btn_all_in.mousePressed(() => _on_percent_clicked(1));

  casinoButtons.push(btn_10_percent);
  casinoButtons.push(btn_25_percent);
  casinoButtons.push(btn_50_percent);
  casinoButtons.push(btn_75_percent)
  casinoButtons.push(btn_all_in);

  btn_hit = createButton("Hit");
  btn_stand = createButton("Stand");

  btn_hit.size(50, 50);
  btn_stand.size(50, 50);

  btn_hit.position(150, 200);
  btn_stand.position(200, 200);

  btn_hit.mousePressed(_on_hit_clicked);
  btn_stand.mousePressed(_on_stand_clicked);

  btn_go_home = createButton("Go to: home");
  btn_go_home.size(100, 25);
  btn_go_home.position(300, 0);
  btn_go_home.mousePressed(_on_home_button_pressed);

  btn_go_to_casino = createButton("Go to: casino");
  btn_go_to_casino.size(100, 25);
  btn_go_to_casino.position(300, 0);
  btn_go_to_casino.mousePressed(_on_casino_button_pressed);

  btn_go_to_shop = createButton("Go to: shop");
  btn_go_to_shop.size(100, 25);
  btn_go_to_shop.position(300, 25);
  btn_go_to_shop.mousePressed(_on_shop_button_pressed);

  btn_sleep = createButton("Sleep");
  btn_sleep.size(100, 25);
  btn_sleep.position(300, 375);
  btn_sleep.mousePressed(_on_sleep_button_pressed);

  btn_rehab = createButton("Rehab");
  btn_rehab.size(100, 25);
  btn_rehab.position(300, 350);
  btn_rehab.mousePressed(_on_rehab_button_pressed);

  _show_casino_buttons(false);
  _show_hit_and_stand_buttons(false);
  _show_shop_buttons(false);

  load_data();

  // Offline shit
  if (lastSavedDate != null) {
    let change_in_hours = floor((currentTime - lastSavedDate) / 3600000);
    if (change_in_hours <= 24 && change_in_hours >= 1) {
      offline_work(change_in_hours);
    }
    else if (change_in_hours >= 24){
      offline_work(24);
    }
  }
}

function preload() {
  normalTexture = loadImage("Assets/Happy.png");
  hungryTexture = loadImage("Assets/Hungry.png");
  sickTexture = loadImage("Assets/Sick.png");
  sleepyTexture = loadImage("Assets/Sleepy.png");
  sadTexture = loadImage("Assets/Sad.png");
  sleepingTexture = loadImage("Assets/Sleeping.png");
  fatTexture = loadImage("Assets/Fat.png");
  deathSadnessTexture = loadImage("Assets/deathSadness.png");
  deathNormalTexture = loadImage("Assets/HungerDeath.png");
  rehabTexture = loadImage("Assets/Rehab.png");

  dealerTexture = loadImage("Assets/Dealer.png");
  blackjackTableTexture = loadImage("Assets/BlackjackTable.png");

  backgroundCasinoTexture = loadImage("Assets/BackgroundCasino.png");
  backgroundTexture = loadImage("Assets/Background.png");

  shopTexture = loadImage("Assets/Shop.png")
}

function create_new_pet() {
  hunger = random(75, 90);
  energy = random(75, 100);
  happyness = random(75, 100);
  health = random(75, 100);
  addiction = random(0, 0.025);

  let is_happy_with_name = false

  while (!is_happy_with_name) {
  petName = prompt("Name your pet!");
  if (petName === null) {
    alert("He needs a name!");
    continue;
  }
  else if (petName.length >= 10) {
    alert("That name is too long, it won't be able to remember that!");
    continue;
  }
  else if (petName.length <= 1) {
    alert("His name can't be THAT short!");
    continue;
  }

  is_happy_with_name = confirm("Are you sure you want to name it " + petName + "?");
    
  }
}

function draw() {
  if (petName === null) {
    create_new_pet();
  }
  background("#14ecf3ff");

  times.currentSecond = second();
  times.currentMinute = minute();

  if (times.currentSecond != lastSecond) {
    lastSecond = times.currentSecond;
    hunger -= foodDecay + foodDecayBonus;
    energy -= energyDecay + energyDecayBonus;
    health -= healthDecay + healthDecayBonus;
    happyness -= funDecay + addiction + funDecayBonus;
    _check_for_state(false);
    save_data();
  }
  if (times.currentMinute != lastMinute) {
    cash += 1;
    lastMinute = times.currentMinute;
  }
  
  textAlign(CENTER, CENTER);
  textSize(32);

  switch (gameState) {
    case gameStates.mainGame:
      image(backgroundTexture, canvasXSize / 2, canvasYSize / 2, 400, 400);
      image(currentSprite, canvasXSize / 2, canvasYSize / 2, 200, 200);
      break;
    case gameStates.bettingInCasino:
      image(backgroundCasinoTexture, canvasXSize / 2, canvasYSize / 2, 400, 400);
      image(dealerTexture, canvasXSize / 2, canvasYSize / 2, 200, 200);
      image(blackjackTableTexture, canvasXSize / 2, canvasYSize / 2, 400, 400);
      image(currentSprite, canvasXSize / 2, canvasYSize - 40, 100, 100);
      fill("white");
      text("Choose your bet:", canvasXSize / 2, 280);
      _show_casino_buttons(true);
      _show_hit_and_stand_buttons(false);
      break;
    case gameStates.playingInCasino:
      _show_casino_buttons(false);
      _show_hit_and_stand_buttons(true);
      image(backgroundCasinoTexture, canvasXSize / 2, canvasYSize / 2, 400, 400);
      image(dealerTexture, canvasXSize / 2, canvasYSize / 2, 200, 200);
      image(blackjackTableTexture, canvasXSize / 2, canvasYSize / 2, 400, 400);
      image(currentSprite, canvasXSize / 2, canvasYSize - 40, 100, 100);
      fill("white");
      text("Your hand: " + currentHand, canvasXSize / 2, 280);
      break;
    case gameStates.inShop:
      image(shopTexture, canvasXSize / 2, canvasYSize / 2, 400, 400);
      break;
  }

  textAlign(LEFT, LEFT);
  textSize(12);
  fill("Green");
  text("Cash: " + floor(cash), 0, 12);
}

function _check_for_state(ignoreDeath) {
  if (hunger <= 0 && !ignoreDeath) {
    currentSprite = deathNormalTexture;
    otterState = otterStates.deathHunger;
    _death("hunger")
  }
  else if (health <= 0 && !ignoreDeath) {
    currentSprite = deathNormalTexture;
    otterState = otterStates.deathHealth;
    _death("bad health");
  }
  else if (happyness <= 0 && !ignoreDeath) {
    currentSprite = deathSadnessTexture;
    otterState = otterStates.deathSadness;
    _death("sadness");
  }
  else if (energy <= 0) {
    currentSprite = sleepingTexture;
    otterState = otterStates.sleeping;
    energyDecayBonus = -1;
    foodDecayBonus = -0.005;
    healthDecayBonus = 0;
    funDecayBonus = 0;
  }
  else if (otterState === otterStates.rehab) {
    let multiplier = 1;

    multiplier = ceil(addiction);
    
    let cost = 1/30 * multiplier
    if (cash <= cost) {
      currentSprite = normalTexture;
      otterState = otterStates.normal;
      alert("You can't afford rehab!");
      _check_for_state(false);
      return
    }
    currentSprite = rehabTexture;
    cash -= cost;
    addiction -= 1/60 * multiplier;
    energyDecayBonus = 0.2;
    foodDecayBonus = 0;
    healthDecayBonus = -0.05;
    funDecayBonus = 0.1;
    if (energy <= 40) {
      currentSprite = normalTexture;
      otterState = otterStates.normal;
      alert(petName + " is too tired to attend rehab!");
      _check_for_state(false);
    }
    else if (addiction <= 0) {
      addiction = 0;
      alert(petName + " is no longer an addict!");
      currentSprite = normalTexture;
      otterState = otterStates.normal;
      _check_for_state(false);
    }
  }
  else if (otterState === otterStates.sleeping) {
    currentSprite = sleepingTexture;
    energyDecayBonus = -1;
    foodDecayBonus = -0.005;
    healthDecayBonus = 0;
    funDecayBonus = 0;
    if (energy >= 100) {
      currentSprite = normalTexture;
      otterState = otterStates.normal;
      _check_for_state(false);
    }
  }
  else if (energy <= 40) {
    currentSprite = sleepyTexture;
    otterState = otterStates.sleepy;
    energyDecayBonus = 0.2;
    foodDecayBonus = 0;
    healthDecayBonus = 0.125;
    funDecayBonus = 0.3;
    if (gameState != gameStates.mainGame) {
      alert(petName + " was getting tired and returned home!");
      _set_button_visibility_home()
      gameState = gameStates.mainGame;
    }
  }
  else if (hunger >= 90) {
    currentSprite = fatTexture;
    otterState = otterStates.fat;
    energyDecayBonus = 0;
    foodDecayBonus = -0.25;
    healthDecayBonus = 1;
    funDecayBonus = 1;
  }
  else if (hunger <= 40) {
    currentSprite = hungryTexture;
    otterState = otterStates.hungry;
    energyDecayBonus = 0;
    foodDecayBonus = 0;
    healthDecayBonus = 0;
    funDecayBonus = 0;
  }
  else if (health <= 40) {
    currentSprite = sickTexture;
    otterState = otterStates.sick;
    energyDecayBonus = 0;
    foodDecayBonus = 0;
    healthDecayBonus = 0;
    funDecayBonus = 0;
  }
  else if (happyness <= 40) {
    currentSprite = sadTexture;
    otterState = otterStates.sad;
    energyDecayBonus = 0;
    foodDecayBonus = 0;
    healthDecayBonus = 0;
    funDecayBonus = 0;
  }
  else {
    currentSprite = normalTexture;
    otterState = otterStates.normal;
    energyDecayBonus = 0;
    foodDecayBonus = 0;
    healthDecayBonus = 0;
    funDecayBonus = 0;
  }
}

function _death(cause) {
  if (is_dead) {
    return
  }
  is_dead = true
  gameState = gameStates.mainGame
  btn_go_home.hide();
  btn_sleep.show();
  btn_rehab.show();
  btn_go_to_casino.show();
  btn_go_to_shop.show();
  _show_casino_buttons(false);
  _show_hit_and_stand_buttons(false);
  _show_shop_buttons(false);
  setTimeout(() => {
  if (floor(cash) <= 0) {
    alert(petName + " has died of " + cause + ", you got donated a new otter!");
  }
  else {
    alert(petName + " has died of " + cause + ", you spent $" + floor(cash) + " to buy a new otter!");
  }
  is_dead = false;
  cash = 0;
  create_new_pet();
  }, 1000);
}

function _set_bet(amount) {
  currentHand = floor(random(2,22));
  dealerHand = floor(random(2,12));
  while (dealerHand <= 16) {
    dealerHand += floor(random(1,11));
  }
  if (cash >= amount) {
    cash -= amount;
    currentBet = amount;
    gameState = gameStates.playingInCasino;
  }
  else {
    alert("You can't afford that!");
    return
  }
  if (currentHand === 21) {
    alert("BLACKJACK! AUTOMATIC WIN! YOU WON " + currentBet * 3 + " CASH!");
    cash += currentBet * 3;
    gameState = gameStates.bettingInCasino;
  }
  else if (dealerHand > 21) {
    alert("Dealer busted! Automatic victory! You won " + currentBet * 2 + " cash!");
    cash += currentBet * 2;
    gameState = gameStates.bettingInCasino;
  }

  addiction += 0.03 * currentBet;
  happyness += 3 * currentBet - addiction * 5;

}

function _on_hit_clicked() {
  currentHand += floor(random(2, 11));
  if (currentHand > 21) {
    alert("You got over 21, you lose! You had " + currentHand + " as your hand!");
    gameState = gameStates.bettingInCasino;
  }
}

function _on_stand_clicked() {
  alert("Your hand: " + currentHand + "\nDealers hand: " + dealerHand);
  if (dealerHand < currentHand) {
    alert("You won " + currentBet * 2 + " cash!");
    cash += currentBet * 2;
  }
  else if (dealerHand > currentHand) {
    alert("You lost " + currentBet + " cash!");
  }
  else if (dealerHand === currentHand) {
    alert("You tied! Your bet of " + currentBet + " has been returned!");
    cash += currentBet;
  }
  gameState = gameStates.bettingInCasino;
}

function _on_percent_clicked(percent) {
  let cash_to_use = floor(cash)
  if (cash_to_use > 0) {
    let amount = ceil(cash_to_use / percent);
    if (amount <= 0) {
      amount = 1;
    }
    _set_bet(amount);
  }
  else {
    alert("You can't bet nothing!");
  }
}

function _show_casino_buttons(Show) {
  if (Show) {
    for (let i = 0; i < casinoButtons.length; i++) {
      casinoButtons[i].show();
    }
  }
  else {
      for (let i = 0; i < casinoButtons.length; i++) {
        casinoButtons[i].hide();
    }
  }
}

function _show_hit_and_stand_buttons(Show) {
  if (Show) {
    btn_hit.show();
    btn_stand.show();
  }
  else {
    btn_hit.hide();
    btn_stand.hide();
  }
}

function _show_shop_buttons(Show) {
  if (Show) {
    for (let i = 0; i < shopButtons.length; i++) {
      shopButtons[i].show();
    }
  }
  else {
      for (let i = 0; i < shopButtons.length; i++) {
        shopButtons[i].hide();
    }
  }
}


function _on_home_button_pressed() {
  gameState = gameStates.mainGame;
  btn_go_home.hide();
  btn_sleep.show();
  btn_rehab.show();
  btn_go_to_casino.show();
  btn_go_to_shop.show();
  _show_hit_and_stand_buttons(false);
  _show_casino_buttons(false);
  _show_shop_buttons(false);
}

function _on_casino_button_pressed() {
  if (otterState === otterStates.sleeping) {
    alert(petName + " can't go there when he's asleep!");
    return
  }
  else if (otterState === otterStates.sleepy) {
    alert(petName + " is too tired to do that!");
    return
  }
  else if (otterState === otterStates.rehab) {
    alert(petName + " can't go during rehab!");
    return
  }
  gameState = gameStates.bettingInCasino;
  _show_hit_and_stand_buttons(false);
  _show_casino_buttons(true);
  _show_shop_buttons(false);
  btn_go_home.show();
  btn_sleep.hide();
  btn_rehab.hide();
  btn_go_to_casino.hide();
  btn_go_to_shop.hide();
}

function _on_shop_button_pressed() {
  if (otterState === otterStates.sleeping) {
    alert(petName + " can't go there when it's asleep!");
    return
  }
  else if (otterState === otterStates.sleepy) {
    alert(petName + " is too tired to do that!");
    return
  }
  else if (otterState === otterStates.rehab) {
    alert(petName + " can't go during rehab!");
    return
  }
  gameState = gameStates.inShop;
  btn_go_home.show();
  btn_sleep.hide();
  btn_rehab.hide();
  btn_go_to_casino.hide();
  btn_go_to_shop.hide();
  _show_hit_and_stand_buttons(false);
  _show_casino_buttons(false);
  _show_shop_buttons(true);
}

function _on_sleep_button_pressed() {
  if (otterState == otterStates.rehab) {
    alert(petName + " cant go to sleep during rehab!");
  }
  else if (otterState == otterStates.sleeping) {
    otterState = otterStates.normal;
    _check_for_state(false);
  }
  else {
    otterState = otterStates.sleeping;
  }
}

function _on_rehab_button_pressed() {
  if (addiction === 0) {
    alert(petName + " isn't addicted!");
  }
  else if (otterState == otterStates.sleeping) {
    alert(petName + " cant go to rehab whilst asleep!");
  }
  else if (otterState == otterStates.rehab) {
    otterState = otterStates.normal;
    _check_for_state(false);
  }
  else {
    otterState = otterStates.rehab;
  }
}

function _set_button_visibility_home() {
  btn_go_home.hide();
  btn_sleep.show()
  btn_go_to_casino.show();
  btn_go_to_shop.show();
  _show_hit_and_stand_buttons(false);
  _show_casino_buttons(false);
  _show_shop_buttons(false);
}

function load_data() {
  const data = localStorage.getItem("tamagotchiData");
  if (data === null) {
    return
  }

  const saved = JSON.parse(data);
  hunger = saved.hunger;
  energy = saved.energy;
  happyness = saved.happyness;
  health = saved.health;
  addiction = saved.addiction;
  cash = saved.cash;
  petName = saved.petName;
  lastSavedDate = saved.lastSavedDate;
}

function save_data() {
  const to_save = {
    hunger, energy, happyness, health, addiction, cash, petName, lastSavedDate: Date.now()
  }
  localStorage.setItem("tamagotchiData", JSON.stringify(to_save));
}

function buy_and_use_food(food_id) {
  if (foodItems[food_id].cost <= cash) {
    cash -= foodItems[food_id].cost
    hunger += foodItems[food_id].foodValue;
    health += foodItems[food_id].diseaseValue;
    happyness += foodItems[food_id].funValue;
  }
  else {
    alert("You can't afford that!");
  }
}

function offline_work(totalHours) {
  let changes = [];
  var forced_to_skip_gamble = false
  for (let i = 0; i < totalHours; i++) {
    cash += 0.1;
    if (i % 5 === 0) {
      forced_to_skip_gamble = true;
    }
    else {
      forced_to_skip_gamble = false;
    }
    if (random(0,100) < addiction * 10 && cash > 0 && !forced_to_skip_gamble) {
      let bet = round(random(1, cash));
      changes.push(pet_gamble(bet));
    }
    else if (hunger <= 40 && cash >= 5) { // Hungry
      if (cash >= 30) {
        buy_and_use_food(2);
        changes.push("Ate some sushi!");
      }
      else if (cash >= 10) {
        buy_and_use_food(1);
        changes.push("Ate a fish.");
      }
      else if (cash >= 5) {
        buy_and_use_food(0);
        changes.push("Ate a rotten fish.");
      }
      else {
        changes.push("Was hungry, but couldn't afford food.");
      }
    }
    else if (health <= 40 && cash >= 5) { // Unhealthy
      if (cash >= 20) {
        buy_and_use_food(5);
        changes.push("Used small medicine!");
      }
      else if (cash >= 15) {
        buy_and_use_food(4);
        changes.push("Used medium medicine!");
      }
      else if (cash >= 5) {
        buy_and_use_food(3);
        changes.push("Used small medicine!");
      }
      else {
        changes.push("Was feeling sick... but couldn't afford medicine.");
      }
    }
    else if (energy <= 40) {
      changes.push("Went to sleep.");
      energy += 25
    }
    else if (addiction >= 2 && cash >= 4) {
      changes.push("Went to rehab!");
      addiction -= 2;
      cash -= 4;
    }
    else if (happyness <= 40) {
      changes.push("Went for a fun swim!");
      happyness += 24;
    }
    else if (random() < 0.1) {
      changes.push("Found a dollar!");
      cash += 1
    }
    else {
      changes.push(random(possibleLeftoverActions));
      happyness += 5
    }
    hunger = constrain(hunger - foodDecay + foodDecayBonus, 10, 100);
    energy = constrain(energy - energyDecay + energyDecayBonus, 10, 100);
    health = constrain(health - healthDecay + healthDecayBonus, 10, 100);
    happyness = constrain(happyness - funDecay + addiction + funDecayBonus, 10, 100);
    _check_for_state(true);
  }
  alert("While you were gone, " + petName + " did: \n - " + changes.join("\n - "));
}

function pet_gamble(bet) {
  let changes = "Did something... but nobody knows."
  let outcome = random();
  if (outcome <= 0.33) { // LOSER!!!!
    addiction += 0.03 * bet;
    happyness += 3 * bet - addiction * 5;
    cash -= bet;
    changes = ("Lost $" + bet + " at the casino!");
      }
  else if (outcome <= 0.66) { // Winner!!!!
    addiction += 0.03 * bet;
    happyness += 3 * bet - addiction * 5;
    cash += bet;
    changes = ("Won $" + bet + " at the casino!");
    }
  else { // Tie!!!!
    addiction += 0.03 * bet;
    happyness += 3 * bet - addiction * 5;
    cash += bet;
    changes = ("Tied at the casino!");
  }
  return changes;
}