Instructions Jenkins Doxygen

[Added] withSceneObjectFromId function to lua object manager. 32/1332/1
authorLoshult <loshult@swgemu.com>
Sun, 12 Jan 2014 06:51:10 +0000 (06:51 +0000)
committerLoshult <loshult@swgemu.com>
Sun, 12 Jan 2014 06:58:54 +0000 (06:58 +0000)
[Added] isFromSpawn function to lua spawn mobile module.
[Removed] unused variable in encounter.lua.

Change-Id: I2d275dd7df57297cbc550bf3d0efc2e47ac1c709

MMOCoreORB/bin/scripts/managers/object/object_manager.lua
MMOCoreORB/bin/scripts/managers/object/tests/object_manager_Test.lua
MMOCoreORB/bin/scripts/quest/tasks/encounter.lua
MMOCoreORB/bin/scripts/utils/mocks/spawn_mobiles_mocks.lua
MMOCoreORB/bin/scripts/utils/spawn_mobiles.lua
MMOCoreORB/bin/scripts/utils/tests/spawn_mobiles_Test.lua

index bf46da1..30e3618 100644 (file)
@@ -108,5 +108,12 @@ function ObjectManager.withInventoryPointer(pCreatureObject, performThisFunction
        end)
 end
 
+-- Perform the supplied function with a scene object with the supplied object id.
+-- @param objectId the object id of the scene object to send to the function.
+-- @param performThisFunction a function that takes a scene object as argument.
+-- @return whatever performThisFunction returns or nil if no object with the specified id can be found.
+function ObjectManager.withSceneObjectFromId(objectId, performThisFunction)
+       return ObjectManager.withSceneObject(getSceneObject(objectId), performThisFunction)
+end
 
 return ObjectManager
index 2e38e1e..2e97953 100644 (file)
@@ -1,6 +1,8 @@
 local ObjectManager = require("managers.object.object_manager")
 local DirectorManagerMocks = require("screenplays.mocks.director_manager_mocks")
 
