Changeset 1207

Show
Ignore:
Timestamp:
03/07/2010 10:47:08 AM (6 months ago)
Author:
Paroxysm
Message:

FIXED :

  • Crashes related to invalid references being used.
  • GetRandomEnemy/Friend? should function correctly.
  • LCF:RegisterLuaEvent shouldn't treat repeats as unlimited.

ADDED :

  • Unit/GameObject:GetObject(guid)
  • LCF_Creature/Player/GameObject:GetLocalCreature/GameObject(entry).
  • LCF_Creature/GameObject:GetCreator(target)/SetCreator()

  • Extracted a dump of all functions each wow object has, none of the functions are documented though, it's just a list.

** Began rewrite of those shitty scripts in /lua folder.

Location:
Trunk
Files:
7 added
4 removed
9 modified

Legend:

Unmodified
Added
Removed
  • Trunk/LUA_ENGINE_COMMANDS.txt

    r1190 r1207  
    461461                - Note : if you want to register a Lua METHOD, then you will have to pass in LCF:BindMethod(method_name,methods_table) for func. method being the lua function and methods_table being the TABLE, method is located in. This goes for the other classes as well. 
    462462        RemoveLuaEvent(func) -- removes all events currently registered under the invoking player. Specify func to remove only those events who invoke func. 
    463         FaceUnit(tar) - makes the Creature face the target specified. 
    464         GetNearestUnitWithEntry(entry) - returns the nearest unit to the creature with the specified entry. 
     463        FaceUnit(tar) - makes the Creature face the target specified. 
     464    GetNearestUnitWithEntry(entry) - returns the nearest unit to the creature with the specified entry. 
     465        GetLocalCreature(creature_entry) - simply calls GetCreatureNearestCoords with the unit's position. 
     466        GetLocalGameObject(go_entry) 
     467        GetCreator() - returns the unit whos guid is stored in UNIT_FIELD_CREATEDBY/SUMMONEDBY 
     468        SetCreator(creator_obj) - sets UNIT_FIELD_CREATEDBY/SUMMONEDBY to creator_obj:GetGUID() 
    465469 Player: 
    466470        GetStrength() 
     
    475479        RegisterLuaEvent(func,delay,repeats,extra_args_go_here) - same as creature 
    476480        RemoveLuaEvent(func) -- same as creature 
    477         GetNearestUnitWithEntry(entry) - same as creature 
     481    GetNearestUnitWithEntry(entry) - same as creature 
     482        GetLocalCreature(creature_entry) - simply calls GetCreatureNearestCoords with the player's position. 
     483        GetLocalGameObject(go_entry) 
    478484 GameObject: 
    479485        SetClickable() 
     
    488494        RegisterLuaEvent(func,delay,repeats,extra_args_go_here) - same as creature. 
    489495        RemoveLuaEvent(func) - same as creature 
     496        GetCreator() - returns the unit whos guid is stored in OBJECT_FIELD_CREATEDBY 
     497        SetCreator(creator_obj) - sets OBJECT_FIELD_CREATEDBY to creator_obj:GetGUID() 
     498        GetLocalCreature(creature_entry) - simply calls GetCreatureNearestCoords with the go's position. 
     499        GetLocalGameObject(go_entry) 
    490500EXTRA METHODS 
    491501        LCF:RegisterZoneHook(zone_id,func) - this takes the desired zone id and a lua function value(not a string unlike RegisterUnitEvent...) It will then call this function when the server hook with the zone id triggers 
  • Trunk/lua/Misc/0LCF_Includes/LCF_Creature.lua

    r1190 r1207  
    2020local CREATURE = LCF.CreatureMethods 
    2121assert(CREATURE) 
     22--[[CREATURE.spell_events = {} 
     23CREATURE.CAST_FLAG_ON_SELF =  0x0 
     24CREATURE.CAST_FLAG_ON_TARGET = 0x1 
     25CREATURE.CAST_FLAG_DEST = 0x2 
     26CREATURE.CAST_FLAG_ALL_PLAYERS = 0x3 
     27CREATURE.CAST_FLAG_FRIENDLY_UNITS = 0x4 
     28CREATURE.CAST_FLAG_ENEMY_UNITS = 0x5 
     29CREATURE.CAST_ATTRIBUTE_INSTANT = 0x8 -- or 1000B]] 
    2230 
    2331function CREATURE:SetUnselectable() 
     
    6876        self:SendChatMessage(LCF.CHAT_MSG_MONSTER_EMOTE,LCF.LANG_UNIVERSAL,msg) 
    6977end 
     78function CREATURE:MonsterWhisper(msg) 
     79        self:SendChatMessage(LCF.CHAT_MSG_MONSTER_WHISPER,LCF.LANG_UNIVERSAL,msg) 
     80end 
    7081function CREATURE:MoveToUnit(target) 
    7182        local x,y,z = target:GetLocation() 
     
    8293        LCF:RemoveLuaEvent(tostring(self),func) 
    8394end 
    84 function CREATURE:GetNearestUnitWithEntry(entry) 
    85    return (self:GetCreatureNearestCoords(entry, self:GetX(), self:GetY(), self:GetZ(), self:GetO())) 
     95function CREATURE:IsCasting() 
     96        return self:GetCurrentSpell() ~= nil 
    8697end 
    87 function CREATURE:FaceUnit(tar) 
    88    if (type(tar) == "userdata" and tar:GetObjectType() == "Unit") 
    89       self:SetFacing(self:CalcRadAngle(self:GetX(), self:GetY(), tar:GetX(), tar:GetY())) 
    90       return true 
    91    else 
    92       return nil 
    93    end 
     98function CREATURE:SetCreator(creator) 
     99        self:SetUInt64Value(LCF.UNIT_FIELD_CREATEDBY,creator:GetGUID()) 
    94100end 
     101function CREATURE:GetCreator() 
     102        local creator_guid = self:GetUInt64Value(LCF.UNIT_FIELD_CREATEDBY) 
     103        if(creator_guid == nil) then 
     104                creator_guid = self:GetUInt64Value(LCF.UNIT_FIELD_SUMMONEDBY) 
     105        end 
     106        return self:GetObject(creator_guid) 
     107end 
     108function CREATURE:GetLocalCreature(entry) 
     109        local x,y,z = self:GetLocation() 
     110        local crc = self:GetCreatureNearestCoords(x,y,z,entry) 
     111        return crc 
     112end 
     113function CREATURE:GetLocalGameObject(entry) 
     114        local x,y,z = self:GetLocation() 
     115        local go = self:GetGameObjectNearestCoords(x,y,z,entry) 
     116        return go 
     117end 
  • Trunk/lua/Misc/0LCF_Includes/LCF_Extra.lua

    r1174 r1207  
    110110        if(type(delay) ~= "number") then error("Invalid argument #2, expected number got ->"..type(delay)) end 
    111111        if(type(repeats) ~= "number") then error("Invalid argument #3, expected number got->"..type(repeats)) end 
    112         assert(type(arg) == "table","Interval error, arg is not a table.") 
     112        assert(type(arg) == "table","Internal error, arg is not a table.") 
    113113        if(delay <= 0) then error("Invalid argument #2, delay must be greater than 0.") end 
    114         local struct = {evt_typ,{func,delay,interval,repeats,arg} } 
     114        local struct = {evt_typ,{func,delay,repeats,repeats,arg} } 
    115115        table.insert(self.call_backs,struct) 
    116116end 
     
    149149        return function(...) return method(method_table,unpack(arg)) end 
    150150end 
    151          
    152          
    153 RegisterServerHook(15,"LCF:HandleOnZone") 
    154 RegisterServerHook(26,"LCF:HandleOnAreaTrigger") 
     151RegisterServerHook(15,LCF:BindMethod(LCF.HandleOnZone,LCF)) 
     152RegisterServerHook(26,LCF:BindMethod(LCF.HandleOnAreaTrigger,LCF)) 
    155153RegisterTimedEvent("LCF:UpdateCallBacks",100,0) 
  • Trunk/lua/Misc/0LCF_Includes/LCF_Gameobject.lua

    r1174 r1207  
    6262        LCF:RemoveLuaEvent(tostring(self),func) 
    6363end 
     64function GAMEOBJECT:SetCreator(creator) 
     65        local guid = creator:GetGUID() 
     66        self:SetUInt64Value(LCF.OBJECT_FIELD_CREATED_BY,guid) 
     67end 
     68function GAMEOBJECT:GetCreator() 
     69        local guid = self:GetUInt64Value(LCF.OBJECT_FIELD_CREATED_BY) 
     70        return self:GetObject(guid) 
     71end 
     72function GAMEOBJECT:GetLocalCreature(entry) 
     73        local x,y,z = self:GetLocation() 
     74        local crc = self:GetCreatureNearestCoords(x,y,z,entry) 
     75        return crc 
     76end 
     77function GAMEOBJECT:GetLocalGameObject(entry) 
     78        local x,y,z = self:GetLocation() 
     79        local go = self:GetGameObjectNearestCoords(x,y,z,entry) 
     80        return go 
     81end 
     82function GAMEOBJECT:GetCreator() 
     83        local creator_guid = self:GetUInt64Value(LCF.UNIT_FIELD_CREATEDBY) 
     84        if(creator_guid == nil) then 
     85                creator_guid = self:GetUInt64Value(LCF.UNIT_FIELD_SUMMONEDBY) 
     86        end 
     87        return self:GetObject(creator_guid) 
     88end 
  • Trunk/lua/Misc/0LCF_Includes/LCF_Player.lua

    r1190 r1207  
    6666        LCF:RemoveLuaEvent(tostring(self),func) 
    6767end 
    68 function PLAYER:GetNearestUnitWithEntry(entry) 
    69    return (self:GetCreatureNearestCoords(entry, self:GetX(), self:GetY(), self:GetZ(), self:GetO())) 
     68function PLAYER:IsCasting() 
     69        return player:GetCurrentSpell() ~= nil 
    7070end 
     71function PLAYER:GetLocalCreature(entry) 
     72        local x,y,z = self:GetLocation() 
     73        local crc = self:GetCreatureNearestCoords(x,y,z,entry) 
     74        return crc 
     75end 
     76function PLAYER:GetLocalGameObject(entry) 
     77        local x,y,z = self:GetLocation() 
     78        local go = self:GetGameObjectNearestCoords(x,y,z,entry) 
     79        return go 
     80end 
  • Trunk/src/LuaEngine/FunctionTables.h

    r1190 r1207  
    524524        { "GetSpawnLocation", &luaUnit::GetSpawnLocation }, 
    525525        { "GetPlayerMovementFlags", &luaUnit::GetPlayerMovementFlags}, 
     526        { "GetObject", &luaUnit::GetObject }, 
    526527        { NULL, NULL }, 
    527528}; 
     
    623624        { "GetLocation", &luaGameObject::GetLocation }, 
    624625        { "GetSpawnLocation", &luaGameObject::GetSpawnLocation }, 
     626        { "GetObject", &luaGameObject::GetObject }, 
    625627        { NULL, NULL }, 
    626628}; 
  • Trunk/src/LuaEngine/GameobjectFunctions.h

    r1174 r1207  
    10731073                return 4; 
    10741074        } 
     1075        int GetObject(lua_State * L, GameObject * ptr) 
     1076        { 
     1077                TEST_GO(); 
     1078                uint64 guid = CHECK_GUID(L,1); 
     1079                Object * obj = ptr->GetMapMgr()->_GetObject(guid); 
     1080                if(obj != NULL && obj->IsUnit() ) 
     1081                        PUSH_UNIT(L,obj); 
     1082                else if(obj != NULL && obj->IsGameObject() ) 
     1083                        PUSH_GO(L,obj); 
     1084                else 
     1085                        lua_pushnil(L); 
     1086                return 1; 
     1087        } 
    10751088} 
    10761089#endif 
  • Trunk/src/LuaEngine/LUAEngine.cpp

    r1206 r1207  
    236236void LuaEngine::BeginCall(uint16 fReference) 
    237237{ 
    238         if(fReference > 0) 
    239         { 
    240                 lua_settop(lu,0); //stack should be empty 
    241                 lua_rawgeti(lu,LUA_REGISTRYINDEX,fReference); 
    242                 if(!lua_isfunction(lu,-1) ) 
    243                         luaL_error(lu,"Attempted to call invalid function. \n"); 
    244         } 
     238        lua_settop(lu,0); //stack should be empty 
     239        lua_getref(lu,fReference); 
    245240} 
    246241bool LuaEngine::ExecuteCall(uint8 params, uint8 res) 
     
    556551                } 
    557552        } 
    558         if(functionRef) 
     553        if(functionRef > 0) 
    559554                sLuaMgr.RegisterEvent(REGTYPE_SERVHOOK,0,ev,functionRef); 
    560555        return 0; 
     
    627622                } 
    628623        } 
    629         if(functionRef) 
     624        if(functionRef > 0) 
    630625                sLuaMgr.RegisterEvent(REGTYPE_DUMMYSPELL,entry,0,functionRef); 
    631626        return 0; 
     
    692687                } 
    693688        } 
    694         if(functionRef) 
     689        if(functionRef > 0) 
    695690                sLuaMgr.RegisterEvent(REGTYPE_UNIT,entry,ev,functionRef); 
    696691        return 0; 
     
    757752                } 
    758753        } 
    759         if(functionRef) 
     754        if(functionRef > 0) 
    760755                sLuaMgr.RegisterEvent(REGTYPE_QUEST,entry,ev,functionRef); 
    761756        return 0; 
     
    822817                } 
    823818        } 
    824         if(functionRef) 
     819        if(functionRef > 0) 
    825820                sLuaMgr.RegisterEvent(REGTYPE_GO,entry,ev,functionRef); 
    826821        return 0; 
     
    887882                } 
    888883        } 
    889         if(functionRef) 
     884        if(functionRef > 0) 
    890885                sLuaMgr.RegisterEvent(REGTYPE_UNIT_GOSSIP,entry,ev,functionRef); 
    891886        return 0; 
     
    951946                } 
    952947        } 
    953         if(functionRef) 
     948        if(functionRef > 0) 
    954949                sLuaMgr.RegisterEvent(REGTYPE_ITEM_GOSSIP,entry,ev,functionRef); 
    955950        return 0; 
     
    10121007                } 
    10131008        } 
    1014         if(functionRef) 
     1009        if(functionRef > 0) 
    10151010                sLuaMgr.RegisterEvent(REGTYPE_GO_GOSSIP,entry,ev,functionRef); 
    10161011        return 0; 
     
    25402535                                                } 
    25412536                                                else 
     2537                                                { 
     2538                                                        if(bind->m_functionReferences[evt] > 0) 
     2539                                                                lua_unref(lu,bind->m_functionReferences[evt]); 
    25422540                                                        bind->m_functionReferences[evt] = functionRef; 
     2541                                                } 
    25432542                                        } 
    25442543                                }break; 
     
    25542553                                                } 
    25552554                                                else 
     2555                                                { 
     2556                                                        if(bind->m_functionReferences[evt] > 0) 
     2557                                                                lua_unref(lu,bind->m_functionReferences[evt]); 
    25562558                                                        bind->m_functionReferences[evt] = functionRef; 
     2559                                                } 
    25572560                                        } 
    25582561                                }break; 
     
    25682571                                                } 
    25692572                                                else 
     2573                                                { 
     2574                                                        if(bind->m_functionReferences[evt] > 0) 
     2575                                                                lua_unref(lu,bind->m_functionReferences[evt]); 
    25702576                                                        bind->m_functionReferences[evt] = functionRef; 
     2577                                                } 
    25712578                                        } 
    25722579                                }break; 
     
    25922599                                                } 
    25932600                                                else 
     2601                                                { 
     2602                                                        if(bind->m_functionReferences[evt] > 0) 
     2603                                                                lua_unref(lu,bind->m_functionReferences[evt]); 
    25942604                                                        bind->m_functionReferences[evt] = functionRef; 
     2605                                                } 
    25952606                                        } 
    25962607                                }break; 
     
    26062617                                                } 
    26072618                                                else 
     2619                                                { 
     2620                                                        if(bind->m_functionReferences[evt] > 0) 
     2621                                                                lua_unref(lu,bind->m_functionReferences[evt]); 
    26082622                                                        bind->m_functionReferences[evt] = functionRef; 
     2623                                                } 
    26092624                                        } 
    26102625                                }break; 
     
    26202635                                                } 
    26212636                                                else 
     2637                                                { 
     2638                                                        if(bind->m_functionReferences[evt] > 0) 
     2639                                                                lua_unref(lu,bind->m_functionReferences[evt]); 
    26222640                                                        bind->m_functionReferences[evt] = functionRef; 
     2641                                                } 
    26232642                                        } 
    26242643                                }break; 
     
    26312650        RemoveTimedEvents(lu); 
    26322651        // clean up the engine of any existing defined variables 
     2652        for(UnitBindingMap::iterator itr = m_unitBinding.begin(); itr != m_unitBinding.end(); ++itr) 
     2653        { 
     2654                for(int i = 0; i < CREATURE_EVENT_COUNT; ++i) 
     2655                { 
     2656                        if(itr->second.m_functionReferences[i] > 0) 
     2657                                lua_unref(lu,itr->second.m_functionReferences[i]); 
     2658                } 
     2659        } 
    26332660        m_unitBinding.clear(); 
     2661        for(GameObjectBindingMap::iterator itr = m_gameobjectBinding.begin(); itr != m_gameobjectBinding.end(); ++itr) 
     2662        { 
     2663                for(int i = 0; i < GAMEOBJECT_EVENT_COUNT; ++i) 
     2664                { 
     2665                        if(itr->second.m_functionReferences[i] > 0) 
     2666                                lua_unref(lu,itr->second.m_functionReferences[i]); 
     2667                } 
     2668        } 
    26342669        m_gameobjectBinding.clear(); 
     2670        for(QuestBindingMap::iterator itr = m_questBinding.begin(); itr != m_questBinding.end(); ++itr) 
     2671        { 
     2672                for(int i = 0; i < QUEST_EVENT_COUNT; ++i) 
     2673                { 
     2674                        if(itr->second.m_functionReferences[i] > 0) 
     2675                                lua_unref(lu,itr->second.m_functionReferences[i]); 
     2676                } 
     2677        } 
    26352678        m_questBinding.clear(); 
     2679        for(GossipUnitScriptsBindingMap::iterator itr = m_unit_gossipBinding.begin(); itr != m_unit_gossipBinding.end(); ++itr) 
     2680        { 
     2681                for(int i = 0; i < GOSSIP_EVENT_COUNT; ++i) 
     2682                { 
     2683                        if(itr->second.m_functionReferences[i] > 0) 
     2684                                lua_unref(lu,itr->second.m_functionReferences[i]); 
     2685                } 
     2686        } 
    26362687        m_unit_gossipBinding.clear(); 
     2688        for(GossipItemScriptsBindingMap::iterator itr = m_item_gossipBinding.begin(); itr != m_item_gossipBinding.end(); ++itr) 
     2689        { 
     2690                for(int i = 0; i < GOSSIP_EVENT_COUNT; ++i) 
     2691                { 
     2692                        if(itr->second.m_functionReferences[i] > 0) 
     2693                                lua_unref(lu,itr->second.m_functionReferences[i]); 
     2694                } 
     2695        } 
    26372696        m_item_gossipBinding.clear(); 
     2697        for(GossipGOScriptsBindingMap::iterator itr = m_go_gossipBinding.begin(); itr != m_go_gossipBinding.end(); ++itr) 
     2698        { 
     2699                for(int i = 0; i < GOSSIP_EVENT_COUNT; ++i) 
     2700                { 
     2701                        if(itr->second.m_functionReferences[i] > 0) 
     2702                                lua_unref(lu,itr->second.m_functionReferences[i]); 
     2703                } 
     2704        } 
    26382705        m_go_gossipBinding.clear(); 
    26392706        //Serv hooks : had forgotten these. 
     
    26412708        { 
    26422709                vector<uint16> & next = EventAsToFuncName[i]; 
     2710                for(vector<uint16>::iterator itr = next.begin(); itr != next.end(); ++itr) 
     2711                        lua_unref(lu,(*itr)); 
    26432712                next.clear(); 
    26442713        } 
     2714        for(map<uint32,uint16>::iterator itr = m_luaDummySpells.begin(); itr != m_luaDummySpells.end(); ++itr) 
     2715                lua_unref(lu,itr->second); 
    26452716        m_luaDummySpells.clear(); 
     2717        for(set<int>::iterator itr = m_pendingThreads.begin(); itr != m_pendingThreads.end(); ++itr) 
     2718                lua_unref(lu,(*itr)); 
    26462719        m_pendingThreads.clear(); 
    26472720 
  • Trunk/src/LuaEngine/UnitFunctions.h

    r1205 r1207  
    886886                int flag = luaL_checkint( L, 1 ); 
    887887                Player* ret = NULL; 
     888                vector<Player*> players; 
    888889                switch( flag ) 
    889890                { 
     
    907908 
    908909                        { 
    909                                 uint32 count = 0; 
    910910                                for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    911911                                { 
    912912                                        Player* obj = TO_PLAYER(*itr); 
    913913                                        if (obj && obj->CalcDistance(obj,ptr)<=8) 
    914                                                 ++count; 
     914                                                players.push_back(obj); 
    915915                                } 
    916                                 if (count) 
    917                                 { 
    918                                         uint32 r = RandomUInt(count-1); 
    919                                         count=0; 
    920                                         for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    921                                         { 
    922                                                 Player* obj = TO_PLAYER(*itr); 
    923                                                 if (obj && obj->CalcDistance(obj,ptr)<=8 && count==r) 
    924                                                 { 
    925                                                         ret=obj; 
    926                                                         break; 
    927                                                 } 
    928                                                 ++count; 
    929                                         } 
    930                                 } 
     916                                ret = players[RandomUInt(players.size()-1)]; 
    931917                        } 
    932918                        break; 
    933919                case RANDOM_IN_MIDRANGE: 
    934920                        { 
    935                                 uint32 count = 0; 
    936921                                for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    937922                                { 
    938923                                        Player* obj = TO_PLAYER(*itr); 
    939                                         if (!obj) 
    940                                                 continue; 
    941924                                        float distance = obj->CalcDistance(obj,ptr); 
    942925                                        if (distance<20 && distance>8) 
    943                                                 ++count; 
     926                                                players.push_back(obj); 
    944927                                } 
    945                                 if (count) 
    946                                 { 
    947                                         uint32 r = RandomUInt(count-1); 
    948                                         count=0; 
    949                                         for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    950                                         { 
    951                                                 Player* obj = TO_PLAYER(*itr); 
    952                                                 if (!obj) 
    953                                                         continue; 
    954                                                 float distance = obj->CalcDistance(obj,ptr); 
    955                                                 if (distance<20 && distance>8 && count==r) 
    956                                                 { 
    957                                                         ret=obj; 
    958                                                         break; 
    959                                                 } 
    960                                                 ++count; 
    961                                         } 
    962                                 } 
     928                                ret = players[RandomUInt(players.size()-1)]; 
    963929                        } 
    964930                        break; 
    965931                case RANDOM_IN_LONGRANGE: 
    966932                        { 
    967                                 uint32 count = 0; 
    968933                                for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    969934                                { 
    970935                                        Player* obj = TO_PLAYER(*itr); 
    971936                                        if (obj && obj->CalcDistance(obj,ptr)>=20) 
    972                                                 ++count; 
     937                                                players.push_back(obj); 
    973938                                } 
    974                                 if (count) 
    975                                 { 
    976                                         uint32 r = RandomUInt(count-1); 
    977                                         count=0; 
    978                                         for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    979                                         { 
    980                                                 Player* obj = TO_PLAYER(*itr); 
    981                                                 if (obj && obj->CalcDistance(obj,ptr)>=20 && count==r) 
    982                                                 { 
    983                                                         ret=obj; 
    984                                                         break; 
    985                                                 } 
    986                                                 ++count; 
    987                                         } 
    988                                 } 
     939                                ret = players[RandomUInt(players.size()-1)]; 
    989940                        } 
    990941                        break; 
    991942                case RANDOM_WITH_MANA: 
    992943                        { 
    993                                 uint32 count = 0; 
    994944                                for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    995945                                { 
    996946                                        Player* obj = TO_PLAYER(*itr); 
    997947                                        if (obj && obj->GetPowerType() == POWER_TYPE_MANA) 
    998                                                 ++count; 
     948                                                players.push_back(obj); 
    999949                                } 
    1000                                 if (count) 
    1001                                 { 
    1002                                         uint32 r = RandomUInt(count-1); 
    1003                                         count=0; 
    1004                                         for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    1005                                         { 
    1006                                                 Player* obj = TO_PLAYER(*itr); 
    1007                                                 if (obj && obj->GetPowerType() == POWER_TYPE_MANA && count==r) 
    1008                                                 { 
    1009                                                         ret=obj; 
    1010                                                         break; 
    1011                                                 } 
    1012                                                 ++count; 
    1013                                         } 
    1014                                 } 
     950                                ret = players[RandomUInt(players.size()-1)]; 
    1015951                        } 
    1016952                        break; 
    1017953                case RANDOM_WITH_ENERGY: 
    1018954                        { 
    1019                                 uint32 count = 0; 
    1020955                                for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    1021956                                { 
    1022957                                        Player* obj = TO_PLAYER(*itr); 
    1023958                                        if (obj && obj->GetPowerType() == POWER_TYPE_ENERGY) 
    1024                                                 ++count; 
     959                                                players.push_back(obj); 
    1025960                                } 
    1026                                 if (count) 
    1027                                 { 
    1028                                         uint32 r = RandomUInt(count-1); 
    1029                                         count=0; 
    1030                                         for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    1031                                         { 
    1032                                                 Player* obj = TO_PLAYER(*itr); 
    1033                                                 if (obj && obj->GetPowerType() == POWER_TYPE_ENERGY && count==r) 
    1034                                                 { 
    1035                                                         ret=obj; 
    1036                                                         break; 
    1037                                                 } 
    1038                                                 ++count; 
    1039                                         } 
    1040                                 } 
     961                                ret = players[RandomUInt(players.size()-1)]; 
    1041962                        } 
    1042963                        break; 
    1043964                case RANDOM_WITH_RAGE: 
    1044965                        { 
    1045                                 uint32 count = 0; 
    1046966                                for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    1047967                                { 
    1048968                                        Player* obj = TO_PLAYER(*itr); 
    1049969                                        if (obj && obj->GetPowerType() == POWER_TYPE_RAGE) 
    1050                                                 ++count; 
     970                                                players.push_back(obj); 
    1051971                                } 
    1052                                 if (count) 
    1053                                 { 
    1054                                         uint32 r = RandomUInt(count-1); 
    1055                                         count=0; 
    1056                                         for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    1057                                         { 
    1058                                                 Player* obj = TO_PLAYER(*itr); 
    1059                                                 if (obj && obj->GetPowerType() == POWER_TYPE_RAGE && count==r) 
    1060                                                 { 
    1061                                                         ret=obj; 
    1062                                                         break; 
    1063                                                 } 
    1064                                                 ++count; 
    1065                                         } 
    1066                                 } 
     972                                ret = players[RandomUInt(players.size()-1)]; 
    1067973                        } 
    1068974                        break; 
     
    1078984                                        Player* obj = TO_PLAYER(*itr); 
    1079985                                        if (obj != mt) 
    1080                                                 ++count; 
     986                                                players.push_back(obj); 
    1081987                                } 
    1082                                 if (count) 
    1083                                 { 
    1084                                         uint32 r = RandomUInt(count-1); 
    1085                                         count=0; 
    1086                                         for(set< Object* >::iterator itr = ptr->GetInRangePlayerSetBegin(); itr != ptr->GetInRangePlayerSetEnd(); ++itr) 
    1087                                         { 
    1088                                                 Player* obj = TO_PLAYER(*itr); 
    1089                                                 if (obj && obj != mt && count==r) 
    1090                                                 { 
    1091                                                         ret=obj; 
    1092                                                         break; 
    1093                                                 } 
    1094                                                 ++count; 
    1095                                         } 
    1096                                 } 
     988                                ret = players[RandomUInt(players.size()-1)]; 
    1097989                        } 
    1098990                        break; 
     
    11081000        int GetRandomFriend(lua_State * L, Unit * ptr) 
    11091001        { 
    1110                 if(!ptr) 
    1111                         return 0; 
     1002                TEST_UNITPLAYER(); 
    11121003 
    11131004                Unit * ret=NULL; 
    11141005                uint32 count = 0; 
     1006                vector<Object*> allies; 
    11151007 
    11161008                for(set<Object*>::iterator itr = ptr->GetInRangeSetBegin(); itr != ptr->GetInRangeSetEnd(); ++itr) 
     
    11181010                        Object* obj = TO_OBJECT(*itr); 
    11191011                        if (obj->IsUnit() && isFriendly(obj,ptr)) 
    1120                                 ++count; 
    1121                 } 
    1122  
    1123                 if (count) 
    1124                 { 
    1125                         uint32 r = RandomUInt(count-1); 
    1126                         count=0; 
    1127                         for(set<Object*>::iterator itr = ptr->GetInRangeSetBegin(); itr != ptr->GetInRangeSetEnd(); ++itr) 
    1128                         { 
    1129                                 Object* obj = TO_OBJECT(*itr); 
    1130                                 if (obj->IsUnit() && isFriendly(obj,ptr) && count==r) 
    1131                                 { 
    1132                                         ret= TO_UNIT(obj); 
    1133                                         break; 
    1134                                 } 
    1135                                 ++count; 
    1136                         } 
    1137                 } 
    1138                 if(ret==NULL) 
    1139                         lua_pushnil(L); 
    1140                 else 
    1141                         PUSH_UNIT(L,ret); 
     1012                                allies.push_back(obj); 
     1013                } 
     1014                PUSH_UNIT(L,allies[RandomUInt(allies.size()-1)]); 
    11421015                return 1; 
    11431016        } 
    11441017        int GetRandomEnemy(lua_State * L, Unit * ptr) 
    11451018        { 
    1146                 if(!ptr) 
    1147                         return 0; 
     1019                TEST_UNITPLAYER(); 
    11481020 
    11491021                Unit * ret=NULL; 
    11501022                uint32 count = 0; 
     1023                vector<Object*> enemies; 
    11511024 
    11521025                for(set<Object*>::iterator itr = ptr->GetInRangeSetBegin(); itr != ptr->GetInRangeSetEnd(); ++itr) 
     
    11541027                        Object* obj = TO_OBJECT(*itr); 
    11551028                        if (obj->IsUnit() && !isFriendly(obj,ptr)) 
    1156                                 ++count; 
    1157                 } 
    1158  
    1159                 if (count) 
    1160                 { 
    1161                         uint32 r = RandomUInt(count-1); 
    1162                         count=0; 
    1163                         for(set<Object*>::iterator itr = ptr->GetInRangeSetBegin(); itr != ptr->GetInRangeSetEnd(); ++itr) 
    1164                         { 
    1165                                 Object* obj = TO_OBJECT(*itr); 
    1166                                 if (obj->IsUnit() && !isFriendly(obj,ptr) && count==r) 
    1167                                 { 
    1168                                         ret= TO_UNIT(obj); 
    1169                                         break; 
    1170                                 } 
    1171                                 ++count; 
    1172                         } 
    1173                 } 
    1174                 if(ret==NULL) 
    1175                         lua_pushnil(L); 
    1176                 else 
    1177                         PUSH_UNIT(L,ret); 
     1029                                enemies.push_back(obj); 
     1030                } 
     1031                PUSH_UNIT(L,enemies[RandomUInt(enemies.size()-1)]); 
    11781032                return 1; 
    11791033        } 
     
    11891043        int RemoveAura(lua_State * L, Unit * ptr) 
    11901044        { 
    1191                 if (!ptr) return 0; 
     1045                TEST_UNITPLAYER() 
    11921046                int auraid = luaL_checkint(L,1); 
    11931047                ptr->RemoveAura(auraid); 
     
    11971051        int CanAttack(lua_State * L, Unit * ptr) 
    11981052        { 
     1053                TEST_UNITPLAYER_RET(); 
    11991054                Unit * target = CHECK_UNIT(L,1); 
    1200                 if (!ptr || !target) return 0; 
     1055                if (!target) return 0; 
    12011056                if (isAttackable(ptr, target)) 
    12021057                        lua_pushboolean(L, 1); 
     
    12081063        int PlaySoundToSet(lua_State * L, Unit * ptr) 
    12091064        { 
    1210                 if (!ptr) return 0; 
     1065                TEST_UNITPLAYER(); 
    12111066                int soundid = luaL_checkint(L,1); 
    12121067                ptr->PlaySoundToSet(soundid); 
     
    12161071        int PlaySoundToPlayer(lua_State * L, Unit * ptr) 
    12171072        { 
     1073                TEST_PLAYER(); 
    12181074                int soundid = luaL_checkint(L,1); 
    12191075                Player* plr = TO_PLAYER(ptr); 
    1220                 if (!plr) return 0; 
    12211076                WorldPacket data; 
    12221077        data.Initialize(SMSG_PLAY_OBJECT_SOUND); 
     
    59985853                return 4; 
    59995854        } 
     5855        int GetObject(lua_State * L, Unit * ptr) 
     5856        { 
     5857                TEST_UNIT(); 
     5858                uint64 guid = CHECK_GUID(L,1); 
     5859                Object * obj = ptr->GetMapMgr()->_GetObject(guid); 
     5860                if(obj != NULL && obj->IsUnit() ) 
     5861                        PUSH_UNIT(L,obj); 
     5862                else if(obj != NULL && obj->IsGameObject() ) 
     5863                        PUSH_GO(L,obj); 
     5864                else 
     5865                        lua_pushnil(L); 
     5866                return 1; 
     5867        } 
    60005868} 
    60015869#endif