1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
local testing = require('testing_util')
local self = require('openmw.self')
local util = require('openmw.util')
local core = require('openmw.core')
local input = require('openmw.input')
local types = require('openmw.types')
local nearby = require('openmw.nearby')
local camera = require('openmw.camera')
local matchers = require('matchers')
types.Player.setControlSwitch(self, types.Player.CONTROL_SWITCH.Controls, false)
types.Player.setControlSwitch(self, types.Player.CONTROL_SWITCH.Fighting, false)
types.Player.setControlSwitch(self, types.Player.CONTROL_SWITCH.Jumping, false)
types.Player.setControlSwitch(self, types.Player.CONTROL_SWITCH.Looking, false)
types.Player.setControlSwitch(self, types.Player.CONTROL_SWITCH.Magic, false)
types.Player.setControlSwitch(self, types.Player.CONTROL_SWITCH.VanityMode, false)
types.Player.setControlSwitch(self, types.Player.CONTROL_SWITCH.ViewMode, false)
local function rotate(object, targetPitch, targetYaw)
local endTime = core.getSimulationTime() + 1
while core.getSimulationTime() < endTime do
object.controls.jump = false
object.controls.run = true
object.controls.movement = 0
object.controls.sideMovement = 0
if targetPitch ~= nil then
object.controls.pitchChange = util.normalizeAngle(targetPitch - object.rotation:getPitch()) * 0.5
end
if targetYaw ~= nil then
object.controls.yawChange = util.normalizeAngle(targetYaw - object.rotation:getYaw()) * 0.5
end
coroutine.yield()
end
end
local function rotateByYaw(object, target)
rotate(object, nil, target)
end
local function rotateByPitch(object, target)
rotate(object, target, nil)
end
testing.registerLocalTest('player yaw rotation',
function()
local initialAlphaXZ, initialGammaXZ = self.rotation:getAnglesXZ()
local initialAlphaZYX, initialBetaZYX, initialGammaZYX = self.rotation:getAnglesZYX()
local targetYaw = math.rad(90)
rotateByYaw(self, targetYaw)
testing.expectEqualWithDelta(self.rotation:getYaw(), targetYaw, 0.05, 'Incorrect yaw rotation')
local alpha1, gamma1 = self.rotation:getAnglesXZ()
testing.expectEqualWithDelta(alpha1, initialAlphaXZ, 0.05, 'Alpha rotation in XZ convention should not change')
testing.expectEqualWithDelta(gamma1, targetYaw, 0.05, 'Incorrect gamma rotation in XZ convention')
local alpha2, beta2, gamma2 = self.rotation:getAnglesZYX()
testing.expectEqualWithDelta(alpha2, targetYaw, 0.05, 'Incorrect alpha rotation in ZYX convention')
testing.expectEqualWithDelta(beta2, initialBetaZYX, 0.05, 'Beta rotation in ZYX convention should not change')
testing.expectEqualWithDelta(gamma2, initialGammaZYX, 0.05, 'Gamma rotation in ZYX convention should not change')
end)
testing.registerLocalTest('player pitch rotation',
function()
local initialAlphaXZ, initialGammaXZ = self.rotation:getAnglesXZ()
local initialAlphaZYX, initialBetaZYX, initialGammaZYX = self.rotation:getAnglesZYX()
local targetPitch = math.rad(90)
rotateByPitch(self, targetPitch)
testing.expectEqualWithDelta(self.rotation:getPitch(), targetPitch, 0.05, 'Incorrect pitch rotation')
local alpha1, gamma1 = self.rotation:getAnglesXZ()
testing.expectEqualWithDelta(alpha1, targetPitch, 0.05, 'Incorrect alpha rotation in XZ convention')
testing.expectEqualWithDelta(gamma1, initialGammaXZ, 0.05, 'Gamma rotation in XZ convention should not change')
local alpha2, beta2, gamma2 = self.rotation:getAnglesZYX()
testing.expectEqualWithDelta(alpha2, initialAlphaZYX, 0.05, 'Alpha rotation in ZYX convention should not change')
testing.expectEqualWithDelta(beta2, initialBetaZYX, 0.05, 'Beta rotation in ZYX convention should not change')
testing.expectEqualWithDelta(gamma2, targetPitch, 0.05, 'Incorrect gamma rotation in ZYX convention')
end)
testing.registerLocalTest('player pitch and yaw rotation',
function()
local targetPitch = math.rad(-30)
local targetYaw = math.rad(-60)
rotate(self, targetPitch, targetYaw)
testing.expectEqualWithDelta(self.rotation:getPitch(), targetPitch, 0.05, 'Incorrect pitch rotation')
testing.expectEqualWithDelta(self.rotation:getYaw(), targetYaw, 0.05, 'Incorrect yaw rotation')
local alpha1, gamma1 = self.rotation:getAnglesXZ()
testing.expectEqualWithDelta(alpha1, targetPitch, 0.05, 'Incorrect alpha rotation in XZ convention')
testing.expectEqualWithDelta(gamma1, targetYaw, 0.05, 'Incorrect gamma rotation in XZ convention')
local alpha2, beta2, gamma2 = self.rotation:getAnglesZYX()
testing.expectEqualWithDelta(alpha2, math.rad(-56), 0.05, 'Incorrect alpha rotation in ZYX convention')
testing.expectEqualWithDelta(beta2, math.rad(-25), 0.05, 'Incorrect beta rotation in ZYX convention')
testing.expectEqualWithDelta(gamma2, math.rad(-16), 0.05, 'Incorrect gamma rotation in ZYX convention')
end)
testing.registerLocalTest('player rotation',
function()
local rotation = math.sqrt(2)
local endTime = core.getSimulationTime() + 3
while core.getSimulationTime() < endTime do
self.controls.jump = false
self.controls.run = true
self.controls.movement = 0
self.controls.sideMovement = 0
self.controls.pitchChange = rotation
self.controls.yawChange = rotation
coroutine.yield()
local alpha1, gamma1 = self.rotation:getAnglesXZ()
testing.expectThat(alpha1, matchers.isNotNan(), 'Alpha rotation in XZ convention is nan')
testing.expectThat(gamma1, matchers.isNotNan(), 'Gamma rotation in XZ convention is nan')
local alpha2, beta2, gamma2 = self.rotation:getAnglesZYX()
testing.expectThat(alpha2, matchers.isNotNan(), 'Alpha rotation in ZYX convention is nan')
testing.expectThat(beta2, matchers.isNotNan(), 'Beta rotation in ZYX convention is nan')
testing.expectThat(gamma2, matchers.isNotNan(), 'Gamma rotation in ZYX convention is nan')
end
end)
testing.registerLocalTest('player forward running',
function()
local startPos = self.position
local endTime = core.getSimulationTime() + 1
while core.getSimulationTime() < endTime do
self.controls.jump = false
self.controls.run = true
self.controls.movement = 1
self.controls.sideMovement = 0
self.controls.yawChange = 0
coroutine.yield()
end
local direction, distance = (self.position - startPos):normalize()
testing.expectGreaterThan(distance, 0, 'Run forward, distance')
testing.expectEqualWithDelta(direction.x, 0, 0.1, 'Run forward, X coord')
testing.expectEqualWithDelta(direction.y, 1, 0.1, 'Run forward, Y coord')
end)
testing.registerLocalTest('player diagonal walking',
function()
local startPos = self.position
local endTime = core.getSimulationTime() + 1
while core.getSimulationTime() < endTime do
self.controls.jump = false
self.controls.run = false
self.controls.movement = -1
self.controls.sideMovement = -1
self.controls.yawChange = 0
coroutine.yield()
end
local direction, distance = (self.position - startPos):normalize()
testing.expectGreaterThan(distance, 0, 'Walk diagonally, distance')
testing.expectEqualWithDelta(direction.x, -0.707, 0.1, 'Walk diagonally, X coord')
testing.expectEqualWithDelta(direction.y, -0.707, 0.1, 'Walk diagonally, Y coord')
end)
testing.registerLocalTest('findPath',
function()
local src = util.vector3(4096, 4096, 1745)
local dst = util.vector3(4500, 4500, 1745.95263671875)
local options = {
agentBounds = types.Actor.getPathfindingAgentBounds(self),
includeFlags = nearby.NAVIGATOR_FLAGS.Walk + nearby.NAVIGATOR_FLAGS.Swim,
areaCosts = {
water = 1,
door = 2,
ground = 1,
pathgrid = 1,
},
destinationTolerance = 1,
}
local status, path = nearby.findPath(src, dst, options)
testing.expectEqual(status, nearby.FIND_PATH_STATUS.Success, 'Status')
testing.expectLessOrEqual((path[#path] - dst):length(), 1,
'Last path point ' .. testing.formatActualExpected(path[#path], dst))
end)
testing.registerLocalTest('findRandomPointAroundCircle',
function()
local position = util.vector3(4096, 4096, 1745.95263671875)
local maxRadius = 100
local options = {
agentBounds = types.Actor.getPathfindingAgentBounds(self),
includeFlags = nearby.NAVIGATOR_FLAGS.Walk,
}
local result = nearby.findRandomPointAroundCircle(position, maxRadius, options)
testing.expectGreaterThan((result - position):length(), 1,
'Random point ' .. testing.formatActualExpected(result, position))
end)
testing.registerLocalTest('castNavigationRay',
function()
local src = util.vector3(4096, 4096, 1745)
local dst = util.vector3(4500, 4500, 1745.95263671875)
local options = {
agentBounds = types.Actor.getPathfindingAgentBounds(self),
includeFlags = nearby.NAVIGATOR_FLAGS.Walk + nearby.NAVIGATOR_FLAGS.Swim,
}
local result = nearby.castNavigationRay(src, dst, options)
testing.expectLessOrEqual((result - dst):length(), 1,
'Navigation hit point ' .. testing.formatActualExpected(result, dst))
end)
testing.registerLocalTest('findNearestNavMeshPosition',
function()
local position = util.vector3(4096, 4096, 2000)
local options = {
agentBounds = types.Actor.getPathfindingAgentBounds(self),
includeFlags = nearby.NAVIGATOR_FLAGS.Walk + nearby.NAVIGATOR_FLAGS.Swim,
searchAreaHalfExtents = util.vector3(1000, 1000, 1000),
}
local result = nearby.findNearestNavMeshPosition(position, options)
local expected = util.vector3(4096, 4096, 1746.27099609375)
testing.expectLessOrEqual((result - expected):length(), 1,
'Navigation mesh position ' .. testing.formatActualExpected(result, expected))
end)
testing.registerLocalTest('player memory limit',
function()
local ok, err = pcall(function()
local str = 'a'
while true do
str = str .. str
end
end)
testing.expectEqual(ok, false, 'Script reaching memory limit should fail')
testing.expectEqual(err, 'not enough memory')
end)
testing.registerLocalTest('player weapon attack',
function()
camera.setMode(camera.MODE.ThirdPerson)
local options = {
agentBounds = types.Actor.getPathfindingAgentBounds(self),
}
local duration = 10
local endTime = core.getSimulationTime() + duration
local nextTime = 0
local use = self.ATTACK_TYPE.NoAttack
local attributes = {}
for k, v in pairs(types.Actor.stats.attributes) do
attributes[k] = v(self).base
end
types.Actor.stats.attributes.speed(self).base = 100
types.Actor.stats.attributes.strength(self).base = 1000
types.Actor.stats.attributes.agility(self).base = 1000
local weaponId = 'basic_dagger1h'
local weapon = types.Actor.inventory(self):find(weaponId)
local isWeapon = function(actual)
if actual == nil then
return weaponId .. ' is not found'
end
if actual.recordId ~= weaponId then
return 'found weapon recordId does not match expected: actual=' .. tostring(actual.id)
.. ', expected=' .. weaponId
end
return ''
end
testing.expectThat(weapon, isWeapon)
types.Actor.setEquipment(self, {[types.Actor.EQUIPMENT_SLOT.CarriedRight] = weapon})
coroutine.yield()
testing.expectThat(types.Actor.getEquipment(self, types.Actor.EQUIPMENT_SLOT.CarriedRight), isWeapon)
types.Actor.setStance(self, types.Actor.STANCE.Weapon)
local previousHealth = nil
local targetActor = nil
while true do
local time = core.getSimulationTime()
testing.expectLessOrEqual(time, endTime, 'Did not damage any targets in ' .. duration .. ' seconds')
if targetActor ~= nil and types.Actor.stats.dynamic.health(targetActor).current < previousHealth then
print('Dealt ' .. (previousHealth - types.Actor.stats.dynamic.health(targetActor).current) .. ' damage to ' .. tostring(targetActor))
break
end
local minDistance = nil
for i, actor in ipairs(nearby.actors) do
if actor.id ~= self.id then
local distance = (actor.position - self.position):length()
if minDistance == nil or minDistance > distance then
minDistance = distance
targetActor = actor
end
end
end
testing.expectNotEqual(targetActor, nil, 'No attack targets found')
previousHealth = types.Actor.stats.dynamic.health(targetActor).current
local destination = nil
if minDistance > 100 then
local status, path = nearby.findPath(self.position, targetActor.position, options)
testing.expectEqual(status, nearby.FIND_PATH_STATUS.Success,
'Failed to find path from ' .. tostring(self.position) .. ' to ' .. tostring(targetActor.position))
destination = path[2]
use = self.ATTACK_TYPE.NoAttack
self.controls.run = true
self.controls.movement = 1
else
local halfExtents = types.Actor.getPathfindingAgentBounds(targetActor).halfExtents
destination = targetActor.position - util.vector3(0, 0, halfExtents.z)
if nextTime < time then
if use == 0 then
use = self.ATTACK_TYPE.Any
nextTime = time + 0.5
else
use = self.ATTACK_TYPE.NoAttack
end
end
end
self.controls.use = use
local direction = destination - self.position
direction = direction:normalize()
self.controls.yawChange = util.normalizeAngle(math.atan2(direction.x, direction.y) - self.rotation:getYaw())
self.controls.pitchChange = util.normalizeAngle(math.asin(util.clamp(-direction.z, -1, 1)) - self.rotation:getPitch())
coroutine.yield()
end
for k, v in pairs(types.Actor.stats.attributes) do
v(self).base = attributes[k]
end
end)
return {
engineHandlers = {
onFrame = testing.updateLocal,
},
eventHandlers = testing.localEventHandlers,
}
|