| 1 |
--[[ Copyright (c) 2009 Manuel Wolf |
| 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 |
class "UseStaffRoomAction" (HumanoidAction) |
| 22 |
|
| 23 |
---@type UseStaffRoomAction |
| 24 |
local UseStaffRoomAction = _G["UseStaffRoomAction"] |
| 25 |
|
| 26 |
function UseStaffRoomAction:UseStaffRoomAction() |
| 27 |
self:HumanoidAction("use_staffroom") |
| 28 |
end |
| 29 |
|
| 30 |
-- decide on the next source of relaxation the humanoid will go to |
| 31 |
-- returns target_obj, ox, oy, new_type |
| 32 |
-- the function will always return a value for new_type, depending on what type was chosen, |
| 33 |
-- but target_obj, ox and oy will be nil, if no object of the chosen target was found |
| 34 |
local decide_next_target = permanent"use_staffroom_action_decide_next_target"( function(action, humanoid) |
| 35 |
assert(action.name == "use_staffroom", "decide_next_target only works with the use_staffroom action") |
| 36 |
local cur_type = action.target_type |
| 37 |
assert(class.is(humanoid, Staff), "decide_next_target called for non-staff humanoid") |
| 38 |
local h_class = humanoid.humanoid_class |
| 39 |
|
| 40 |
local chance = math.random(1, 10) |
| 41 |
|
| 42 |
-- take sofa as default |
| 43 |
local new_type = "sofa" |
| 44 |
|
| 45 |
-- With a chance of 20%, look for a pool table (Doctor and Handyman only) |
| 46 |
-- Don't choose pool table two times in a row though |
| 47 |
if (h_class == "Doctor" or h_class == "Handyman") and cur_type ~= "pool_table" and chance <= 2 then |
| 48 |
new_type = "pool_table" |
| 49 |
end |
| 50 |
|
| 51 |
-- With a chance of 20%, look for a video game (Doctor and Nurse only) |
| 52 |
-- Don't choose video game two times in a row though |
| 53 |
if (h_class == "Doctor" or h_class == "Nurse") and cur_type ~= "video_game" and 2 < chance and chance <= 4 then |
| 54 |
new_type = "video_game" |
| 55 |
end |
| 56 |
|
| 57 |
-- Take the a near object but not always the nearest (decreasing probability over distance) for some variation. |
| 58 |
local obj, ox, oy = humanoid.world:findFreeObjectNearToUse(humanoid, new_type, "near") |
| 59 |
return obj, ox, oy, new_type |
| 60 |
end) |
| 61 |
|
| 62 |
-- randomly generate the use time for a given source of relaxation |
| 63 |
local generate_use_time = permanent"use_staffroom_action_generate_use_time"( function(type) |
| 64 |
if type == "sofa" then |
| 65 |
return math.random(50, 80) |
| 66 |
elseif type == "pool_table" then |
| 67 |
return math.random(2, 5) |
| 68 |
elseif type == "video_game" then |
| 69 |
return math.random(2, 15) |
| 70 |
end |
| 71 |
end) |
| 72 |
|
| 73 |
-- table of how much relaxation an object gives per tick |
| 74 |
local relaxation = { |
| 75 |
sofa = 0.001, |
| 76 |
pool_table = 0.05, |
| 77 |
video_game = 0.05, |
| 78 |
} |
| 79 |
|
| 80 |
-- main function of the staffroom action |
| 81 |
local function use_staffroom_action_start(action, humanoid) |
| 82 |
assert(class.is(humanoid, Staff), "use_staffroom action called for non-staff humanoid") |
| 83 |
assert(not class.is(humanoid, Receptionist), "use_staffroom action called for receptionist") |
| 84 |
|
| 85 |
-- For initial call of this function we have to decide a new target now |
| 86 |
-- Else, there should be already a new target defined |
| 87 |
if not action.next_target_type then |
| 88 |
action.target_obj, action.ox, action.oy, action.target_type = decide_next_target(action, humanoid) |
| 89 |
if action.target_obj then |
| 90 |
action.target_obj.reserved_for = humanoid |
| 91 |
end |
| 92 |
else |
| 93 |
action.target_obj = action.next_target_obj |
| 94 |
action.ox = action.next_ox |
| 95 |
action.oy = action.next_oy |
| 96 |
action.target_type = action.next_target_type |
| 97 |
|
| 98 |
action.next_target_obj = nil |
| 99 |
action.next_ox = nil |
| 100 |
action.next_oy = nil |
| 101 |
action.next_target_type = nil |
| 102 |
end |
| 103 |
|
| 104 |
-- If no target was found, then walk around for a bit and try again later |
| 105 |
if not action.target_obj then |
| 106 |
humanoid:queueAction(MeanderAction():setCount(2), 0) |
| 107 |
return |
| 108 |
end |
| 109 |
|
| 110 |
-- Otherwise, walk to and use the object: |
| 111 |
-- Note: force prolonged_usage, because video_game wouldn't get it by default (because it has no begin and end animation) |
| 112 |
local obj_use_time = generate_use_time(action.target_type) |
| 113 |
|
| 114 |
local loop_callback_use = --[[persistable:use_staffroom_action_loop_callback]] function(obj_action) |
| 115 |
humanoid:wake(relaxation[action.target_type]) |
| 116 |
-- if staff is no longer fatigued, make them leave the staff room |
| 117 |
if humanoid:getAttribute("fatigue") == 0 then |
| 118 |
humanoid:setNextAction(humanoid:getRoom():createLeaveAction()) |
| 119 |
|
| 120 |
local room = humanoid.last_room |
| 121 |
-- Send back to the last room if that room is still empty. |
| 122 |
-- (applies to training and research only) |
| 123 |
-- Make sure that the room is still there though. |
| 124 |
-- If not, just answer the call |
| 125 |
if room and room.is_active and |
| 126 |
(room.room_info.id == "research" or room.room_info.id == "training") and |
| 127 |
room:testStaffCriteria(room:getMaximumStaffCriteria(), humanoid) then |
| 128 |
humanoid:queueAction(room:createEnterAction(humanoid)) |
| 129 |
humanoid:setDynamicInfoText(_S.dynamic_info.staff.actions.heading_for:format(room.room_info.name)) |
| 130 |
else |
| 131 |
-- Send the staff out of the room |
| 132 |
if room and room.is_active and |
| 133 |
(room.room_info.id == "research" or room.room_info.id == "training") then |
| 134 |
-- Clear the last room if it was training/research as we can't go back here anymore |
| 135 |
humanoid.last_room = nil |
| 136 |
end |
| 137 |
humanoid:queueAction(MeanderAction()) |
| 138 |
end |
| 139 |
else |
| 140 |
obj_use_time = obj_use_time - 1 |
| 141 |
-- if staff is done using object |
| 142 |
if obj_use_time == 0 then |
| 143 |
-- Decide on the next target. If it happens to be of the same type as the current, just continue using the current. |
| 144 |
-- also check x,y co-ords to see if the object actually exists in the room |
| 145 |
action.next_target_obj, action.next_ox, action.next_oy, action.next_target_type = decide_next_target(action, humanoid) |
| 146 |
if (not action.next_ox and not action.next_oy) or action.next_target_type == action.target_type then |
| 147 |
obj_use_time = generate_use_time(action.target_type) |
| 148 |
else |
| 149 |
if action.next_target_obj then |
| 150 |
action.next_target_obj.reserved_for = humanoid |
| 151 |
end |
| 152 |
obj_action.prolonged_usage = false |
| 153 |
end |
| 154 |
end |
| 155 |
end |
| 156 |
end |
| 157 |
|
| 158 |
local obj_action = UseObjectAction(action.target_obj):setProlongedUsage(true) |
| 159 |
:setLoopCallback(loop_callback_use) |
| 160 |
|
| 161 |
humanoid:queueAction(WalkAction(action.ox, action.oy), 0) |
| 162 |
humanoid:queueAction(obj_action, 1) |
| 163 |
end |
| 164 |
|
| 165 |
return use_staffroom_action_start |