// ... where trg the node to be destroyed
// you must call a function like find, to get the pointer to the node u want to delete
void LinkedList::destroyNode(listNode *trg){

	if(numberOfNodes > 0){
		// There are still elements in the list
		if(Head == Tail){
			// This is the last Node in the list
			Head = Tail = NULL;			
			std::cout<<"\n  [LL] last node deleted\n";			
		}else{
			// There are at least 2 Nodes in the list
			trg->prev->next = trg->next;
			trg->next->prev = trg->prev;
			if(trg == Head){
				Head = trg->next;	// set new Head
				std::cout<<"\n  * New Head has been set\n";
			}
			if(trg == Tail){
				Tail = trg->prev;	// set new Tail
				std::cout<<"\n  * New Tail Has been set\n";
			}			
		}	

		userData *dbg = getUnitData(trg);

		delete dbg;	// de-allocates the memory we allocated for the data, outside of the LinkedList class
		delete trg;			// de-allocates the momory used by the list node		

		std::cout<<"  ["<<numberOfNodes<<"] List node has been destroyed\n";
		numberOfNodes--;		
	}	
}
void UnitInfoManager::drawUnitInformation(float x,float y) const
{
    if (!m_bot.Config().DrawEnemyUnitInfo)
    {
        return;
    }

    std::stringstream ss;

    // for each unit in the queue
    for (int t(0); t < 255; t++)
    {
        int numUnits =      m_unitData.at(Players::Self).getNumUnits(t);
        int numDeadUnits =  m_unitData.at(Players::Enemy).getNumDeadUnits(t);

        // if there exist units in the vector
        if (numUnits > 0)
        {
            ss << numUnits << "   " << numDeadUnits << "   " << sc2::UnitTypeToName(t) << "\n";
        }
    }
    
    for (auto & kv : getUnitData(Players::Enemy).getUnitInfoMap())
    {
        m_bot.Debug()->DebugSphereOut(kv.second.lastPosition, 0.5f);
        m_bot.Debug()->DebugTextOut(sc2::UnitTypeToName(kv.second.type), kv.second.lastPosition);
    }


}
// saves all changed users to the db
// and performs minimal cleanup (if keepAlive = false, delete user...)
void LinkedList::SaveUsersToDb(Database *dbPtr){
	before = RakNet::GetTime();

	listNode *current = Head;

	for(int i = 0; i<numberOfNodes; i++)
	{
		userData* _user = getUnitData(current);
		// get current user's uuid 
		char uuid[4];
		strcpy(uuid, _user->uuid.c_str());

		if(!_user->savedInDb)
		{				
			dbPtr->UpdatePosition(uuid, &_user->userPos);
			dbPtr->UpdateOrientation(uuid, _user->orientation);
			_user->savedInDb = true;
			std::cout<<"  > user " << uuid <<" has been saved to DB...\n";
		}

		// check if the current user, should be in the list
		if(!_user->keepAlive){
			removeUnitFromList(uuid);
			// send destroyUnitPacket to all
		}

		// get the next element of the list
		current = current->next;
	}

	// how many ms the system needs to run this?
	after = RakNet::GetTime();
	if((after-before) > 0)
		std::cout<<"  > SaveUsersToDb(*) was executed in "<<after-before<<" MS\n\n";
}
void StatusDataManager::addNewUnit(uint32_t newid)
{
    CCLOG("add new unit: id (%d)", newid);
    auto unitdata = getUnitData(newid);
    auto newrecord = new UnitOfPlayerRecord(unitdata,
                                            time(NULL), //time_t getTimestamp,
                                            1, //int level,
                                            0   //int exp
                                            );
    this->_unitRecords.push_back(newrecord);
    
    // persistence
    writeUnitOfPlayerRecordsToCSV();
}
void LinkedList::calculate_pozition(){
	listNode *current = Head;

	if(current != NULL)
	{
		do{
			userData* thisUser = getUnitData(current);
		
			if(thisUser->isMoving){
				thisUser->userPos.x += 0.44;
			}

			current = current->next;		
		}while(current != Head);
	}
}
void UnitInfoManager::getNearbyForce(std::vector<UnitInfo> & unitInfo, sc2::Point2D p,int player, float radius) const
{
    bool hasBunker = false;
    // for each unit we know about for that player
    for (const auto & kv : getUnitData(player).getUnitInfoMap())
    {
        const UnitInfo & ui(kv.second);

        // if it's a combat unit we care about
        // and it's finished! 
        if (Util::IsCombatUnitType(ui.type, m_bot) && Util::Dist(ui.lastPosition,p) <= radius)
        {
            // add it to the vector
            unitInfo.push_back(ui);
        }
    }
}
listNode* LinkedList::findNode(char uuid[UUID_LEN]){
	listNode *current = Head;			// get the first node of the list
	
	// if list not empty, search
	if(Head != NULL)
	{
		do{
			userData *cData = getUnitData(current);
		
			std::string searchUUID(uuid);		
			if(searchUUID == cData->uuid)	// node found
				return current;
		
			current = current->next;
		}while(current != Head);
	}

	return NULL;						// return NULL if the node hasn't been found
}
listNode* LinkedList::findNode(SystemAddress clientID){
	listNode *current = Head;

	if(current != NULL){
		do{
			userData *cData = getUnitData(current);		

			if(clientID == cData->clientID){
				/*
				char thisIp[50];
				cData->clientID.ToString(true, thisIp);
				std::cout<<"[.] node found: "<<thisIp<<"\n";
				*/

				return current;
			}
			current = current->next;		
		}while(current != Head);
	}

	return NULL;
}
Beispiel #9
0
void UnitScript::loadUnit(tinyxml2::XMLElement * xmlElement)
{
	//////////////////////////////////////////////////////////////////////////
	//Load and set the unit data.
	auto unitDataID = xmlElement->IntAttribute("UnitDataID");
	const auto resourceLoader = SingletonContainer::getInstance()->get<ResourceLoader>();
	auto unitData = resourceLoader->getUnitData(unitDataID);
	assert(unitData && "UnitScript::setUnitData() with nullptr.");

	auto ownerActor = m_OwnerActor.lock();
	//#TODO: This only shows the first frame of the animation. Update the code to show the whole animation.
	auto sceneNode = static_cast<cocos2d::Sprite*>(pimpl->m_RenderComponent->getSceneNode());
	sceneNode->setSpriteFrame(unitData->getAnimation()->getFrames().at(0)->getSpriteFrame());

	//Scale the sprite so that it meets the real game grid size.
	pimpl->m_TransformComponent->setScaleToSize(resourceLoader->getDesignGridSize());

	pimpl->m_UnitData = std::move(unitData);

	//////////////////////////////////////////////////////////////////////////
	//#TODO: Load more data, such as the hp, level and so on, from the xml.
	pimpl->m_State = utilities::createUnitState(UnitStateTypeCode::Idle);
}
const std::map<int,UnitInfo> & UnitInfoManager::getUnitInfoMap(int player) const
{
    return getUnitData(player).getUnitInfoMap();
}