void CShopManager::CreateShopEntity(SHOP_HANDLE shop)
{
auto& spawn = shop->GetSpawn();
if (auto sectree = SECTREE_MANAGER::Instance().Get(spawn.map, spawn.x, spawn.y))
{
long new_x = spawn.x, new_y = spawn.y; // Orijinal değerleri koru
bool findShop = true;
int max_try = 10; // Maksimum deneme sayisi, sonsuz döngü önlemi icin. Degistirilebilir.
auto ch = FindOwnerCharacter();
while (findShop && max_try > 0)
{
findShop = false;
auto check_shops = [&](LPENTITY ent)
{
if (ent->IsType(ENTITY_NEWSHOPS)) // <<ika'da mevcut.
{
if (DISTANCE_APPROX(ent->GetX() - new_x, ent->GetY() - new_y) < 100.0f)
findShop = true;
}
};
ch->GetSectree()->ForEachAround(check_shops);
if (findShop)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(-100, 100);
new_x = spawn.x + dis(gen);
new_y = spawn.y + dis(gen);
// İstege bagli kisim: emin degilim.
// Yeni pozisyonun gecerli olup olmadigini kontrol et :
// suyun ici, bloklu bölgeler gibi yerleri pas geç.
/*
PIXEL_POSITION pos;
pos.x = new_x;
pos.y = new_y;
if (!SECTREE_MANAGER::instance().GetValidLocation(spawn.map, new_x, new_y, spawn.map, pos, ch->GetDesc()->GetEmpire())) // spawn.map: mapindex'in döndürdügünü varsayiyorum
{
sys_err("CShopManager::CreateShopEntity: Invalid Position (%ld, %ld)", new_x, new_y);
findShop = true; // tekrar dene
}
else
{
new_x = pos.x;
new_y = pos.y;
}
*/
--max_try;
}
}
if (findShop) // eger hic uygun konum bulunmazsa
{
// Oyuncuya hata mesajı göster
// return; // veya işlemi iptal et
// burada ne yapacagina sen karar ver
}
auto entity = std::make_shared<ShopEntity>();
entity->SetShopName(shop->GetName());
entity->SetMapIndex(spawn.map);
//entity->SetXYZ(spawn.x, spawn.y, 0); düzenlenmis hali altta
entity->SetXYZ(new_x, new_y, 0);
entity->SetShop(shop);
entity->SetShopType(shop->GetDecoration());
sectree->InsertEntity(entity.get());
entity->UpdateSectree();
shop->SetEntity(entity);
m_entityByVID[entity->GetVID()] = entity;
}
}