Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

agent.cc

Go to the documentation of this file.
00001 /*
00002   svas_server -- virtual World Server of Svas
00003   Copyright (c) 2001, 2002 David Moreno Montero
00004  
00005  
00006   This program is free software; you can redistribute it and/or modify
00007   it under the terms of the GNU General Public License as published by
00008   the Free Software Foundation; either version 2 of the License, or
00009   (at your option) any later version.
00010  
00011   This program is distributed in the hope that it will be useful, but
00012   WITHOUT ANY WARRANTY; without even the implied warranty of
00013   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014   General Public License for more details.
00015  
00016   You should have received a copy of the GNU General Public License
00017   along with this program; if not, write to the Free Software
00018   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00019   02111-1307, USA.  
00020 
00021 */
00022 
00023 #include <thread.h>
00024 #include <math.h>
00025 #include "client.h"
00026 #include "agent.h"
00027 #include "log.h"
00028 #include "world.h"
00029 #include "collision.h"
00030 
00031 unsigned long int Agent::agentMaxId=0;
00032 
00033 /**
00034  * Creates an empty agent; default sizes and energies,
00035  * position... Just to begin an agentClient
00036  */
00037 Agent::Agent(Client *_client, const Point3d &_position, const char *creationMessage, unsigned long int messageSize){
00038   cylinder=new Cylinder(_client->getWorld(), NULL, 0x0FFFF, 1, 1, Vector3d(0,0,0), true);
00039   cylinder->setNewAgent(this);
00040   cylinder->movePosition(_position);
00041 
00042   cylinderCount=1;
00043 
00044 
00045   client=_client;
00046   id=agentMaxId++;
00047 
00048   client->sendMessage(Message(getId(), true, messageNewAgent, messageSize, creationMessage));
00049 }
00050 
00051 /**
00052  * Creates an Agent based on a dropCylinder cylinder drop. Position,
00053  * energy... all is deduced from cylinder
00054  **/
00055 
00056 Agent::Agent(Client *cli, Cylinder *cyl, const char *creationMessage, unsigned long int messageSize){
00057   log <<"New agent from cylinder"<<endl;
00058   cylinder=cyl;
00059   
00060   cylinderCount=1;
00061 
00062   client=cli;
00063   id=agentMaxId++;
00064 
00065   cyl->setNewAgent(this);
00066 
00067   client->sendMessage(Message(getId(), true, messageNewAgent, messageSize, creationMessage));
00068 }
00069 
00070 
00071 Agent::~Agent(){
00072   delete cylinder;
00073 }
00074 
00075 /**
00076  * Performs the actions that cylinders said they wanted to perform.
00077  *
00078  * As usually this involves energy consumption, a list of energy
00079  * cylinders created is returned
00080  */
00081 vector<Cylinder *> Agent::performActions(){
00082   vector<Cylinder *> ret;
00083   unsigned int i;
00084   for (i=0;i<256;i++)
00085     if ((*cylinder)[i]!=NULL){
00086       Cylinder *cyl;
00087       cyl=(*cylinder)[i]->performAction();
00088       if (NULL!=cyl)
00089     ret.push_back(cyl);
00090     }
00091   return ret;
00092 }
00093 
00094 /**
00095  * Return the world in which the agent lives
00096  */
00097 World *Agent::getWorld(void){ 
00098   if (client==NULL) 
00099     return NULL; 
00100   else 
00101     return client->getWorld(); 
00102 }
00103 
00104 void Agent::receiveMessage(unsigned int long _agentId, const char *message, unsigned long int size){
00105  client->sendMessage(Message(getId(), true, messageNewAgent, size, message));
00106 }

Generated on Mon Jun 17 19:53:43 2002 for Svas Server by doxygen1.2.16