+-- TODO: several of the functions in this module needs more testing to test the return value and that functions are called correctly.
+
 describe("Object Manager", function()
        local pCreatureObject = { "creatureObjectPointer" }
        local pPlayerObject = { "playerObjectPointer" }
@@ -12,6 +14,7 @@ describe("Object Manager", function()
        local playerObject = { "playerObject" }
        local cityRegion = { "cityRegion" }
        local pInventory = { "inventoryPointer" }
+       local objectId = 12345
 
        setup(function()
                DirectorManagerMocks.mocks.setup()
@@ -34,121 +37,178 @@ describe("Object Manager", function()
                DirectorManagerMocks.creatureObjects[pCreatureObject] = creatureObject
        end)
 
-       it("Shall return nil if the creature object pointer is nil to the withCreatureAiAgent function.", function()
-               assert.is.Nil(ObjectManager.withCreatureAiAgent(nil, function(sceneObject) end))
-       end)
-       it("Shall return nil if the scene object pointer is nil to the withSceneObject function.", function()
-               assert.is.Nil(ObjectManager.withSceneObject(nil, function(sceneObject) end))
-       end)
+       describe("withCreatureAiAgent", function()
+               it("Should return nil if the creature object pointer is nil to the withCreatureAiAgent function.", function()
+                       assert.is.Nil(ObjectManager.withCreatureAiAgent(nil, function(sceneObject) end))
+               end)
 
-       it("Shall return nil if the creature object pointer is nil to the withCreatureObject function.", function()
-               assert.is.Nil(ObjectManager.withCreatureObject(nil, function(creatureObject) end))
-       end)
+               it("Should call the supplied lambda expression with the AiAgent when calling the withCreatureAiAgent function.", function()
+                       local aiAgentArgument = nil
 
-       it("Shall return nil if the creature object pointer is nil to the withCreaturePlayerObject function.", function()
-               assert.is.Nil(ObjectManager.withCreaturePlayerObject(nil, function(playerObject) end))
+                       ObjectManager.withCreatureAiAgent(pCreatureObject, function(aiAgent) aiAgentArgument = aiAgent end)
+                       assert.same(aiAgentArgument, aiAgent)
+               end)
        end)
 
-       it("Shall return nil if the player object pointer is nil to the withPlayerObject function.", function()
-               assert.is.Nil(ObjectManager.withPlayerObject(nil, function(playerObject) end))
-       end)
+       describe("withSceneObject", function()
+               it("Should return nil if the scene object pointer is nil to the withSceneObject function.", function()
+                       assert.is.Nil(ObjectManager.withSceneObject(nil, function(sceneObject) end))
+               end)
 
-       it("Shall return nil if the city region pointer is nil to the withCityRegion function.", function()
-               assert.is.Nil(ObjectManager.withCityRegion(nil, function(cityRegion) end))
-       end)
+               it("Should call the supplied lambda expression with the SceneObject when calling the withSceneObject function.", function()
+                       local sceneObjectArgument = nil
 
-       it("Shall return nil if the creature object pointer is nil to the withCreatureAndPlayerObject function.", function()
-               assert.is.Nil(ObjectManager.withCreatureAndPlayerObject(nil, function(creatureObject, playerObject) end))
+                       ObjectManager.withSceneObject(pSceneObject, function(sceneObject) sceneObjectArgument = sceneObject end)
+                       assert.same(sceneObjectArgument, sceneObject)
+               end)
        end)
 
-       it("Shall return nil if the creature object pointer is nil to the withInventoryPointer function.", function()
-               assert.is.Nil(ObjectManager.withInventoryPointer(nil, function(pInventory) end))
-       end)
+       describe("withCreatureObject", function()
+               it("Should return nil if the creature object pointer is nil to the withCreatureObject function.", function()
+                       assert.is.Nil(ObjectManager.withCreatureObject(nil, function(creatureObject) end))
+               end)
 
-       it("Shall call the supplied lambda expression with the AiAgent when calling the withCreatureAiAgent function.", function()
-               local aiAgentArgument = nil
+               it("Should call the supplied lambda expression with the CreatureObject when calling the withCreatureObject function.", function()
+                       local creatureObjectArgument = nil
 
-               ObjectManager.withCreatureAiAgent(pCreatureObject, function(aiAgent) aiAgentArgument = aiAgent end)
-               assert.same(aiAgentArgument, aiAgent)
+                       ObjectManager.withCreatureObject(pCreatureObject, function(creatureObject) creatureObjectArgument = creatureObject end)
+                       assert.same(creatureObjectArgument, creatureObject)
+               end)
        end)
 
-       it("Shall call the supplied lambda expression with the SceneObject when calling the withSceneObject function.", function()
-               local sceneObjectArgument = nil
+       describe("withCreaturePlayerObject", function()
+               it("Should return nil if the creature object pointer is nil to the withCreaturePlayerObject function.", function()
+                       assert.is.Nil(ObjectManager.withCreaturePlayerObject(nil, function(playerObject) end))
+               end)
+
+               it("Should return nil if the creature object has a nil pointer to the player object when calling the withCreaturePlayerObject function.", function()
+                       creatureObject.getPlayerObject = spy.new(function() return nil end)
 
-               ObjectManager.withSceneObject(pSceneObject, function(sceneObject) sceneObjectArgument = sceneObject end)
-               assert.same(sceneObjectArgument, sceneObject)
+                       assert.is.Nil(ObjectManager.withCreaturePlayerObject(pCreatureObject, function(playerObject) end))
+               end)
        end)
 
-       it("Shall call the supplied lambda expression with the CreatureObject when calling the withCreatureObject function.", function()
-               local creatureObjectArgument = nil
+       describe("withPlayerObject", function()
+               it("Should return nil if the player object pointer is nil to the withPlayerObject function.", function()
+                       assert.is.Nil(ObjectManager.withPlayerObject(nil, function(playerObject) end))
+               end)
+
+               it("Should call the supplied lambda expression with the PlayerObject when calling the withPlayerObject function.", function()
+                       local playerObjectArgument = nil
 
-               ObjectManager.withCreatureObject(pCreatureObject, function(creatureObject) creatureObjectArgument = creatureObject end)
-               assert.same(creatureObjectArgument, creatureObject)
+                       ObjectManager.withPlayerObject(pPlayerObject, function(playerObject) playerObjectArgument = playerObject end)
+                       assert.same(playerObjectArgument, playerObject)
+               end)
        end)
 
-       it("Shall call the supplied lambda expression with the PlayerObject when calling the withPlayerObject function.", function()
-               local playerObjectArgument = nil
+       describe("withCityRegion", function()
+               it("Should return nil if the city region pointer is nil to the withCityRegion function.", function()
+                       assert.is.Nil(ObjectManager.withCityRegion(nil, function(cityRegion) end))
+               end)
+
+               it("Should call the supplied lambda expression with the CityRegion when calling the withCityRegion function.", function()
+                       local cityRegionArgument = nil
 
-               ObjectManager.withPlayerObject(pPlayerObject, function(playerObject) playerObjectArgument = playerObject end)
-               assert.same(playerObjectArgument, playerObject)
+                       ObjectManager.withCityRegion(pCityRegion, function(cityRegion) cityRegionArgument = cityRegion end)
+                       assert.same(cityRegionArgument, cityRegion)
+               end)
        end)
 
-       it("Shall call the supplied lambda expression with the CityRegion when calling the withCityRegion function.", function()
-               local cityRegionArgument = nil
+       describe("withCreatureAndPlayerObject", function()
+               it("Should return nil if the creature object pointer is nil to the withCreatureAndPlayerObject function.", function()
+                       assert.is.Nil(ObjectManager.withCreatureAndPlayerObject(nil, function(creatureObject, playerObject) end))
+               end)
+
+               it("Should return nil if the creature object has a nil pointer to the player object when calling the withCreatureAndPlayerObject function.", function()
+                       creatureObject.getPlayerObject = spy.new(function() return nil end)
 
-               ObjectManager.withCityRegion(pCityRegion, function(cityRegion) cityRegionArgument = cityRegion end)
-               assert.same(cityRegionArgument, cityRegion)
-       end)
+                       assert.is.Nil(ObjectManager.withCreatureAndPlayerObject(pCreatureObject, function(playerObject) end))
+               end)
 
-       it("Shall return nil if the creature object has a nil pointer to the player object when calling the withCreaturePlayerObject function.", function()
-               creatureObject.getPlayerObject = spy.new(function() return nil end)
+               it("Should call the supplied lambda expression with the PlayerObject when calling the withCreaturePlayerObject function.", function()
+                       local playerObjectArgument = nil
 
-               assert.is.Nil(ObjectManager.withCreaturePlayerObject(pCreatureObject, function(playerObject) end))
-       end)
+                       ObjectManager.withCreaturePlayerObject(pCreatureObject, function(playerObject) playerObjectArgument = playerObject end)
+                       assert.same(playerObjectArgument, playerObject)
+               end)
 
-       it("Shall return nil if the creature object has a nil pointer to the player object when calling the withCreatureAndPlayerObject function.", function()
-               creatureObject.getPlayerObject = spy.new(function() return nil end)
+               it("Should call the supplied lambda expression with the CreatureObject and PlayerObject when calling the withCreatureAndPlayerObject function.", function()
+                       local creatureObjectArgument = nil
+                       local playerObjectArgument = nil
 
-               assert.is.Nil(ObjectManager.withCreatureAndPlayerObject(pCreatureObject, function(playerObject) end))
+                       ObjectManager.withCreatureAndPlayerObject(pCreatureObject, function(creatureObject, playerObject)
+                               creatureObjectArgument = creatureObject
+                               playerObjectArgument = playerObject
+                       end)
+                       assert.same(creatureObjectArgument, creatureObject)
+                       assert.same(playerObjectArgument, playerObject)
+               end)
        end)
 
-       it("Shall call the supplied lambda expression with the PlayerObject when calling the withCreaturePlayerObject function.", function()
-               local playerObjectArgument = nil
+       describe("withInventoryPointer", function()
+               it("Should return nil if the creature object pointer is nil to the withInventoryPointer function.", function()
+                       assert.is.Nil(ObjectManager.withInventoryPointer(nil, function(pInventory) end))
+               end)
 
-               ObjectManager.withCreaturePlayerObject(pCreatureObject, function(playerObject) playerObjectArgument = playerObject end)
-               assert.same(playerObjectArgument, playerObject)
-       end)
+               it("Should call the getSlottedObject function with 'inventory' as argument when calling the withInventoryPointer function.", function()
+                       ObjectManager.withInventoryPointer(pCreatureObject, function() end)
+
+                       assert.spy(creatureObject.getSlottedObject).was.called_with(creatureObject, "inventory")
+               end)
 
-       it("Shall call the supplied lambda expression with the CreatureObject and PlayerObject when calling the withCreatureAndPlayerObject function.", function()
-               local creatureObjectArgument = nil
-               local playerObjectArgument = nil
+               it("Should call the supplied lambda expression with the pointer to inventory when calling the withInventoryPointer function.", function()
+                       local pInventoryArgument = nil
 
-               ObjectManager.withCreatureAndPlayerObject(pCreatureObject, function(creatureObject, playerObject)
-                       creatureObjectArgument = creatureObject
-                       playerObjectArgument = playerObject
+                       ObjectManager.withInventoryPointer(pCreatureObject, function(pInventory) pInventoryArgument = pInventory end)
+                       assert.same(pInventoryArgument, pInventory)
                end)
-               assert.same(creatureObjectArgument, creatureObject)
-               assert.same(playerObjectArgument, playerObject)
-       end)
 
-       it("Shall call the getSlottedObject function with 'inventory' as argument when calling the withInventoryPointer function.", function()
-               ObjectManager.withInventoryPointer(pCreatureObject, function() end)
+               it("Should not call the supplied lambda expression with the pointer to inventory when calling the withInventoryPointer function if the creature does not have an inventory.", function()
+                       local lambdaExpressionCalled = false
+                       creatureObject.getSlottedObject = spy.new(function() return nil end)
 
-               assert.spy(creatureObject.getSlottedObject).was.called_with(creatureObject, "inventory")
+                       ObjectManager.withInventoryPointer(pCreatureObject, function(pInventory) lambdaExpressionCalled = true end)
+                       assert.is_false(lambdaExpressionCalled)
+               end)
        end)
 
-       it("Shall call the supplied lambda expression with the pointer to inventory when calling the withInventoryPointer function.", function()
-               local pInventoryArgument = nil
+       describe("withSceneObjectFromId", function()
+               it("Should call getSceneObject with the id.", function()
+                       ObjectManager.withSceneObjectFromId(objectId, function() end)
 
-               ObjectManager.withInventoryPointer(pCreatureObject, function(pInventory) pInventoryArgument = pInventory end)
-               assert.same(pInventoryArgument, pInventory)
-       end)
+                       assert.spy(getSceneObject).was.called_with(objectId)
+               end)
+
+               describe("and a object is returned from getSceneObject", function()
+                       before_each(function()
+                               getSceneObject = spy.new(function() return pSceneObject end)
+                       end)
 
-       it("Shall not call the supplied lambda expression with the pointer to inventory when calling the withInventoryPointer function if the creature does not have an inventory.", function()
-               local lambdaExpressionCalled = false
-               creatureObject.getSlottedObject = spy.new(function() return nil end)
+                       it("Should call the supplied lambda expression witht he returned object as argument", function()
+                               local sceneObjectArgument = nil
 
-               ObjectManager.withInventoryPointer(pCreatureObject, function(pInventory) lambdaExpressionCalled = true end)
-               assert.is_false(lambdaExpressionCalled)
+                               ObjectManager.withSceneObjectFromId(objectId, function(sceneObject) sceneObjectArgument = sceneObject end)
+
+                               assert.same(sceneObject, sceneObjectArgument)
+                       end)
+
+                       it("Should return the value returned from the lambda expression.", function()
+                               assert.same(234, ObjectManager.withSceneObjectFromId(objectId, function(sceneObject) return 234 end))
+                       end)
+               end)
+
+               describe("and nil is returned from getSceneObject", function()
+                       it("Should not call the supplied lambda expression.", function()
+                               local lambdaExpression = spy.new(function() end)
+
+                               ObjectManager.withSceneObjectFromId(objectId, lambdaExpression)
+
+                               assert.spy(lambdaExpression).was.not_called()
+                       end)
+
+                       it("Should return nil.", function()
+                               assert.is.Nil(ObjectManager.withSceneObjectFromId(objectId, function() end))
+                       end)
+               end)
        end)
 end)
index 4d5c8f0..216589b 100644 (file)
@@ -28,7 +28,6 @@ local SpawnMobiles = require("utils.spawn_mobiles")
 local Logger = require("utils.logger")
 
 local NOTINABUILDING = 0
-local ENCOUNTER_OBJECTS_STRING = "encounter_objects"
 local ENCOUNTER_CLOSING_IN_TIME = 10 * 1000
 local ENCOUNTER_AT_PLAYER_TIME = 30 * 1000
 
index f486acf..0130b0c 100644 (file)
@@ -2,6 +2,7 @@ local SpawnMobiles = require("utils.spawn_mobiles")
 local realSpawnMobiles
 local realDespawnMobiles
 local realGetSpawnedMobiles
+local realIsFromSpawn
 
 local SpawnMobilesMocks = SpawnMobiles
 SpawnMobilesMocks.mocks = {}
@@ -10,18 +11,21 @@ function SpawnMobilesMocks.mocks.setup()
        realSpawnMobiles = SpawnMobiles.spawnMobiles
        realDespawnMobiles = SpawnMobiles.despawnMobiles
        realGetSpawnedMobiles = SpawnMobiles.getSpawnedMobiles
+       realIsFromSpawn = SpawnMobiles.isFromSpawn
 end
 
 function SpawnMobilesMocks.mocks.teardown()
        SpawnMobiles.spawnMobiles = realSpawnMobiles
        SpawnMobiles.despawnMobiles = realDespawnMobiles
        SpawnMobiles.getSpawnedMobiles = realGetSpawnedMobiles
+       SpawnMobiles.isFromSpawn = realIsFromSpawn
 end
 
 function SpawnMobilesMocks.mocks.before_each()
        SpawnMobiles.spawnMobiles = spy.new(function() return nil end)
        SpawnMobiles.despawnMobiles = spy.new(function() end)
        SpawnMobiles.getSpawnedMobiles = spy.new(function() return nil end)
+       SpawnMobiles.isFromSpawn = spy.new(function() return false end)
 end
 
 function SpawnMobilesMocks.mocks.after_each()
index 8e17feb..889e7b9 100644 (file)
@@ -6,9 +6,11 @@
 -- To spawn the the mobiles, call the spawnMobiles function with the appropriate arguments. The format of the arguments are described below.
 -- This function will return a list with pointers to all spawned mobiles.
 -- To despawn the mobiles, call the despawnMobiles function with the appropriate arguments.
--- To get a list with all spawned mobiles, call the getSpawnedMobiles function with the approprate arguments.
+-- To get a list with all spawned mobiles, call the getSpawnedMobiles function with the appropriate arguments.
+-- To check if a creature belongs to a spawn call the isFromSpawn function with the appropriate arguments
 --
 -- Arguments:
+-- pSceneObject - pointer to the scene object of the spawn is related to.
 -- prefix - a string used to store the id of all spawned mobiles. The information will not be stored persistent.
 --          The module will check that no data is overwritten before storing anything.
 -- x - the center x coordinate for the spawn. The spawned mobiles will use this point as the center for their spawn points.
@@ -33,22 +35,22 @@ local IN_USE_STRING = "_in_use"
 local NUMBER_OF_SPAWNS_STRING = "_number_of_spawns"
 
 -- Check if the prefix is free to use.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to check.
 -- @return true if the prefix is free to use.
-function SpawnMobiles.isPrefixFree(pCreatureObject, prefix)
-       return ObjectManager.withSceneObject(pCreatureObject, function(sceneObject)
+function SpawnMobiles.isPrefixFree(pSceneObject, prefix)
+       return ObjectManager.withSceneObject(pSceneObject, function(sceneObject)
                return readData(sceneObject:getObjectID() .. prefix .. SPAWN_MOBILES_STRING .. IN_USE_STRING) ~= PREFIX_IN_USE
        end)
 end
 
 -- Get the spawn point parameters for the specified spawn point generation.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param mobileList a list with information about what mobiles to spawn.
 -- @param spawnPoints a list of already generated spawn points.
 -- @param spawnObjectNumber the sequence number of the spawn point that should be generated.
 -- @return a table with the relvant spawn point parameters containing, x, y, min and max.
-function SpawnMobiles.getSpawnPointParameters(pCreatureObject, mobileList, spawnPoints, spawnObjectNumber)
+function SpawnMobiles.getSpawnPointParameters(pSceneObject, mobileList, spawnPoints, spawnObjectNumber)
        if spawnObjectNumber < 1 or spawnObjectNumber > table.getn(mobileList) or mobileList[spawnObjectNumber]["referencePoint"] < 0 then
                return nil
        end
@@ -64,7 +66,7 @@ function SpawnMobiles.getSpawnPointParameters(pCreatureObject, mobileList, spawn
        parameters["max"] = mobileList[spawnObjectNumber]["maximumDistance"]
 
        if mobileList[spawnObjectNumber]["referencePoint"] == 0 then
-               ObjectManager.withSceneObject(pCreatureObject, function(sceneObject)
+               ObjectManager.withSceneObject(pSceneObject, function(sceneObject)
                        parameters["x"] = sceneObject:getWorldPositionX()
                        parameters["y"] = sceneObject:getWorldPositionY()
                end)
@@ -78,16 +80,16 @@ end
 
 -- Generate spawn points for all objects in the mobile list.
 -- Return nil if not all spawn points were generated.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param mobileList a list with information about what mobiles to spawn.
 -- @return a list with spawn points to use for the mobiles, or nil if generation of spawn points failed.
-function SpawnMobiles.generateSpawnPoints(pCreatureObject, mobileList)
+function SpawnMobiles.generateSpawnPoints(pSceneObject, mobileList)
        local spawnPoints = {}
 
        for spawnObjectNumber = 1, table.getn(mobileList), 1 do
-               local spawnPointParams = SpawnMobiles.getSpawnPointParameters(pCreatureObject, mobileList, spawnPoints, spawnObjectNumber)
+               local spawnPointParams = SpawnMobiles.getSpawnPointParameters(pSceneObject, mobileList, spawnPoints, spawnObjectNumber)
                if spawnPointParams ~= nil then
-                       local spawnPoint = getSpawnPoint(pCreatureObject, spawnPointParams["x"], spawnPointParams["y"], spawnPointParams["min"], spawnPointParams["max"])
+                       local spawnPoint = getSpawnPoint(pSceneObject, spawnPointParams["x"], spawnPointParams["y"], spawnPointParams["min"], spawnPointParams["max"])
                        if spawnPoint ~= nil then
                                table.insert(spawnPoints, spawnPoint)
                        else
@@ -102,15 +104,15 @@ function SpawnMobiles.generateSpawnPoints(pCreatureObject, mobileList)
 end
 
 -- Spawn the objects at the spawn points.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param mobileList a list with information about what mobiles to spawn.
 -- @param spawnPoints a list of spawn points to use.
 -- @return a list with pointers to the spawned mobiles.
-function SpawnMobiles.spawnMobileObjects(pCreatureObject, mobileList, spawnPoints)
+function SpawnMobiles.spawnMobileObjects(pSceneObject, mobileList, spawnPoints)
        local spawnedObjects = {}
        local success = true
 
-       ObjectManager.withSceneObject(pCreatureObject, function(sceneObject)
+       ObjectManager.withSceneObject(pSceneObject, function(sceneObject)
                for spawnNumber = 1, table.getn(spawnPoints), 1 do
                        local spawnedObject = spawnMobile(sceneObject:getZoneName(),
                                                                                          mobileList[spawnNumber]["template"],
@@ -142,11 +144,11 @@ function SpawnMobiles.spawnMobileObjects(pCreatureObject, mobileList, spawnPoint
 end
 
 -- Save id of the spawned mobile objects on the player for later use.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to use for storing information about the spawned mobiles.
 -- @param spawnedObjects list with pointers to the spawned objects.
-function SpawnMobiles.saveSpawnedMobileObjects(pCreatureObject, prefix, spawnedObjects)
-       ObjectManager.withSceneObject(pCreatureObject, function(playerSceneObject)
+function SpawnMobiles.saveSpawnedMobileObjects(pSceneObject, prefix, spawnedObjects)
+       ObjectManager.withSceneObject(pSceneObject, function(playerSceneObject)
                writeData(playerSceneObject:getObjectID() .. prefix .. SPAWN_MOBILES_STRING .. IN_USE_STRING, PREFIX_IN_USE)
                Logger:log(playerSceneObject:getObjectID() .. prefix .. SPAWN_MOBILES_STRING .. IN_USE_STRING .. " set to " .. PREFIX_IN_USE)
                writeData(playerSceneObject:getObjectID() .. prefix .. SPAWN_MOBILES_STRING .. NUMBER_OF_SPAWNS_STRING, table.getn(spawnedObjects))
@@ -162,43 +164,43 @@ function SpawnMobiles.saveSpawnedMobileObjects(pCreatureObject, prefix, spawnedO
 end
 
 -- Generate the mobile objects.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to use for storing information about the spawned mobiles.
 -- @param mobileList a list with information about what mobiles to spawn.
 -- @param spawnPoints a list with spawn points to use for the mobile objects.
-function SpawnMobiles.generateMobileObjects(pCreatureObject, prefix, mobileList, spawnPoints)
-       local spawnedObjects = SpawnMobiles.spawnMobileObjects(pCreatureObject, mobileList, spawnPoints)
+function SpawnMobiles.generateMobileObjects(pSceneObject, prefix, mobileList, spawnPoints)
+       local spawnedObjects = SpawnMobiles.spawnMobileObjects(pSceneObject, mobileList, spawnPoints)
 
        if spawnedObjects ~= nil then
-               SpawnMobiles.saveSpawnedMobileObjects(pCreatureObject, prefix, spawnedObjects)
+               SpawnMobiles.saveSpawnedMobileObjects(pSceneObject, prefix, spawnedObjects)
        end
 
        return spawnedObjects
 end
 
 -- Spawn the mobiles with the supplied prefix.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to use for storing information about the spawned mobiles.
 -- @param mobileList a list with information about what mobiles to spawn.
 -- @return a list with pointers to all spawned mobiles. If anything fails in this function it will clean up and return nil.
-function SpawnMobiles.spawnMobilesWithPrefix(pCreatureObject, prefix, mobileList)
-       local spawnPoints = SpawnMobiles.generateSpawnPoints(pCreatureObject, mobileList)
+function SpawnMobiles.spawnMobilesWithPrefix(pSceneObject, prefix, mobileList)
+       local spawnPoints = SpawnMobiles.generateSpawnPoints(pSceneObject, mobileList)
 
        if spawnPoints ~= nil then
-               return SpawnMobiles.generateMobileObjects(pCreatureObject, prefix, mobileList, spawnPoints)
+               return SpawnMobiles.generateMobileObjects(pSceneObject, prefix, mobileList, spawnPoints)
        else
                return nil
        end
 end
 
 -- Returns a list with pointers to the mobiles saved with the prefix.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to read the spawned mobiles from.
 -- @return a list with pointers to the spawned mobiles or nil if none have been spawned.
-function SpawnMobiles.getSpawnedMobilePointersList(pCreatureObject, prefix)
+function SpawnMobiles.getSpawnedMobilePointersList(pSceneObject, prefix)
        local spawnedMobiles = {}
 
-       ObjectManager.withSceneObject(pCreatureObject, function(playerSceneObject)
+       ObjectManager.withSceneObject(pSceneObject, function(playerSceneObject)
                local numberOfSpawns = readData(playerSceneObject:getObjectID() .. prefix .. SPAWN_MOBILES_STRING .. NUMBER_OF_SPAWNS_STRING)
                Logger:log(playerSceneObject:getObjectID() .. prefix .. SPAWN_MOBILES_STRING .. NUMBER_OF_SPAWNS_STRING .. " = " .. numberOfSpawns, LT_INFO)
 
@@ -217,13 +219,13 @@ function SpawnMobiles.getSpawnedMobilePointersList(pCreatureObject, prefix)
 end
 
 -- Return a list with pointers to the objects that was spawned on the prefix.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to read the spawned mobiles from.
 -- @return a list with pointers to the spawned mobiles or nil if none have been spawned.
-function SpawnMobiles.getSpawnedMobiles(pCreatureObject, prefix)
+function SpawnMobiles.getSpawnedMobiles(pSceneObject, prefix)
        Logger:log("Getting spawned mobiles for prefix '" .. prefix .. "'.", LT_INFO)
-       if not SpawnMobiles.isPrefixFree(pCreatureObject, prefix) then
-               return SpawnMobiles.getSpawnedMobilePointersList(pCreatureObject, prefix)
+       if not SpawnMobiles.isPrefixFree(pSceneObject, prefix) then
+               return SpawnMobiles.getSpawnedMobilePointersList(pSceneObject, prefix)
        else
                return nil
        end
@@ -242,13 +244,13 @@ function SpawnMobiles.despawnMobilesInList(spawnedMobilesList)
 end
 
 -- Spawn the mobiles and store information about their ids.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to use for storing information about the spawned mobiles.
 -- @param mobileList a list with information about what mobiles to spawn.
 -- @return a list with pointers to all spawned mobiles. If anything fails in this function it will clean up and return nil.
-function SpawnMobiles.spawnMobiles(pCreatureObject, prefix, mobileList)
-       if SpawnMobiles.isPrefixFree(pCreatureObject, prefix) then
-               return SpawnMobiles.spawnMobilesWithPrefix(pCreatureObject, prefix, mobileList)
+function SpawnMobiles.spawnMobiles(pSceneObject, prefix, mobileList)
+       if SpawnMobiles.isPrefixFree(pSceneObject, prefix) then
+               return SpawnMobiles.spawnMobilesWithPrefix(pSceneObject, prefix, mobileList)
        else
                return nil
        end
@@ -256,13 +258,35 @@ end
 
 -- Despawn the mobiles stored with the prefix.
 -- This function will clean up the information stored with the prefix so that spawnMobiles can be called again with the same prefix.
--- @param pCreatureObject pointer to the creature object of the player.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
 -- @param prefix the prefix to use for reading information about the mobiles to despawn.
-function SpawnMobiles.despawnMobiles(pCreatureObject, prefix)
-       SpawnMobiles.despawnMobilesInList(SpawnMobiles.getSpawnedMobiles(pCreatureObject, prefix))
-       ObjectManager.withSceneObject(pCreatureObject, function(sceneObject)
+function SpawnMobiles.despawnMobiles(pSceneObject, prefix)
+       SpawnMobiles.despawnMobilesInList(SpawnMobiles.getSpawnedMobiles(pSceneObject, prefix))
+       ObjectManager.withSceneObject(pSceneObject, function(sceneObject)
                writeData(sceneObject:getObjectID() .. prefix .. SPAWN_MOBILES_STRING .. IN_USE_STRING, PREFIX_FREE)
        end)
 end
 
+-- Despawn the mobiles stored with the prefix.
+-- This function will clean up the information stored with the prefix so that spawnMobiles can be called again with the same prefix.
+-- @param pSceneObject pointer to the scene object that the spawn is related to.
+-- @param prefix the prefix to use for reading information about the mobiles to despawn.
+-- @param pMobile pointer to the mobile that should be checked if it belongs to the spawn or not.
+-- @return true if the mobile is from the spawn, false otherwise.
+function SpawnMobiles.isFromSpawn(pSceneObject, prefix, pMobile)
+       local spawnedMobiles = SpawnMobiles.getSpawnedMobiles(pSceneObject, prefix)
+       local objectIdToCheck = ObjectManager.withSceneObject(pMobile, function(mobile) return mobile:getObjectID() end)
+
+       if spawnedMobiles ~= nil then
+               for i = 1, table.getn(spawnedMobiles), 1 do
+                       local objectId = ObjectManager.withSceneObject(spawnedMobiles[i], function(spawnedObject) return spawnedObject:getObjectID() end)
+                       if objectIdToCheck == objectId then
+                               return true
+                       end
+               end
+       end
+
+       return false
+end
+
 return SpawnMobiles
index 976bce5..6247fc9 100644 (file)
@@ -268,6 +268,58 @@ describe("Spawn Mobile", function()
                                end)
                        end)
                end)
+
+               describe("isFromSpawn", function()
+                       describe("When called with a pointer to a creature and a prefix", function()
+                               local realGetSpawnedMobiles
+
+                               setup(function()
+                                       realGetSpawnedMobiles = SpawnMobiles.getSpawnedMobiles
+                               end)
+
+                               teardown(function()
+                                       SpawnMobiles.getSpawnedMobiles = realGetSpawnedMobiles
+                               end)
+
+                               before_each(function()
+                                       SpawnMobiles.getSpawnedMobiles = spy.new(function() return spawnedMobilesList end)
+                               end)
+
+                               it("Should get the list of spawned mobiles for the prefix.", function()
+                                       SpawnMobiles.isFromSpawn(pCreatureObject, prefix, pSpawnedObject1)
+
+                                       assert.spy(SpawnMobiles.getSpawnedMobiles).was.called_with(pCreatureObject, prefix)
+                               end)
+
+                               it("Should get the object id of the creature.", function()
+                                       SpawnMobiles.isFromSpawn(pCreatureObject, prefix, pSpawnedObject1)
+
+                                       assert.spy(spawnedObject1.getObjectID).was.called_with(spawnedObject1)
+                               end)
+
+                               describe("and the creature was spawned with the prefix", function()
+                                       it("Should return true.", function()
+                                               assert.is_true(SpawnMobiles.isFromSpawn(pCreatureObject, prefix, pSpawnedObject1))
+                                       end)
+                               end)
+
+                               describe("and the creature was not spawned with the prefix", function()
+                                       it("Should return false.", function()
+                                               assert.is_false(SpawnMobiles.isFromSpawn(pCreatureObject, prefix, pCreatureObject))
+                                       end)
+
+                                       it("Should get the object id of every mobile in the spawn.", function()
+                                               SpawnMobiles.isFromSpawn(pCreatureObject, prefix, pCreatureObject)
+
+                                               assert.spy(spawnedObject1.getObjectID).was.called_with(spawnedObject1)
+                                               assert.spy(spawnedObject2.getObjectID).was.called_with(spawnedObject2)
+                                               assert.spy(spawnedObject3.getObjectID).was.called_with(spawnedObject3)
+                                               assert.spy(spawnedObject4.getObjectID).was.called_with(spawnedObject4)
+                                               assert.spy(spawnedObject5.getObjectID).was.called_with(spawnedObject5)
+                                       end)
+                               end)
+                       end)
+               end)
        end)
 
        describe("Private functions", function()