2018-06-06 18:29:02 +00:00
|
|
|
//
|
|
|
|
// Created by Antoine Bartuccio on 06/06/2018.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <string.h>
|
2018-06-15 17:14:30 +00:00
|
|
|
#include <pthread.h>
|
2018-06-06 18:29:02 +00:00
|
|
|
#include "Resident.h"
|
2018-06-20 15:49:18 +00:00
|
|
|
#include "../SharedData/SharedData.h"
|
2018-06-06 18:29:02 +00:00
|
|
|
|
|
|
|
GETTER(Resident, char *, name);
|
2018-06-20 20:06:45 +00:00
|
|
|
GETTER(Resident, int, destination);
|
2018-06-06 18:29:02 +00:00
|
|
|
GETTER(Resident, int, id);
|
|
|
|
GETTER(Resident, int, apartment_floor);
|
|
|
|
|
2018-06-10 23:58:52 +00:00
|
|
|
void * runnable_Resident(void * void_this){
|
|
|
|
Resident * this = (Resident*) void_this;
|
2018-06-20 15:49:18 +00:00
|
|
|
SharedData* data = GET_INSTANCE(SharedData);
|
2018-06-20 20:06:45 +00:00
|
|
|
Passenger * passenger = NEW(Passenger, (void*) this, RESIDENT);
|
2018-06-15 17:14:30 +00:00
|
|
|
|
2018-06-20 15:49:18 +00:00
|
|
|
AGENT_OPTIONS;
|
|
|
|
|
2018-06-21 14:15:33 +00:00
|
|
|
this->passenger = passenger;
|
2018-06-20 17:36:00 +00:00
|
|
|
passenger->resident = this;
|
|
|
|
passenger->type = RESIDENT;
|
2018-06-15 17:14:30 +00:00
|
|
|
|
2018-06-21 23:50:27 +00:00
|
|
|
printf("Résident %s : Je suis a l'etage %d, je vais en direction de l'etage %d\n",
|
2018-06-10 23:58:52 +00:00
|
|
|
this->name, this->apartment_floor, this->destination);
|
2018-06-21 22:48:43 +00:00
|
|
|
if(this->position == this->destination)
|
2018-06-21 23:50:27 +00:00
|
|
|
printf("Résident %s : Je reste chez moi\n", this->name);
|
2018-06-21 22:48:43 +00:00
|
|
|
else
|
|
|
|
data->main_building->go_to_floor(data->main_building, this->position, this->destination, passenger);
|
2018-06-10 23:58:52 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-06-06 18:29:02 +00:00
|
|
|
void _free__Resident(THIS(Resident)){
|
|
|
|
if (this->name != NULL)
|
|
|
|
free(this->name);
|
2018-06-21 14:15:33 +00:00
|
|
|
if(this->passenger != NULL)
|
|
|
|
free(this->passenger);
|
2018-06-06 18:29:02 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2018-06-10 15:57:39 +00:00
|
|
|
Resident *_init_Resident(int id, char* name, int apartment_floor, int destination){
|
2018-06-11 00:01:56 +00:00
|
|
|
/* If the destination is the same as the apartment_floor or negative, the RESIDENT will not move */
|
2018-06-06 18:29:02 +00:00
|
|
|
Resident * new_resident = malloc_or_die(sizeof(Resident));
|
2018-06-06 18:33:34 +00:00
|
|
|
new_resident->name = strdup(name);
|
2018-06-06 18:29:02 +00:00
|
|
|
new_resident->id = id;
|
|
|
|
new_resident->apartment_floor = apartment_floor;
|
|
|
|
new_resident->position = new_resident->apartment_floor;
|
2018-06-10 15:57:39 +00:00
|
|
|
new_resident->destination = destination;
|
2018-06-21 14:15:33 +00:00
|
|
|
new_resident->passenger = NULL;
|
2018-06-06 18:29:02 +00:00
|
|
|
|
|
|
|
LINK_ALL(Resident, new_resident,
|
2018-06-20 20:06:45 +00:00
|
|
|
get_name,
|
|
|
|
get_destination,
|
|
|
|
get_id,
|
|
|
|
runnable,
|
|
|
|
get_apartment_floor
|
2018-06-06 18:29:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return new_resident;
|
|
|
|
}
|