Instructions Jenkins Doxygen

[Added] the old man now runs to the player when spawned. 83/983/1
authorLoshult <loshult@swgemu.com>
Thu, 24 Oct 2013 11:09:56 +0000 (11:09 +0000)
committerLoshult <loshult@swgemu.com>
Thu, 24 Oct 2013 11:09:56 +0000 (11:09 +0000)
[Added] withAiAgent method to ScreenPlay.

Change-Id: I1068e1038a2568f6c2cee3e0806c6b64a1b8bc10

MMOCoreORB/bin/scripts/managers/jedi/village/old_man.lua
MMOCoreORB/bin/scripts/managers/jedi/village/tests/old_man_Test.lua
MMOCoreORB/bin/scripts/screenplays/mocks/director_manager_mocks.lua
MMOCoreORB/bin/scripts/screenplays/screenplay.lua
MMOCoreORB/bin/scripts/screenplays/tests/screenplay_Test.lua

index 055c6b7..474f014 100644 (file)
@@ -7,6 +7,10 @@ OLD_MAN_TEMPLATE = "old_man"
 OLD_MAN_RESPAWN_TIME = 0
 OLD_MAN_HEADING = 0
 OLD_MAN_ID_STRING = ":old_man_id"
+--OLD_MAN_SPAWN_TIME = 12 * 60 * 60 * 1000 -- 12 hours as base
+--OLD_MAN_SPAWN_TIME = 12 * 60 * 1000 -- 12 minutes as base for testing
+OLD_MAN_SPAWN_TIME = 12 * 1000 -- 12 minutes as base for testing
+OLD_MAN_STOP_FOLLOW_TIME = 20 * 1000 -- 20 seconds
 
 OldMan = ScreenPlay:new {}
 
@@ -22,6 +26,15 @@ function OldMan.spawnOldMan(pCreatureObject)
        end)
 end
 
+-- Read the id of the old man from the player he was spawned for.
+-- @param pCreatureObject pointer to the creature object of the player who got the old man spawned.
+-- @return the id of the old man stored on the player.
+function OldMan.readOldManIdFromPlayer(pCreatureObject)
+       return OldMan.withCreatureObject(pCreatureObject, function(creatureObject)
+               return readData(creatureObject:getObjectID() .. OLD_MAN_ID_STRING)
+       end)
+end
+
 -- Save the id of the old man to only enable the old man for the player he was spawned for.
 -- @param pCreatureObject pointer to the creature object of the player who got the old man spawned.
 -- @param pOldMan pointer to the creature object of the spawned old man.
@@ -71,6 +84,23 @@ function OldMan.canOldManBeSpawned(pCreatureObject)
        return (OldMan.isPlayerOnline(pCreatureObject) and not OldMan.isPlayerInABuilding(pCreatureObject) and not OldMan.isPlayerInNpcCity(pCreatureObject))
 end
 
+-- Set a creature to follow another creature.
+-- @param pCreatureObject pointer to the creature who should follow another creature.
+-- @param pFollowObject pointer to the creature object of the creature to follow.
+function OldMan.setToFollow(pCreatureObject, pFollowObject)
+       OldMan.withCreatureAiAgent(pCreatureObject, function(aiAgent)
+               aiAgent:setFollowObject(pFollowObject)
+       end)
+end
+
+-- Function to handle the stop follow player event for the old man.
+-- @param pCreatureObject pointer to the creature object of the player who got an old man that should stop following.
+function OldMan:handleStopFollowPlayerEvent(pCreatureObject)
+       local oldManId = OldMan.readOldManIdFromPlayer(pCreatureObject)
+       local pOldMan = getSceneObject(oldManId)
+       OldMan.setToFollow(pOldMan, nil)
+end
+
 -- Try to spawn the old man and create the needed events.
 -- @param pCreatureObject pointer to the creature object of the player who should get the old man spawned.
 -- @return true if everything were successful.
@@ -78,6 +108,8 @@ function OldMan.tryToSpawnOldMan(pCreatureObject)
        local pOldMan = OldMan.spawnOldMan(pCreatureObject)
        if pOldMan ~= nil then
                OldMan.saveOldManIdOnPlayer(pCreatureObject, pOldMan)
+               OldMan.setToFollow(pOldMan, pCreatureObject)
+               createEvent(OLD_MAN_STOP_FOLLOW_TIME, "OldMan", "handleStopFollowPlayerEvent", pCreatureObject)
                return true
        else
                return false
@@ -99,7 +131,7 @@ end
 -- Generate an event to spawn the old man for the player.
 -- @param pCreatureObject pointer to the creature object who should have an event created for spawning the old man.
 function OldMan.createSpawnOldManEvent(pCreatureObject)
