Last active
January 9, 2020 01:17
-
-
Save scriptype/8e0f4be82072eafbda784b91898e157b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function defensive() { | |
function initialState() { | |
return { | |
toEscape: 25, | |
escaping: false, | |
health: health() | |
}; | |
} | |
const Duck = { | |
state: initialState(), | |
getSafeDirection() { | |
const buffer = 30; | |
if (getX() <= buffer) { | |
return Math.random() * 180 - 90; | |
} else if (getX() >= 100 - buffer) { | |
return Math.random() * 180 + 90; | |
} else if (getY() <= buffer) { | |
return Math.random() * 180; | |
} else if (getY() >= 100 - buffer) { | |
return Math.random() * 180 + 180; | |
} | |
return Math.random(); | |
}, | |
escape() { | |
this.state.escaping = true; | |
this.state.toEscape--; | |
swim(this.getSafeDirection(), 80); | |
}, | |
tick() { | |
if (health() < this.state.health) { | |
if (!this.state.escaping) { | |
this.escape(); | |
} else if (this.state.toEscape-- < 0) { | |
this.state = initialState(); | |
} | |
} | |
} | |
}; | |
return Duck; | |
} | |
function offensive() { | |
const initialPrecision = 15; | |
var precision = initialPrecision; | |
var scanLimit = 30; | |
var scanRange = [0, scanLimit]; | |
function detectEnemy(precision, range) { | |
let enemyFound = false; | |
let enemyDistance = Infinity; | |
var direction = range[0]; | |
while (direction < range[1] && !enemyFound) { | |
direction += precision; | |
enemyDistance = scan(direction, precision); | |
if (enemyDistance < Infinity) { | |
enemyFound = true; | |
break; | |
} | |
} | |
return { | |
enemyFound, | |
enemyDirection: direction, | |
enemyDistance | |
}; | |
} | |
function around(number1, number2, proximity) { | |
return Math.abs(number1 - number2) < proximity; | |
} | |
function tick({ wait }) { | |
if (wait) { | |
return; | |
} | |
let { enemyFound, enemyDirection, enemyDistance } = detectEnemy(precision, scanRange); | |
if (enemyFound) { | |
if (enemyDistance > 70) { | |
swim(enemyDirection, 100); | |
} else if ( around(getY(), 50, 5) ) { | |
swim(90, 50); | |
} else { | |
stop(); | |
cannon(enemyDirection, enemyDistance); | |
precision = Math.max(1, precision / 3); | |
} | |
scanRange = [ enemyDirection - scanLimit, enemyDirection ]; | |
} else { | |
precision = initialPrecision; | |
scanRange = [ scanRange[0] + scanLimit, scanRange[1] + scanLimit ]; | |
} | |
if (scanRange[0] >= 720) { | |
scanRange = [0, scanLimit]; | |
swim(Math.random() * 360); | |
} | |
} | |
return { | |
tick | |
}; | |
} | |
const defense = defensive(); | |
const offense = offensive(); | |
while (true) { | |
defense.tick(); | |
offense.tick({ wait: defense.state.escaping }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment