|
| 1 | +--||||||||||||||||||||| EXTENDED SCENE AGENT FUNCTIONS ||||||||||||||||||||| |
| 2 | +--||||||||||||||||||||| EXTENDED SCENE AGENT FUNCTIONS ||||||||||||||||||||| |
| 3 | +--||||||||||||||||||||| EXTENDED SCENE AGENT FUNCTIONS ||||||||||||||||||||| |
| 4 | +--these are extensions of the existing telltale scene agent functions |
| 5 | +--they are here mostly to make it easier to work with agents, but also reduce code clutter and the chance of errors |
| 6 | + |
| 7 | +--============ PROPERTIES - BOOL ============ |
| 8 | +--============ PROPERTIES - BOOL ============ |
| 9 | +--============ PROPERTIES - BOOL ============ |
| 10 | + |
| 11 | +--checks if an agent has a property by name |
| 12 | +Custom_AgentHasProperty = function(agentName, propertyString, sceneObject) |
| 13 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 14 | + return AgentHasProperty(agent, propertyString) |
| 15 | +end |
| 16 | + |
| 17 | +--============ PROPERTIES - SET ============ |
| 18 | +--============ PROPERTIES - SET ============ |
| 19 | +--============ PROPERTIES - SET ============ |
| 20 | + |
| 21 | +--sets a property by an agent object |
| 22 | +Custom_PropertySet = function(agent, propertyString, propertyValue) |
| 23 | + local agent_props = AgentGetRuntimeProperties(agent) |
| 24 | + PropertySet(agent_props, propertyString, propertyValue) |
| 25 | +end |
| 26 | + |
| 27 | +--sets a property on an agent by name |
| 28 | +Custom_AgentSetProperty = function(agentName, propertyString, propertyValue, sceneObject) |
| 29 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 30 | + Custom_PropertySet(agent, propertyString, propertyValue) |
| 31 | +end |
| 32 | + |
| 33 | +--forcibly sets a property on an agent by name |
| 34 | +Custom_AgentForceSetProperty = function(agentName, propertyString, propertyValueType, propertyValue, sceneObject) |
| 35 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 36 | + local agent_props = AgentGetProperties(agent) |
| 37 | + --local agent_props = AgentGetRuntimeProperties(agent) |
| 38 | + |
| 39 | + if not (AgentHasProperty(agent, propertyString)) then |
| 40 | + PropertyCreate(agent_props, propertyString, propertyValueType, propertyValue) |
| 41 | + end |
| 42 | + |
| 43 | + PropertySet(agent_props, propertyString, propertyValue) |
| 44 | +end |
| 45 | + |
| 46 | +--sets a property on all agents with the given prefix in a scene |
| 47 | +Custom_SetPropertyOnAgentsWithPrefix = function(sceneObject, prefixString, propertyString, propertyValue) |
| 48 | + --get all agents in the scene |
| 49 | + local scene_agents = SceneGetAgents(sceneObject) |
| 50 | + |
| 51 | + --initalize an empty list that will contain all the agents we found by name |
| 52 | + local agents_names = {} |
| 53 | + |
| 54 | + --fill out rig agents list |
| 55 | + for i, agent_object in pairs(scene_agents) do |
| 56 | + --get the agent name |
| 57 | + local agent_name = tostring(AgentGetName(agent_object)) |
| 58 | + |
| 59 | + --check if the agent name contains the prefix, if it does then add it to our agent_names table |
| 60 | + if (string.match)(agent_name, prefixString) then |
| 61 | + table.insert(agents_names, agent_name) |
| 62 | + end |
| 63 | + end |
| 64 | + |
| 65 | + --find each agent in the scene and set the desired property |
| 66 | + for x, list_agent_name in pairs(agents_names) do |
| 67 | + Custom_AgentSetProperty(list_agent_name, propertyString, propertyValue, sceneObject) |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +--sets a property on all cameras in a scene |
| 72 | +Custom_SetPropertyOnAllCameras = function(sceneObject, propertyString, propertyValue) |
| 73 | + --get all agents in the scene |
| 74 | + local scene_agents = SceneGetAgents(sceneObject) |
| 75 | + |
| 76 | + --initalize an empty list that will contain all the agents we found by name |
| 77 | + local agents_names = {} |
| 78 | + |
| 79 | + --fill out rig agents list |
| 80 | + for i, agent_object in pairs(scene_agents) do |
| 81 | + --get the agent name |
| 82 | + local agent_name = tostring(AgentGetName(agent_object)) |
| 83 | + |
| 84 | + --check if the agent name contains the prefix, if it does then add it to our agent_names table |
| 85 | + if (string.match)(agent_name, "cam_") then |
| 86 | + table.insert(agents_names, agent_name) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + --find each agent in the scene and set the desired property |
| 91 | + for x, list_agent_name in pairs(agents_names) do |
| 92 | + Custom_AgentSetProperty(list_agent_name, propertyString, propertyValue, sceneObject) |
| 93 | + end |
| 94 | +end |
| 95 | + |
| 96 | +--============ PROPERTIES - GET ============ |
| 97 | +--============ PROPERTIES - GET ============ |
| 98 | +--============ PROPERTIES - GET ============ |
| 99 | + |
| 100 | +--gets a property by an agent object |
| 101 | +Custom_PropertyGet = function(agent, propertyString) |
| 102 | + local agent_props = AgentGetRuntimeProperties(agent) |
| 103 | + return PropertyGet(agent_props, propertyString) |
| 104 | +end |
| 105 | + |
| 106 | +--gets a property on an agent by name |
| 107 | +Custom_AgentGetProperty = function(agentName, propertyString, sceneObject) |
| 108 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 109 | + return Custom_PropertyGet(agent, propertyString) |
| 110 | +end |
| 111 | + |
| 112 | +--gets properties on an agent by name |
| 113 | +Custom_AgentGetProperties = function(agentName, sceneObject) |
| 114 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 115 | + return AgentGetProperties(agent) |
| 116 | +end |
| 117 | + |
| 118 | +--gets runtime properties on an agent by name |
| 119 | +Custom_AgentGetRuntimeProperties = function(agentName, sceneObject) |
| 120 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 121 | + return AgentGetRuntimeProperties(agent) |
| 122 | +end |
| 123 | + |
| 124 | +--============ TRANSFORMATION - SET ============ |
| 125 | +--============ TRANSFORMATION - SET ============ |
| 126 | +--============ TRANSFORMATION - SET ============ |
| 127 | +--for moving and rotating agents |
| 128 | + |
| 129 | +--rotates an agent by name |
| 130 | +Custom_SetAgentRotation = function(agentName, rotationValue, sceneObject) |
| 131 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 132 | + AgentSetRot(agent, rotationValue) |
| 133 | +end |
| 134 | + |
| 135 | +--positions an agent by name |
| 136 | +Custom_SetAgentPosition = function(agentName, positionValue, sceneObject) |
| 137 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 138 | + AgentSetPos(agent, positionValue) |
| 139 | +end |
| 140 | + |
| 141 | +--rotates an agent in world space by name |
| 142 | +Custom_SetAgentWorldRotation = function(agentName, rotationValue, sceneObject) |
| 143 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 144 | + AgentSetWorldRot(agent, rotationValue) |
| 145 | +end |
| 146 | + |
| 147 | +--positions an agent in world space by name |
| 148 | +Custom_SetAgentWorldPosition = function(agentName, positionValue, sceneObject) |
| 149 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 150 | + AgentSetWorldPos(agent, positionValue) |
| 151 | +end |
| 152 | + |
| 153 | +--============ TRANSFORMATION - GET ============ |
| 154 | +--============ TRANSFORMATION - GET ============ |
| 155 | +--============ TRANSFORMATION - GET ============ |
| 156 | +--for getting rotation/position of agents |
| 157 | + |
| 158 | +--gets an agents rotation by name |
| 159 | +Custom_GetAgentRotation = function(agentName, sceneObject) |
| 160 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 161 | + return AgentGetRot(agent) |
| 162 | +end |
| 163 | + |
| 164 | +--gets an agents position by name |
| 165 | +Custom_GetAgentPosition = function(agentName, sceneObject) |
| 166 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 167 | + return AgentGetPos(agent) |
| 168 | +end |
| 169 | + |
| 170 | +--gets an agents world rotation by name |
| 171 | +Custom_GetAgentWorldRotation = function(agentName, sceneObject) |
| 172 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 173 | + return AgentGetWorldRot(agent) |
| 174 | +end |
| 175 | + |
| 176 | +--gets an agents world position by name |
| 177 | +Custom_GetAgentWorldPosition = function(agentName, sceneObject) |
| 178 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 179 | + return AgentGetWorldPos(agent) |
| 180 | +end |
| 181 | + |
| 182 | +--============ UTILLITY ============ |
| 183 | +--============ UTILLITY ============ |
| 184 | +--============ UTILLITY ============ |
| 185 | + |
| 186 | +--hides an agent by name |
| 187 | +Custom_HideAgent = function(agentName, sceneObject) |
| 188 | + local agent = AgentFindInScene(agentName, sceneObject) |
| 189 | + AgentHide(agent) |
| 190 | +end |
| 191 | + |
| 192 | +--sets an agents visibility by name |
| 193 | +Custom_SetAgentVisibillity = function(agentName, visibilityValue, sceneObject) |
| 194 | + Custom_AgentSetProperty(agentName, "Runtime: Visible", visibilityValue, sceneObject) |
| 195 | +end |
| 196 | + |
| 197 | +--sets an agents culling mode |
| 198 | +Custom_SetAgentCulling = function(agentName, cullValue, sceneObject) |
| 199 | + Custom_AgentSetProperty(agentName, "Render Cull", cullValue, sceneObject) |
| 200 | +end |
| 201 | + |
| 202 | +--sets an agent render scale |
| 203 | +Custom_SetAgentScale = function(agentName, scaleValue, sceneObject) |
| 204 | + Custom_AgentSetProperty(agentName, "Render Global Scale", scaleValue, sceneObject) |
| 205 | +end |
| 206 | + |
| 207 | +--sets an agent shadow casting |
| 208 | +Custom_SetAgentShadowCasting = function(agentName, castsShadows, sceneObject) |
| 209 | + Custom_AgentSetProperty(agentName, "Render EnvLight Shadow Cast Enable", castsShadows, sceneObject) |
| 210 | + Custom_AgentSetProperty(agentName, "Render Shadow Force Visible", castsShadows, sceneObject) |
| 211 | +end |
| 212 | + |
| 213 | +--checks if an agent exists, then removes an agent by name |
| 214 | +Custom_RemoveAgent = function(agentName, sceneObj) |
| 215 | + if AgentExists(AgentGetName(agentName)) then |
| 216 | + local agent = AgentFindInScene(agentName, sceneObj) |
| 217 | + AgentDestroy(agent) |
| 218 | + end |
| 219 | +end |
| 220 | + |
| 221 | +--removes agents in a scene with a prefix |
| 222 | +Custom_RemovingAgentsWithPrefix = function(sceneObject, prefixString) |
| 223 | + --get all agents in the scene |
| 224 | + local scene_agents = SceneGetAgents(sceneObject) |
| 225 | + |
| 226 | + --initalize an empty list that will contain all the agents we found by name |
| 227 | + local agents_names = {} |
| 228 | + |
| 229 | + --fill out rig agents list |
| 230 | + for i, agent_object in pairs(scene_agents) do |
| 231 | + --get the agent name |
| 232 | + local agent_name = tostring(AgentGetName(agent_object)) |
| 233 | + |
| 234 | + --check if the agent name contains the prefix, if it does then add it to our agent_names table |
| 235 | + if (string.match)(agent_name, prefixString) then |
| 236 | + table.insert(agents_names, agent_name) |
| 237 | + end |
| 238 | + end |
| 239 | + |
| 240 | + --start removing agents in the list |
| 241 | + for x, list_agent_name in pairs(agents_names) do |
| 242 | + Custom_RemoveAgent(list_agent_name, sceneObject) |
| 243 | + end |
| 244 | +end |
| 245 | + |
| 246 | +--removes agents in a scene with a prefix |
| 247 | +Custom_ReplaceAgentsWithPrefixWithDummy = function(sceneObject, prefixString) |
| 248 | + --get all agents in the scene |
| 249 | + local scene_agents = SceneGetAgents(sceneObject) |
| 250 | + |
| 251 | + --initalize an empty list that will contain all the agents we found by name |
| 252 | + local agents_names = {} |
| 253 | + |
| 254 | + --fill out rig agents list |
| 255 | + for i, agent_object in pairs(scene_agents) do |
| 256 | + --get the agent name |
| 257 | + local agent_name = tostring(AgentGetName(agent_object)) |
| 258 | + |
| 259 | + --check if the agent name contains the prefix, if it does then add it to our agent_names table |
| 260 | + if (string.match)(agent_name, prefixString) then |
| 261 | + table.insert(agents_names, agent_name) |
| 262 | + end |
| 263 | + end |
| 264 | + |
| 265 | + --start removing agents in the list |
| 266 | + for x, list_agent_name in pairs(agents_names) do |
| 267 | + Custom_RemoveAgent(list_agent_name, sceneObject) |
| 268 | + |
| 269 | + local dummyAgent = AgentCreate(list_agent_name, "group.prop", Vector(0,0,0), Vector(0,0,0), sceneObject, false, false) |
| 270 | + end |
| 271 | +end |
| 272 | + |
| 273 | +--using a comparison agent, returns the nearest agent of the two given |
| 274 | +Custom_GetNearestAgent = function(comparisonAgent, agentOne, agentTwo) |
| 275 | + local distance_agentOne = AgentDistanceToAgent(comparisonAgent, agentOne); --number type |
| 276 | + local distance_agentTwo = AgentDistanceToAgent(comparisonAgent, agentTwo); --number type |
| 277 | + |
| 278 | + if (distance_agentOne < distance_agentTwo) then |
| 279 | + return agentOne; |
| 280 | + else |
| 281 | + return agentTwo; |
| 282 | + end |
| 283 | +end |
| 284 | + |
| 285 | +--using a comparison agent, returns the farthest agent of the two given |
| 286 | +Custom_GetFarthestAgent = function(comparisonAgent, agentOne, agentTwo) |
| 287 | + local distance_agentOne = AgentDistanceToAgent(comparisonAgent, agentOne); --number type |
| 288 | + local distance_agentTwo = AgentDistanceToAgent(comparisonAgent, agentTwo); --number type |
| 289 | + |
| 290 | + if (distance_agentOne > distance_agentTwo) then |
| 291 | + return agentOne; |
| 292 | + else |
| 293 | + return agentTwo; |
| 294 | + end |
| 295 | +end |
| 296 | + |
| 297 | +--performs a raycast from a given agent, to another agent |
| 298 | +--returns true when raycast intersects with scene geometry |
| 299 | +--returns false when raycast doesn't intersect with geometry |
| 300 | +Custom_RaycastFromAgentToAgent = function(fromAgent, toAgent) |
| 301 | + --calculate ray origin |
| 302 | + local rayOrigin = AgentGetWorldPos(fromAgent); |
| 303 | + |
| 304 | + if AgentHasNode(fromAgent, "eye_L") and AgentHasNode(fromAgent, "eye_R") then |
| 305 | + rayOrigin = AgentGetWorldPosBetweenNodes(fromAgent, "eye_R", "eye_L"); |
| 306 | + else |
| 307 | + if AgentHasNode(fromAgent, "Head") then |
| 308 | + rayOrigin = AgentGetWorldPos(fromAgent, "Head"); |
| 309 | + end |
| 310 | + end |
| 311 | + |
| 312 | + --calculate ray direction |
| 313 | + local rayDirection = AgentGetWorldPos(toAgent) - rayOrigin; |
| 314 | + |
| 315 | + if AgentHasNode(toAgent, "Root") then |
| 316 | + rayDirection = AgentGetWorldPos(toAgent, "Root") - rayOrigin; |
| 317 | + else |
| 318 | + if AgentHasNode(toAgent, "Head") then |
| 319 | + rayDirection = AgentGetWorldPos(toAgent, "Head") - rayOrigin; |
| 320 | + end |
| 321 | + end |
| 322 | + |
| 323 | + --perform a raycast |
| 324 | + if MathRaySceneIntersect(rayOrigin, rayDirection, AgentGetScene(fromAgent)) then |
| 325 | + return true; |
| 326 | + else |
| 327 | + return false; |
| 328 | + end |
| 329 | +end |
| 330 | + |
| 331 | +--plays a .chore specifically on an agent |
| 332 | +Custom_ChorePlayOnAgent = function(chore, agentName, priority, bWait) |
| 333 | + --if a priority value is not given (nil) |
| 334 | + if not priority then |
| 335 | + priority = 100; |
| 336 | + end |
| 337 | + |
| 338 | + if bWait then --if bWait value is given |
| 339 | + ChorePlayAndWait(chore, priority, "default", agentName); |
| 340 | + else --if there is no bWait value given (nil) |
| 341 | + return ChorePlay(chore, priority, "default", agentName); |
| 342 | + end |
| 343 | +end |
| 344 | + |
| 345 | + |
| 346 | + |
| 347 | + |
| 348 | + |
| 349 | + |
| 350 | + |
| 351 | + |
| 352 | + |
| 353 | + |
| 354 | + |
| 355 | + |
| 356 | + |
| 357 | + |
| 358 | + |
0 commit comments