Example #1
0
void NPCManager::handleNewMap() {

	Map_NPC mn;
	ItemStack item_roll;

	// remove existing NPCs
	for (unsigned i=0; i<npcs.size(); i++)
		delete(npcs[i]);

	npcs.clear();

	// read the queued NPCs in the map file
	while (!map->npcs.empty()) {
		mn = map->npcs.front();
		map->npcs.pop();

		NPC *npc = new NPC(map, items);
		npc->load(mn.id, stats->level);
		npc->pos.x = mn.pos.x;
		npc->pos.y = mn.pos.y;

		// if this NPC needs randomized items
		while (npc->random_stock > 0 && npc->stock_count < NPC_VENDOR_MAX_STOCK) {
			item_roll.item = loot->randomItem(npc->level);
			item_roll.quantity = rand() % items->items[item_roll.item].rand_vendor + 1;
			npc->stock.add(item_roll);
			npc->random_stock--;
		}
		npc->stock.sort();
		npcs.push_back(npc);
	}

}
Example #2
0
void MapLoader::loadNpcs(MapData& data, Screen* screen, Map* map) const {
	MapMainCharacter* mainCharacter = dynamic_cast<MapScreen*>(screen)->getMainCharacter();
	if (mainCharacter == nullptr) {
		g_logger->logError("MapLoader", "Could not find main character of map screen");
		return;
	}

	// calculate npcs
	for (auto& it : data.npcs) {
		NPC* mapNPC = new NPC(map);
		mapNPC->load(mainCharacter, it);
		screen->addObject(mapNPC);
	}
}