[go: up one dir, main page]

Menu

[r35]: / grueworld / container.cpp  Maximize  Restore  History

Download this file

149 lines (136 with data), 4.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#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&notthis) {
std::string retval;
for (std::vector<shared_ptr<Thing> >::iterator it=items.begin(),end=items.end();
it!=end;
++it) {
if (it->get()!=&notthis) {
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&notthis) {
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);
}
}