-       createEvent(20000, "OldMan", "handleSpawnOldManEvent", pCreatureObject)
+       createEvent(OLD_MAN_SPAWN_TIME + math.random(OLD_MAN_SPAWN_TIME), "OldMan", "handleSpawnOldManEvent", pCreatureObject)
 end
 
 -- Check if the old man belongs to the player or not.
@@ -107,10 +139,8 @@ end
 -- @param pConversingNpc pointer to the creature object of the old man.
 -- @return true if the old man belongs to the player.
 function OldMan.oldManBelongsToThePlayer(pConversingPlayer, pConversingNpc)
-       return OldMan.withCreatureObject(pConversingPlayer, function(playerCreatureObject)
-               return OldMan.withCreatureObject(pConversingNpc, function(oldManCreatureObject)
-                       return readData(playerCreatureObject:getObjectID() .. OLD_MAN_ID_STRING) == oldManCreatureObject:getObjectID()
-               end) == true
+       return OldMan.withCreatureObject(pConversingNpc, function(oldManCreatureObject)
+               return OldMan.readOldManIdFromPlayer(pConversingPlayer) == oldManCreatureObject:getObjectID()
        end) == true
 end
 
index 2d00b01..9117f66 100644 (file)
@@ -15,6 +15,7 @@ describe("Old Man", function()
        local sceneObject
        local playerObject
        local cityRegion
+       local aiAgent
 
        setup(function()
                DirectorManagerMocks.setup()
@@ -28,6 +29,7 @@ describe("Old Man", function()
                DirectorManagerMocks.before_each()
 
                getCityRegionAt = spy.new(function() return pCityRegion end)
+               getSceneObject = spy.new(function() return pOldMan end)
                getSpawnPoint = spy.new(function() return { 1, 2, 3 } end)
                readData = spy.new(function() return oldManId end)
                spawnMobile = spy.new(function() return pOldMan end)
@@ -49,6 +51,11 @@ describe("Old Man", function()
 
                cityRegion = { isClientRegion = nil }
                cityRegion.isClientRegion = spy.new(function() return false end)
+               
+               aiAgent = { setFollowObject = nil }
+               aiAgent.setFollowObject = spy.new(function() end)
+               
+               DirectorManagerMocks.aiAgents[pOldMan] = aiAgent
 
                DirectorManagerMocks.creatureObjects[pCreatureObject] = creatureObjectPlayer
                DirectorManagerMocks.creatureObjects[pOldMan] = creatureObjectOldMan
@@ -102,62 +109,56 @@ describe("Old Man", function()
                describe("handleSpawnOldManEvent", function()
                        describe("When event to spawn old man is triggered", function()
                                local realCanOldManBeSpawned
-                               local realSpawnOldMan
+                               local realTryToSpawnOldMan
                                local realCreateSpawnOldManEvent
                                local realSaveOldManIdOnPlayer
 
                                setup(function()
                                        realCanOldManBeSpawned = OldMan.canOldManBeSpawned
-                                       realSpawnOldMan = OldMan.spawnOldMan
+                                       realTryToSpawnOldMan = OldMan.tryToSpawnOldMan
                                        realCreateSpawnOldManEvent = OldMan.createSpawnOldManEvent
                                        realSaveOldManIdOnPlayer = OldMan.saveOldManIdOnPlayer
                                end)
 
                                teardown(function()
                                        OldMan.canOldManBeSpawned = realCanOldManBeSpawned
-                                       OldMan.spawnOldMan = realSpawnOldMan
+                                       OldMan.tryToSpawnOldMan = realTryToSpawnOldMan
                                        OldMan.createSpawnOldManEvent = realCreateSpawnOldManEvent
                                        OldMan.saveOldManIdOnPlayer = realSaveOldManIdOnPlayer
                                end)
 
                                before_each(function()
                                        OldMan.canOldManBeSpawned = spy.new(function() return true end)
-                                       OldMan.spawnOldMan = spy.new(function() return pOldMan end)
+                                       OldMan.tryToSpawnOldMan = spy.new(function() return true end)
                                        OldMan.createSpawnOldManEvent = spy.new(function() end)
                                        OldMan.saveOldManIdOnPlayer = spy.new(function() end)
                                end)
 
-                               it("Should spawn the old man if the player is in a place where the old man can be spawned.", function()
+                               it("Should try to spawn the old man if the player is in a place where the old man can be spawned.", function()
                                        OldMan:handleSpawnOldManEvent(pCreatureObject)
 
                                        assert.spy(OldMan.canOldManBeSpawned).was.called_with(pCreatureObject)
-                                       assert.spy(OldMan.spawnOldMan).was.called_with(pCreatureObject)
+                                       assert.spy(OldMan.tryToSpawnOldMan).was.called_with(pCreatureObject)
                                        assert.spy(OldMan.createSpawnOldManEvent).was.not_called()
                                end)
 
-                               it("Should save the object id of the spawned old man on the player.", function()
-                                       OldMan:handleSpawnOldManEvent(pCreatureObject)
-
-                                       assert.spy(OldMan.saveOldManIdOnPlayer).was.called_with(pCreatureObject, pOldMan)
-                               end)
-
                                it("Should reschedule the event if the player is not in a place where the old man can be spawned.", function()
                                        OldMan.canOldManBeSpawned = spy.new(function() return false end)
 
                                        OldMan:handleSpawnOldManEvent(pCreatureObject)
 
                                        assert.spy(OldMan.canOldManBeSpawned).was.called_with(pCreatureObject)
-                                       assert.spy(OldMan.spawnOldMan).was.not_called()
+                                       assert.spy(OldMan.tryToSpawnOldMan).was.not_called()
                                        assert.spy(OldMan.createSpawnOldManEvent).was.called_with(pCreatureObject)
                                end)
 
                                it("Should reschedule the event if the spawning of the old man fails.", function()
-                                       OldMan.spawnOldMan = spy.new(function() return nil end)
+                                       OldMan.tryToSpawnOldMan = spy.new(function() return false end)
 
                                        OldMan:handleSpawnOldManEvent(pCreatureObject)
 
                                        assert.spy(OldMan.canOldManBeSpawned).was.called_with(pCreatureObject)
-                                       assert.spy(OldMan.spawnOldMan).was.called_with(pCreatureObject)
+                                       assert.spy(OldMan.tryToSpawnOldMan).was.called_with(pCreatureObject)
                                        assert.spy(OldMan.createSpawnOldManEvent).was.called_with(pCreatureObject)
                                end)
                        end)
@@ -381,5 +382,160 @@ describe("Old Man", function()
                                end)
                        end)
                end)
+               
+               describe("tryToSpawnOldMan", function()
+                       describe("When called with a player creature object", function()
+                               local realSpawnOldMan
+                               local realSaveOldManIdOnPlayer
+                               local realSetToFollow
+                               
+                               setup(function()
+                                       realSpawnOldMan = OldMan.spawnOldMan
+                                       realSaveOldManIdOnPlayer = OldMan.saveOldManIdOnPlayer
+                                       realSetToFollow = OldMan.setToFollow
+                               end)
+                               
+                               teardown(function()
+                                       OldMan.spawnOldMan = realSpawnOldMan
+                                       OldMan.saveOldManIdOnPlayer = realSaveOldManIdOnPlayer
+                                       OldMan.setToFollow = realSetToFollow
+                               end)
+                               
+                               before_each(function()
+                                       OldMan.spawnOldMan = spy.new(function() end)
+                                       OldMan.saveOldManIdOnPlayer = spy.new(function() end)
+                                       OldMan.setToFollow = spy.new(function() end)
+                               end)
+                       
+                               it("Should spawn the old man.", function()
+                                       OldMan.tryToSpawnOldMan(pCreatureObject)
+                                       
+                                       assert.spy(OldMan.spawnOldMan).was.called_with(pCreatureObject)
+                               end)
+                               
+                               describe("and spawning the old man was successful", function()
+                                       before_each(function()
+                                               OldMan.spawnOldMan = spy.new(function() return pOldMan end)
+                                       end)
+                                       
+                                       it("Should save the id of the old man on the player.", function()
+                                               OldMan.tryToSpawnOldMan(pCreatureObject)
+                                               
+                                               assert.spy(OldMan.saveOldManIdOnPlayer).was.called_with(pCreatureObject, pOldMan)
+                                       end)
+                                       
+                                       it("Should return true.", function()
+                                               assert.is_true(OldMan.tryToSpawnOldMan(pCreatureObject))
+                                       end)
+                                       
+                                       it("Should set the old man to follow the player.", function()
+                                               OldMan.tryToSpawnOldMan(pCreatureObject)
+                                               
+                                               assert.spy(OldMan.setToFollow).was.called_with(pOldMan, pCreatureObject)
+                                       end)
+                                       
+                                       it("Should create an event for stopping the old man from following the player.", function()
+                                               OldMan.tryToSpawnOldMan(pCreatureObject)
+                                               
+                                               assert.spy(createEvent).was.called(1)
+                                       end)
+                               end)
+                               
+                               describe("and spawning the old man failed", function()
+                                       before_each(function()
+                                               OldMan.spawnOldMan = spy.new(function() return nil end)
+                                       end)
+                                       
+                                       it("Should not save the id of the old man on the player, not make the old man follow the player nor create an event.", function()
+                                               OldMan.tryToSpawnOldMan(pCreatureObject)
+                                               assert.spy(OldMan.saveOldManIdOnPlayer).was.not_called()
+                                               assert.spy(OldMan.setToFollow).was.not_called()
+                                               assert.spy(createEvent).was.not_called()
+                                       end)
+                                       
+                                       it("Should return false.", function()
+                                               assert.is_false(OldMan.tryToSpawnOldMan(pCreatureObject))
+                                       end)
+                               end)
+                       end)
+               end)
+               
+               describe("setToFollow", function()
+                       describe("When called with a old man and a player", function()
+                               it("Should set the old man to follow the player", function()
+                                       OldMan.setToFollow(pOldMan, pCreatureObject)
+                                       
+                                       assert.spy(aiAgent.setFollowObject).was.called_with(aiAgent, pCreatureObject)
+                               end)
+                       end)
+                       
+                       describe("When called with old man argument equal to nil", function()
+                               it("Should not do anything.", function()
+                                       OldMan.setToFollow(nil, pCreatureObject)
+                                       
+                                       assert.spy(aiAgent.setFollowObject).was.not_called()
+                               end)
+                       end)
+                       
+                       describe("When called with player argument equal to nil", function()
+                               it("Should not do anything.", function()
+                                       OldMan.setToFollow(pOldMan, nil)
+                                       
+                                       assert.spy(aiAgent.setFollowObject).was.called_with(aiAgent, nil)
+                               end)
+                       end)
+               end)
+               
+               describe("handleStopFollowPlayerEvent", function()
+                       local realSetToFollow
+                       local realReadOldManIdFromPlayer
+                       
+                       setup(function()
+                               realSetToFollow = OldMan.setToFollow
+                               realReadOldManIdFromPlayer = OldMan.readOldManIdFromPlayer
+                       end)
+                       
+                       teardown(function()
+                               OldMan.setToFollow = realSetToFollow
+                               OldMan.readOldManIdFromPlayer = realReadOldManIdFromPlayer
+                       end)
+                       
+                       before_each(function()
+                               OldMan.setToFollow = spy.new(function() end)
+                               OldMan.readOldManIdFromPlayer = spy.new(function() return oldManId end)
+                       end)
+                       
+                       describe("When called with the player as argument", function()
+                               it("Should read the old man id from the player.", function()
+                                       OldMan:handleStopFollowPlayerEvent(pCreatureObject)
+                                       
+                                       assert.spy(OldMan.readOldManIdFromPlayer).was.called_with(pCreatureObject)
+                               end)
+                               
+                               it("Should get a pointer to the old man.", function()
+                                       OldMan:handleStopFollowPlayerEvent(pCreatureObject)
+                                       
+                                       assert.spy(getSceneObject).was.called_with(oldManId)
+                               end)
+                               
+                               it("Should tell the old man to follow no one.", function()
+                                       OldMan:handleStopFollowPlayerEvent(pCreatureObject)
+                                       
+                                       assert.spy(OldMan.setToFollow).was.called_with(pOldMan, nil)
+                               end)
+                       end)
+               end)
+               
+               describe("readOldManIdFromPlayer", function()
+                       describe("When called with a pointer to a creature object of a player as argument", function()
+                               it("Should read the old man id from the player.", function()
+                                       OldMan.readOldManIdFromPlayer(pCreatureObject)
+                                       
+                                       assert.spy(readData).was.called_with(playerId .. OLD_MAN_ID_STRING)
+                                       assert.spy(LuaCreatureObject).was.called(1)
+                                       assert.spy(creatureObjectPlayer.getObjectID).was.called(1)
+                               end)
+                       end)
+               end)
        end)
 end)
index 88561db..eeb66e2 100644 (file)
@@ -1,10 +1,12 @@
 local realCreateEvent
 local realGetCityRegionAt
+local realGetSceneObject
 local realGetSpawnPoint
 local realReadData
 local realSpawnMobile
 local realWriteData
 
+local realLuaAiAgent
 local realLuaCityRegion
 local realLuaCreatureObject
 local realLuaPlayerObject
@@ -12,6 +14,7 @@ local realLuaSceneObject
 
 local DirectorManagerMocks = { }
 
+DirectorManagerMocks.aiAgents = {}
 DirectorManagerMocks.cityRegions = {}
 DirectorManagerMocks.creatureObjects = {}
 DirectorManagerMocks.playerObjects = {}
@@ -21,11 +24,13 @@ DirectorManagerMocks.sceneObjects = {}
 function DirectorManagerMocks.setup()
        realCreateEvent = createEvent
        realGetCityRegionAt = getCityRegionAt
+       realGetSceneObject = getSceneObject
        realGetSpawnPoint = getSpawnPoint
        realReadData = readData
        realSpawnMobile = spawnMobile
        realWriteData = writeData
 
+       realLuaAiAgent = LuaAiAgent
        realLuaCityRegion = LuaCityRegion
        realLuaCreatureObject = LuaCreatureObject
        realLuaPlayerObject = LuaPlayerObject
@@ -36,11 +41,13 @@ end
 function DirectorManagerMocks.teardown()
        createEvent = realCreateEvent
        getCityRegionAt = realGetCityRegionAt
+       getSceneObject = realGetSceneObject
        getSpawnPoint = realGetSpawnPoint
        readData = realReadData
        spawnMobile = realSpawnMobile
        writeData = realWriteData
 
+       LuaAiAgent = realLuaAiAgent 
        LuaCityRegion = realLuaCityRegion
        LuaCreatureObject = realLuaCreatureObject
        LuaPlayerObject = realLuaPlayerObject
@@ -51,11 +58,20 @@ end
 function DirectorManagerMocks.before_each()
        createEvent = spy.new(function() end)
        getCityRegionAt = spy.new(function() return nil end)
+       getSceneObject = spy.new(function() return nil end)
        getSpawnPoint = spy.new(function() return nil end)
        readData = spy.new(function() return nil end)
        spawnMobile = spy.new(function() return nil end)
        writeData = spy.new(function() end)
 
+       LuaAiAgent = spy.new(function(pAiAgent)
+               if pAiAgent == nil then
+                       assert.not_nil(pAiAgent)
+                       return nil
+               end
+               return DirectorManagerMocks.aiAgents[pAiAgent]
+       end)
+
        LuaCityRegion = spy.new(function(pCityRegion)
                if pCityRegion == nil then
                        assert.not_nil(pCityRegion)
index b4dfb1f..81e57bf 100644 (file)
@@ -78,6 +78,18 @@ function ScreenPlay:spawnContainerLoot(pContainer)
        writeData(container:getObjectID(), time + self.lootContainerRespawn)
 end
 
+-- Perform the supplied function with an ai agent created from the pointer.
+-- @param pCreatureObject a pointer to a creature object that is an AiAgent.
+-- @param performThisFunction a function that takes an ai agent as its argument.
+-- @return whatever performThisFunction returns or nil if the pCreatureObject pointer is nil.
+function ScreenPlay.withCreatureAiAgent(pCreatureObject, performThisFunction)
+       if pCreatureObject == nil then
+               return nil
+       end
+       local aiAgent = LuaAiAgent(pCreatureObject)
+       return performThisFunction(aiAgent)
+end
+
 -- Perform the supplied function with a scene object created from the pointer.
 -- @param pSceneObject a pointer to a scene object.
 -- @param performThisFunction a function that takes a scene object as its argument.
index 41913e6..0ae4d66 100644 (file)
@@ -2,6 +2,9 @@ package.path = package.path .. ";scripts/screenplays/?.lua"
 require("screenplay")
 
 describe("ScreenPlay", function()
+       it("Shall return nil if the creature object pointer is nil to the withCreatureAiAgent function.", function()
+               assert.is.Nil(ScreenPlay.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(ScreenPlay.withSceneObject(nil, function(sceneObject) end))
        end)
@@ -26,6 +29,19 @@ describe("ScreenPlay", function()
                assert.is.Nil(ScreenPlay.withCreatureAndPlayerObject(nil, function(creatureObject, playerObject) end))
        end)
 
+       it("Shall call the supplied lambda expression with the AiAgent when calling the withCreatureAiAgent function.", function()
+               local pCreatureObject = { "creatureObjectPointer" }
+               local aiAgentCreated = { "sceneObject" }
+               local aiAgentArgument = nil
+               local realLuaAiAgent = LuaAiAgent
+               LuaAiAgent = spy.new(function() return aiAgentCreated end)
+
+               ScreenPlay.withCreatureAiAgent(pCreatureObject, function(aiAgent) aiAgentArgument = aiAgent end)
+               assert.same(aiAgentArgument, aiAgentCreated)
+
+               LuaAiAgent = realLuaAiAgent
+       end)
+
        it("Shall call the supplied lambda expression with the SceneObject when calling the withSceneObject function.", function()
                local pSceneObject = { "sceneObjectPointer" }
                local sceneObjectCreated = { "sceneObject" }