Skip to content

Commit

Permalink
Refactors grass update logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tsanth committed Oct 12, 2024
1 parent 3336ddd commit b4adc75
Showing 1 changed file with 159 additions and 95 deletions.
254 changes: 159 additions & 95 deletions js/grass.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,13 @@
// DEBUGGING OUTPUT
//logOnce('updateEnter', `.../js/grass.js:update() ${JSON.stringify(state)}`)

// Unload grass if we leave its microtab
// Grass isn't loaded if we leave its microtab
if (player.g.isGrassLoaded && !state.onGrassMicrotab) {
layers.g.unloadGrass()
player.g.isGrassLoaded = false
}

// Unload golden grass if we leave its microtab
// Golden grass isn't loaded if we leave its microtab
if (player.g.isGoldGrassLoaded && !state.onGoldGrassMicrotab) {
layers.g.unloadGoldGrass()
player.g.isGoldGrassLoaded = false
}

Expand All @@ -108,30 +106,21 @@
updateGrass(delta)
updateGoldGrass(delta)
},
unloadGrass()
{
// XXX: let the timer keep its time
//player.g.grassTimer = new Decimal(0)
player.g.grassCount = new Decimal(0)
unloadGrass() {
// N.B. this space intentionally left blank
},
loadGrass()
{
// savedGrass should never be negative!
if (player.g.savedGrass < 0) {
player.g.savedGrass = player.g.grassCount > 0
? player.g.grassCount
: 0
// grassCount should never be negative!
if (player.g.grassCount < 0) {
player.g.grassCount = new Decimal(0)
}

removeAllGrass();
createGrass(player.g.savedGrass);
player.g.grassCount = player.g.savedGrass
createGrass(player.g.grassCount);
},
unloadGoldGrass()
{
// XXX: let the timer keep its time
//player.g.goldGrassTimer = new Decimal(0)
player.g.goldGrassCount = new Decimal(0)
unloadGoldGrass() {
// N.B. this space intentionally left blank
},
loadGoldGrass()
{
Expand Down Expand Up @@ -662,12 +651,12 @@

const updateGrass = (delta) => {
// Sanity check: grass should never go negative!
if (player.g.grassCount < 0) {
if (player.g.grassCount.lt(0)) {
player.g.grassCount = new Decimal(0)
}

// Pre-calculate how much grass we're adding this tick
const grassToAdd = new Decimal(1).mul(delta)
const seconds = new Decimal(1).mul(delta)

// Cap grass grow rate at 200
// XXX: in what cases does this get pushed over 200, and why?
Expand All @@ -678,27 +667,40 @@ const updateGrass = (delta) => {
// =================================================================
// Timer logic

// DEBUGGING OUTPUT
//logOnce('grassTimer1', `grassCount:${player.g.grassCount}; grassCap: ${player.g.grassCap}; grassTimer:${player.g.grassTimer}`)

// Timer is always running if we're below grass cap
// DEBUG
// logOnce('grassTimer1', `grassCount:${player.g.grassCount}; grassCap: ${player.g.grassCap}; grassTimer:${player.g.grassTimer}`)
if (player.g.grassCount < player.g.grassCap) {
player.g.grassTimer = player.g.grassTimer.add(grassToAdd)
const belowGrassCap = player.g.grassCount.lt(player.g.grassCap)
if (belowGrassCap) {
player.g.grassTimer = player.g.grassTimer.add(seconds)
}

const passedGrassSpawnTime = player.g.grassTimer.gte(player.g.grassReq)
const belowGrassCap = player.g.grassCount < player.g.grassCap
if (passedGrassSpawnTime && belowGrassCap) {
const grassToAdd = player.g.grassTimer
.div(player.g.grassReq)
.floor()

// Add grass
if (player.g.savedGrass < player.g.grassCap) {
player.g.savedGrass++;
if (belowGrassCap) {
player.g.grassCount = player.g.grassCount.add(grassToAdd)
}

// Sanity check: respect the cap
const aboveGrassCap = player.g.grassCount.gt(player.g.grassCap)
if (aboveGrassCap) {
player.g.grassCount = player.g.grassCap
}

// Only create when we're loaded
if (player.g.isGrassLoaded) {
console.log('creating')
createGrass(1);
createGrass(grassToAdd)
}

// Reset the timer
player.g.grassTimer = new Decimal(0)
} else if (passedGrassSpawnTime && !belowGrassCap) {
// Reset the timer
player.g.grassTimer = new Decimal(0)
}
Expand All @@ -707,100 +709,165 @@ const updateGrass = (delta) => {
// Effect logic

// XXX: is the intent to update the effect before or after adding grass?
player.g.grassEffect = player.g.grass.mul(0.3).pow(0.7).add(1)
player.g.grassEffect = player.g.grass
.mul(0.3)
.pow(0.7)
.add(1)

// =================================================================
// Currency logic

// Add currency
// Modified by Grasshop, buyable, Grass Study 1
player.g.grass = player.g.grass.add(
player.g.grassVal.mul(buyableEffect("gh", 11).mul(delta)))
player.g.grass = player.g.grass
.add(player.g.grassVal
.mul(buyableEffect('gh', 11)
.mul(delta)
)
)

// Rocket Fuel, upgrade: Rocket Fuel Upgrade 2
// Straight 20% bonus
if (hasUpgrade("rf", 12)) {
player.g.grass = player.g.grass.add(
player.g.grassVal.mul(Decimal.mul(0.2, delta)))
if (hasUpgrade('rf', 12)) {
player.g.grass = player.g.grass
.add(player.g.grassVal
.mul(Decimal
.mul(0.2, delta)
)
)
}

// Infinity Points, milestone: 4 Infinities
// Straight 5% bonus
if (hasMilestone("ip", 13) && !inChallenge("ip", 14)) {
player.g.grass = player.g.grass.add(
player.g.grassVal.mul(Decimal.mul(0.05, delta)))
if (hasMilestone('ip', 13) && !inChallenge('ip', 14)) {
player.g.grass = player.g.grass
.add(player.g.grassVal
.mul(Decimal
.mul(0.05, delta)
)
)
}

// =================================================================
// Value logic

player.g.grassVal = new Decimal(1)
player.g.grassVal = player.g.grassVal.mul(buyableEffect("g", 11))

// Grass, upgrade: Grass Upgrade 1
if (hasUpgrade("g", 11)) {
player.g.grassVal = player.g.grassVal.mul(upgradeEffect("g", 11))
.mul(buyableEffect('g', 11))
.mul(player.g.goldGrassEffect)
.mul(buyableEffect('t', 17))
.mul(player.gh.grasshopperEffects[4])
.mul(player.gh.fertilizerEffect)
.mul(buyableEffect('f', 1))
.mul(buyableEffect('f', 2))
.mul(buyableEffect('f', 3))
.mul(buyableEffect('f', 4))
.mul(buyableEffect('f', 5))
.mul(buyableEffect('f', 6))
.mul(buyableEffect('f', 7))
.mul(buyableEffect('f', 8))
.mul(player.cb.commonPetEffects[3][0])
.mul(player.d.diceEffects[5])
.mul(player.rf.abilityEffects[2])
.div(player.pe.pestEffect[4])

// Grass upgrade: Grass Upgrade 1
if (hasUpgrade('g', 11)) {
player.g.grassVal = player.g.grassVal
.mul(upgradeEffect('g', 11))
}

player.g.grassVal = player.g.grassVal.mul(player.g.goldGrassEffect)
player.g.grassVal = player.g.grassVal.mul(buyableEffect("t", 17))
player.g.grassVal = player.g.grassVal.mul(player.gh.grasshopperEffects[4])
player.g.grassVal = player.g.grassVal.mul(player.gh.fertilizerEffect)
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 1))
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 2))
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 3))
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 4))
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 5))
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 6))
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 7))
player.g.grassVal = player.g.grassVal.mul(buyableEffect("f", 8))
player.g.grassVal = player.g.grassVal.mul(player.cb.commonPetEffects[3][0])
player.g.grassVal = player.g.grassVal.mul(player.d.diceEffects[5])
player.g.grassVal = player.g.grassVal.mul(player.rf.abilityEffects[2])
if (hasUpgrade("ad", 14)) {
player.g.grassVal = player.g.grassVal.mul(upgradeEffect("ad", 14))
// AD upgrade: AD Upgrade 4
if (hasUpgrade('ad', 14)) {
player.g.grassVal = player.g.grassVal
.mul(upgradeEffect('ad', 14))
}
player.g.grassVal = player.g.grassVal.div(player.pe.pestEffect[4])
if (inChallenge("ip", 13)) {
player.g.grassVal = player.g.grassVal.pow(0.75)

// -------------------------
// REORDERING BOUNDARY: above stays above, below stays below
// -------------------------

// IP challenge: Challenge 3
if (inChallenge('ip', 13)) {
player.g.grassVal = player.g.grassVal
.pow(0.75)
}
if (inChallenge("ip", 13) || player.po.hex) {
player.g.grassVal = player.g.grassVal.mul(buyableEffect("h", 14))

// -------------------------
// REORDERING BOUNDARY: above stays above, below stays below
// -------------------------

// IP challenge: Challenge 3
if (inChallenge('ip', 13) || player.po.hex) {
player.g.grassVal = player.g.grassVal
.mul(buyableEffect('h', 14))
}

// Antidebuff: Grass
if (player.de.antidebuffIndex.eq(2)) {
player.g.grassVal = player.g.grassVal.mul(player.de.antidebuffEffect)
}
if (inChallenge("tad", 11)) {
player.g.grassVal = player.g.grassVal.pow(0.4)
player.g.grassVal = player.g.grassVal
.mul(player.de.antidebuffEffect)
}
if (inChallenge("tad", 11)) {
player.g.grassVal = player.g.grassVal.pow(buyableEffect("de", 15))

// -------------------------
// REORDERING BOUNDARY: above stays above, below stays below
// -------------------------

// Tav's Domain challenge: Tav's Domain
if (inChallenge('tad', 11)) {
player.g.grassVal = player.g.grassVal
.pow(0.4)
.pow(buyableEffect('de', 15))
}
player.g.grassVal = player.g.grassVal.mul(buyableEffect("gh", 33))
player.g.grassVal = player.g.grassVal.mul(player.r.timeCubeEffects[2])
player.g.grassVal = player.g.grassVal.pow(buyableEffect("rm", 25))

if (inChallenge("ip", 18) && player.g.grass.gt(player.g.grass.mul(0.4 * delta)))
{
player.g.grass = player.g.grass.sub(player.g.grass.mul(0.4 * delta))
// -------------------------
// REORDERING BOUNDARY: above stays above, below stays below
// -------------------------

player.g.grassVal = player.g.grassVal
.mul(buyableEffect('gh', 33))
.mul(player.r.timeCubeEffects[2])

// -------------------------
// REORDERING BOUNDARY: above stays above, below stays below
// -------------------------

player.g.grassVal = player.g.grassVal
.pow(buyableEffect('rm', 25))

// -------------------------
// REORDERING BOUNDARY: above stays above, below stays below
// -------------------------

const grassAtC18Threshold = player.g.grass.gt(player.g.grass
.mul(0.4 * delta))

// IP challenge: Challenge 8
if (inChallenge('ip', 18) && grassAtC18Threshold) {
player.g.grass = player.g.grass
.sub(player.g.grass
.mul(0.4 * delta)
)
}

// =================================================================
// Spawn-time logic

player.g.grassReq = new Decimal(4)
player.g.grassReq = player.g.grassReq.div(buyableEffect("g", 12))
.div(buyableEffect('g', 12))

// =================================================================
// Cap logic

player.g.grassCap = new Decimal(100)
player.g.grassCap = player.g.grassCap.add(buyableEffect("g", 13))
if (hasUpgrade("g", 18)) {
player.g.grassCap = player.g.grassCap.add(150)
.add(buyableEffect('g', 13))

if (hasUpgrade('g', 18)) {
player.g.grassCap = player.g.grassCap
.add(150)
}
if (hasUpgrade("g", 19)) {
player.g.grassCap = player.g.grassCap.add(upgradeEffect("g", 19))
if (hasUpgrade('g', 19)) {
player.g.grassCap = player.g.grassCap
.add(upgradeEffect('g', 19))
}
}

Expand Down Expand Up @@ -883,12 +950,12 @@ const updateGoldGrass = (delta) => {

function createGrass(quantity) {
// This _shouldn't_ happen, but there existed cases where e.g.
// player.g.savedGrass somehow got a massively-negative number and
// as a result, the loop down below never finished.
// We now dump some info to console if this happens.
// player.g.savedGrass (when it used to exist) somehow got a
// massively-negative number and as a result, the loop down below
// never finished. We now dump some info to console if this happens.
if (quantity < 0) {
console.log('%cCalled .../js/grass.js:createGrass with negative quantity!', 'color:red; font-size: 150%; font-weight: bold')
console.log(`%cInfo for the devs: fx: .../js/grass.js:createGrass; quantity: ${quantity}; player.g.grassCount ${player.g.grassCount}; player.g.savedGrass: ${player.g.savedGrass}`, 'color: yellow; font-size: 125%')
console.log(`%cInfo for the devs: fx: .../js/grass.js:createGrass; quantity: ${quantity}; player.g.grassCount ${player.g.grassCount}`, 'color: yellow; font-size: 125%')
return
}

Expand Down Expand Up @@ -933,11 +1000,10 @@ function createGrass(quantity) {

const distance = getDistance(cursorX, cursorY, squareCenterX, squareCenterY);

// If the cursor is within 150 pixels, remove the grass square
// If the cursor is within a certain pixel range, remove the grass square
if (distance <= 100) {
removeGrass(greenSquare);
player.g.grassCount--; // Decrease grass count
player.g.savedGrass--; // Decrease saved grass count
player.g.grassCount = player.g.grassCount.sub(1);
player.g.grass = player.g.grass.add(player.g.grassVal);

// Remove the mousemove listener once grass is collected
Expand All @@ -947,8 +1013,6 @@ function createGrass(quantity) {

// Add the mousemove event listener to check the distance from the cursor
document.addEventListener('mousemove', checkCursorDistance);

player.g.grassCount++; // Increase grass count
}
}

Expand Down

0 comments on commit b4adc75

Please sign in to comment.