#include "common.hpp"
void Container::Say(Avatar&speaker, string text) {
for (std::vector<shared_ptr<Thing> >::iterator it=items.begin(),end=items.end();
it!=end;
++it) {
it->get()->privateMessage(HEAR,text);
}
}
float Container::lightness(){
return 0;
}
void Container::tick(shared_ptr<Thing>&location, unsigned int myindex) {
for (size_t i=0;
i<items.size();
++i) {
int tmp=static_cast<Container*>(location.get())->items.size();
items[i]->tick(static_cast<Container*>(location.get())->items[myindex],i);
}
}
unsigned int Container::numItems() {
return items.size();
}
Thing*Container::examineItem(unsigned int which) {
if (which>=this->numItems()) return NULL;
return items[which].get();
}
bool Container::addThing(shared_ptr<Thing> thang) {
string desc=thang->description();
unsigned int numpeople=numItems();
for (unsigned int i=0;i<numpeople;++i) {
this->examineItem(i)->privateMessage(SEE,desc);
}
items.push_back(thang);
return true;
}
bool Container::getThing(unsigned int thingindex, Container&previous) {
unsigned int size=previous.items.size();
if (size>thingindex) {
if (previous.items[thingindex]->takeMe(*this,previous)) {
string desc=previous.items[thingindex]->description();
unsigned int numpeople=numItems();
for (unsigned int i=0;i<numpeople;++i) {
this->examineItem(i)->privateMessage(SEE,desc);
}
items.push_back(previous.items[thingindex]);
if (size>1) previous.items[thingindex]=previous.items.back();
previous.items.pop_back();
numpeople=previous.numItems();
for (unsigned int i=0;i<numpeople;++i) {
previous.examineItem(i)->privateMessage(NOTSEE,desc);
}
return true;
}
}
return false;
}
Container::~Container() {
}
string Container::look(Thing¬this) {
std::string retval;
for (std::vector<shared_ptr<Thing> >::iterator it=items.begin(),end=items.end();
it!=end;
++it) {
if (it->get()!=¬this) {
retval+=it->get()->description();
retval+='\n';
}
}
return retval;
}
map<string, weak_ptr<Room> > *Room::exits(){
return &links;
}
Room::Room(float lightness):light(lightness) {
}
string Room::look(Thing¬this) {
string retval;
static string ext("x:");
for (map<string, weak_ptr<Room> >::iterator it=links.begin(),end=links.end();
it!=end;
++it) {
retval+=ext+it->first;
retval+='\n';
}
retval+=this->Container::look(notthis);
int light100=(int)(light*99.9);
if (light100<0) light100=0;
if (light100>100)light100=100;
char lightvalm100='0'+(char)(light100/10)%10;
char lightvalm10='0'+(char)(light100%10);
retval+='l';
retval+=lightvalm100;
retval+='\n';//only have sigfig
//printf ("lr%f\n",light);
return retval;
}
float Room::lightness(){
return light;
}
static map<string,weak_ptr<Room> > emptiness;
map<string,weak_ptr<Room> >* Container::exits(){
return &emptiness;
}
Farm::Farm(float lightness, float foodrate, float poisonrate):Room(lightness) {
this->foodrate=foodrate;
this->poisonrate=poisonrate;
if (foodrate>.5||rand()%2==0) {
shared_ptr<Thing> food(new Food);
addThing(food);
}
if (poisonrate>.5||rand()%2==0) {
shared_ptr<Thing> poison(new Poison);
addThing(poison);
}
}
float overallfoodrate=.06125;
bool noFoodHere(Container*where, const std::string foodname) {
unsigned int numitems=where->numItems();
for (unsigned int i=0;i<numitems;++i) {
if (where->examineItem(i)->description()==foodname) return false;
}
return true;
}
static std::string foodname="food";
static std::string poisonname="poison";
void Farm::tick(shared_ptr<Thing>&location,unsigned int myindex) {
this->Room::tick(location,myindex);
if (rand()<RAND_MAX*overallfoodrate*this->foodrate&&noFoodHere(this,foodname)) {
shared_ptr<Thing> food(new Food);
addThing(food);
}
if (rand()<RAND_MAX*overallfoodrate*this->poisonrate&&noFoodHere(this,poisonname)) {
shared_ptr<Thing> poison(new Poison);
addThing(poison);
}
}