[go: up one dir, main page]

Menu

[r5]: / grueworld / main.cpp  Maximize  Restore  History

Download this file

122 lines (114 with data), 3.8 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
#include "common.hpp"
#include "inet.hpp"
#include "ppm.h"
shared_ptr<Container> world(new Room(0.0f));//space is black
void loadWorld(const char *filename) {
unsigned char*data;unsigned int width,height;
string east("e");
string west("w");
string north("n");
string south("s");
if (readPPM(filename,&data,&width,&height) ) {
vector<ptrdiff_t>tmprooms(width*height);
vector<shared_ptr<Room> >tmpworld;
for (unsigned int i=0;i<height;++i) {
for (unsigned int j=0;j<width;++j) {
tmprooms[i*width+j]=-1;
unsigned char idata[3];
memcpy(idata,data+3*(j+width*i),3);
if ((idata[0]==127||idata[0]==128)&&
(idata[1]==127||idata[1]==128)&&
(idata[2]==127||idata[2]==128)) {
//no room here
}else {
shared_ptr<Room> roomptr(new Room((float)(idata[2]/255.0f)));
//printf ("r %5d ", tmpworld.size());
tmprooms[i*width+j]=tmpworld.size();
tmpworld.push_back(roomptr);
world->addThing(roomptr);
if (i>0) {
ptrdiff_t index=tmprooms[(i-1)*width+j];
if (index>=0){
tmpworld.back()->exits()[0][north]=tmpworld[index];
tmpworld[index]->exits()[0][south]=tmpworld.back();
}
}
if (j>0) {
ptrdiff_t index=tmprooms[(i)*width+(j-1)];
if (index>=0) {
tmpworld.back()->exits()[0][west]=tmpworld[index];
tmpworld[index]->exits()[0][east]=tmpworld.back();
}
}
if (idata[1]>128) {
shared_ptr<Thing> food(new Food);
tmpworld.back()->addThing(food);
}
if (idata[0]>128) {
shared_ptr<Thing> poison(new Poison);
tmpworld.back()->addThing(poison);
}
}
}
}
}
}
extern vector<std::pair<GeneticCode,shared_ptr<Thing> > >newlyborngrue;
extern vector<std::pair<GeneticCode,shared_ptr<Thing> > >newlybornsnark;
int main (int argc, char ** argv) {
unsigned short snarkport=argc>1?atoi(argv[1]):7876;
unsigned short grueport=argc>1?atoi(argv[1]):7877;
unsigned short adminport=argc>2?atoi(argv[2]):7878;
INETsocket admin=-1;
INET_startup();
INETsocket adminsocket=INET_listen(adminport);
INETsocket snarksocket=INET_listen(snarkport);
INETsocket gruesocket=INET_listen(grueport);
while(snarksocket==-1) {
snarksocket=INET_listen(++snarkport);
}
while(gruesocket==-1) {
gruesocket=INET_listen(++grueport);
}
while(adminsocket==-1) {
adminsocket=INET_listen(++adminport);
}
if (adminsocket==-1||snarksocket==-1||gruesocket==-1) {
printf ("Failed to bind ports");
exit(1);
}
loadWorld(argc>3?argv[3]:"world.ppm");
shared_ptr<Thing> tmpworld((shared_ptr<Thing>)world);
while(1)
{
if (INET_BytesToRead(adminsocket)) {
admin=INET_Accept(adminsocket);
}
if (INET_BytesToRead(snarksocket)) {
shared_ptr<Thing> newCreature(new Snark(INET_Accept(snarksocket)));
if (newlybornsnark.empty()) {
dynamic_cast<Container*>(world->examineItem(rand()%world->numItems()))->addThing(newCreature);
printf ("Warning, no new snarks needed");
}else {
dynamic_cast<Container*>(newlybornsnark.back().second.get())->addThing(newCreature);
newlybornsnark.pop_back();
}
}
if (INET_BytesToRead(gruesocket)) {
shared_ptr<Thing> newCreature(new Grue(INET_Accept(gruesocket)));
if (newlyborngrue.empty()) {
dynamic_cast<Container*>(world->examineItem(rand()%world->numItems()))->addThing(newCreature);
printf ("Warning, no new grues needed");
}else {
dynamic_cast<Container*>(newlyborngrue.back().second.get())->addThing(newCreature);
newlyborngrue.pop_back();
}
}
unsigned int numrooms=world->numItems();
for (unsigned int index=0;index<numrooms;++index) {
world->examineItem(index)->tick(tmpworld,index);
}
}
INET_cleanup();
return 0;
}