// ==UserScript==
// @name Tykinruoka's Auto-Fighter
// @namespace http://gm.taistelumarsu.org
// @description If you're not hurt, this script hits again
// @match *.kingdomofloathing.com/*
// @match kingdomofloathing.com/*
// @version 1.7.1
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
var tykinruokaAutoFighter = {
tries: 0,
mainDoc: null,
stopWordText: 'Set stop word',
getSelectElem: function(name) {
var selects = this.mainDoc.getElementsByTagName('select');
for (var i = 0; i < selects.length; i++) {
if (selects[i].name == name) {
return selects[i];
}
}
return null;
},
getSubmitButton: function(value) {
var inputs = this.mainDoc.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].type == 'submit' && inputs[i].value == value) {
return inputs[i];
}
}
return null;
},
findFrame: function(framename) {
var tFrame = top.document.getElementsByName(framename);
if (tFrame.length == 0) {
var dframes = top.document.getElementsByTagName('frame');
for (var i = 0; i < dframes.length; i++) {
if (dframes[i].src.indexOf(framename) != -1) {
return dframes[i];
}
}
} else {
return tFrame[0];
}
},
restartMe: function() {
this.tries++;
window.setTimeout(function() {
window.tykinruokaAutoFighter.runStuff();
}, 200);
},
runStuff: function() {
if (location.href.indexOf('topmenu') != -1) {
this.createSelectToTopmenu();
} else {
this.hitMeBaby();
}
},
createSelectToTopmenu: function() {
var tykinruoka_autofighter = document.createElement("span");
with (tykinruoka_autofighter.style) {
background = "white";
position = "fixed";
top = "3px";
left = "5px";
padding = "3px 5px 3px 5px";
MozUserSelect = "none";
fontSize = "xx-small";
}
tykinruoka_autofighter.innerHTML = 'AutoFight™';
var selectElem = document.createElement("select");
selectElem.id = 'tykinruoka_aa_select';
selectElem.style.fontSize = "xx-small";
var noneOption = document.createElement("option");
noneOption.value = 'none';
noneOption.text = 'Disabled';
selectElem.add(noneOption, null);
var weaponOption = document.createElement("option");
weaponOption.value = 'weapon';
weaponOption.text = "Weapon";
selectElem.add(weaponOption, null);
var itemOption = document.createElement("option");
itemOption.value = 'item';
itemOption.text = "Item";
selectElem.add(itemOption, null);
var skillOption = document.createElement("option");
skillOption.value = 'skill';
skillOption.text = "Skill";
selectElem.add(skillOption, null);
var macroOption = document.createElement("option");
macroOption.value = 'macro';
macroOption.text = "Macro";
selectElem.add(macroOption, null);
tykinruoka_autofighter.innerHTML += '
';
selectElem.value = GM_getValue("aaM", "none");
tykinruoka_autofighter.appendChild(selectElem);
var inputElem = document.createElement("input");
inputElem.id = 'tykinruoka_aa_input';
inputElem.value = GM_getValue("aaP", "100");
inputElem.size = 3;
inputElem.maxlength = 3;
inputElem.style.fontSize = "xx-small";
inputElem.title = '% of HP required to keep fighting';
tykinruoka_autofighter.appendChild(inputElem);
tykinruoka_autofighter.appendChild(document.createElement('br'));
var stopWordElem = document.createElement('span');
stopWordElem.id = 'tykinruoka_aa_stopword';
stopWordElem.title = this.stopWordText;
stopWordElem.innerHTML = GM_getValue("aaS", this.stopWordText);
if (stopWordElem.innerHTML.length == 0) {
stopWordElem.innerHTML = this.stopWordText;
}
tykinruoka_autofighter.appendChild(stopWordElem);
document.body.appendChild(tykinruoka_autofighter);
var self = this;
var tykinruoka_aa_select = document.getElementById('tykinruoka_aa_select');
if (tykinruoka_aa_select) {
tykinruoka_aa_select.onchange = function() {
GM_setValue("aaM", this.value);
}
}
var tykinruoka_aa_disable = document.getElementById('tykinruoka_aa_disable');
if (tykinruoka_aa_disable) {
tykinruoka_aa_disable.style.textDecoration = 'none';
tykinruoka_aa_disable.onclick = function() {
var sIndex = document.getElementById('tykinruoka_aa_select').selectedIndex;
if (sIndex == 0 && typeof(this.rel) != 'undefined') {
sIndex = this.rel;
} else {
this.rel = sIndex;
sIndex = 0;
}
document.getElementById('tykinruoka_aa_select').selectedIndex = sIndex;
};
}
var tykinruoka_aa_input = document.getElementById('tykinruoka_aa_input');
if (tykinruoka_aa_input) {
tykinruoka_aa_input.onblur = function(event) {
GM_setValue("aaP", this.value);
};
}
var tykinruoka_aa_stopword = document.getElementById('tykinruoka_aa_stopword');
if (tykinruoka_aa_stopword) {
tykinruoka_aa_stopword.onclick = function() {
this.innerHTML = prompt('Set stop word');
if (this.innerHTML == '') {
this.innerHTML = self.stopWordText;
}
GM_setValue("aaS", this.innerHTML);
};
}
},
hitMeBaby: function() {
if (this.tries > 10) {
return false;
}
var topmenu = this.findFrame('topmenu');
if (!topmenu) {
return false;
}
var topmenuDoc = topmenu.contentDocument;
if (!topmenuDoc.body) {
this.restartMe();
return false;
}
var tykinruoka_aa_select = topmenuDoc.getElementById('tykinruoka_aa_select');
if (!tykinruoka_aa_select || tykinruoka_aa_select.value == 'none') {
return false;
}
var tykinruoka_aa_stopword = topmenuDoc.getElementById('tykinruoka_aa_stopword');
var stopWord = false;
if (tykinruoka_aa_stopword && tykinruoka_aa_stopword.innerHTML != this.stopWordText) {
stopWord = tykinruoka_aa_stopword.innerHTML;
}
var mainFrame = this.findFrame('main');
if (!mainFrame) {
mainFrame = this.findFrame('mainpane');
}
if (!mainFrame) {
return;
}
var mainDoc = mainFrame.contentDocument;
if (!mainDoc || !mainDoc.body) {
this.restartMe();
return;
}
this.mainDoc = mainDoc;
var mainBody = mainDoc.body;
if (
mainBody &&
mainBody.innerHTML.match(/macro aborted/i) ||
mainBody.innerHTML.indexOf('You run away') != -1
) {
return;
}
if (stopWord && mainBody.innerHTML.toLowerCase().indexOf(stopWord.toLowerCase()) != -1) {
return;
}
if (mainBody.innerHTML.match(/(dodecagram|box of birthday candles|eldritch butterknife|(whips out a (hammer|crescent wrench|pair of pliers|screwdriver)))/i)) {
return;
}
if (
mainBody.innerHTML.indexOf('You lose') != -1 ||
mainBody.innerHTML.indexOf('You loos') != -1
) {
var charPane = this.findFrame('charpane');
if (!charPane) {
return;
}
var charDoc = charPane.contentDocument;
if (!charDoc || !charDoc.body) {
return;
}
var hpRe = new RegExp('([0-9]+) / ([0-9]+)', 'gmi');
var hpValues = hpRe.exec(charDoc.body.innerHTML);
if (hpValues.length != 3) {
return;
}
var currentHP = hpValues[1];
var maxHP = hpValues[2];
var tykinruoka_aa_input = topmenuDoc.getElementById('tykinruoka_aa_input');
if (!tykinruoka_aa_input || currentHP/maxHP*100 < tykinruoka_aa_input.value) {
return;
}
}
var tack = mainDoc.getElementById('tack');
if (tack) {
switch (tykinruoka_aa_select.value) {
default:
case 'none':
return;
case 'weapon':
tack.click();
return;
case 'item':
var itemSelect = this.getSelectElem('whichitem');
var itemSelect2 = this.getSelectElem('whichitem2');
if ((itemSelect != null && itemSelect.value != "0") ||
(itemSelect2 != null && itemSelect2.value != "0")) {
var submitButton = this.getSubmitButton('Use Item');
if (submitButton != null) {
submitButton.click();
} else {
submitButton = this.getSubmitButton('Use Item(s)');
if (submitButton != null) {
submitButton.click();
}
}
}
return;
case 'skill':
var skillSelect = this.getSelectElem('whichskill');
if (skillSelect != null && skillSelect.value != "none") {
submitButton = this.getSubmitButton('Use Skill');
if (submitButton != null) {
submitButton.click();
}
}
return;
case 'macro':
var macroSelect = this.getSelectElem('whichmacro');
if (macroSelect != null && macroSelect.value != "0") {
submitButton = this.getSubmitButton('Execute Macro');
if (submitButton != null) {
submitButton.click();
}
}
return;
}
}
var aElems = this.mainDoc.getElementsByTagName('a');
for (var i = 0; i < aElems.length; i++) {
if (aElems[i].innerHTML.indexOf('Adventure Again') != -1 ||
aElems[i].innerHTML.indexOf('You investigate the now-still nightstand') != -1 ||
aElems[i].innerHTML.indexOf('Eat another black pudding') != -1
) {
aElems[i].click();
// mainFrame.contentWindow.location.replace(aElems[i].href);
return;
}
}
this.restartMe();
}
};
window.tykinruokaAutoFighter = tykinruokaAutoFighter;
tykinruokaAutoFighter.runStuff();