//----- Begin of function TownArray::add_town -------// // // <int> nationRecno - the nation recno // <int> raceId - the race of the majority of the town poulation // <int> xLoc, yLoc - location of the town // int TownArray::add_town(int nationRecno, int raceId, int xLoc, int yLoc) { Town* townPtr; townPtr = new Town; linkin(&townPtr); townPtr->town_recno = recno(); townPtr->init(nationRecno, raceId, xLoc, yLoc); nation_array.update_statistic(); // update largest_town_recno return recno(); }
// ----------------------------------------------------------------- // Name : createFromServer // ----------------------------------------------------------------- void Map::createFromServer(MapReader * pMapReader, LocalClient * pLocalClient) { m_iWidth = pMapReader->getMapWidth(); m_iHeight = pMapReader->getMapHeight(); int * pTiles = pMapReader->getMap(); m_pTiles = new MapTile**[m_iWidth]; for (u16 x = 0; x < m_iWidth; x++) { m_pTiles[x] = new MapTile*[m_iHeight]; for (u16 y = 0; y < m_iHeight; y++) { if (IS_VALID_TERRAIN(pTiles[y * m_iWidth + x])) m_pTiles[x][y] = new MapTile(pTiles[y * m_iWidth + x], pLocalClient->getServer()->getSolver()->getGlobalSpellsPtr()); else { m_pTiles[x][y] = new MapTile(TERRAIN_SEA, pLocalClient->getServer()->getSolver()->getGlobalSpellsPtr()); char sError[512]; snprintf(sError, 512, "Invalid terrain type at (%d,%d)", (int)x, (int)y); pLocalClient->getDebug()->notifyErrorMessage(sError); } } } // Reset used town names and heroes Edition * pEdition = pLocalClient->getDataFactory()->getFirstEdition(); while (pEdition != NULL) { Ethnicity * pEthn = (Ethnicity*) pEdition->getEthnicities()->getFirst(0); while (pEthn != NULL) { pEthn->resetUsedTownNames(); pEthn->resetUsedHeroes(); pEthn = (Ethnicity*) pEdition->getEthnicities()->getNext(0); } pEdition = pLocalClient->getDataFactory()->getNextEdition(); } // Create towns std::vector<TownData> * towns = pMapReader->getTowns(); for (u16 i = 0; i < towns->size(); i++) { if (getTileAt((*towns)[i].position)->m_uTerrainType == TERRAIN_SEA) continue; Edition * pEdition = pLocalClient->getDataFactory()->findEdition((*towns)[i].sEthnEdition); if (pEdition != NULL) { Ethnicity * pEthn = pEdition->findEthnicity((*towns)[i].sEthnId); if (pEthn != NULL) { Town * pTown = new Town((*towns)[i].position, this, pLocalClient->getServer()->getSolver()->getGlobalSpellsPtr()); pTown->init(m_iNbTowns++, (*towns)[i].size, pEthn, pLocalClient); pTown->initServer(); m_pTownsRef->addLast(pTown); } } } // Create temples std::vector<TempleData> * temples = pMapReader->getTemples(); for (u16 i = 0; i < temples->size(); i++) { if (getTileAt((*temples)[i].position)->m_uTerrainType == TERRAIN_SEA) continue; Temple * pTemple = new Temple((*temples)[i].position, this, pLocalClient->getServer()->getSolver()->getGlobalSpellsPtr()); pTemple->init(m_iNbTemples++, (*temples)[i].mana, (*temples)[i].amount); m_pTemplesRef->addLast(pTemple); } // Create special tiles std::vector<CoordsMap> * spectiles = pMapReader->getSpecialTiles(); char pAllTerrains[7][64] = LTERRAIN_NAMES; for (u16 i = 0; i < spectiles->size(); i++) { MapTile * pTile = getTileAt((*spectiles)[i]); // First get total frequency int totalFreq = 0; Edition * pEd = pLocalClient->getDataFactory()->getFirstEdition(); while (pEd != NULL) { SpecialTile * pSpec = (SpecialTile*) pEd->getSpecialTiles()->getFirst(0); while (pSpec != NULL) { pSpec->callLuaFunction("isAllowedOn", 1, "s", pAllTerrains[pTile->m_uTerrainType]); if (pSpec->getLuaNumber() > 0) totalFreq += pSpec->getFrequency(); pSpec = (SpecialTile*) pEd->getSpecialTiles()->getNext(0); } pEd = pLocalClient->getDataFactory()->getNextEdition(); } if (totalFreq > 0) { bool bBreak = false; int iRnd = getRandom(totalFreq); pEd = pLocalClient->getDataFactory()->getFirstEdition(); while (pEd != NULL) { SpecialTile * pSpec = (SpecialTile*) pEd->getSpecialTiles()->getFirst(0); while (pSpec != NULL) { pSpec->callLuaFunction("isAllowedOn", 1, "s", pAllTerrains[pTile->m_uTerrainType]); if (pSpec->getLuaNumber() > 0) { iRnd -= pSpec->getFrequency(); if (iRnd < 0) { pTile->m_pSpecialTile = pSpec->instanciate((*spectiles)[i], pLocalClient->getDebug()); bBreak = true; break; } } pSpec = (SpecialTile*) pEd->getSpecialTiles()->getNext(0); } if (bBreak) break; pEd = pLocalClient->getDataFactory()->getNextEdition(); } } } // Pathfinder m_pPathfinder = new Pathfinder(m_pTiles, m_iWidth, m_iHeight); }