Created
June 5, 2017 19:41
-
-
Save bonzaiferroni/a18b0b390822e1edceb8884298153191 to your computer and use it in GitHub Desktop.
The previous method of testing whether a creep has moved vs. new
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
testCPU() { | |
const STATE_PREV_X = 0; | |
const STATE_PREV_Y = 1; | |
const STATE_STUCK = 2; | |
const STATE_CPU = 3; | |
const STATE_DEST_X = 4; | |
const STATE_DEST_Y = 5; | |
const STATE_DEST_ROOMNAME = 6; | |
let newTraveler = Game.creeps["newTraveler"]; | |
let oldTraveler = Game.creeps["oldTraveler"]; | |
let newTravelData = newTraveler.memory._trav; | |
let oldTravelData = oldTraveler.memory._travel; | |
let destination = Game.flags["travFlag"].pos; | |
let state = Traveler.deserializeState(newTravelData.state, destination); | |
let options = {} as any; | |
let newTrav = () => { | |
// check if creep is stuck | |
if (Traveler.isStuck(newTraveler, state)) { | |
// state.stuckCount++; | |
Traveler.circle(newTraveler.pos, "magenta", state.stuckCount * .2); | |
} else { | |
// state.stuckCount = 0; | |
} | |
// handle case where creep is stuck | |
if (!options.stuckValue) { options.stuckValue = 2; } | |
if (state.stuckCount >= options.stuckValue && Math.random() > .5) { | |
options.ignoreCreeps = false; | |
options.freshMatrix = true; | |
delete newTravelData.path; | |
} | |
// TODO:handle case where creep moved by some other function, but destination is still the same | |
// delete path cache if destination is different | |
if (!Traveler.samePos(state.destination, destination)) { | |
if (options.movingTarget && state.destination.isNearTo(destination)) { | |
newTravelData.path += state.destination.getDirectionTo(destination); | |
state.destination = destination; | |
} else { | |
delete newTravelData.path; | |
} | |
} | |
if (options.repath && Math.random() < options.repath) { | |
// add some chance that you will find a new path randomly | |
delete newTravelData.path; | |
} | |
}; | |
let oldTrav = () => { | |
let hasMoved = true; | |
if (oldTravelData.prev) { | |
oldTravelData.prev = OldTraveler.initPosition(oldTravelData.prev); | |
if (oldTraveler.pos.inRangeTo(oldTravelData.prev, 0)) { | |
hasMoved = false; | |
// travelData.stuck++; | |
} else { | |
// travelData.stuck = 0; | |
} | |
} | |
// handle case where creep is stuck | |
if (oldTravelData.stuck >= 2 && !options.ignoreStuck) { | |
options.ignoreCreeps = false; | |
delete oldTravelData.path; | |
} | |
// handle case where creep wasn't traveling last tick and may have moved, but destination is still the same | |
if (Game.time - oldTravelData.tick > 1 && hasMoved) { | |
delete oldTravelData.path; | |
} | |
oldTravelData.tick = Game.time; | |
// delete path cache if destination is different | |
if (!oldTravelData.dest || oldTravelData.dest.x !== destination.x || oldTravelData.dest.y !== destination.y || | |
oldTravelData.dest.roomName !== destination.roomName) { | |
delete oldTravelData.path; | |
} | |
}; | |
let tests = [newTrav, oldTrav]; | |
for (let test of tests) { | |
let cpu = Game.cpu.getUsed(); | |
for (let i = 0; i < 100; i++) { | |
test(); | |
} | |
console.log(`test: ${test.name}, cpu: ${Game.cpu.getUsed() - cpu}`); | |
} | |
/* output: | |
[1:38:18 PM]test: newTrav, cpu: 0.11773299999998699 | |
[1:38:18 PM]test: oldTrav, cpu: 0.23347499999999854 | |
[1:38:26 PM]test: newTrav, cpu: 0.16482600000000502 | |
[1:38:26 PM]test: oldTrav, cpu: 0.27060299999999415 | |
[1:38:30 PM]test: newTrav, cpu: 0.019552000000004455 | |
[1:38:30 PM] test: oldTrav, cpu: 0.32025100000001316 | |
*/ | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment