void AI::obedientZombie(int index, std::deque<int> myOrders) { int x, y, humanIndex, wallIndex; Zombie* me = &zombies[index]; switch (myOrders.front()) { case 0: x = nextX(me->x(), me->y(), me->facing()); y = nextY(me->x(), me->y(), me->facing()); humanIndex = getHuman(x, y); if (humanIndex > -1) { myOrders.clear(); me->attack(humans[humanIndex]); } else me->move(); break; case 1: me->turn(-1); break; case 2: me->turn(1); break; } if (myOrders.size() > 0) myOrders.pop_front(); if (myOrders.size() > 0) orders[me->id()] = myOrders; else orders.erase(me->id()); }
char Agent::stuck() { int x = head().X; int y = head().Y; string moves; if(Team()==1) moves="uldr"; else moves="drul"; for(int i=0;i<3;i++) { Point nextp; nextp.X=nextX(x,moves[i]); nextp.Y=nextY(y,moves[i]); //cout << "distans for stuck " << getdistance(head(),nextp) << endl; if(isEmpty(nextp.X,nextp.Y)) //&& getdistance(head(),nextp)>1 ) { /*cout << "raft too if!!" << endl;*/ cout << "stuck "<< moves[i] << endl; return moves[i]; } } return moves[3]; }
//human i runs around randomly and attacks things if they get in the way void AI::scaredHuman(int i) { int attacks = 1; int moves; int tries; int dir; int x, y; int oldX, oldY; int zombieIndex, crateIndex; tries = 0; moves = humans[i].moves(); oldX = humans[i].x(); oldY = humans[i].y(); while ((moves > 0 || attacks > 0) && tries < 12) { dir = rand() % 6; x = nextX(oldX, oldY, dir); y = nextY(oldX, oldY, dir); zombieIndex = getZombie(x, y); crateIndex = getCrate(x, y); if (crateIndex > -1) { humans[i].grab(crates[crateIndex]); } if (zombieIndex == -1 && getWall(x, y) == -1 && moves > 0) { humans[i].move(x, y); oldX = x; oldY = y; moves -= 1; } else if (zombieIndex > -1 && attacks > 0) { humans[i].attack(zombies[zombieIndex]); attacks -= 1; } tries += 1; } }
//Attacks nearby zombies, then builds walls around himself. //Does not move. void AI::builderHuman(int i) { int attacks = 1; int tries = 0; int dir; int x, y; int zombieIndex, crateIndex, humanIndex, wallIndex; int moves = humans[i].moves(); while ((moves > 0 || attacks > 0) && tries < 12) { dir = rand() % 6; x = nextX(humans[i].x(), humans[i].y(), dir); y = nextY(humans[i].x(), humans[i].y(), dir); zombieIndex = getZombie(x, y); crateIndex = getCrate(x, y); humanIndex = getHuman(x, y); wallIndex = getHuman(x, y); if (crateIndex > -1) { humans[i].grab(crates[crateIndex]); } if (zombieIndex == -1 && humanIndex == -1 && moves > 0) { humans[i].build(x, y); moves -= 1; } else if (zombieIndex > -1 && attacks > 0) { humans[i].attack(zombies[zombieIndex]); attacks -= 1; } tries += 1; } }
//Zombie i is given orders based soley on its smell and the object // directly in front of them. void AI::blindZombie(int i) { int x, y, wallIndex, humanIndex, zombieIndex; bool allowEat = (int(getZombieCap()) == zombies.size()); if (zombies[i].smell() == 0) { zombies[i].turn((rand()%2) * 2 - 1); } else { x = nextX(zombies[i].x(), zombies[i].y(), zombies[i].facing()); y = nextY(zombies[i].x(), zombies[i].y(), zombies[i].facing()); wallIndex = getWall(x, y); humanIndex = getHuman(x, y); zombieIndex = getZombie(x, y); if (humanIndex > -1) { zombies[i].attack(humans[humanIndex]); } else if (wallIndex > -1) { zombies[i].attack(walls[wallIndex]); } else if (zombieIndex > -1 && allowEat) { zombies[i].eat(zombies[zombieIndex]); eaten += 1; } else { zombies[i].move(); } } }
void rayCast(Vector2i src, Vector2i dst, RAY_CALLBACK callback, void *data) { if (!callback(src, 0, data) || src == dst) // Start at src. { return; // Callback gave up after the first point, or there are no other points. } Vector2i srcM = map_coord(src); Vector2i dstM = map_coord(dst); Vector2i step, tile, cur, end; initSteps(srcM.x, dstM.x, tile.x, step.x, cur.x, end.x); initSteps(srcM.y, dstM.y, tile.y, step.y, cur.y, end.y); Vector2i prev(0, 0); // Dummy initialisation. bool first = true; Vector2i nextX(0, 0), nextY(0, 0); // Dummy initialisations. bool canX = tryStep(tile.x, step.x, cur.x, end.x, nextX.x, nextX.y, src.x, src.y, dst.x, dst.y); bool canY = tryStep(tile.y, step.y, cur.y, end.y, nextY.y, nextY.x, src.y, src.x, dst.y, dst.x); while (canX || canY) { int32_t xDist = abs(nextX.x - src.x) + abs(nextX.y - src.y); int32_t yDist = abs(nextY.x - src.x) + abs(nextY.y - src.y); Vector2i sel; Vector2i selTile; if (canX && (!canY || xDist < yDist)) // The line crosses a vertical grid line next. { sel = nextX; selTile = tile; canX = tryStep(tile.x, step.x, cur.x, end.x, nextX.x, nextX.y, src.x, src.y, dst.x, dst.y); } else // The line crosses a horizontal grid line next. { assert(canY); sel = nextY; selTile = tile; canY = tryStep(tile.y, step.y, cur.y, end.y, nextY.y, nextY.x, src.y, src.x, dst.y, dst.x); } if (!first) { // Find midpoint. Vector2i avg = (prev + sel) / 2; // But make sure it's on the right tile, since it could be off-by-one if the line passes exactly through a grid intersection. avg.x = std::min(std::max(avg.x, world_coord(selTile.x)), world_coord(selTile.x + 1) - 1); avg.y = std::min(std::max(avg.y, world_coord(selTile.y)), world_coord(selTile.y + 1) - 1); if (!worldOnMap(avg) || !callback(avg, iHypot(avg), data)) { return; // Callback doesn't want any more points, or we reached the edge of the map, so return. } } prev = sel; first = false; } // Include the endpoint. if (!worldOnMap(dst)) { return; // Stop, since reached the edge of the map. } callback(dst, iHypot(dst), data); }
/*! This function decodes some data into image changes. Returns the number of bytes consumed. */ int QGIFFormat::decode(QImage *image, const uchar *buffer, int length, int *nextFrameDelay, int *loopCount, QSize *nextSize) { // We are required to state that // "The Graphics Interchange Format(c) is the Copyright property of // CompuServe Incorporated. GIF(sm) is a Service Mark property of // CompuServe Incorporated." #define LM(l, m) (((m)<<8)|l) digress = false; int initial = length; while (!digress && length) { length--; unsigned char ch=*buffer++; switch (state) { case Header: hold[count++]=ch; if (count==6) { // Header gif89=(hold[3]!='8' || hold[4]!='7'); state=LogicalScreenDescriptor; count=0; } break; case LogicalScreenDescriptor: hold[count++]=ch; if (count==7) { // Logical Screen Descriptor swidth=LM(hold[0], hold[1]); sheight=LM(hold[2], hold[3]); gcmap=!!(hold[4]&0x80); //UNUSED: bpchan=(((hold[4]&0x70)>>3)+1); //UNUSED: gcmsortflag=!!(hold[4]&0x08); gncols=2<<(hold[4]&0x7); bgcol=(gcmap) ? hold[5] : -1; //aspect=hold[6] ? double(hold[6]+15)/64.0 : 1.0; trans_index = -1; count=0; ncols=gncols; if (gcmap) { ccount=0; state=GlobalColorMap; globalcmap = new QRgb[gncols+1]; // +1 for trans_index globalcmap[gncols] = Q_TRANSPARENT; } else { state=Introducer; } } break; case GlobalColorMap: case LocalColorMap: hold[count++]=ch; if (count==3) { QRgb rgb = qRgb(hold[0], hold[1], hold[2]); if (state == LocalColorMap) { if (ccount < lncols) localcmap[ccount] = rgb; } else { globalcmap[ccount] = rgb; } if (++ccount >= ncols) { if (state == LocalColorMap) state=TableImageLZWSize; else state=Introducer; } count=0; } break; case Introducer: hold[count++]=ch; switch (ch) { case ',': state=ImageDescriptor; break; case '!': state=ExtensionLabel; break; case ';': // ### Changed: QRect(0, 0, swidth, sheight) state=Done; break; default: digress=true; // Unexpected Introducer - ignore block state=Error; } break; case ImageDescriptor: hold[count++]=ch; if (count==10) { int newleft=LM(hold[1], hold[2]); int newtop=LM(hold[3], hold[4]); int newwidth=LM(hold[5], hold[6]); int newheight=LM(hold[7], hold[8]); // disbelieve ridiculous logical screen sizes, // unless the image frames are also large. if (swidth/10 > qMax(newwidth,200)) swidth = -1; if (sheight/10 > qMax(newheight,200)) sheight = -1; if (swidth <= 0) swidth = newleft + newwidth; if (sheight <= 0) sheight = newtop + newheight; QImage::Format format = trans_index >= 0 ? QImage::Format_ARGB32 : QImage::Format_RGB32; if (image->isNull() || (image->size() != QSize(swidth, sheight)) || image->format() != format) { (*image) = QImage(swidth, sheight, format); memset(image->bits(), 0, image->numBytes()); // ### size of the upcoming frame, should rather // be known before decoding it. *nextSize = QSize(swidth, sheight); } disposePrevious(image); disposed = false; left = newleft; top = newtop; width = newwidth; height = newheight; right=qMax(0, qMin(left+width, swidth)-1); bottom=qMax(0, qMin(top+height, sheight)-1); lcmap=!!(hold[9]&0x80); interlace=!!(hold[9]&0x40); //bool lcmsortflag=!!(hold[9]&0x20); lncols=lcmap ? (2<<(hold[9]&0x7)) : 0; if (lncols) { if (localcmap) delete [] localcmap; localcmap = new QRgb[lncols+1]; localcmap[lncols] = Q_TRANSPARENT; ncols = lncols; } else { ncols = gncols; } frame++; if (frame == 0) { if (left || top || width<swidth || height<sheight) { // Not full-size image - erase with bg or transparent if (trans_index >= 0) { fillRect(image, 0, 0, swidth, sheight, color(trans_index)); // ### Changed: QRect(0, 0, swidth, sheight) } else if (bgcol>=0) { fillRect(image, 0, 0, swidth, sheight, color(bgcol)); // ### Changed: QRect(0, 0, swidth, sheight) } } } if (disposal == RestoreImage) { int l = qMin(swidth-1,left); int r = qMin(swidth-1,right); int t = qMin(sheight-1,top); int b = qMin(sheight-1,bottom); int w = r-l+1; int h = b-t+1; if (backingstore.width() < w || backingstore.height() < h) { // We just use the backing store as a byte array backingstore = QImage(qMax(backingstore.width(), w), qMax(backingstore.height(), h), QImage::Format_RGB32); memset(image->bits(), 0, image->numBytes()); } for (int ln=0; ln<h; ln++) { memcpy(backingstore.scanLine(ln), image->scanLine(t+ln)+l, w*sizeof(QRgb)); } } count=0; if (lcmap) { ccount=0; state=LocalColorMap; } else { state=TableImageLZWSize; } x = left; y = top; accum = 0; bitcount = 0; sp = stack; firstcode = oldcode = 0; needfirst = true; out_of_bounds = left>=swidth || y>=sheight; } break; case TableImageLZWSize: { lzwsize=ch; if (lzwsize > max_lzw_bits) { state=Error; } else { code_size=lzwsize+1; clear_code=1<<lzwsize; end_code=clear_code+1; max_code_size=2*clear_code; max_code=clear_code+2; int i; for (i=0; i<clear_code; i++) { table[0][i]=0; table[1][i]=i; } state=ImageDataBlockSize; } count=0; break; } case ImageDataBlockSize: expectcount=ch; if (expectcount) { state=ImageDataBlock; } else { state=Introducer; digress = true; newFrame = true; } break; case ImageDataBlock: count++; accum|=(ch<<bitcount); bitcount+=8; while (bitcount>=code_size && state==ImageDataBlock) { int code=accum&((1<<code_size)-1); bitcount-=code_size; accum>>=code_size; if (code==clear_code) { if (!needfirst) { code_size=lzwsize+1; max_code_size=2*clear_code; max_code=clear_code+2; } needfirst=true; } else if (code==end_code) { bitcount = -32768; // Left the block end arrive } else { if (needfirst) { firstcode=oldcode=code; if (!out_of_bounds && image->height() > y && firstcode!=trans_index) ((QRgb*)image->scanLine(y))[x] = color(firstcode); x++; if (x>=swidth) out_of_bounds = true; needfirst=false; if (x>=left+width) { x=left; out_of_bounds = left>=swidth || y>=sheight; nextY(image); } } else { incode=code; if (code>=max_code) { *sp++=firstcode; code=oldcode; } while (code>=clear_code+2) { *sp++=table[1][code]; if (code==table[0][code]) { state=Error; break; } if (sp-stack>=(1<<(max_lzw_bits))*2) { state=Error; break; } code=table[0][code]; } *sp++=firstcode=table[1][code]; code=max_code; if (code<(1<<max_lzw_bits)) { table[0][code]=oldcode; table[1][code]=firstcode; max_code++; if ((max_code>=max_code_size) && (max_code_size<(1<<max_lzw_bits))) { max_code_size*=2; code_size++; } } oldcode=incode; const int h = image->height(); const QRgb *map = lcmap ? localcmap : globalcmap; QRgb *line = 0; if (!out_of_bounds && h > y) line = (QRgb*)image->scanLine(y); while (sp>stack) { const uchar index = *(--sp); if (!out_of_bounds && h > y && index!=trans_index) { if (index > ncols) line[x] = Q_TRANSPARENT; else line[x] = map ? map[index] : 0; } x++; if (x>=swidth) out_of_bounds = true; if (x>=left+width) { x=left; out_of_bounds = left>=swidth || y>=sheight; nextY(image); if (!out_of_bounds && h > y) line = (QRgb*)image->scanLine(y); } } } } } partialNewFrame = true; if (count==expectcount) { count=0; state=ImageDataBlockSize; } break; case ExtensionLabel: switch (ch) { case 0xf9: state=GraphicControlExtension; break; case 0xff: state=ApplicationExtension; break; #if 0 case 0xfe: state=CommentExtension; break; case 0x01: break; #endif default: state=SkipBlockSize; } count=0; break; case ApplicationExtension: if (count<11) hold[count]=ch; count++; if (count==hold[0]+1) { if (qstrncmp((char*)(hold+1), "NETSCAPE", 8)==0) { // Looping extension state=NetscapeExtensionBlockSize; } else { state=SkipBlockSize; } count=0; } break; case NetscapeExtensionBlockSize: expectcount=ch; count=0; if (expectcount) state=NetscapeExtensionBlock; else state=Introducer; break; case NetscapeExtensionBlock: if (count<3) hold[count]=ch; count++; if (count==expectcount) { *loopCount = hold[1]+hold[2]*256; state=SkipBlockSize; // Ignore further blocks } break; case GraphicControlExtension: if (count<5) hold[count]=ch; count++; if (count==hold[0]+1) { disposePrevious(image); disposal=Disposal((hold[1]>>2)&0x7); //UNUSED: waitforuser=!!((hold[1]>>1)&0x1); int delay=count>3 ? LM(hold[2], hold[3]) : 1; // IE and mozilla use a minimum delay of 10. With the minimum delay of 10 // we are compatible to them and avoid huge loads on the app and xserver. *nextFrameDelay = (delay < 2 ? 10 : delay) * 10; bool havetrans=hold[1]&0x1; trans_index = havetrans ? hold[4] : -1; count=0; state=SkipBlockSize; } break; case SkipBlockSize: expectcount=ch; count=0; if (expectcount) state=SkipBlock; else state=Introducer; break; case SkipBlock: count++; if (count==expectcount) state=SkipBlockSize; break; case Done: digress=true; /* Netscape ignores the junk, so we do too. length++; // Unget state=Error; // More calls to this is an error */ break; case Error: return -1; // Called again after done. } }
/*! This function decodes some data into image changes. Returns the number of bytes consumed. */ int QGIFFormat::decode(QImage& img, QImageConsumer* consumer, const uchar* buffer, int length) { // We are required to state that // "The Graphics Interchange Format(c) is the Copyright property of // CompuServe Incorporated. GIF(sm) is a Service Mark property of // CompuServe Incorporated." #define LM(l, m) (((m)<<8)|l) digress = FALSE; int initial = length; QRgb** line = (QRgb **)img.jumpTable(); while (!digress && length) { length--; unsigned char ch=*buffer++; switch (state) { case Header: hold[count++]=ch; if (count==6) { // Header gif89=(hold[3]!='8' || hold[4]!='7'); state=LogicalScreenDescriptor; count=0; } break; case LogicalScreenDescriptor: hold[count++]=ch; if (count==7) { // Logical Screen Descriptor swidth=LM(hold[0], hold[1]); sheight=LM(hold[2], hold[3]); gcmap=!!(hold[4]&0x80); //UNUSED: bpchan=(((hold[4]&0x70)>>3)+1); //UNUSED: gcmsortflag=!!(hold[4]&0x08); gncols=2<<(hold[4]&0x7); bgcol=(gcmap) ? hold[5] : -1; //aspect=hold[6] ? double(hold[6]+15)/64.0 : 1.0; trans_index = -1; count=0; ncols=gncols; if (gcmap) { ccount=0; state=GlobalColorMap; globalcmap = new QRgb[gncols+1]; // +1 for trans_index globalcmap[gncols] = Q_TRANSPARENT; } else { state=Introducer; } } break; case GlobalColorMap: case LocalColorMap: hold[count++]=ch; if (count==3) { QRgb rgb = qRgb(hold[0], hold[1], hold[2]); if ( state == LocalColorMap ) { if ( ccount < lncols ) localcmap[ccount] = rgb; } else { globalcmap[ccount] = rgb; } if (++ccount >= ncols) { if ( state == LocalColorMap ) state=TableImageLZWSize; else state=Introducer; } count=0; } break; case Introducer: hold[count++]=ch; switch (ch) { case ',': state=ImageDescriptor; break; case '!': state=ExtensionLabel; break; case ';': if (consumer) { if ( out_of_bounds ) // flush anything that survived consumer->changed(QRect(0,0,swidth,sheight)); consumer->end(); } state=Done; break; default: digress=TRUE; // Unexpected Introducer - ignore block state=Error; } break; case ImageDescriptor: hold[count++]=ch; if (count==10) { int newleft=LM(hold[1], hold[2]); int newtop=LM(hold[3], hold[4]); int width=LM(hold[5], hold[6]); int height=LM(hold[7], hold[8]); // disbelieve ridiculous logical screen sizes, // unless the image frames are also large. if ( swidth/10 > QMAX(width,200) ) swidth = -1; if ( sheight/10 > QMAX(height,200) ) sheight = -1; if ( swidth <= 0 ) swidth = newleft + width; if ( sheight <= 0 ) sheight = newtop + height; if (img.isNull()) { img.create(swidth, sheight, 32); memset( img.bits(), 0, img.numBytes() ); if (consumer) consumer->setSize(swidth, sheight); } img.setAlphaBuffer(trans_index >= 0); line = (QRgb **)img.jumpTable(); disposePrevious( img, consumer ); disposed = FALSE; left = newleft; top = newtop; // Sanity check frame size - must fit on "screen". if (left >= swidth) left=QMAX(0, swidth-1); if (top >= sheight) top=QMAX(0, sheight-1); if (left+width >= swidth) { if ( width <= swidth ) left=swidth-width; else width=swidth-left; } if (top+height >= sheight) { if ( height <= sheight ) top=sheight-height; else height=sheight-top; } right=QMAX( 0, left+width-1); bottom=QMAX(0, top+height-1); lcmap=!!(hold[9]&0x80); interlace=!!(hold[9]&0x40); //bool lcmsortflag=!!(hold[9]&0x20); lncols=lcmap ? (2<<(hold[9]&0x7)) : 0; if (lncols) { if ( localcmap ) delete [] localcmap; localcmap = new QRgb[lncols+1]; localcmap[lncols] = Q_TRANSPARENT; ncols = lncols; } else { ncols = gncols; } frame++; if ( frame == 0 ) { if ( left || top || width!=swidth || height!=sheight ) { // Not full-size image - erase with bg or transparent if ( trans_index >= 0 ) { fillRect(img, 0, 0, swidth, sheight, color(trans_index)); if (consumer) consumer->changed(QRect(0,0,swidth,sheight)); } else if ( bgcol>=0 ) { fillRect(img, 0, 0, swidth, sheight, color(bgcol)); if (consumer) consumer->changed(QRect(0,0,swidth,sheight)); } } } if ( disposal == RestoreImage ) { int l = QMIN(swidth-1,left); int r = QMIN(swidth-1,right); int t = QMIN(sheight-1,top); int b = QMIN(sheight-1,bottom); int w = r-l+1; int h = b-t+1; if (backingstore.width() < w || backingstore.height() < h) { // We just use the backing store as a byte array backingstore.create( QMAX(backingstore.width(), w), QMAX(backingstore.height(), h), 32); memset( img.bits(), 0, img.numBytes() ); } for (int ln=0; ln<h; ln++) { memcpy(backingstore.scanLine(ln), line[t+ln]+l, w*sizeof(QRgb)); } } count=0; if (lcmap) { ccount=0; state=LocalColorMap; } else { state=TableImageLZWSize; } x = left; y = top; accum = 0; bitcount = 0; sp = stack; needfirst = FALSE; out_of_bounds = FALSE; } break; case TableImageLZWSize: { lzwsize=ch; if ( lzwsize > max_lzw_bits ) { state=Error; } else { code_size=lzwsize+1; clear_code=1<<lzwsize; end_code=clear_code+1; max_code_size=2*clear_code; max_code=clear_code+2; int i; for (i=0; i<clear_code && i<(1<<max_lzw_bits); i++) { table[0][i]=0; table[1][i]=i; } for (i=clear_code; i<(1<<max_lzw_bits); i++) { table[0][i]=table[1][i]=0; } state=ImageDataBlockSize; } count=0; break; } case ImageDataBlockSize: expectcount=ch; if (expectcount) { state=ImageDataBlock; } else { if (consumer) { consumer->frameDone(); digress = TRUE; } state=Introducer; } break; case ImageDataBlock: count++; accum|=(ch<<bitcount); bitcount+=8; while (bitcount>=code_size && state==ImageDataBlock) { int code=accum&((1<<code_size)-1); bitcount-=code_size; accum>>=code_size; if (code==clear_code) { if (!needfirst) { int i; code_size=lzwsize+1; max_code_size=2*clear_code; max_code=clear_code+2; for (i=0; i<clear_code; i++) { table[0][i]=0; table[1][i]=i; } for (i=clear_code; i<(1<<max_lzw_bits); i++) { table[0][i]=table[1][i]=0; } } needfirst=TRUE; } else if (code==end_code) { bitcount = -32768; // Left the block end arrive } else { if (needfirst) { firstcode=oldcode=code; if (!out_of_bounds && line && firstcode!=trans_index) line[y][x] = color(firstcode); x++; if (x>=swidth) out_of_bounds = TRUE; needfirst=FALSE; if (x>right) { x=left; if (out_of_bounds) out_of_bounds = left>=swidth || y>=sheight; nextY(img,consumer); } } else { incode=code; if (code>=max_code) { *sp++=firstcode; code=oldcode; } while (code>=clear_code) { *sp++=table[1][code]; if (code==table[0][code]) { state=Error; break; } if (sp-stack>=(1<<(max_lzw_bits))*2) { state=Error; break; } code=table[0][code]; } *sp++=firstcode=table[1][code]; code=max_code; if (code<(1<<max_lzw_bits)) { table[0][code]=oldcode; table[1][code]=firstcode; max_code++; if ((max_code>=max_code_size) && (max_code_size<(1<<max_lzw_bits))) { max_code_size*=2; code_size++; } } oldcode=incode; while (sp>stack) { --sp; if (!out_of_bounds && *sp!=trans_index) line[y][x] = color(*sp); x++; if (x>=swidth) out_of_bounds = TRUE; if (x>right) { x=left; if (out_of_bounds) out_of_bounds = left>=swidth || y>=sheight; nextY(img,consumer); } } } } } if (count==expectcount) { count=0; state=ImageDataBlockSize; } break; case ExtensionLabel: switch (ch) { case 0xf9: state=GraphicControlExtension; break; case 0xff: state=ApplicationExtension; break; #if 0 case 0xfe: state=CommentExtension; break; case 0x01: break; #endif default: state=SkipBlockSize; } count=0; break; case ApplicationExtension: if (count<11) hold[count]=ch; count++; if (count==hold[0]+1) { if (qstrncmp((char*)(hold+1), "NETSCAPE", 8)==0) { // Looping extension state=NetscapeExtensionBlockSize; } else { state=SkipBlockSize; } count=0; } break; case NetscapeExtensionBlockSize: expectcount=ch; count=0; if (expectcount) state=NetscapeExtensionBlock; else state=Introducer; break; case NetscapeExtensionBlock: if (count<3) hold[count]=ch; count++; if (count==expectcount) { int loop = hold[0]+hold[1]*256; if (consumer) consumer->setLooping(loop); state=SkipBlockSize; // Ignore further blocks } break; case GraphicControlExtension: if (count<5) hold[count]=ch; count++; if (count==hold[0]+1) { disposePrevious( img, consumer ); disposal=Disposal((hold[1]>>2)&0x7); //UNUSED: waitforuser=!!((hold[1]>>1)&0x1); int delay=count>3 ? LM(hold[2], hold[3]) : 1; // IE and mozilla use a minimum delay of 10. With the minumum delay of 10 // we are compatible to them and avoid huge loads on the app and xserver. if ( delay < 10 ) delay = 10; bool havetrans=hold[1]&0x1; trans_index = havetrans ? hold[4] : -1; if (consumer) consumer->setFramePeriod(delay*10); count=0; state=SkipBlockSize; } break; case SkipBlockSize: expectcount=ch; count=0; if (expectcount) state=SkipBlock; else state=Introducer; break; case SkipBlock: count++; if (count==expectcount) state=SkipBlockSize; break; case Done: digress=TRUE; /* Netscape ignores the junk, so we do too. length++; // Unget state=Error; // More calls to this is an error */ break; case Error: return -1; // Called again after done. } }
char Agent::percept() { int x = head().X; int y = head().Y; //cout << "head : " << x << " , " << y << endl; string moves; if(Team()==1) moves="drul"; //harkat da jahate padsaat gard else moves="uldr"; // harkat da jahate saat gard for (int i =0;i<map->MapSize.X;i++) for (int j =0;j<map->MapSize.Y;j++) mark[i][j]=false; //hameye mark haro false mikone! hamaro pak mikone for (int i =0;i<map->MapSize.X;i++) for (int j =0;j<map->MapSize.Y;j++) enemy_mark[i][j]=false; //hameye enemy_mark haro false mikone! hamaro pak mikone for (int i =0;i<4;i++) for (int j =0;j<4;j++) score[i][j]=0; //hameye score haro 0 mikone for (int move=0;move<4;move++) // emtiaz dehi be har harekat { Point here_now; here_now.X=nextX(x,moves[move]); here_now.Y=nextY(y,moves[move]); //mark[here_now.X][here_now.Y]=true;//jaii ke mire! if (isEmpty(here_now.X,here_now.Y)) // age por bood ke hichi! { //cout << "x and y now :" << here_now.X << " , "<<here_now.Y; score[move][1]=painting(here_now,'m')+1; //chand ta khoone mitoone bere //cout << "painting of " << moves[move] <<" is : "<< score[move][1] << endl; Point enemy_head=ehead(); //sare yaroo ro mirize to enemy_head int enemy_score[4]={0}; // emtiaz haro hame 0 mokine! enemy_mark[here_now.X][here_now.Y]=true; //injaii ro ke alan hast mark mikone! for (int enemy_move=0;enemy_move<4;enemy_move++) //baraye har harekat harif mikhad emtiaz hesab bokone { Point enemy_pos; enemy_pos.X=nextX(enemy_head.X,moves[enemy_move]); enemy_pos.Y=nextY(enemy_head.Y,moves[enemy_move]); enemy_mark[enemy_pos.X][enemy_pos.Y]=true; if (isEmpty(enemy_pos.X,enemy_pos.Y)) { enemy_score[enemy_move]=painting(enemy_pos,'e'); //cout << "\t painting enemy " << moves[enemy_move] << " is : "<< enemy_score[enemy_move] << endl; //cout << "\t"<< "enemy pos: "<< enemy_pos.X << " , " << enemy_pos.Y << endl; for (int i =0;i<map->MapSize.Y;i++) //enemy mark ro 0 mikone for (int j =0;j<map->MapSize.X;j++) enemy_mark[i][j]=false; //hameye enemy_mark haro false mikone! hamaro pak mikone } } int minus=findmin(enemy_score,4,0); for (int i=0;i<4;i++) { if (enemy_score[i]!=0) enemy_score[i]=enemy_score[i]-minus; score[move][2]+=enemy_score[i]; //cout << "\t now enemy score of " << moves[i] <<" is " << enemy_score[i] <<endl; } score[move][3]=getdistance(here_now,ehead()); if (score[move][3] <= 1) {/*cout << "nazdiiiiiiiiiiik!"<<endl;*/ stuck(); } for (int i =0;i<map->MapSize.Y;i++) for (int j =0;j<map->MapSize.X;j++) mark[i][j]=false; //hameye mark haro false mikone! } } int min=10000; for (int i=0;i<4;i++) if (score[i][1]<min && score[i][1]!=0) min = score[i][1]; for (int i=0;i<4;i++) if (score[i][1]!=0) { score[i][1]=score[i][1]-min; //cout << "now the painting of " <<moves[i] << " is : " << score[i][1] <<"and the min is " << min <<endl; } min=10000; int min_id; for (int i=0;i<4;i++) if (score[i][3]<min && score[i][3]!=0) { min = score[i][3]; min_id=i; } for (int i=0;i<4;i++) { if (i!=min_id) score[i][3]=0; else score[i][3]=2; /////////////////////////////////////////////////////////////////////////////////////////////////// } //cout << "now the closest is : "<< moves[min_id] << " Point : " << score[min_id][3] << endl; //jamm score ha for (int move=0;move<4;move++) { score[move][0]+=score[move][1]+score[move][3]-score[move][2]; } int max_char_id=0; int max_score=0; for (int move=0;move<4;move++) { if (max_score<score[move][0]) { //cout <<"score move "<< moves[move] <<" is bigger than previouse :"<< score[move][0] <<endl; max_score = score[move][0]; max_char_id = move; } } cout << "our algorithm output: "<< moves[max_char_id] <<endl << "\t and the score is " << max_score<<endl ; if (max_score!=0) return moves[max_char_id]; return stuck(); //return stuck(); }
//Zombie i is given orders based on an A* search to the nearest human // where nearest is defined by the zombieDistance function //Smart zombies will not break down walls, and will do nothing if // no path is available to the nearest human. void AI::smartZombie(int index) { const int FORGET = 5; //forget the last x of the generated path. const int REMEMBER = 5; //go no more than x turns without recalulating path int forgotten = 0; Zombie* me = &zombies[index]; Human* target = NULL; int bestDis = 9999999; int nextDis; int x, y, humanIndex, wallIndex; std::deque<int> path; std::cout << "smart" << std::endl; for (int i = 0; i < humans.size(); i++) { nextDis = zombieDistance(me->x(), me->y(), humans[i].x(), humans[i].y(), me->facing()); if (nextDis < bestDis) { bestDis = nextDis; target = &humans[i]; } } if (target != NULL) { path = findZombiePath(me->x(), me->y(), target->x(), target->y(), me->facing()); std::cout << "(" << me->x() << "," << me->y() << "," << target->x() << "," << target->y() << "," << me->facing() << ") = "; for (int k = 0; k < path.size(); k++) { std::cout << path[k] << " "; } if (path.size() == 0) { std::cout << "No action"; } std::cout << std::endl; if (path.size() > 1) { path.pop_front(); switch (path.front()) { case 0: x = nextX(me->x(), me->y(), me->facing()); y = nextY(me->x(), me->y(), me->facing()); humanIndex = getHuman(x, y); wallIndex = getWall(x, y); if (humanIndex > -1) me->attack(humans[humanIndex]); else if (wallIndex > -1) me->attack(walls[wallIndex]); else me->move(); break; case 1: me->turn(-1); break; case 2: me->turn(1); break; } /* while (path.size() > 0 && forgotten < FORGET) { path.pop_back(); forgotten += 1; } while (path.size() > REMEMBER) { path.pop_back(); } if (path.size() > 0) orders[me->id()] = path; */ } } std::cout << "end" << std::endl; }