From ceb051cb7555a0dfa43c928f8dc94bce51e96cab Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Fri, 27 Apr 2012 03:30:54 -0300 Subject: [PATCH] finally reloadable vip, skills, inventory, chat and minimap --- modules/game_console/console.lua | 2 +- modules/game_inventory/inventory.lua | 41 ++++++++++++---------------- modules/game_minimap/minimap.lua | 9 ++++-- modules/game_skills/skills.lua | 20 ++++++++++++++ modules/game_viplist/viplist.lua | 9 ++++++ src/otclient/const.h | 15 ++++++++++ src/otclient/core/game.cpp | 5 +++- src/otclient/core/game.h | 5 ++++ src/otclient/core/localplayer.cpp | 10 +++++++ src/otclient/core/localplayer.h | 3 ++ src/otclient/luafunctions.cpp | 3 ++ src/otclient/ui/uimap.cpp | 4 ++- 12 files changed, 98 insertions(+), 28 deletions(-) diff --git a/modules/game_console/console.lua b/modules/game_console/console.lua index 01106acc..b526a71d 100644 --- a/modules/game_console/console.lua +++ b/modules/game_console/console.lua @@ -113,7 +113,7 @@ end local function onOpenPrivateChannel(receiver) local privateTab = Console.getTab(receiver) if privateTab == nil then - Console.addTab(receiver) + Console.addTab(receiver, true) end end diff --git a/modules/game_inventory/inventory.lua b/modules/game_inventory/inventory.lua index 8070e0ca..bbbbebfe 100644 --- a/modules/game_inventory/inventory.lua +++ b/modules/game_inventory/inventory.lua @@ -7,10 +7,9 @@ local inventoryButton -- public functions function Inventory.init() - connect(g_game, { onGameEnd = Inventory.clear, - onInventoryChange = Inventory.onInventoryChange, - onFreeCapacityChange = Inventory.onFreeCapacityChange, - onSoulChange = Inventory.onSoulChange }) + connect(LocalPlayer, { onInventoryChange = Inventory.onInventoryChange, + onFreeCapacityChange = Inventory.onFreeCapacityChange }) + connect(g_game, { onGameEnd = Inventory.clear }) Keyboard.bindKeyDown('Ctrl+I', Inventory.toggle) @@ -20,16 +19,13 @@ function Inventory.init() inventoryButton = TopMenu.addGameToggleButton('inventoryButton', tr('Inventory') .. ' (Ctrl+I)', 'inventory.png', Inventory.toggle) inventoryButton:setOn(true) - if g_game.isOnline() then - Inventory.reload() - end + Inventory.refresh() end function Inventory.terminate() - connect(g_game, { onGameEnd = Inventory.clear, - onInventoryChange = Inventory.onInventoryChange, - onFreeCapacityChange = Inventory.onFreeCapacityChange, - onSoulChange = Inventory.onSoulChange }) + disconnect(LocalPlayer, { onInventoryChange = Inventory.onInventoryChange, + onFreeCapacityChange = Inventory.onFreeCapacityChange }) + disconnect(g_game, { onGameEnd = Inventory.clear }) Keyboard.unbindKeyDown('Ctrl+I') @@ -42,30 +38,29 @@ function Inventory.terminate() Inventory = nil end +function Inventory.refresh() + local player = g_game.getLocalPlayer() + if not player then return end + + for i=1,10 do + Inventory.onInventoryChange(player, i, player:getInventoryItem(i)) + end +end + function Inventory.toggle() local visible = not inventoryWindow:isExplicitlyVisible() inventoryWindow:setVisible(visible) inventoryButton:setOn(visible) end -function Inventory.clear() -end - -function Inventory.reload() -end - -- hooked events -function Inventory.onInventoryChange(slot, item) +function Inventory.onInventoryChange(player, slot, item) local itemWidget = inventoryPanel:getChildById('slot' .. slot) itemWidget:setItem(item) end -function Inventory.onFreeCapacityChange(freeCapacity) +function Inventory.onFreeCapacityChange(player, freeCapacity) local widget = inventoryPanel:getChildById('capacity') widget:setText("Cap:\n" .. freeCapacity) end -function Inventory.onSoulChange(soul) - local widget = inventoryPanel:getChildById('soul') - widget:setText("Soul:\n" .. soul) -end diff --git a/modules/game_minimap/minimap.lua b/modules/game_minimap/minimap.lua index bd23773a..1c998b13 100644 --- a/modules/game_minimap/minimap.lua +++ b/modules/game_minimap/minimap.lua @@ -3,6 +3,7 @@ Minimap = {} -- private variables local minimapWidget local minimapButton +local DEFAULT_ZOOM = 45 -- private functions function onMinimapMouseRelease(self, mousePosition, mouseButton) @@ -40,6 +41,8 @@ function Minimap.init() minimapWidget.onMouseRelease = onMinimapMouseRelease minimapWidget.onMouseWheel = onMinimapMouseWheel minimapWidget:hide() + + Minimap.reset() end function Minimap.terminate() @@ -61,7 +64,9 @@ function Minimap.toggle() end function Minimap.reset() - minimapWidget:followCreature(g_game.getLocalPlayer()) - for i=1,10 do minimapWidget:zoomOut() end + local player = g_game.getLocalPlayer() + if not player then return end + minimapWidget:followCreature(player) + minimapWidget:setZoom(DEFAULT_ZOOM) end diff --git a/modules/game_skills/skills.lua b/modules/game_skills/skills.lua index c9914d3f..b3816fd7 100644 --- a/modules/game_skills/skills.lua +++ b/modules/game_skills/skills.lua @@ -39,6 +39,8 @@ function Skills.init() skillsButton = TopMenu.addGameToggleButton('skillsButton', tr('Skills') .. ' (Ctrl+S)', 'skills.png', Skills.toggle) skillsButton:setOn(true) Keyboard.bindKeyDown('Ctrl+S', Skills.toggle) + + Skills.refresh() end function Skills.terminate() @@ -63,6 +65,24 @@ function Skills.terminate() Skills = nil end +function Skills.refresh() + local player = g_game.getLocalPlayer() + if not player then return end + + Skills.onExperienceChange(player, player:getExperience()) + Skills.onLevelChange(player, player:getLevel(), player:getLevelPercent()) + Skills.onHealthChange(player, player:getHealth(), player:getMaxHealth()) + Skills.onManaChange(player, player:getMana(), player:getMaxMana()) + Skills.onSoulChange(player, player:getSoul()) + Skills.onFreeCapacityChange(player, player:getFreeCapacity()) + Skills.onStaminaChange(player, player:getStamina()) + Skills.onMagicLevelChange(player, player:getMagicLevel(), player:getMagicLevelPercent()) + + for i=0,6 do + Skills.onSkillChange(player, i, player:getSkillLevel(i), player:getSkillLevelPercent(i)) + end +end + function Skills.toggle() local visible = not skillsWindow:isExplicitlyVisible() skillsWindow:setVisible(visible) diff --git a/modules/game_viplist/viplist.lua b/modules/game_viplist/viplist.lua index ce978215..6674f6f9 100644 --- a/modules/game_viplist/viplist.lua +++ b/modules/game_viplist/viplist.lua @@ -15,6 +15,8 @@ function VipList.init() vipWindow = displayUI('viplist.otui', GameInterface.getLeftPanel()) vipButton = TopMenu.addGameToggleButton('vipListButton', tr('VIP list'), 'viplist.png', VipList.toggle) vipButton:setOn(true) + + VipList.refresh() end function VipList.terminate() @@ -30,6 +32,13 @@ function VipList.terminate() VipList = nil end +function VipList.refresh() + VipList.clear() + for id,vip in pairs(g_game.getVips()) do + VipList.onAddVip(id, unpack(vip)) + end +end + function VipList.clear() local vipList = vipWindow:getChildById('contentsPanel') vipList:destroyChildren() diff --git a/src/otclient/const.h b/src/otclient/const.h index 3f51a765..5d30f6e3 100644 --- a/src/otclient/const.h +++ b/src/otclient/const.h @@ -154,6 +154,21 @@ namespace Otc LastSkill }; + enum Inventory { + NoInventory = 0, + Head, + Neck, + Bag, + Armor, + RightHand, + LeftHand, + Legs, + Feet, + Ring, + Ammo, + LastInventory + }; + enum Direction { North = 0, East, diff --git a/src/otclient/core/game.cpp b/src/otclient/core/game.cpp index 8af44c6f..b2bfd3d2 100644 --- a/src/otclient/core/game.cpp +++ b/src/otclient/core/game.cpp @@ -55,6 +55,7 @@ void Game::resetGameStates() container->close(); } m_containers.clear(); + m_vips.clear(); m_worldName = ""; } @@ -241,7 +242,7 @@ void Game::processInventoryChange(int slot, const ItemPtr& item) if(item) item->setPosition(Position(65535, slot, 0)); - g_lua.callGlobalField("g_game","onInventoryChange", slot, item); + m_localPlayer->setInventoryItem((Otc::Inventory)slot, item); } void Game::processCreatureMove(const CreaturePtr& creature, const Position& oldPos, const Position& newPos) @@ -288,11 +289,13 @@ void Game::processCloseChannel(int channelId) void Game::processVipAdd(uint id, const std::string& name, bool online) { + m_vips[id] = Vip(name, online); g_lua.callGlobalField("g_game", "onAddVip", id, name, online); } void Game::processVipStateChange(uint id, bool online) { + std::get<1>(m_vips[id]) = online; g_lua.callGlobalField("g_game", "onVipStateChange", id, online); } diff --git a/src/otclient/core/game.h b/src/otclient/core/game.h index 360c95e3..bb7b6814 100644 --- a/src/otclient/core/game.h +++ b/src/otclient/core/game.h @@ -29,6 +29,8 @@ #include #include +typedef std::tuple Vip; + class Game { public: @@ -221,6 +223,7 @@ public: ContainerPtr getContainer(int index) { return m_containers[index]; } std::map getContainers() { return m_containers; } + std::map getVips() { return m_vips; } CreaturePtr getAttackingCreature() { return m_attackingCreature; } CreaturePtr getFollowingCreature() { return m_followingCreature; } int getServerBeat() { return m_serverBeat; } @@ -238,6 +241,8 @@ private: CreaturePtr m_followingCreature; ProtocolGamePtr m_protocolGame; std::map m_containers; + std::map m_vips; + bool m_dead; int m_serverBeat; Otc::FightModes m_fightMode; diff --git a/src/otclient/core/localplayer.cpp b/src/otclient/core/localplayer.cpp index 4ef55c60..651ed75f 100644 --- a/src/otclient/core/localplayer.cpp +++ b/src/otclient/core/localplayer.cpp @@ -284,3 +284,13 @@ void LocalPlayer::setStamina(double stamina) callLuaField("onStaminaChange", stamina, oldStamina); } } + +void LocalPlayer::setInventoryItem(Otc::Inventory inventory, const ItemPtr& item) +{ + if(m_inventoryItems[inventory] != item) { + ItemPtr oldItem = m_inventoryItems[inventory]; + m_inventoryItems[inventory] = item; + + callLuaField("onInventoryChange", inventory, item, oldItem); + } +} diff --git a/src/otclient/core/localplayer.h b/src/otclient/core/localplayer.h index 758cbddf..4e64fdfb 100644 --- a/src/otclient/core/localplayer.h +++ b/src/otclient/core/localplayer.h @@ -50,6 +50,7 @@ public: void setSoul(double soul); void setStamina(double stamina); void setKnown(bool known) { m_known = known; } + void setInventoryItem(Otc::Inventory inventory, const ItemPtr& item); int getStates() { return m_states; } int getSkillLevel(Otc::Skill skill) { return m_skillsLevel[skill]; } @@ -66,6 +67,7 @@ public: double getMagicLevelPercent() { return m_magicLevelPercent; } double getSoul() { return m_soul; } double getStamina() { return m_stamina; } + ItemPtr getInventoryItem(Otc::Inventory inventory) { return m_inventoryItems[inventory]; } bool isKnown() { return m_known; } bool isPreWalking() { return m_preWalking; } @@ -92,6 +94,7 @@ private: bool m_walkLocked; Position m_lastPrewalkDestionation; Timer m_walkLockTimer; + ItemPtr m_inventoryItems[Otc::LastInventory]; std::array m_skillsLevel; std::array m_skillsLevelPercent; diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index a2be20ef..98c060e1 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -151,6 +151,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassStaticFunction("g_game", "isFollowing", std::bind(&Game::isFollowing, &g_game)); g_lua.bindClassStaticFunction("g_game", "getContainer", std::bind(&Game::getContainer, &g_game, std::placeholders::_1)); g_lua.bindClassStaticFunction("g_game", "getContainers", std::bind(&Game::getContainers, &g_game)); + g_lua.bindClassStaticFunction("g_game", "getVips", std::bind(&Game::getVips, &g_game)); g_lua.bindClassStaticFunction("g_game", "getAttackingCreature", std::bind(&Game::getAttackingCreature, &g_game)); g_lua.bindClassStaticFunction("g_game", "getFollowingCreature", std::bind(&Game::getFollowingCreature, &g_game)); g_lua.bindClassStaticFunction("g_game", "getServerBeat", std::bind(&Game::getServerBeat, &g_game)); @@ -272,6 +273,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("setSoul", &LocalPlayer::setSoul); g_lua.bindClassMemberFunction("setStamina", &LocalPlayer::setStamina); g_lua.bindClassMemberFunction("setKnown", &LocalPlayer::setKnown); + g_lua.bindClassMemberFunction("setInventoryItem", &LocalPlayer::setInventoryItem); g_lua.bindClassMemberFunction("getStates", &LocalPlayer::getStates); g_lua.bindClassMemberFunction("getSkillLevel", &LocalPlayer::getSkillLevel); g_lua.bindClassMemberFunction("getSkillLevelPercent", &LocalPlayer::getSkillLevelPercent); @@ -287,6 +289,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("getMagicLevelPercent", &LocalPlayer::getMagicLevelPercent); g_lua.bindClassMemberFunction("getSoul", &LocalPlayer::getSoul); g_lua.bindClassMemberFunction("getStamina", &LocalPlayer::getStamina); + g_lua.bindClassMemberFunction("getInventoryItem", &LocalPlayer::getInventoryItem); g_lua.bindClassMemberFunction("isKnown", &LocalPlayer::isKnown); g_lua.bindClassMemberFunction("isPreWalking", &LocalPlayer::isPreWalking); g_lua.bindClassMemberFunction("asLocalPlayer", &LocalPlayer::asLocalPlayer); diff --git a/src/otclient/ui/uimap.cpp b/src/otclient/ui/uimap.cpp index b12c38dc..c957e188 100644 --- a/src/otclient/ui/uimap.cpp +++ b/src/otclient/ui/uimap.cpp @@ -36,6 +36,7 @@ UIMap::UIMap() m_aspectRatio = 0.0f; m_maxZoomIn = 3; m_maxZoomOut = 512; + m_mapRect.resize(1,1); g_map.addMapView(m_mapView); } @@ -58,7 +59,8 @@ void UIMap::drawSelf() bool UIMap::setZoom(int zoom) { - //TODO + m_zoom = std::min(std::max(zoom, m_maxZoomIn), m_maxZoomOut); + updateVisibleDimension(); return false; }