/*
 * Check location for agent. If present check to see if due to die.  If so then remove all sugar.
 * If agent is not dying then check each parent to see if they are dying.
 * If they are take the correct share of their sugar (total sugar divided by number of children.
 * Note: We do not bother to check if any of their other children may be dying - just to simplify code
 * @param loc :location to check for agent
 * @see Inheritance Rule
 * @return true if agent was presnt otherwise false
 * @exception none
 */
bool Inheritance::executeAction(Location * loc, group *)
{
    //offloads to intel MIC (experiment)
    //will only work with intel devices with Xeon Phi
#pragma offload target(mic:0)
    {
        if (loc->hasAgent()) {
            Agent *theAgent = loc->getAgent();
            if (theAgent->getAge() == theAgent->getMaxAge()) {//I am dying
                theAgent->setSugar(0);
            }
            else {//I am alive check to see if parents are dying
                if (theAgent->getFather()->getAge() == theAgent->getFather()->getMaxAge()) {
                    theAgent->incSugar(theAgent->getFather()->getSugar() / theAgent->getFather()->getChildrenCount());
                }
                if (theAgent->getMother()->getAge() == theAgent->getMother()->getMaxAge()) {
                    theAgent->incSugar(theAgent->getMother()->getSugar() / theAgent->getMother()->getChildrenCount());
                }
            }
            return true;
        } else {
            return false;/*!< no agent present so did nothing */
        }
    }
}
/**
 * Decrements an agents sugar by their metabolism
 * @param loc :Location in lattice we are running action on
 * @param group pointer not used - nullptr passed in
 * @see Metabolism in book
 * @return true if agent was at location else false
 * @exception none
 */
bool AgentMetabolism::executeAction(Location * loc, group * ){
    if (loc->hasAgent()) {
        Agent *agent =loc->getAgent();
        agent->incSugar(-agent->getMetabolism());
        agent->incAge();
    }
    return true;
}
Example #3
0
/**
 * Agent removes agent in location in group from world and takes resources
 * It also removes links to agents killed by combat (lenders, borrowers, etc.)
 * @param loc :Pointer to Location of the winning agent
 * @param grp : Pointer to Group containing location of losing agent (or possibly an empty location)
 * @see Combat Rule
 * @return true if agent exists at loc else false
 * @exception none
 */
bool AgentCombat::executeAction(Location *loc, group *grp)
{
    if (loc->hasAgent()) {
        Agent *winner = grp->getPrimeMover()->getAgent(); //must equal loc->getAgent()!
        if (grp->getMembers().size()!=0)/*!< we are moving somewhere */
        {
            if (grp->getMembers()[0]->hasAgent()==false) {/*!< moving to empty location */
                loc->getAgent()->incSugar(grp->getMembers()[0]->getSugar());/*!< eat sugar at destination */
                grp->getMembers()[0]->setSugar(0);/*!< sugar at location is consumed */
            }
            else{
                //std::cout <<"COMBAT!"<<std::endl;
                //1. Update victorious agent resources
                Agent *loser = grp->getMembers()[0]->getAgent();//only one member in group - the loser
                winner->incSugar(grp->getMembers()[0]->getReward());//*WHAT IF WE ADD SPICE?*
                grp->getMembers()[0]->setSugar(0);/*!< sugar at location is consumed */
                //2. Kill vanquished agent
                std::pair<int,int> currPosition=loser->getPosition();
                if (loser!=sim->killAgent(currPosition))
                {
                    std::cerr << "Delete of agent failed"<<std::endl;
                }
            }
            //Move agent to new position
            std::pair<int,int> currPosition=winner->getPosition();
            sim->setAgent(currPosition, nullptr);//remove old location ptr to agent
            winner->setPosition(grp->getMembers()[0]->getPosition());//set new position to new location
            sim->setAgent(grp->getMembers()[0]->getPosition(),winner);//add ptr to agent at new location
        }
        else{/*!< we are staying put-not moving */
            loc->getAgent()->incSugar(loc->getSugar());/*!< eat sugar at destination */
            loc->setSugar(0);/*!< sugar at location is consumed */
        }
        for(auto ag:winner->getChildren())/*!< Remove any links to killed children */
        {
            if (ag->isKilled()) {
                winner->removeChild(ag);
            }
        }
        winner->removeKilledLoans();/*!< remove loans with killed agents */
        winner->removeKilledFather();/*!< remove father link if he is killed */
        winner->removeKilledMother();/*!< remove mother link if she is killed */
        return true;
    }else{
        return false;/*!< no agent present so did nothing */
    }
}