From 19b4223c336f942a8f696468a30d59e9a3b9b2b2 Mon Sep 17 00:00:00 2001
From: Svornost <11434-svornost@users.noreply.gitgud.io>
Date: Wed, 10 Jun 2020 11:39:46 -0700
Subject: [PATCH] 1. Allow removeJob to run on agents that aren't assigned
 arcologies. 2. Automatically fire agents who aren't assigned arcologies at
 week end. 3. If attempting to assign an agent to an arcology that already has
 one, fire the existing agent first. 4. Never attempt to auto-reassign agents
 with assignJobSafely (we don't know what arcology they were leading, so it's
 never safe).

---
 src/endWeek/slaveAssignmentReport.js | 13 +++++++++++++
 src/js/assignJS.js                   | 22 ++++++++++++++++++----
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/src/endWeek/slaveAssignmentReport.js b/src/endWeek/slaveAssignmentReport.js
index ee8212872be..c9f614f9dc5 100644
--- a/src/endWeek/slaveAssignmentReport.js
+++ b/src/endWeek/slaveAssignmentReport.js
@@ -747,6 +747,19 @@ App.EndWeek.slaveAssignmentReport = function() {
 						slave.subTarget = 0;
 					}
 				}
+				break;
+			case Job.AGENT:
+			{
+				const arc = V.arcologies.find((a) => a.leaderID === slave.ID);
+				if (!arc) {
+					_printSlaveUnassignedNote(slave, "is not assigned to an arcology");
+					removeJob(slave, Job.AGENT);
+				} else if (arc.government !== "your agent") {
+					_printSlaveUnassignedNote(slave, "is assigned to an arcology that is not lead by an agent");
+					removeJob(slave, Job.AGENT);
+				}
+				break;
+			}
 		}
 
 		if (slave.ID === V.LurcherID) {
diff --git a/src/js/assignJS.js b/src/js/assignJS.js
index a79213523ca..877f22a63ea 100644
--- a/src/js/assignJS.js
+++ b/src/js/assignJS.js
@@ -388,8 +388,18 @@ globalThis.assignJob = function(slave, job) {
 			WombCleanGenericReserve(slave, 'incubator', 9999);
 			WombCleanGenericReserve(slave, 'nursery', 9999);
 			if (job === Job.AGENT) {
-				App.activeArcology().leaderID = slave.ID;
-				App.activeArcology().government = "your agent";
+				if (App.activeArcology().direction !== 0) { // never assign an agent to the player's arcology
+					if (App.activeArcology().leaderID !== 0) {
+						const oldAgent = getSlave(App.activeArcology().leaderID);
+						if (oldAgent && oldAgent.assignment === Job.AGENT) {
+							// this is an error...you should never be able to assign an agent over the top of another
+							// but in order to prevent game state corruption, we are going to remove the old agent
+							removeJob(oldAgent, oldAgent.assignment, false);
+						}
+					}
+					App.activeArcology().leaderID = slave.ID;
+					App.activeArcology().government = "your agent";
+				}
 			}
 			break;
 
@@ -436,6 +446,8 @@ globalThis.assignJobSafely = function(slave, assignmentStr) {
 	if (V.assignmentRecords[slave.ID] === Job.CHOICE) {
 		assignJob(slave, Job.REST);
 		slave.choosesOwnAssignment = 1;
+	} else if ([Job.AGENT, Job.AGENTPARTNER].includes(V.assignmentRecords[slave.ID])) { // it is NEVER safe to auto-reassign agents (we don't know which arcology they came from)
+		assignJob(slave, Job.REST);
 	} else if (!App.Utils.jobForAssignment(assignmentStr).canEmploy(slave).length) {  // If nothing complains about job requirements not being met
 		assignJob(slave, assignmentStr);
 	} else {
@@ -629,8 +641,10 @@ globalThis.removeJob = function(slave, assignment, saveRecord = false) {
 			case Job.AGENTPARTNER:
 				if (slave.assignment === Job.AGENT) {
 					const arc = V.arcologies.find((a) => a.leaderID === slave.ID);
-					arc.leaderID = 0;
-					arc.government = "your trustees";
+					if (arc) {
+						arc.leaderID = 0;
+						arc.government = "your trustees";
+					}
 				}
 				slave.assignment = Job.REST;
 				if (slave.relationshipTarget > 0) {
-- 
GitLab