Agent_space.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006-2008 by Antonello Lobianco                         *
00003  *   http://regmas.org                                                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 3 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "Agent_space.h"
00021 #include "Pixel.h"
00022 #include "ThreadManager.h"
00023 #include "Scheduler.h"
00024 #include "Manager_base.h"
00025 
00026 Agent_space::Agent_space(ThreadManager* MTHREAD_h, Manager_base* manager_h): Agent_base(MTHREAD_h, manager_h){
00027     homePlot = NULL;
00028 }
00029 
00030 Agent_space::Agent_space(ThreadManager* MTHREAD_h, int uniqueID_h, Manager_base* manager_h): Agent_base(MTHREAD_h, uniqueID_h, manager_h){
00031     if(MTHREAD->RD->getBoolSetting("agentsView")){
00032         MTHREAD->treeViewerAddAgentProperty(agentUniqueID, "owned land");
00033         MTHREAD->treeViewerAddAgentProperty(agentUniqueID, "rented land");
00034     }
00035     homePlot = NULL;
00036 }
00037 
00038 Agent_space::~Agent_space(){
00039 
00040 }
00041 
00042 int
00043 Agent_space::uncompletePlotDotation (int landCode_h, int ptype){
00044     map<int, double>::iterator p;
00045     int freePlots=0;
00046     int alreadyOwnedPlots =0;
00047     int alreadyRentedPlots =0;
00048 
00049     // getting the pixels already owned and calculating the free ones..
00050     if (ptype==PLOTS_ALL || ptype ==PLOTS_OWNED){
00051         for (uint i=0;i<ownedPlots.size();i++){
00052             if (ownedPlots.at(i)->getDoubleValue("landUse") == landCode_h) alreadyOwnedPlots++;
00053         }
00054         p = initialOwnedPlots.find(landCode_h);
00055         if(p != initialOwnedPlots.end()){
00056             int initialOwned = (int) p->second;
00057             freePlots += initialOwned - alreadyOwnedPlots;
00058         }
00059     }
00060     // getting the pixels already rented and calculating the free ones..
00061     if (ptype==PLOTS_ALL || ptype == PLOTS_RENTED){
00062         for (uint i=0;i<rentedPlots.size();i++){
00063             if (rentedPlots.at(i)->getDoubleValue("landUse") == landCode_h) alreadyRentedPlots++;
00064         }
00065         p = initialRentedPlots.find(landCode_h);
00066         if(p != initialRentedPlots.end()){
00067             int initialRented = (int) p->second;
00068             freePlots += initialRented - alreadyRentedPlots;
00069         }   
00070     }
00071     return freePlots;
00072 }
00073 
00078 vector <Pixel *>
00079 Agent_space::getPlots(int ptype, int landCode_h){
00080     vector <Pixel *> toReturn;
00081     switch (ptype) {
00082         case PLOTS_ALL:
00083             merge(ownedPlots.begin(), ownedPlots.end(), rentedPlots.begin(), rentedPlots.end(), back_inserter(toReturn));
00084             break;
00085         case PLOTS_OWNED:
00086             toReturn = ownedPlots;
00087             break;
00088         case PLOTS_RENTED:
00089             toReturn = rentedPlots;
00090             break;
00091         default:
00092             msgOut(MSG_ERROR, "You have called Agent_space::getPlots(int ptype) with an invalid parameter. All plots have been returned.");
00093             merge(ownedPlots.begin(), ownedPlots.end(), rentedPlots.begin(), rentedPlots.end(), back_inserter(toReturn));
00094     }
00095     if(landCode_h == 0){return toReturn;}
00096     else{
00097         vector <Pixel *> toReturn2;
00098         for(uint i=0;i<toReturn.size();i++){
00099             if(toReturn.at(i)->getDoubleValue("landUse") == landCode_h){
00100                 toReturn2.push_back(toReturn.at(i));
00101             }
00102         }
00103         return toReturn2;
00104     }
00105     
00106 }
00107 
00112 int
00113 Agent_space::getNPlots(int ptype, int landCode_h){
00114 
00115     if(landCode_h == 0){ // we don't need the array as we have just to count them...
00116         switch (ptype) {
00117             case PLOTS_ALL:
00118                 return ownedPlots.size()+rentedPlots.size();
00119             case PLOTS_OWNED:
00120                 return ownedPlots.size();
00121             case PLOTS_RENTED:
00122                 return rentedPlots.size();
00123             default:
00124                 msgOut(MSG_ERROR, "You have called Agent_space::getNPlots(int ptype) with an invalid parameter. Count of all plots has been returned.");
00125                 return ownedPlots.size()+rentedPlots.size();
00126         }
00127     }
00128     else{ // we need the array of Pixel* as we need to perform a check on them...
00129         vector <Pixel* > plots = getPlots(ptype, landCode_h);
00130         return plots.size();
00131     }
00132 }
00133 
00134 int
00135 Agent_space::countMyObjects(){
00136     int counter = myObjects.size();
00137     for (uint i=0; i<ownedPlots.size();i++){
00138         counter += ownedPlots.at(i)->countMyObjects();
00139     }
00140     return counter;
00141 }
00142 
00143 double
00144 Agent_space::getDistance(const Pixel* px1){
00145     return MTHREAD->GIS->getDistance(px1, homePlot);
00146 }
00147 
00148 void
00149 Agent_space::payInitialYearCosts(){
00150     sunkCostsPaid = 0;
00151     // paying rental cost of still rented plots...
00152     for (uint i=0;i<rentedPlots.size();i++){
00153         sunkCostsPaid += rentedPlots[i]->getRentalCosts();
00154     }
00155     pay (sunkCostsPaid,false); // remove liquidity. Don't updateResourceValues() as done a full initMIP just next function in prepare() 
00156     // no investment mainainance costs for now...
00157 }
00158 
00164 void
00165 Agent_space::freeLand(){
00166 
00167     for(uint i=0;i<rentedPlots.size();i++){
00168         rentedPlots[i]->destroyRentalContract();
00169     }
00170     rentedPlots.clear(); // remove all the elements of the vector
00171     for(uint i=0;i<ownedPlots.size();i++){
00172         ownedPlots[i]->setOwner(0);
00173     }
00174 
00175 }