[go: up one dir, main page]

Menu

[r119]: / grueworld / avatar.cpp  Maximize  Restore  History

Download this file

574 lines (547 with data), 17.2 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "common.hpp"
#include <iostream>
#include "inet.hpp"
#include <math.h>
#include <set>
using std::set;
float stdmaint=(float)MAXHEALTH/16384.0f;
float gruemaint=(float)MAXHEALTH/65536.0f;
float stdrepro=(float)MAXHEALTH/4.0f;
float gruerepro=(float)MAXHEALTH*.25;
float stdinitrepro=(float)MAXHEALTH/8.0f;
float grueinitrepro=MAXHEALTH*.5;
float snarkbite=MAXHEALTH/128.;
float gruebite=MAXHEALTH/16.;
float gruelightbite=MAXHEALTH/256.;
float snarkbitecost=MAXHEALTH/256;
int gettimecost=10;
int movetimecost=20;
int bitetimecost=10;
int speaktimecost=2;
int shouttimecost=5;
int eattimecost=2;
int combinetimecost=10;
int reproducetimecost=50;
int reproducefailcost=1;
int descriptiontimecost=20;
Avatar::Avatar(INETsocket controller){
sock=controller;
maintenance=stdmaint;
health=(float)MAXHEALTH;
lastactiontime=0;
}
bool Avatar::bite(Thing&thing,Container&room) {
return false;
}
bool Snark::bite(Thing&thing,Container&room) {
Grue *targ=dynamic_cast<Grue*>(&thing);
if (targ==NULL) return false;
unsigned int numthings=room.numItems();
float numsnarks=0;
for (unsigned int i=0;i<numthings;++i) {
if (dynamic_cast<Snark*>(room.examineItem(i))!=NULL) {
numsnarks++;
}
}
targ->addHealth(-snarkbite*numsnarks);//this squares the snarkbite defense
health-=snarkbitecost;//don't want to warn them
return true;
}
bool Grue::bite(Thing&thing,Container&room) {
Snark *targ=dynamic_cast<Snark*>(&thing);
if (targ==NULL) return false;
float bite=gruebite*(1-room.lightness());
if (targ->health>bite) {
addHealth(bite);
}else {
addHealth(targ->health);
}
targ->addHealth(-bite-gruelightbite);//does more damage than health gained
return true;
}
void Avatar::hungermessage() {
std::string hungry("h:");
int howhungry=(int)(health*99.9f/(float)MAXHEALTH);
hungry+='0'+(howhungry/10)%10;
hungry+='0'+(howhungry%10);
hungry+='\n';
this->privateMessage(FEEL,hungry);
}
float movedecrement=(float)MAXHEALTH/4096.0f;
float thoughtdecrement=(float)MAXHEALTH/65536.0f;//so as not to spam the server
bool Avatar::Move(string destination, Container&location, unsigned int myindex) {
map<string,weak_ptr<Room> >* links=location.exits();
map<string,weak_ptr<Room> >::iterator where=links->find(destination);
if (where!=links->end()) {
shared_ptr<Room> dest=where->second.lock();
int numpeople=dest->numItems();
dest->getThing(myindex,location);
this->privateMessage(SEE,dest->look(*this));
float tmp1=(float)floor(health);
health-=movedecrement*(1+items.size());//carrying stuff is heavy
float tmp2=(float)floor(health);
if (tmp1!=tmp2)
hungermessage();
return true;
}
return false;
}
string getSArg(int socket);
string getSArg(std::string &order) {
string retval=order=order.substr(1);
string::size_type where1=order.find('\r');
string::size_type newline=order.find('\n');
string::size_type where2=newline;
if (where1==string::npos) where1=where2;
if (where2==string::npos) where2=where1;
if (where2==string::npos) {
printf ("String error\n");
order=std::string();
return retval;
}
if (where2<where1)where1=where2;
retval=order.substr(0,where1);
if (newline!=string::npos)
order=order.substr(newline+1);
else order=string();
return retval;
}
void Avatar::privateMessage(SENSES sense,std::string message) {
size_t len=message.length();
unsigned char csense[4]={(unsigned char)(len%256),(unsigned char)((len/256)%256),(unsigned char)((len/65536)%256),sense};
INET_Write(sock,4,(const char*)csense);
INET_Write(sock,message.length(),message.data());
}
shared_ptr<Room> deathbed(new Room(0));
void Avatar::Die(int myindex, Container&place) {
deathbed->getThing(myindex,place);
INET_close(sock);
sock=-1;
}
float healthdecrement=1.0f/16384;
void Avatar::tick(shared_ptr<Thing>&location, unsigned int myindex) {
Container::tick(location,myindex);
Container * place=static_cast<Container*>(location.get());
float tmp1=(float)floor(health);
health-=maintenance;
float tmp2=(float)floor(health);
if (health<0){
Die(myindex,*place);
return;
}
if (tmp1!=tmp2) {
hungermessage();
}
bool bytestoread=INET_BytesToRead(sock);
if (lastactiontime>0)
--lastactiontime;
bool failed=false;
if ( (bytestoread||orderqueue.length())&&lastactiontime<=0) {
failed=true;
char cmd[16];
string arg;
int rsize;
if (bytestoread&&(rsize=INET_Recv(sock,cmd,16))<=0) {
sock=-1;
Die(myindex,*place);
return;
}
if (bytestoread){
orderqueue+=string(cmd,rsize);
}
if (orderqueue.find('\n')!=string::npos) {
float tmp1=(float)floor(health);
health-=thoughtdecrement;
float tmp2=(float)floor(health);
if (tmp1!=tmp2)
hungermessage();
switch(orderqueue[0]) {
case 'b'://bite
for (ptrdiff_t index=place->numItems()-1;index>=0;--index) {
Thing*thang;
if (index!=myindex&&(arg=="all"||(thang=place->examineItem(index))->description()==arg)) {
if (!bite(*thang,*place)) {
//return failure
}else {
failed=false;
lastactiontime+=bitetimecost;
}
}
}
break;
case 'g'://get
arg=getSArg(orderqueue);
for (ptrdiff_t index=place->numItems()-1;index>=0;--index) {
if (index!=myindex&&(arg=="all"||place->examineItem(index)->description()==arg)) {
if (!getThing(index,*place)) {
//return failure
}else {
failed=false;
lastactiontime+=gettimecost;
privateMessage(INVENTORY,this->look(*this));
}
}
}
break;
case 's':
failed=false;
lastactiontime+=speaktimecost;
{
string msg=getSArg(orderqueue);
for (int index=0;index<place->numItems();++index) {
if (index!=myindex) {
place->examineItem(index)->privateMessage(HEAR,msg);
}
}
}
break;
case '!':
failed=false;
lastactiontime+=shouttimecost;
{
string msg=getSArg(orderqueue);
for (int index=0;index<place->numItems();++index) {
if (index!=myindex) {
place->examineItem(index)->privateMessage(HEAR,msg);
}
}
for (map<string,weak_ptr<Room> >::iterator link=place->exits()->begin();
link!=place->exits()->end();
++link) {
shared_ptr<Room> linkdest(link->second.lock());
map<string,weak_ptr<Room> >::iterator returnlink=linkdest->exits()->begin();
map<string,weak_ptr<Room> >::iterator returnlinkend=linkdest->exits()->end();
string prefix;
for (;returnlink!=returnlinkend;++returnlink) {
shared_ptr<Room> tmp(returnlink->second.lock());
if (tmp.get()==place) {
prefix=returnlink->first+':';
break;
}
}
prefix+=msg;
int numitems=linkdest->numItems();
for (int index=0;index<numitems;++index) {
linkdest->examineItem(index)->privateMessage(HEAR,prefix);
}
}
}
break;
case 'm'://move
if (!Move(getSArg(orderqueue),*place,myindex)) {
//return failure
}else {
failed=false;
lastactiontime+=movetimecost;
}
break;
case 'e'://eat
if (!Eat(getSArg(orderqueue))) {
//return failure
}else {
failed=false;
lastactiontime+=eattimecost;
privateMessage(INVENTORY,this->look(*this));
}
break;
case 'c'://combine
lastactiontime+=combinetimecost;
arg=getSArg(orderqueue);
if ((arg=Combine(arg)).length()!=0) {
privateMessage(SEE,arg);
failed=false;
privateMessage(INVENTORY,this->look(*this));
}else {
}
break;
case 'i'://inventory
getSArg(orderqueue);
privateMessage(INVENTORY,this->look(*this));
failed=false;
break;
case 'l':
getSArg(orderqueue);
privateMessage(SEE,place->look(*this));
failed=false;
break;
case 'r'://reproduce
arg=getSArg(orderqueue);
for (int index=0;index<place->numItems();++index) {
if (index!=myindex&&(arg=="all"||place->examineItem(index)->description()==arg)) {
Avatar * target=dynamic_cast<Avatar*>(place->examineItem(index));
if (target) {
if (!this->reproduce(*target,location)) {
//return failure
lastactiontime+=reproducefailcost;
}else {
failed=false;
lastactiontime+=reproducetimecost;
break;
}
}
}
}
break;
case 'x'://allow reproduce
lastactiontime+=0;
arg=getSArg(orderqueue);
for (int index=0;index<place->numItems();++index) {
if (index!=myindex&&(arg=="all"||place->examineItem(index)->description()==arg)) {
if (!this->consentreproduce(*place->examineItem(index),location)) {
//return failure
}else{
failed=false;
}
}
}
break;
case 'y'://disallow reproduce
lastactiontime+=0;
arg=getSArg(orderqueue);
this->noconsentreproduce();
failed=false;
break;
case 'd'://change description
lastactiontime+=descriptiontimecost;
desc=getSArg(orderqueue);
failed=false;
break;
default:
printf ("unknown command %s\n",orderqueue.c_str());
getSArg(orderqueue);//clear it out
break;
}
}
}
if (failed) {
privateMessage(FEEL,"failure");
}
//FIXME process client commands
//FIXME send update to client
}
void Avatar::addHealth(float health) {
this->health+=health;
if (this->health>MAXHEALTH)
this->health=MAXHEALTH;
if (health>0) {
privateMessage(TASTE,"tasty");
}else {
privateMessage(TASTE,"foul");
}
}
typedef std::pair<Thing*,Thing*> TwoThings;
bool operator < (const TwoThings&a, const TwoThings &b) {
if (a.first<b.first) return true;
if (a.first==b.first) return a.second<b.second;
return false;
}
set <TwoThings> consent;
vector<std::pair<GeneticCode,std::pair<float,shared_ptr<Thing> > > >newlyborngrue;
vector<std::pair<GeneticCode,std::pair<float,shared_ptr<Thing> > > >newlybornsnark;
bool Grue::canConnect() {
return newlyborngrue.size()>1;
}
bool Snark::canConnect() {
return newlybornsnark.size()>1;
}
bool hasConsentDestructive(Avatar* thus, Avatar*partner) {
set<TwoThings>::iterator iter=consent.find(TwoThings(partner,thus));
if (iter!=consent.end()) {
consent.erase(iter);
return true;
}
return false;
}
bool Grue::reproduce(Avatar&who, shared_ptr<Thing>&currentLocation) {
Grue*partner;
if (!(partner=dynamic_cast<Grue*>(&who))) {
return false;
}
if (hasConsentDestructive(this,partner)) {
health-=grueinitrepro;
who.health-=gruerepro;
GeneticCode combinedcode(code);
if (combinedcode.combine(code,partner->code)) {
privateMessage(FEEL,"happy");
partner->privateMessage(FEEL,"happy");
float h=stdrepro+stdinitrepro;
if (health<0) h+=health; //dying soon, not a healthy child
if (who.health<0) h+=who.health;//dying soon
newlyborngrue.push_back(std::pair<GeneticCode,std::pair<float,shared_ptr<Thing> > >(combinedcode,std::pair<float,shared_ptr<Thing> > (h,currentLocation)));
}else {
privateMessage(FEEL,"sad");
partner->privateMessage(FEEL,"sad");
return false;
}
}else {
return false;
}
return true;
}
bool Snark::reproduce(Avatar&who, shared_ptr<Thing>&currentLocation) {
Snark*partner;
if (!(partner=dynamic_cast<Snark*>(&who))) {
return false;
}
if (hasConsentDestructive(this,partner)) {
who.health-=stdrepro;
health-=stdinitrepro;
GeneticCode combinedcode(code);
if (combinedcode.combine(code,partner->code)) {
float h=stdrepro+stdinitrepro;
if (health<0) h+=health; //dying soon, not a healthy child
if (who.health<0) h+=who.health;//dying soon
newlybornsnark.push_back(std::pair<GeneticCode,std::pair<float,shared_ptr<Thing> > >(combinedcode,std::pair<float,shared_ptr<Thing> > (h,currentLocation)));
privateMessage(FEEL,"happy");
partner->privateMessage(FEEL,"happy");
}else {
privateMessage(FEEL,"sad");
partner->privateMessage(FEEL,"sad");
return false;
}
}else {
return false;
}
return true;
}
bool Avatar::noconsentreproduce() {
set<TwoThings>::iterator where;
while((where=consent.lower_bound(TwoThings(this,NULL)))!=consent.end()) {
if (where->first!=this) break;
consent.erase(where);
}
return true;
}
bool Avatar::consentreproduce(Thing&who, shared_ptr<Thing>&currentLocation) {
consent.insert(TwoThings(this,&who));
return true;
}
string Avatar::Combine(string desc) {
string retval;
static string food("food");
static string poison("poison");
int poisoncount=0;
int foodcount=0;
for (std::vector<shared_ptr<Thing> >::iterator it=items.begin(),end=items.end();
it!=end;
++it) {
if (it->get()->description()==food) foodcount++;
if (it->get()->description()==poison) poisoncount++;
}
if (poisoncount>=2&&foodcount>0) {
int deadpoison=0;
for (ptrdiff_t index=items.size()-1;index>=0;--index) {
if (items[index]->description()==poison){
items.erase(items.begin()+index);
deadpoison++;
if (deadpoison==2) break;
}
}
shared_ptr<Thing>myfood(new FoodMeal());
this->addThing(myfood);
retval=myfood->description();
}
return retval;
}
bool Avatar::Eat(string desc) {
bool all=(desc=="all");
bool gotone=false;
for (ptrdiff_t index=items.size()-1;
index>=0;
--index) {
if (desc==items[index]->description()||all) {
if (items[index]->EatMe(*this)) {
if(index+1!=items.size()) {
items[index]=items.back();
}
items.pop_back();
gotone=true;
if (!all)
return true;
}
}
}
if (gotone) return true;
return false;
}
Avatar::~Avatar() {
if (sock!=-1)
INET_close(sock);
std::cout << description()<<" died\n";
noconsentreproduce();//so no leakage
}
Grue::~Grue(){}
Snark::~Snark(){}
bool Grue::getThing(unsigned int thingindex, Container&previous) {
Thing* item=previous.examineItem(thingindex);
Grue * tmp=dynamic_cast<Grue*> (item);
if (tmp) return false;
Avatar * tmp1=dynamic_cast<Avatar*> (item);
if (tmp1&&previous.lightness()>.5) {
return false;
}
if (!tmp1) return false;//carnivore
return this->Avatar::getThing(thingindex,previous);
}
bool Grue::takeMe(Container& destination, Container& currentLocation) {
return dynamic_cast<Room*>(&destination)!=NULL;///impervious to attack
}
bool Snark::EatMe(Avatar& thing) {
thing.addHealth(health);
return true;
}
bool Snark::getThing(unsigned int thingindex, Container&previous) {
Thing* item=previous.examineItem(thingindex);
Avatar * tmp=dynamic_cast<Avatar*> (item);
if (tmp)return false;
return Avatar::getThing(thingindex,previous);
}
bool Snark::takeMe(Container& destination, Container& currentLocation) {
if (dynamic_cast<Room*>(&destination)!=NULL)return true;
float tot=0;
for (std::vector<shared_ptr<Thing> >::iterator it=currentLocation.items.begin(),end=currentLocation.items.end();
it!=end;
++it) {
Thing*tmp=it->get();
Snark* healther;
if (tmp!=this&&(healther=dynamic_cast<Snark*>(tmp))!=NULL) {
tot+=healther->health;
}
}
if (tot>MAXHEALTH*.25)
return false;
return Avatar::takeMe(destination,currentLocation);
}
void Avatar::SendCode() {
string codedata=code.tostring();
size_t len=codedata.length();
unsigned char dat[4]={(len/(65536*256))%256,
(len/(65536))%256,
(len/(256))%256,
len%256};
INET_Write(sock,4,(char*)dat);
INET_Write(sock,len,codedata.data());
}
Grue::Grue (INETsocket controller):Avatar(controller) {
static string grue("Grue");
desc=grue;
health=grueinitrepro;
health+=gruerepro;
maintenance=gruemaint;//cheaper
if (newlyborngrue.size()) {
health=newlyborngrue.back().second.first;
code=newlyborngrue.back().first;
}
SendCode();
}
Snark::Snark (INETsocket controller):Avatar(controller) {
static string snark("Snark");
desc=snark;
health=stdinitrepro;
health+=stdrepro;
if (newlybornsnark.size()) {
health=newlybornsnark.back().second.first;
code=newlybornsnark.back().first;
}
SendCode();
}