diff --git a/CorsixTH/Lua/room.lua b/CorsixTH/Lua/room.lua index e338916..0aadacf 100644 --- a/CorsixTH/Lua/room.lua +++ b/CorsixTH/Lua/room.lua @@ -678,13 +678,13 @@ function Room:roomFinished() self:tryAdvanceQueue() end ---! Try to move a patient from another room to us. ---!param room (Room) Other room to 'steal' from. +--! Try to move a patient from the old room to the new room. +--!param old_room (Room) Room that currently has the patient in the queue. +--!param new_room (Room) Room that wants the patient in the queue. --!param patient (Humanoid) Patient to move. ---!return (boolean) Whether we are done with this room (no more patients will come from it). -function Room:tryMovePatient(room, patient) - local world = self.world - local other_queue = room.door.queue +--!return (boolean) Whether we are done with the old room (no more patients will come from it). +local function tryMovePatient(old_room, new_room, patient) + local world = new_room.world local px, py = patient.tile_x, patient.tile_y -- Don't reroute the patient if he just decided to go to the toilet @@ -692,26 +692,27 @@ function Room:tryMovePatient(room, patient) return false end - local our_x, our_y = self:getEntranceXY(true) - local other_x, other_y = room:getEntranceXY(true) - local distance_to_us = world:getPathDistance(px, py, our_x, our_y) - if not distance_to_us then return true end -- Patient cannot reach us, quit trying + local new_x, new_y = new_room:getEntranceXY(true) + local old_x, old_y = old_room:getEntranceXY(true) + local new_distance = world:getPathDistance(px, py, new_x, new_y) + if not new_distance then return true end -- Patient cannot reach us, quit trying - local our_score = self:getUsageScore() + distance_to_us - local other_score - local distance_to_current_room = world:getPathDistance(px, py, other_x, other_y) - if distance_to_current_room then - other_score = room:getUsageScore() + distance_to_current_room + local new_score = new_room:getUsageScore() + new_distance + local old_score + local old_distance = world:getPathDistance(px, py, old_x, old_y) + if old_distance then + old_score = old_room:getUsageScore() + old_distance else - other_score = our_score + 1 -- Make condition below fail. + old_score = new_score + 1 -- Make condition below fail. end - if our_score >= other_score then return true end + if new_score >= old_score then return true end -- Update the queues - other_queue:removeValue(patient) - patient.next_room_to_visit = self - self.door.queue:expect(patient) - self.door:updateDynamicInfo() + local old_queue = old_room.door.queue + old_queue:removeValue(patient) + patient.next_room_to_visit = new_room + new_room.door.queue:expect(patient) + new_room.door:updateDynamicInfo() -- Rewrite the action queue for i, action in ipairs(patient.action_queue) do @@ -724,8 +725,8 @@ function Room:tryMovePatient(room, patient) -- again, is_in_queue is set to false so the callback won't run if action.name == 'queue' then action.is_in_queue = false - elseif action.name == "walk" and action.x == other_x and action.y == other_y then - local action = self:createEnterAction(patient) + elseif action.name == "walk" and action.x == old_x and action.y == old_y then + local action = new_room:createEnterAction(patient) patient:queueAction(action, i) break end @@ -746,15 +747,15 @@ function Room:tryToFindNearbyPatients() return end - for _, room in pairs(self.world.rooms) do - if room.hospital == self.hospital and room ~= self and - room.room_info == self.room_info and room.door.queue and - room.door.queue:reportedSize() >= 2 then - local other_queue = room.door.queue - local pat_number = other_queue:reportedSize() + for _, old_room in pairs(self.world.rooms) do + if old_room.hospital == self.hospital and old_room ~= self and + old_room.room_info == self.room_info and old_room.door.queue and + old_room.door.queue:reportedSize() >= 2 then + local old_queue = old_room.door.queue + local pat_number = old_queue:reportedSize() while pat_number > 1 do - local patient = other_queue:reportedHumanoid(pat_number) - if self:tryMovePatient(room, patient) then break end + local patient = old_queue:reportedHumanoid(pat_number) + if tryMovePatient(old_room, self, patient) then break end -- tryMovePatient may have just removed patient 'pat_number', but it does -- not change the queue in front of it. 'pat_number - 1' thus still exists. pat_number = pat_number - 1