| 1 |
--[[ Copyright (c) 2011 Edvin "Lego3" Linge |
| 2 |
|
| 3 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 4 |
this software and associated documentation files (the "Software"), to deal in |
| 5 |
the Software without restriction, including without limitation the rights to |
| 6 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 7 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 8 |
so, subject to the following conditions: |
| 9 |
|
| 10 |
The above copyright notice and this permission notice shall be included in all |
| 11 |
copies or substantial portions of the Software. |
| 12 |
|
| 13 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 |
SOFTWARE. --]] |
| 20 |
|
| 21 |
--[[ Notes about research speed. |
| 22 |
Original game times at 20 % in all categories with a medium doctor at level 3: |
| 23 |
Slicer discovered in about 120 days. (10000 points) |
| 24 |
Pump improved in about 60 days. (30000 points * 20 % as first improvement) |
| 25 |
Invisibility improved in about 90 days. (10000 points) |
| 26 |
Note that the original requires a nurse and a pharmacy before drugs |
| 27 |
get improved. On the other hand it appears that the research is |
| 28 |
stored for future use anyway. |
| 29 |
--]] |
| 30 |
|
| 31 |
--! Manages all things related to research for one hospital. |
| 32 |
class "ResearchDepartment" |
| 33 |
|
| 34 |
---@type ResearchDepartment |
| 35 |
local ResearchDepartment = _G["ResearchDepartment"] |
| 36 |
|
| 37 |
function ResearchDepartment:ResearchDepartment(hospital) |
| 38 |
self.hospital = hospital |
| 39 |
self.world = hospital.world |
| 40 |
|
| 41 |
-- This list contains a lot of information. |
| 42 |
-- Progress of object discovery, object improvement, drug improvement |
| 43 |
-- dito costs and room build costs. |
| 44 |
self.research_progress = {} |
| 45 |
self:initResearch() |
| 46 |
end |
| 47 |
|
| 48 |
-- Initialize research for the level. |
| 49 |
function ResearchDepartment:initResearch() |
| 50 |
local hospital = self.hospital |
| 51 |
local cfg_objects = self.world.map.level_config.objects |
| 52 |
local cure, diagnosis, improve, drug |
| 53 |
-- Initialize object research |
| 54 |
for _, object in ipairs(TheApp.objects) do |
| 55 |
if cfg_objects[object.thob] and cfg_objects[object.thob].AvailableForLevel == 1 and |
| 56 |
object.research_category then |
| 57 |
self.research_progress[object] = { |
| 58 |
points = 0, |
| 59 |
start_strength = cfg_objects[object.thob].StartStrength, |
| 60 |
cost = not self.world.free_build_mode and cfg_objects[object.thob].StartCost or 0, |
| 61 |
discovered = cfg_objects[object.thob].StartAvail == 1, |
| 62 |
strength_imp = 0, |
| 63 |
cost_imp = 0, |
| 64 |
} |
| 65 |
if cfg_objects[object.thob].StartAvail == 0 then |
| 66 |
if object.research_category == "cure" then |
| 67 |
cure = object |
| 68 |
elseif object.research_category == "diagnosis" then |
| 69 |
diagnosis = object |
| 70 |
end |
| 71 |
end |
| 72 |
if object.default_strength and cfg_objects[object.thob].StartAvail == 1 then |
| 73 |
improve = object |
| 74 |
end |
| 75 |
end |
| 76 |
end |
| 77 |
-- Now add drug research |
| 78 |
for _, disease in pairs(hospital.disease_casebook) do |
| 79 |
if disease.drug then |
| 80 |
self.research_progress[disease] = { |
| 81 |
points = 0, |
| 82 |
effect_imp = 1, |
| 83 |
cost_imp = 1, |
| 84 |
} |
| 85 |
if disease.discovered then |
| 86 |
drug = disease |
| 87 |
end |
| 88 |
end |
| 89 |
end |
| 90 |
-- Add a dummy to specialisation. The difference is that while it still says |
| 91 |
-- 20 % in that area it doesn't cost anything to the player |
| 92 |
local drain = {dummy = true} |
| 93 |
self.research_progress[drain] = {points = 0} |
| 94 |
self.drain = drain |
| 95 |
|
| 96 |
local policy = { |
| 97 |
cure = {frac = cure and 20 or 0, current = cure}, |
| 98 |
diagnosis = {frac = diagnosis and 20 or 0, current = diagnosis}, |
| 99 |
drugs = {frac = 20, points = 0, current = drug and drug or drain}, |
| 100 |
improvements = {frac = improve and 20 or 0, points = 0, current = improve}, |
| 101 |
specialisation = {frac = 20, points = 0, current = drain}, |
| 102 |
} |
| 103 |
local sum = 0 |
| 104 |
for _, cat in pairs(policy) do |
| 105 |
sum = sum + cat.frac |
| 106 |
end |
| 107 |
if sum == 20 then |
| 108 |
-- No research to be done |
| 109 |
policy.specialisation.frac = 0 |
| 110 |
policy.total = 0 |
| 111 |
else |
| 112 |
-- If some category is already done, put the free points in another one. |
| 113 |
if sum < 100 then |
| 114 |
for _, tab in pairs(policy) do |
| 115 |
if tab.frac > 0 then |
| 116 |
tab.frac = tab.frac + 100 - sum |
| 117 |
sum = 100 |
| 118 |
break |
| 119 |
end |
| 120 |
end |
| 121 |
end |
| 122 |
policy.total = sum |
| 123 |
end |
| 124 |
self.research_policy = policy |
| 125 |
|
| 126 |
if not hospital.research_dep_built then |
| 127 |
hospital.research_dep_built = false |
| 128 |
end |
| 129 |
end |
| 130 |
|
| 131 |
function ResearchDepartment:checkAutomaticDiscovery(month) |
| 132 |
for object, progress in pairs(self.research_progress) do |
| 133 |
-- Only check objects |
| 134 |
if object.default_strength then |
| 135 |
local avail_at = self.world.map.level_config.objects[object.thob].WhenAvail |
| 136 |
if not progress.discovered and avail_at ~= 0 and month >= avail_at then |
| 137 |
self:discoverObject(object, true) |
| 138 |
end |
| 139 |
end |
| 140 |
end |
| 141 |
end |
| 142 |
|
| 143 |
--! Find a disease (if it exists) on which research can be concentrated and concentrate on it. |
| 144 |
function ResearchDepartment:setResearchConcentration() |
| 145 |
local casebook = self.hospital.disease_casebook |
| 146 |
if self.research_policy.specialisation.current ~= self.drain then |
| 147 |
return |
| 148 |
end |
| 149 |
for _, disease in pairs(casebook) do |
| 150 |
if disease.discovered and self.hospital:canConcentrateResearch(disease.disease.id) then |
| 151 |
self:concentrateResearch(disease.disease.id) |
| 152 |
return |
| 153 |
end |
| 154 |
end |
| 155 |
end |
| 156 |
|
| 157 |
--! Function that redistributes research points from a finished category to |
| 158 |
--! all the other categories. |
| 159 |
function ResearchDepartment:redistributeResearchPoints() |
| 160 |
local sum, all_finished = 0, true |
| 161 |
local policy = {"cure", "diagnosis", "drugs", "improvements"} |
| 162 |
local specialisation_point = self.research_policy["specialisation"].frac |
| 163 |
for _, research_category in ipairs(policy) do |
| 164 |
sum = sum + self.research_policy[research_category].frac |
| 165 |
if self.research_policy[research_category].current and self.research_policy[research_category].current ~= self.drain then |
| 166 |
all_finished = false |
| 167 |
end |
| 168 |
end |
| 169 |
if not all_finished then |
| 170 |
self.research_policy.total = self.research_policy.total - specialisation_point |
| 171 |
if sum == 0 then |
| 172 |
local num_cat = 0 |
| 173 |
for _, categ in ipairs(policy) do |
| 174 |
if self.research_policy[categ].current then |
| 175 |
num_cat = num_cat + 1 |
| 176 |
end |
| 177 |
end |
| 178 |
for _, categ in ipairs(policy) do |
| 179 |
if self.research_policy[categ].current then |
| 180 |
self.research_policy[categ].frac = math.floor(self.research_policy.total / num_cat) |
| 181 |
end |
| 182 |
end |
| 183 |
sum = math.floor(self.research_policy.total / num_cat) * num_cat |
| 184 |
for _, categ in ipairs(policy) do |
| 185 |
if self.research_policy[categ].current then |
| 186 |
if sum == self.research_policy.total then |
| 187 |
break |
| 188 |
end |
| 189 |
self.research_policy[categ].frac = self.research_policy[categ].frac + 1 |
| 190 |
sum = sum + 1 |
| 191 |
end |
| 192 |
end |
| 193 |
else |
| 194 |
local new_sum, max_value, max_category = 0, 0, "" |
| 195 |
new_sum = new_sum + specialisation_point |
| 196 |
for _, categ in ipairs(policy) do |
| 197 |
local research_cat = self.research_policy[categ] |
| 198 |
if research_cat.frac > max_value then |
| 199 |
max_value = research_cat.frac |
| 200 |
max_category = categ |
| 201 |
end |
| 202 |
research_cat.frac = math.floor(self.research_policy.total * research_cat.frac / sum) |
| 203 |
new_sum = new_sum + research_cat.frac |
| 204 |
end |
| 205 |
--if there are still some points left redistribute them to the research category with the maximum points |
| 206 |
if new_sum < self.research_policy.total then |
| 207 |
local frac = self.research_policy[max_category].frac + self.research_policy.total - new_sum |
| 208 |
self.research_policy[max_category].frac = frac |
| 209 |
end |
| 210 |
end |
| 211 |
self.research_policy.total = self.research_policy.total + specialisation_point |
| 212 |
else |
| 213 |
self.research_policy.total = 0 |
| 214 |
self.research_policy.specialisation.current = nil |
| 215 |
end |
| 216 |
end |
| 217 |
|
| 218 |
--[[! Finds out what to research next in the given research area. |
| 219 |
!param category The research area. One of cure, diagnosis, drugs, |
| 220 |
improvements and specialisation |
| 221 |
--]] |
| 222 |
function ResearchDepartment:nextResearch(category) |
| 223 |
local current = self.research_policy[category].current |
| 224 |
-- First make sure that the current research target actually has been discovered. |
| 225 |
-- Otherwise don't do anything. |
| 226 |
if not (current.drug or self.research_progress[current].discovered) then |
| 227 |
return |
| 228 |
end |
| 229 |
local hospital = self.hospital |
| 230 |
self.research_policy[category].current = nil |
| 231 |
local found_one = false |
| 232 |
if category == "drugs" then |
| 233 |
local worst_effect = 100 |
| 234 |
for _, disease in pairs(hospital.disease_casebook) do |
| 235 |
if disease.cure_effectiveness < worst_effect then |
| 236 |
found_one = true |
| 237 |
if disease.discovered then |
| 238 |
self.research_policy[category].current = disease |
| 239 |
worst_effect = disease.cure_effectiveness |
| 240 |
end |
| 241 |
end |
| 242 |
end |
| 243 |
elseif category == "improvements" then |
| 244 |
-- Find the object which needs improvements the most. |
| 245 |
local max_strength = self.world.map.level_config.gbv.MaxObjectStrength |
| 246 |
local min_strength = max_strength |
| 247 |
for object, progress in pairs(self.research_progress) do |
| 248 |
if object.default_strength then |
| 249 |
-- Don't improve those that already have the max strength |
| 250 |
if progress.start_strength < max_strength then |
| 251 |
if progress.discovered and progress.start_strength < min_strength then |
| 252 |
self.research_policy[category].current = object |
| 253 |
min_strength = progress.start_strength |
| 254 |
else |
| 255 |
found_one = true |
| 256 |
end |
| 257 |
end |
| 258 |
end |
| 259 |
end |
| 260 |
else |
| 261 |
for object, progress in pairs(self.research_progress) do |
| 262 |
if object.research_category == category and not progress.discovered then |
| 263 |
self.research_policy[category].current = object |
| 264 |
end |
| 265 |
end |
| 266 |
end |
| 267 |
if found_one and not self.research_policy[category].current then |
| 268 |
-- There is a drug or machine which needs improving, but it |
| 269 |
-- has not been discovered yet. |
| 270 |
self.research_policy[category].current = self.drain |
| 271 |
self.research_progress[self.drain] = {points = 0} |
| 272 |
end |
| 273 |
if not self.research_policy[category].current then |
| 274 |
local cat = self.research_policy[category] |
| 275 |
-- Nothing more to research |
| 276 |
cat.current = nil |
| 277 |
cat.frac = 0 |
| 278 |
self:redistributeResearchPoints() |
| 279 |
|
| 280 |
self.hospital:giveAdvice({_A.research.drug_fully_researched |
| 281 |
:format(_S.research.categories[category])}) |
| 282 |
|
| 283 |
-- Notify any research window |
| 284 |
local window = self.world.ui:getWindow(UIResearch) |
| 285 |
if window then |
| 286 |
window:updateCategories() |
| 287 |
end |
| 288 |
end |
| 289 |
end |
| 290 |
|
| 291 |
--[[ Returns how many points are required to discover something |
| 292 |
about the given thing. |
| 293 |
It may be an object or a drug, being improved or researched. |
| 294 |
If this thing cannot be processed nil is returned. |
| 295 |
!param thing The thing to check, either a drug or an object. |
| 296 |
--]] |
| 297 |
function ResearchDepartment:getResearchRequired(thing) |
| 298 |
local required |
| 299 |
local level_config = self.world.map.level_config |
| 300 |
local expert = level_config.expertise |
| 301 |
|
| 302 |
if thing.thob then |
| 303 |
-- An object |
| 304 |
required = level_config.objects[thing.thob].RschReqd |
| 305 |
if not required then |
| 306 |
-- It didn't know, so use the fallback instead. |
| 307 |
if not thing.research_fallback then |
| 308 |
-- This object is not researchable! |
| 309 |
print(("Warning: %s has been defined as "):format(thing.id) .. |
| 310 |
"researchable, but no requirements to fulfil could be found.") |
| 311 |
else |
| 312 |
required = expert[thing.research_fallback].RschReqd |
| 313 |
end |
| 314 |
end |
| 315 |
-- Actually want to know how much to improve? |
| 316 |
local research_info = self.research_progress[thing] |
| 317 |
if research_info.discovered then |
| 318 |
local improve_percent = level_config.gbv.RschImproveCostPercent |
| 319 |
local increment = level_config.gbv.RschImproveIncrementPercent |
| 320 |
improve_percent = improve_percent + increment * research_info.cost_imp |
| 321 |
required = required * improve_percent / 100 |
| 322 |
end |
| 323 |
elseif thing.drug then |
| 324 |
-- A drug |
| 325 |
required = expert[thing.disease.expertise_id].RschReqd |
| 326 |
end |
| 327 |
return required |
| 328 |
end |
| 329 |
|
| 330 |
--! This function will give research points to the treatment of the illness of an autopsied patient. |
| 331 |
--!param target_room_id (string) The name of the room |
| 332 |
function ResearchDepartment:addResearchPointsForAutopsy(target_room_id) |
| 333 |
local level_config = self.world.map.level_config |
| 334 |
local room_discovery = self.hospital.room_discoveries[target_room_id] |
| 335 |
-- Do something only if the room is among those not yet discovered. |
| 336 |
if room_discovery.is_discovered then |
| 337 |
return |
| 338 |
end |
| 339 |
-- Find an object within this room that needs research points. |
| 340 |
for object, _ in pairs(room_discovery.room.objects_needed) do |
| 341 |
local research = self.research_progress[TheApp.objects[object]] |
| 342 |
if research and not research.discovered then |
| 343 |
local required = self:getResearchRequired(TheApp.objects[object]) |
| 344 |
-- Research points are added as according to the level config. |
| 345 |
local advance = required * level_config.gbv.AutopsyRschPercent / 100 |
| 346 |
research.points = research.points + advance |
| 347 |
|
| 348 |
-- Maybe we now have enough to discover the object? |
| 349 |
if research.points > required then |
| 350 |
self:discoverObject(TheApp.objects[object]) |
| 351 |
end |
| 352 |
break |
| 353 |
end |
| 354 |
end |
| 355 |
end |
| 356 |
|
| 357 |
--! Add some more research points to research progress. |
| 358 |
--! It will be divided according to the research policy into the different research areas. |
| 359 |
--!param points (integer) The total amount of points before applying any level specific divisors to add to research. |
| 360 |
function ResearchDepartment:addResearchPoints(points) |
| 361 |
local level_config = self.world.map.level_config |
| 362 |
-- Divide the points into the different areas. If total is not at 100 % |
| 363 |
-- the total amount is lowered, but then cost is also reduced. |
| 364 |
|
| 365 |
-- Fetch the level research divisor. |
| 366 |
local divisor = level_config.gbv.ResearchPointsDivisor or 5 |
| 367 |
|
| 368 |
points = math.ceil(points * self.research_policy.total / (100 * divisor)) |
| 369 |
|
| 370 |
-- Divide the points into the different categories and check if |
| 371 |
-- it is time to discover something |
| 372 |
local areas = self.research_policy |
| 373 |
for _, info in pairs(areas) do |
| 374 |
-- Don't touch the value "total". |
| 375 |
if type(info) == "table" then |
| 376 |
-- Some categories may be finished |
| 377 |
if info.current then |
| 378 |
-- Add new points to this category's current focus. |
| 379 |
local research_info = self.research_progress[info.current] |
| 380 |
local stored = research_info.points |
| 381 |
-- Add just a little randomness |
| 382 |
research_info.points = stored + math.t_random(0.75, 1, 1.25) * points * info.frac / 100 |
| 383 |
local required = self:getResearchRequired(info.current) |
| 384 |
if required and required < research_info.points then |
| 385 |
research_info.points = 0 |
| 386 |
-- On the specialisation pass any of these categories are eligible. |
| 387 |
---------------- Discovering objects ---------------------- |
| 388 |
if info.current.thob and not research_info.discovered then |
| 389 |
self:discoverObject(info.current) |
| 390 |
----------------- Improving drugs ------------------------- |
| 391 |
elseif info.current.drug then |
| 392 |
self:improveDrug(info.current) |
| 393 |
--------------- Improving machines ------------------------ |
| 394 |
elseif info.current.thob then |
| 395 |
self:improveMachine(info.current) |
| 396 |
end |
| 397 |
end |
| 398 |
end |
| 399 |
end |
| 400 |
end |
| 401 |
end |
| 402 |
|
| 403 |
--[[ Called when it is time to improve a drug's strength or cost. |
| 404 |
!param drug The drug to improve, table taken from world.available_diseases |
| 405 |
--]] |
| 406 |
function ResearchDepartment:improveDrug(drug) |
| 407 |
local disease = self.hospital.disease_casebook[drug.disease.id] |
| 408 |
local level_config = self.world.map.level_config |
| 409 |
local min_drug_cost = level_config.gbv.MinDrugCost |
| 410 |
|
| 411 |
-- Work out the improvement to make |
| 412 |
local function decideImprovement() |
| 413 |
local at_max_effectiveness = disease.cure_effectiveness >= 100 |
| 414 |
local at_min_cost = disease.drug_cost <= min_drug_cost |
| 415 |
local improvement |
| 416 |
if at_max_effectiveness and at_min_cost then -- Nothing left to improve |
| 417 |
return false |
| 418 |
end |
| 419 |
if at_max_effectiveness then -- Only cost improvement remains |
| 420 |
improvement = "cost" |
| 421 |
elseif at_min_cost then -- Only effectiveness improvement remains |
| 422 |
improvement = "effectiveness" |
| 423 |
else -- Either can be improved |
| 424 |
improvement = math.random(1, 2) == 1 and "cost" or "effectiveness" |
| 425 |
-- A 1/7 chance both are improved |
| 426 |
improvement = math.random(1, 7) == 1 and "both" or improvement |
| 427 |
end |
| 428 |
return improvement |
| 429 |
end |
| 430 |
|
| 431 |
-- Decrease the cost of the drug |
| 432 |
-- Set to 10%, provides good balance vs number of steps on the effectiveness ladder |
| 433 |
local function decreaseDrugCost() |
| 434 |
local new_cost = math.max(min_drug_cost, math.floor(disease.drug_cost * 0.9)) |
| 435 |
disease.drug_cost = new_cost |
| 436 |
end |
| 437 |
|
| 438 |
-- Improve a drug's effectiveness |
| 439 |
local function improveEffectiveness() |
| 440 |
local improve_rate = level_config.gbv.DrugImproveRate |
| 441 |
disease.cure_effectiveness = math.min(100, disease.cure_effectiveness + improve_rate) |
| 442 |
end |
| 443 |
|
| 444 |
-- Work out how to proceed with the next drug research |
| 445 |
local function decideNextResearch() |
| 446 |
-- Check if the player's research specialisation is still valid. |
| 447 |
-- Note, once a drug is 100% effective, it can no longer be specialised. |
| 448 |
-- TODO: This should only really happen if both cost+effectiveness can't be improved? |
| 449 |
if disease.cure_effectiveness >= 100 then |
| 450 |
-- Did the researchers concentrate on this drug? |
| 451 |
if self.research_policy.specialisation.current == drug then |
| 452 |
self.research_policy.specialisation.current = self.drain |
| 453 |
self:setResearchConcentration() |
| 454 |
end |
| 455 |
end |
| 456 |
-- Get a new drug to research |
| 457 |
if self.research_policy.drugs.current == drug then |
| 458 |
self:nextResearch("drugs") |
| 459 |
end |
| 460 |
end |
| 461 |
|
| 462 |
local imp = decideImprovement() |
| 463 |
if not imp then |
| 464 |
-- Nothing to improve, reset anyway |
| 465 |
decideNextResearch() |
| 466 |
return |
| 467 |
end |
| 468 |
if imp == "cost" or imp == "both" then decreaseDrugCost() end |
| 469 |
if imp == "effectiveness" or imp == "both" then improveEffectiveness() end |
| 470 |
decideNextResearch() |
| 471 |
|
| 472 |
if drug.disease.id == "the_squits" then |
| 473 |
self.hospital:giveAdvice({_A.research.drug_improved_1:format(drug.disease.name)}) |
| 474 |
else |
| 475 |
self.hospital:giveAdvice({_A.research.drug_improved:format(drug.disease.name)}) |
| 476 |
end |
| 477 |
end |
| 478 |
|
| 479 |
--[[ Called when it is time to improve a machine's strength or cost. |
| 480 |
!param machine The machine to improve, table taken from TheApp.objects |
| 481 |
--]] |
| 482 |
function ResearchDepartment:improveMachine(machine) |
| 483 |
local research_info = self.research_progress[machine] |
| 484 |
-- Improving strength and cost should alternate |
| 485 |
if research_info.strength_imp > research_info.cost_imp then |
| 486 |
-- Time to improve cost |
| 487 |
-- TODO: This is now 12.5%, based on observations by |
| 488 |
-- Mark L. Maybe add a new config option for this? |
| 489 |
local decrease = math.round(research_info.cost*0.125/10)*10 |
| 490 |
research_info.cost = research_info.cost - decrease |
| 491 |
-- Now find rooms where this object is used and lower the build_cost for them. |
| 492 |
for _, room in ipairs(self.world.available_rooms) do |
| 493 |
for obj, no in pairs(room.objects_needed) do |
| 494 |
if TheApp.objects[obj] == machine then |
| 495 |
local progress = self.research_progress[room] |
| 496 |
progress.build_cost = progress.build_cost - decrease * no |
| 497 |
-- Each room only defines the same object once, so break |
| 498 |
-- from the inner loop. |
| 499 |
break |
| 500 |
end |
| 501 |
end |
| 502 |
end |
| 503 |
research_info.cost_imp = research_info.cost_imp + 1 |
| 504 |
local max = self.world.map.level_config.gbv.MaxObjectStrength |
| 505 |
if research_info.start_strength >= max then |
| 506 |
if self.research_policy.specialisation.current == machine then |
| 507 |
self.research_policy.specialisation.current = self.drain |
| 508 |
self:setResearchConcentration() |
| 509 |
end |
| 510 |
end |
| 511 |
-- No matter what, check if another machine needs improvements more urgently. |
| 512 |
if self.research_policy.improvements.current == machine then |
| 513 |
self:nextResearch("improvements") |
| 514 |
end |
| 515 |
else |
| 516 |
-- Time to improve strength |
| 517 |
local improve_rate = self.world.map.level_config.gbv.ResearchIncrement |
| 518 |
research_info.start_strength = research_info.start_strength |
| 519 |
+ improve_rate |
| 520 |
research_info.strength_imp = research_info.strength_imp + 1 |
| 521 |
if self.world.ui.app.config.new_machine_extra_info then |
| 522 |
self:updateMachinesDynamicInfo(machine.id) |
| 523 |
end |
| 524 |
end |
| 525 |
-- Tell the player that something has been improved |
| 526 |
self.hospital:giveAdvice({_A.research.machine_improved:format(machine.name)}) |
| 527 |
end |
| 528 |
|
| 529 |
--[[ Updates the dynamic info for each matching machine found in the player's |
| 530 |
hospital rooms. |
| 531 |
!param machine_id The machine ID for determining matches. |
| 532 |
--]] |
| 533 |
function ResearchDepartment:updateMachinesDynamicInfo(machine_id) |
| 534 |
for _, room in pairs(self.world.rooms) do |
| 535 |
if room.hospital == self.hospital then |
| 536 |
for obj, _ in pairs(room.objects) do |
| 537 |
if obj.object_type.id == machine_id then |
| 538 |
obj:updateDynamicInfo() |
| 539 |
break |
| 540 |
end |
| 541 |
end |
| 542 |
end |
| 543 |
end |
| 544 |
end |
| 545 |
|
| 546 |
--[[ Called when it is time to discover an object. This may currently only |
| 547 |
happen from research. |
| 548 |
!param object The object to discover, a table from TheApp.objects |
| 549 |
!param automatic If true the discovery was not made by |
| 550 |
the player's research department. |
| 551 |
--]] |
| 552 |
function ResearchDepartment:discoverObject(object, automatic) |
| 553 |
self.research_progress[object].discovered = true |
| 554 |
|
| 555 |
-- Go through all rooms to see if another one can be made available. |
| 556 |
for _, room_disc in pairs(self.hospital.room_discoveries) do |
| 557 |
if not room_disc.is_discovered then |
| 558 |
local room = room_disc.room |
| 559 |
local unveil_room = true |
| 560 |
for needed, _ in pairs(room.objects_needed) do |
| 561 |
local obj = self.research_progress[TheApp.objects[needed]] |
| 562 |
if obj and not obj.discovered then |
| 563 |
unveil_room = false |
| 564 |
break |
| 565 |
end |
| 566 |
end |
| 567 |
if unveil_room then |
| 568 |
room_disc.is_discovered = true |
| 569 |
if automatic then |
| 570 |
self.hospital:giveAdvice({_A.research.new_available:format(object.name)}) |
| 571 |
else |
| 572 |
self.hospital:giveAdvice({_A.research.new_machine_researched:format(object.name)}) |
| 573 |
end |
| 574 |
-- It may now be possible to continue researching machine improvements |
| 575 |
local current_improvement_research = self.research_policy.improvements.current |
| 576 |
local max_strength = self.world.map.level_config.gbv.MaxObjectStrength |
| 577 |
local min_strength = max_strength |
| 578 |
-- If we're not researching any improvement right now, and the newest discovery was |
| 579 |
-- a machine that requires an improvement, switch the current policy. |
| 580 |
if (not current_improvement_research or current_improvement_research.dummy) then |
| 581 |
for research_object, progress in pairs(self.research_progress) do |
| 582 |
if research_object.default_strength then |
| 583 |
-- Don't improve those that already have the max strength |
| 584 |
if progress.start_strength < max_strength then |
| 585 |
if progress.discovered and progress.start_strength < min_strength then |
| 586 |
self.research_policy["improvements"].current = research_object |
| 587 |
min_strength = progress.start_strength |
| 588 |
end |
| 589 |
end |
| 590 |
end |
| 591 |
end |
| 592 |
end |
| 593 |
end |
| 594 |
end |
| 595 |
end |
| 596 |
-- Now find out what to do next. |
| 597 |
self:nextResearch(object.research_category) |
| 598 |
end |
| 599 |
|
| 600 |
--[[ Called when it is time to discover a disease (i.e. after diagnosis in the GP) |
| 601 |
!param disease The disease to discover, a table from world.available_diseases |
| 602 |
--]] |
| 603 |
function ResearchDepartment:discoverDisease(disease) |
| 604 |
self.hospital.disease_casebook[disease.id].discovered = true |
| 605 |
local index = #self.hospital.discovered_diseases + 1 |
| 606 |
self.hospital.discovered_diseases[index] = disease.id |
| 607 |
self.hospital:adviseDiscoverDisease(disease) |
| 608 |
|
| 609 |
-- It may now be possible to continue researching drug improvements |
| 610 |
local casebook_disease = self.hospital.disease_casebook[disease.id] |
| 611 |
local current_drug_research = self.research_policy.drugs.current |
| 612 |
-- If we're not researching any drug right now, and the newest discovery was |
| 613 |
-- a disease that requires a drug, switch the current policy. |
| 614 |
if (not current_drug_research or current_drug_research.dummy) and |
| 615 |
casebook_disease.drug then |
| 616 |
self.research_policy.drugs.current = casebook_disease |
| 617 |
end |
| 618 |
self:setResearchConcentration() |
| 619 |
end |
| 620 |
|
| 621 |
--[[! It also costs to research. |
| 622 |
TODO: This is now just $3 per day and doctor (if at 100%), |
| 623 |
what should it be? |
| 624 |
--]] |
| 625 |
function ResearchDepartment:researchCost() |
| 626 |
local acc_cost = self.hospital.acc_research_cost |
| 627 |
local fraction = 0 |
| 628 |
for _, tab in pairs(self.research_policy) do |
| 629 |
-- Don't pay for categories where nothing is really researched at the moment. |
| 630 |
if type(tab) == "table" then |
| 631 |
if tab.current and not tab.current.dummy then |
| 632 |
fraction = fraction + tab.frac |
| 633 |
end |
| 634 |
end |
| 635 |
end |
| 636 |
-- Find out how many doctors are currently doing research |
| 637 |
local doctors = 0 |
| 638 |
for _, room in pairs(self.world.rooms) do |
| 639 |
if room.room_info.id == "research" then |
| 640 |
for _, _ in pairs(room.staff_member_set) do |
| 641 |
doctors = doctors + 1 |
| 642 |
end |
| 643 |
end |
| 644 |
end |
| 645 |
acc_cost = acc_cost + math.ceil(3 * doctors * fraction/100) |
| 646 |
self.hospital.acc_research_cost = acc_cost |
| 647 |
end |
| 648 |
|
| 649 |
--[[ Concentrates research on a given disease. |
| 650 |
Concentrating on a machine will improve it if it's been discovered |
| 651 |
and help researching it otherwise. |
| 652 |
Concentrating on a drug will improve the drug provided the pharmacy |
| 653 |
is discovered. Otherwise it will research the cabinet. |
| 654 |
TODO: Make it possible to concentrate on psychological diseases |
| 655 |
if the psychiatry hasn't been discovered. |
| 656 |
!param disease_id The id of the disease to focus on. |
| 657 |
--]] |
| 658 |
function ResearchDepartment:concentrateResearch(disease_id) |
| 659 |
local book_entry = self.hospital.disease_casebook[disease_id] |
| 660 |
-- First set flags so that the casebook shows the right thing. |
| 661 |
if book_entry.concentrate_research then |
| 662 |
-- Already concentrated, we actually just want to cancel that. |
| 663 |
book_entry.concentrate_research = nil |
| 664 |
-- Make specialisation a dummy again |
| 665 |
self.research_policy.specialisation.current = self.drain |
| 666 |
else |
| 667 |
for key, disease in pairs(self.hospital.disease_casebook) do |
| 668 |
-- Set flag on previously concentrated disease |
| 669 |
if disease.concentrate_research then |
| 670 |
self.hospital.disease_casebook[key].concentrate_research = nil |
| 671 |
end |
| 672 |
end |
| 673 |
-- Concentrate on the new one |
| 674 |
book_entry.concentrate_research = true |
| 675 |
|
| 676 |
-- Now, find the object related to the disease. |
| 677 |
-- TODO: This assumes it is the last room in the treatment_rooms list |
| 678 |
-- which is the one to concentrate on. |
| 679 |
local room |
| 680 |
if book_entry.disease.treatment_rooms then |
| 681 |
local index = #book_entry.disease.treatment_rooms |
| 682 |
room = book_entry.disease.treatment_rooms[index] |
| 683 |
else |
| 684 |
-- This is a pseudo-disease, it should represent a piece of diagnosis |
| 685 |
-- machinery that we can improve via research. |
| 686 |
assert(book_entry.disease.id:sub(1, 5) == "diag_", "Trying to " .. |
| 687 |
"concentrate research on disease without treatment rooms that " .. |
| 688 |
"isn't a diagnosis machine pseudodisease") |
| 689 |
room = book_entry.disease.id:sub(6) |
| 690 |
end |
| 691 |
local object |
| 692 |
-- TODO: Can these loops be improved upon? |
| 693 |
for obj, _ in pairs(self.world.available_rooms[room].objects_needed) do |
| 694 |
for research, _ in pairs(self.research_progress) do |
| 695 |
if research.id == obj and research.thob then |
| 696 |
object = research |
| 697 |
break |
| 698 |
end |
| 699 |
end |
| 700 |
end |
| 701 |
assert(object, "An object that was about to be improved or discovered".. |
| 702 |
"could not be found") |
| 703 |
if book_entry.drug and self.research_progress[object].discovered then |
| 704 |
-- A drug should be improved |
| 705 |
self.research_policy.specialisation.current = book_entry |
| 706 |
else |
| 707 |
-- No matter if it's a drug or some machine - |
| 708 |
-- we want to discover (including the cabinet) or improve it. |
| 709 |
self.research_policy.specialisation.current = object |
| 710 |
end |
| 711 |
end |
| 712 |
end |
| 713 |
|
| 714 |
function ResearchDepartment:afterLoad(old, new) |
| 715 |
if old < 106 then |
| 716 |
self.level_config = nil |
| 717 |
end |
| 718 |
if old < 238 then |
| 719 |
self.research_policy.total = self.research_policy['global'] |
| 720 |
self.research_policy['global'] = nil |
| 721 |
end |
| 722 |
end |