M2 Download Center
Download Here ( Internal )
Hello!
I'd like to show you how you can check position for shops (probably offline shops too).
I don't like when the map is full of shops or when shops are too close for each other. So I've made few functions for checking if the shop is in safezone or if it too close to other shops.
Let's start
char.h
Find there:
void CloseMyShop();
protected:
LPSHOP m_pkShop;
LPSHOP m_pkMyShop;
std::string m_stShopSign;
LPCHARACTER m_pkChrShopOwner;
Add few new lines:
void CloseMyShop();
void SetShopValidPos(bool value) { m_bShopValidPos = value; }
bool GetShopValidPos() { return m_bShopValidPos; }
protected:
LPSHOP m_pkShop;
LPSHOP m_pkMyShop;
std::string m_stShopSign;
LPCHARACTER m_pkChrShopOwner;
bool m_bShopValidPos;
char.cpp
In 'void CHARACTER::Initialize()' find:
m_pkMyShop = NULL;
Add below:
m_bShopValidPos = true;
Then search for 'void CHARACTER::OpenMyShop(const char * c_pszSign, TShopItemTable * pTable, BYTE bItemCount)'
Add above new function:
struct CheckShopPos
{
LPCHARACTER m_ch;
CheckShopPos(LPCHARACTER ch)
{
m_ch = ch;
}
void operator()(LPENTITY ent)
{
if (ent->IsType(ENTITY_CHARACTER))
{
LPCHARACTER ch = (LPCHARACTER) ent;
if (ch->GetRaceNum()!=30000) //shop mob vnum
return;
if (DISTANCE_APPROX(ch->GetX() - m_ch->GetX(), ch->GetY() - m_ch->GetY()) < 200) //distance between shops
{
m_ch->SetShopValidPos(false);
}
}
}
};
Then in 'void CHARACTER::OpenMyShop(const char * c_pszSign, TShopItemTable * pTable, BYTE bItemCount)' search for:
if (bItemCount == 0)
return;
Add below:
//shops pos check
LPSECTREE sectree = GetSectree();
if (sectree)
{
SetShopValidPos(true);
CheckShopPos f(this);
sectree->ForEachAround(f);
if(!GetShopValidPos())
{
ChatPacket(CHAT_TYPE_INFO, "You cannot open a shop here (too close to other shop).");
return;
}
if (!sectree->IsAttr(GetX(), GetY(), ATTR_BANPK))
{
ChatPacket(CHAT_TYPE_INFO, "You cannot open a shop here (use safezone).");
return;
}
}
//shops pos check
P.s:
That's all, have fun
Regards.