:: rules autosurgery js [script]

window.rulesAutosurgery = (function() {
	"use strict";
	let V;
	let r;
	return rulesAutoSurgery;

	function rulesAutoSurgery(slave) {
		V = State.variables;
		r = "";
		const surgeries = [];
		const thisSurgery = ProcessHGTastes(slave);
		if (slave.health > 20)
			CommitSurgery(slave, thisSurgery, surgeries);
		if (surgeries.length > 0)
			PrintResult(slave, thisSurgery, surgeries);
		return r;
	}

	function autoSurgerySelector(slave, ruleset) {
		const surgery = {};
		ruleset.forEach(rule => {
			Object.keys(rule)
				.filter(key => key.startsWith("surgery_") && rule[key] !== "no default setting")
				.forEach(key => {
					surgery[key] = rule[key];
				});
		});
		return surgery;
	}

	function ProcessHGTastes(slave) {
		let thisSurgery;
		switch (V.HGTastes) {
		case 1:
			thisSurgery = {
				surgery_lactation: 0,
				surgery_cosmetic: 1,
				surgery_faceShape: "cute",
				surgery_lips: 10,
				surgery_hips: 0,
				surgery_hipsImplant: 0,
				surgery_butt: 0,
				surgery_accent: 0,
				surgery_shoulders: 0,
				surgery_shouldersImplant: 0,
				surgery_boobs: 0,
				surgery_holes: 0
			};
			break ;
		case 2:
			thisSurgery = {
				surgery_lactation: 0,
				surgery_cosmetic: 1,
				surgery_faceShape: "cute",
				surgery_lips: 60,
				surgery_hips: 0,
				surgery_hipsImplant: 0,
				surgery_butt: 4,
				surgery_accent: 0,
				surgery_shoulders: 0,
				surgery_shouldersImplant: 0,
				surgery_boobs: 1200,
				surgery_holes: 0
			};
			break;
		case 3:
			thisSurgery = {
				surgery_lactation: 0,
				surgery_cosmetic: 1,
				surgery_faceShape: "cute",
				surgery_lips: 95,
				surgery_hips: 0,
				surgery_hipsImplant: 0,
				surgery_butt: 8,
				surgery_accent: 0,
				surgery_shoulders: 0,
				surgery_shouldersImplant: 0,
				surgery_boobs: 10000,
				surgery_holes: 2
			};
			break;
		case 4:
			thisSurgery = {
				surgery_lactation: 1,
				surgery_cosmetic: 1,
				surgery_faceShape: "cute",
				surgery_lips: 10,
				surgery_hips: 3,
				surgery_hipsImplant: 0,
				surgery_butt: 0,
				surgery_accent: 0,
				surgery_shoulders: 0,
				surgery_shouldersImplant: 0,
				surgery_boobs: 0,
				surgery_holes: 0
			};
			break;
		default:
			thisSurgery = autoSurgerySelector(
				slave,
				V.defaultRules
					.filter(x => ruleApplied(slave, x) && x.set.autoSurgery === 1)
					.map(x => x.set));
			if ((thisSurgery.surgery_hips !== "no default setting") && (thisSurgery.surgery_butt !== "no default setting")) {
				if (slave.hips < -1) {
					if (thisSurgery.surgery_butt > 2)
						thisSurgery.surgery_butt = 2;
				} else if (slave.hips < 0) {
					if (thisSurgery.surgery_butt > 4)
						thisSurgery.surgery_butt = 4;
				} else if (slave.hips > 0) {
					if (thisSurgery.surgery_butt > 8)
						thisSurgery.surgery_butt = 8;
				} else if (slave.hips > 1) {
					true;
				} else {
					if (thisSurgery.surgery_butt > 6)
						thisSurgery.surgery_butt = 6;
				}
			}
			break;
		}
		return thisSurgery;
	}

	function CommitSurgery(slave, thisSurgery, surgeries) {
		if ((slave.eyes == -1) && (thisSurgery.surgery_eyes == 1)) {
			surgeries.push("surgery to correct her vision");
			slave.eyes = 1;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.eyes == 1) && (thisSurgery.surgery_eyes == -1)) {
			surgeries.push("surgery to blur her vision");
			slave.eyes = -1;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.hears == -1) && (thisSurgery.surgery_hears == 1)) {
			surgeries.push("surgery to correct her hearing");
			slave.hears = 0;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.hears == 0) && (thisSurgery.surgery_hears == -1)) {
			surgeries.push("surgery to muffle her hearing");
			slave.hears = -1;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.lactation == 2) && (thisSurgery.surgery_lactation == 0)) {
			surgeries.push("surgery to remove her lactation implants");
			slave.lactation = 0;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if (slave.lactation != 2 && (thisSurgery.surgery_lactation == 1)) {
			surgeries.push("lactation inducing implanted drugs");
			slave.lactation = 2;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.prostate == 2) && (thisSurgery.surgery_prostate == 0)) {
			surgeries.push("surgery to remove her prostate implant");
			slave.prostate = 0;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if (slave.prostate == 1 && (thisSurgery.surgery_prostate == 1)) {
			surgeries.push("a precum production enhancing drug implant");
			slave.prostate = 2;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.anus > 3) && (thisSurgery.surgery_cosmetic > 0)) {
			surgeries.push("a restored anus");
			slave.anus = 3;
			if (slave.analSkill > 10)
				slave.analSkill -= 10;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.vagina > 3) && (thisSurgery.surgery_cosmetic > 0)) {
			surgeries.push("a restored pussy");
			slave.vagina = 3;
			if (slave.vaginalSkill > 10)
				slave.vaginalSkill -= 10;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.faceImplant <= 15) && (slave.face <= 95) && (thisSurgery.surgery_cosmetic > 0)) {
			surgeries.push("a nicer face");
			if (slave.faceShape == "masculine") slave.faceShape = "androgynous";
			slave.faceImplant += 25-5*Math.trunc(V.PC.medicine/50)-5*V.surgeryUpgrade;
			slave.face = Math.clamp(slave.face+20,-100,100);
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.faceImplant <= 15) && (slave.ageImplant != 1) && (slave.visualAge >= 25) && (thisSurgery.surgery_cosmetic > 0)) {
			surgeries.push("an age lift");
			slave.ageImplant = 1;
			slave.faceImplant += 25-5*Math.trunc(V.PC.medicine/50)-5*V.surgeryUpgrade;
			if (slave.visualAge > 80) slave.visualAge -= 40;
			else if (slave.visualAge >= 70) slave.visualAge -= 30;
			else if (slave.visualAge > 50) slave.visualAge -= 20;
			else if (slave.visualAge > 36) slave.visualAge -= 10;
			else slave.visualAge -= 5;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if (((slave.underArmHStyle != "bald" && slave.underArmHStyle != "hairless") || (slave.pubicHStyle != "bald" && slave.pubicHStyle != "hairless")) && (thisSurgery.surgery_bodyhair == 2)) {
			surgeries.push("body hair removal");
			if (slave.underArmHStyle != "hairless") slave.underArmHStyle = "bald";
			if (slave.pubicHStyle != "hairless") slave.pubicHStyle = "bald";
			V.cash -= V.surgeryCost;

		} else if ((slave.bald == 0 || slave.hStyle != "bald" || slave.eyebrowHStyle != "bald") && (thisSurgery.surgery_hair == 2)) {
			surgeries.push("hair removal");
			slave.eyebrowHStyle = "bald";
			slave.hStyle = "bald";
			slave.bald = 1;
			V.cash -= V.surgeryCost;

		} else if ((slave.weight >= 10) && (thisSurgery.surgery_cosmetic > 0)) {
			surgeries.push("liposuction");
			slave.weight -= 50;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.voice == 1) && (slave.voiceImplant == 0) && (thisSurgery.surgery_cosmetic > 0)) {
			surgeries.push("a feminine voice");
			slave.voice += 1;
			slave.voiceImplant += 1;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.waist >= -10) && (thisSurgery.surgery_cosmetic > 0)) {
			surgeries.push("a narrower waist");
			slave.waist -= 20;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if (((slave.boobShape == "saggy") || (slave.boobShape == "downward-facing")) && (thisSurgery.surgery_cosmetic > 0) && (slave.breastMesh != 1)) {
			surgeries.push("a breast lift");
			slave.boobShape = "normal";
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;
			
		} else if (((slave.boobShape == "normal") || (slave.boobShape == "wide-set")) && (thisSurgery.surgery_cosmetic > 0) && (slave.breastMesh != 1)) {
			if (slave.boobs > 800)
				slave.boobShape = "torpedo-shaped";
			else
				slave.boobShape = "perky";
			surgeries.push("more interestingly shaped breasts");
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((thisSurgery.surgery_lips == 0) && (slave.lipsImplant > 0)) {
			surgeries.push("surgery to remove her lip implants");
			slave.lips -= slave.lipsImplant;
			slave.lipsImplant = 0;
			if (slave.oralSkill > 10)
				slave.oralSkill -= 10;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.lips <= 95) && (slave.lips < thisSurgery.surgery_lips)) {
			if (thisSurgery.surgery_lips !== "no default setting") {
				surgeries.push("bigger lips");
				slave.lipsImplant += 10;
				slave.lips += 10;
				if (slave.oralSkill > 10)
					slave.oralSkill -= 10;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}

		} else if ((slave.faceImplant <= 45) && (slave.face <= 95) && (thisSurgery.surgery_cosmetic == 2)) {
			surgeries.push("a nicer face");
			if (slave.faceShape == "masculine") slave.faceShape = "androgynous";
			slave.faceImplant += 25-5*Math.trunc(V.PC.medicine/50)-5*V.surgeryUpgrade;
			slave.face = Math.clamp(slave.face+20,-100,100);
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.hips < 1) && (slave.hips < thisSurgery.surgery_hips) && (V.surgeryUpgrade == 1)) {
			surgeries.push("wider hips");
			slave.hips++;
			slave.hipsImplant++;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.faceImplant <= 45) && (slave.ageImplant != 1) && (slave.visualAge >= 25) && (thisSurgery.surgery_cosmetic == 2)) {
			surgeries.push("an age lift");
			slave.ageImplant = 1;
			if (slave.visualAge > 80) {
				slave.visualAge -= 40;
			} else if (slave.visualAge >= 70) {
				slave.visualAge -= 30;
			} else if (slave.visualAge > 50) {
				slave.visualAge -= 20;
			} else if (slave.visualAge > 36) {
				slave.visualAge -= 10;
			} else {
				slave.visualAge -= 5;
			}
			slave.faceImplant += 25-5*Math.trunc(V.PC.medicine/50)-5*V.surgeryUpgrade;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.waist >= -95) && (thisSurgery.surgery_cosmetic == 2) && (V.seeExtreme == 1)) {
			surgeries.push("a narrower waist");
			slave.waist = Math.clamp(slave.waist-20,-100,100);
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.voice < 3) && (slave.voiceImplant == 0) && (thisSurgery.surgery_cosmetic == 2)) {
			surgeries.push("a bimbo's voice");
			slave.voice += 1;
			slave.voiceImplant += 1;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((thisSurgery.surgery_butt == 0) && (slave.buttImplant > 0)) {
			surgeries.push("surgery to remove her butt implants");
			slave.butt -= slave.buttImplant;
			slave.buttImplant = 0;
			slave.buttImplantType = 0;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((thisSurgery.surgery_boobs == 0) && (slave.boobsImplant > 0)) {
			surgeries.push("surgery to remove her boob implants");
			slave.boobs -= slave.boobsImplant;
			slave.boobsImplant = 0;
			slave.boobsImplantType = 0;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;
		} else if ((slave.butt <= 3) && (slave.butt < thisSurgery.surgery_butt)) {
			if (thisSurgery.surgery_butt !== "no default setting") {
				surgeries.push("a bigger butt");
				slave.buttImplant = 1;
				slave.butt += 1;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}
		} else if ((slave.boobs <= 600) && (slave.lactation < 2) && (slave.boobs+400 <= thisSurgery.surgery_boobs)) {
			if (thisSurgery.surgery_boobs !== "no default setting") {
				surgeries.push("bigger boobs");
				slave.boobsImplant += 400;
				slave.boobs += 400;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}
		} else if ((slave.boobs <= 600) && (slave.lactation < 2) && (slave.boobs+200 <= thisSurgery.surgery_boobs)) {
			if (thisSurgery.surgery_boobs !== "no default setting") {
				surgeries.push("modestly bigger boobs");
				slave.boobsImplant += 200;
				slave.boobs += 200;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}

		} else if ((slave.butt <= 5) && (slave.butt < thisSurgery.surgery_butt)) {
			if (thisSurgery.surgery_butt !== "no default setting") {
				surgeries.push("a bigger butt");
				slave.buttImplant = 1;
				slave.butt += 1;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}

		} else if ((slave.boobs <= 2000) && (slave.lactation < 2) && (slave.boobs+400 < thisSurgery.surgery_boobs)) {
			if (thisSurgery.surgery_boobs !== "no default setting") {
				surgeries.push("bigger boobs");
				slave.boobsImplant += 400;
				slave.boobs += 400;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}

		} else if ((slave.anus > 0) && (V.surgeryUpgrade == 1) && (thisSurgery.surgery_holes == 2)) {
			surgeries.push("a virgin anus");
			slave.anus = 0;
			if (slave.analSkill > 10) {
				slave.analSkill -= 10;
			}
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.vagina > 0) && (V.surgeryUpgrade == 1) && (thisSurgery.surgery_holes == 2)) {
			surgeries.push("a virgin pussy");
			slave.vagina = 0;
			if (slave.vaginalSkill > 10)
				slave.vaginalSkill -= 10;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.hips < 2) && (slave.hips < thisSurgery.surgery_hips) && (V.surgeryUpgrade == 1)) {
			surgeries.push("wider hips");
			slave.hips++;
			slave.hipsImplant++;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.anus > 1) && (thisSurgery.surgery_holes == 1)) {
			surgeries.push("a tighter anus");
			slave.anus = 1;
			if (slave.analSkill > 10) {
				slave.analSkill -= 10;
			}
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.vagina > 1) && (thisSurgery.surgery_holes == 1)) {
			surgeries.push("a tighter pussy");
			slave.vagina = 1;
			if (slave.vaginalSkill > 10) {
				slave.vaginalSkill -= 10;
			}
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if ((slave.butt <= 8) && (slave.butt < thisSurgery.surgery_butt)) {
			if (thisSurgery.surgery_butt !== "no default setting") {
				surgeries.push("a bigger butt");
				slave.buttImplant = 1;
				slave.butt += 1;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}

		} else if ((slave.boobs <= 9000) && (slave.lactation < 2) && (slave.boobs < thisSurgery.surgery_boobs)) {
			if (thisSurgery.surgery_boobs !== "no default setting") {
				surgeries.push("bigger boobs");
				slave.boobsImplant += 200;
				slave.boobs += 200;
				V.cash -= V.surgeryCost;
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			}

		} else if ((slave.hips < 3) && (slave.hips < thisSurgery.surgery_hips) && (V.surgeryUpgrade == 1)) {
			surgeries.push("wider hips");
			slave.hips++;
			slave.hipsImplant++;
			V.cash -= V.surgeryCost;
			if (V.PC.medicine >= 100) slave.health -= 5;
			else slave.health -= 10;

		} else if (slave.bellyImplant < 0 && V.bellyImplants > 0 && thisSurgery.surgery_bellyImplant == "install" && slave.womb.length == 0 && slave.broodmother == 0) {
			slave.bellyImplant = 100;
			slave.preg = -2;
			V.cash -= V.surgeryCost;
			if (slave.ovaries == 1 || slave.mpreg == 1) {
				surgeries.push("belly implant");
				V.surgeryType = "bellyIn";
				if (V.PC.medicine >= 100) slave.health -= 5;
				else slave.health -= 10;
			} else {
				surgeries.push("male belly implant");
				V.surgeryType = "bellyInMale";
				if (V.PC.medicine >= 100) slave.health -= 25;
				else slave.health -= 50;
			}
			bellyIn(slave);

		} else if (slave.bellyImplant >= 0 && thisSurgery.surgery_bellyImplant == "remove") {
			surgeries.push("belly implant removal");
			V.surgeryType = "bellyOut";
			if (V.PC.medicine >= 100)
				slave.health -= 5;
			else
				slave.health -= 10;
			slave.preg = 0;
			slave.bellyImplant = -1;
			V.cash -= V.surgeryCost;
		} else if (slave.balls > 0 && slave.vasectomy === 0 && thisSurgery.surgery_vasectomy === true) {
			surgeries.push("vasectomy");
			V.surgeryType = "vasectomy";
			if (V.PC.medicine >= 100)
				slave.health -= 5;
			else
				slave.health -= 10;
			slave.vasectomy = 1;
			V.cash -= V.surgeryCost;
		} else if (slave.balls > 0 && slave.vasectomy === 1 && thisSurgery.surgery_vasectomy === false) {
			surgeries.push("undo vasectomy");
			V.surgeryType = "vasectomy undo";
			if (V.PC.medicine >= 100)
				slave.health -=5;
			else
				slave.health -= 10;
			slave.vasectomy = 0;
			V.cash -= V.surgeryCost;
		}
	}

	function PrintResult(slave, thisSurgery, surgeries) {
		let surgeriesDisplay = "";
		if (surgeries.length === 1)
			surgeriesDisplay = surgeries[0];
		else {
			surgeriesDisplay = surgeries.slice(0, surgeries.length - 1).join(", ");
			surgeriesDisplay += ", and" + surgeries[surgeries.length - 1];
		}
		r += `${V.assistantName === "your personal assistant" ? "Your personal assistant" : V.assistantName}, ordered to apply surgery, gives ${slave.slaveName} <span class="lime">${surgeriesDisplay}.</span>`;
	}

	function bellyIn(slave) {
		// less hacky version of calling surgery degradation silently
		if (slave.devotion > 50)
			slave.devotion += 4;
		else if (slave.devotion >= -20)
			slave.trust -= 5;
		else {
			slave.trust -= 5;
			slave.devotion -= 5;
		}
	}
})();
