void torped(int v __unused) { int ix, iy; double x, y, dx, dy; double angle; int course, course2; int k; double bigger; double sectsize; int burst; int n; if (Ship.cloaked) { printf("Federation regulations do not permit attack while cloaked.\n"); return; } if (check_out(TORPED)) return; if (Ship.torped <= 0) { printf("All photon torpedos expended\n"); return; } /* get the course */ course = getintpar("Torpedo course"); if (course < 0 || course > 360) return; burst = -1; /* need at least three torpedoes for a burst */ if (Ship.torped < 3) { printf("No-burst mode selected\n"); burst = 0; } else { /* see if the user wants one */ if (!testnl()) { k = ungetc(cgetc(0), stdin); if (k >= '0' && k <= '9') burst = 1; } } if (burst < 0) { burst = getynpar("Do you want a burst"); } if (burst) { burst = getintpar("burst angle"); if (burst <= 0) return; if (burst > 15) { printf("Maximum burst angle is 15 degrees\n"); return; } } sectsize = NSECTS; n = -1; if (burst) { n = 1; course -= burst; } for (; n && n <= 3; n++) { /* select a nice random course */ course2 = course + randcourse(n); angle = course2 * 0.0174532925; /* convert to radians */ dx = -cos(angle); dy = sin(angle); bigger = fabs(dx); x = fabs(dy); if (x > bigger) bigger = x; dx /= bigger; dy /= bigger; x = Ship.sectx + 0.5; y = Ship.secty + 0.5; if (Ship.cond != DOCKED) Ship.torped -= 1; printf("Torpedo track"); if (n > 0) printf(", torpedo number %d", n); printf(":\n%6.1f\t%4.1f\n", x, y); while (1) { ix = x += dx; iy = y += dy; if (x < 0.0 || x >= sectsize || y < 0.0 || y >= sectsize) { printf("Torpedo missed\n"); break; } printf("%6.1f\t%4.1f\n", x, y); switch (Sect[ix][iy]) { case EMPTY: continue; case HOLE: printf("Torpedo disappears into a black hole\n"); break; case KLINGON: for (k = 0; k < Etc.nkling; k++) { if (Etc.klingon[k].x != ix || Etc.klingon[k].y != iy) continue; Etc.klingon[k].power -= 500 + ranf(501); if (Etc.klingon[k].power > 0) { printf("*** Hit on Klingon at %d,%d: extensive damages\n", ix, iy); break; } killk(ix, iy); break; } break; case STAR: nova(ix, iy); break; case INHABIT: kills(ix, iy, -1); break; case BASE: killb(Ship.quadx, Ship.quady); Game.killb += 1; break; default: printf("Unknown object %c at %d,%d destroyed\n", Sect[ix][iy], ix, iy); Sect[ix][iy] = EMPTY; break; } break; } if (damaged(TORPED) || Quad[Ship.quadx][Ship.quady].stars < 0) break; course += burst; } Move.free = 0; }
void RobotAgent::reset() { //Initialize general _iterations = 0; _wm->_isAlive = true; /**/ int x = 0, y = 0; std::string s = ""; s += "agent["; std::stringstream out; out << _wm->_agentId; s += out.str(); s += "].x"; bool randomStart = false; if (gProperties.hasProperty(s)) convertFromString<int>(x, gProperties.getProperty(s), std::dec); else { if (gVerbose) { std::cout << "[warning] Initial X coordinate for agent #" << _wm->_agentId << " not found. Random pick ([!] reset may alter environment, check with x-ray mode [!])." << std::endl; } randomStart = true; x = (int) gAgentWidth + (ranf() * (double) (gSpawnWidth - gAgentWidth)); } s = "agent["; s += out.str(); s += "].y"; if (gProperties.hasProperty(s)) convertFromString<int>(y, gProperties.getProperty(s), std::dec); else { if (gVerbose) { std::cout << "[warning] Initial Y coordinate for agent #" << _wm->_agentId << " not found. Random pick ([!] reset may alter environment, check with x-ray mode [!])." << std::endl; } randomStart = true; y = (int) gAgentHeight + (ranf() * (double) (gSpawnHeight - gAgentHeight)); } setCoordReal(x, y); setCoord(x, y); while (randomStart && isCollision()) { //std::cout << "Collision detected on " << x << ", " << y << " rerolling" << std::endl; x = (int) 20 + (ranf() * (double) (gSpawnWidth)); y = (int) 20 + (ranf() * (double) (gSpawnHeight)); setCoordReal(x, y); setCoord(x, y); } // register agent so others don't start on top of it registerAgent(); //_wm->_xReal = x; //_wm->_yReal = y; /**/ //Initialize coordinate and displacement //_wm->_xReal=gAgentXStart; //_wm->_yReal=gAgentYStart; _xDelta = 0; _yDelta = 0; //Initialize internal variables _xDeltaReal = 0; //gAgentXStart; _yDeltaReal = 0; //gAgentYStart; s = "agent["; s += out.str(); s += "].orientation"; if (gProperties.hasProperty(s)) convertFromString<double>(_wm->_agentAbsoluteOrientation, gProperties.getProperty(s), std::dec); else { if (gVerbose) { std::cout << "[warning] Initial orientation for agent #" << _wm->_agentId << " not found. Random pick." << std::endl; } _wm->_agentAbsoluteOrientation = ranf() * 360. - 180.; } _wm->_agentAbsoluteLinearSpeed = 0; _wm->_desiredRotationalVelocity = 0; _wm->_desiredTranslationalValue = 0; _wm->_maxRotationalDeltaValue = gMaxRotationalSpeed; // ie. change will be instantenous _wm->_maxTranslationalDeltaValue = gMaxTranslationalDeltaValue; //gMaxTranslationalSpeed ; // idem. _wm->_actualTranslationalValue = 0; _wm->_actualRotationalVelocity = 0; for (int i = 0; i != _wm->_sensorCount; i++) // initialize sensor values to max range, no contact { _wm->_sensors[i][5] = gSensorRange; // range: max _wm->_sensors[i][6] = 0; // type: none } _wm->_floorSensor = 0; // floor sensor value (taken from gZoneImage) // Initialize agent observer and Behavior Control Architecture _agentObserver->reset(); _behavior->reset(); }
void klmove(int fl) { int n; struct kling *k; double dx, dy; int nextx, nexty; int lookx, looky; int motion; int fudgex, fudgey; int qx, qy; double bigger; int i; # ifdef xTRACE if (Trace) printf("klmove: fl = %d, Etc.nkling = %d\n", fl, Etc.nkling); # endif for (n = 0; n < Etc.nkling; n++) { k = &Etc.klingon[n]; i = 100; if (fl) i = 100.0 * k->power / Param.klingpwr; if (ranf(i) >= Param.moveprob[2 * Move.newquad + fl]) continue; /* compute distance to move */ motion = ranf(75) - 25; motion *= k->avgdist * Param.movefac[2 * Move.newquad + fl]; /* compute direction */ dx = Ship.sectx - k->x + ranf(3) - 1; dy = Ship.secty - k->y + ranf(3) - 1; bigger = dx; if (dy > bigger) bigger = dy; if (bigger == 0.0) bigger = 1.0; dx = dx / bigger + 0.5; dy = dy / bigger + 0.5; if (motion < 0) { motion = -motion; dx = -dx; dy = -dy; } fudgex = fudgey = 1; /* try to move the klingon */ nextx = k->x; nexty = k->y; for (; motion > 0; motion--) { lookx = nextx + dx; looky = nexty + dy; if (lookx < 0 || lookx >= NSECTS || looky < 0 || looky >= NSECTS) { /* new quadrant */ qx = Ship.quadx; qy = Ship.quady; if (lookx < 0) qx -= 1; else if (lookx >= NSECTS) qx += 1; if (looky < 0) qy -= 1; else if (looky >= NSECTS) qy += 1; if (qx < 0 || qx >= NQUADS || qy < 0 || qy >= NQUADS || Quad[qx][qy].stars < 0 || Quad[qx][qy].klings > MAXKLQUAD - 1) break; if (!damaged(SRSCAN)) { printf("Klingon at %d,%d escapes to quadrant %d,%d\n", k->x, k->y, qx, qy); motion = Quad[qx][qy].scanned; if (motion >= 0 && motion < 1000) Quad[qx][qy].scanned += 100; motion = Quad[Ship.quadx][Ship.quady].scanned; if (motion >= 0 && motion < 1000) Quad[Ship.quadx][Ship.quady].scanned -= 100; } Sect[k->x][k->y] = EMPTY; Quad[qx][qy].klings += 1; Etc.nkling -= 1; *k = Etc.klingon[Etc.nkling]; Quad[Ship.quadx][Ship.quady].klings -= 1; k = 0; break; } if (Sect[lookx][looky] != EMPTY) { lookx = nextx + fudgex; if (lookx < 0 || lookx >= NSECTS) lookx = nextx + dx; if (Sect[lookx][looky] != EMPTY) { fudgex = -fudgex; looky = nexty + fudgey; if (looky < 0 || looky >= NSECTS || Sect[lookx][looky] != EMPTY) { fudgey = -fudgey; break; } } } nextx = lookx; nexty = looky; } if (k && (k->x != nextx || k->y != nexty)) { if (!damaged(SRSCAN)) printf("Klingon at %d,%d moves to %d,%d\n", k->x, k->y, nextx, nexty); Sect[k->x][k->y] = EMPTY; Sect[k->x = nextx][k->y = nexty] = KLINGON; } } compkldist(0); }
/* ** Abandon Ship ** ** The ship is abandoned. If your current ship is the Faire ** Queene, or if your shuttlecraft is dead, you're out of ** luck. You need the shuttlecraft in order for the captain ** (that's you!!) to escape. ** ** Your crew can beam to an inhabited starsystem in the ** quadrant, if there is one and if the transporter is working. ** If there is no inhabited starsystem, or if the transporter ** is out, they are left to die in outer space. ** ** These currently just count as regular deaths, but they ** should count very heavily against you. ** ** If there are no starbases left, you are captured by the ** Klingons, who torture you mercilessly. However, if there ** is at least one starbase, you are returned to the ** Federation in a prisoner of war exchange. Of course, this ** can't happen unless you have taken some prisoners. ** ** Uses trace flag 40 */ void abandon() { register struct quad *q; register int i; int j; register struct event *e; if (Ship.ship == QUEENE) { printf("You may not abandon ye Faire Queene\n"); return; } if (Ship.cond != DOCKED) { if (damaged(SHUTTLE)) { out(SHUTTLE); return; } printf("Officers escape in shuttlecraft\n"); /* decide on fate of crew */ q = &Quad[Ship.quadx][Ship.quady]; if (q->qsystemname == 0 || damaged(XPORTER)) { printf("Entire crew of %d left to die in outer space\n", Ship.crew); Game.deaths += Ship.crew; } else { printf("Crew beams down to planet %s\n", systemname(q)); } } /* see if you can be exchanged */ if (Now.bases == 0 || Game.captives < 20 * Game.skill) lose(L_CAPTURED); /* re-outfit new ship */ printf("You are hereby put in charge of an antiquated but still\n"); printf(" functional ship, the Fairie Queene.\n"); Ship.ship = QUEENE; Ship.shipname = "Fairie Queene"; Param.energy = Ship.energy = 3000; Param.torped = Ship.torped = 6; Param.shield = Ship.shield = 1250; Ship.shldup = 0; Ship.cloaked = 0; Ship.warp = 5.0; Ship.warp2 = 25.0; Ship.warp3 = 125.0; Ship.cond = GREEN; /* clear out damages on old ship */ for (i = 0; i < MAXEVENTS; i++) { e = &Event[i]; if (e->evcode != E_FIXDV) continue; unschedule(e); } /* get rid of some devices and redistribute probabilities */ i = Param.damprob[SHUTTLE] + Param.damprob[CLOAK]; Param.damprob[SHUTTLE] = Param.damprob[CLOAK] = 0; while (i > 0) for (j = 0; j < NDEV; j++) { if (Param.damprob[j] != 0) { Param.damprob[j] += 1; i--; if (i <= 0) break; } } /* pick a starbase to restart at */ i = ranf(Now.bases); Ship.quadx = Now.base[i].x; Ship.quady = Now.base[i].y; /* setup that quadrant */ while (1) { initquad(1); Sect[Ship.sectx][Ship.secty] = EMPTY; for (i = 0; i < 5; i++) { Ship.sectx = Etc.starbase.x + ranf(3) - 1; if (Ship.sectx < 0 || Ship.sectx >= NSECTS) continue; Ship.secty = Etc.starbase.y + ranf(3) - 1; if (Ship.secty < 0 || Ship.secty >= NSECTS) continue; if (Sect[Ship.sectx][Ship.secty] == EMPTY) { Sect[Ship.sectx][Ship.secty] = QUEENE; dock(); compkldist(0); return; } } } }
int main(int argc, char *argv[]) { int x, y; int n; struct worm *w; struct options *op; int h; short *ip; int last, bottom; for (x=1;x<argc;x++) { register char *p; p=argv[x]; if (*p=='-') p++; switch (*p) { case 'f': field="WORM"; break; case 'l': if (++x==argc) goto usage; if ((length=atoi(argv[x]))<2||length>1024) { fprintf(stderr,"%s: Invalid length\n",*argv); exit(1); } break; case 'n': if (++x==argc) goto usage; if ((number=atoi(argv[x]))<1||number>40) { fprintf(stderr,"%s: Invalid number of worms\n",*argv); exit(1); } break; case 't': trail='.'; break; #ifdef TRACE case 'S': singlestep = TRUE; break; case 'T': trace_start = atoi(argv[++x]); trace_end = atoi(argv[++x]); break; case 'N': no_optimize = TRUE; /* declared by ncurses */ break; #endif /* TRACE */ default: usage: fprintf(stderr, "usage: %s [-field] [-length #] [-number #] [-trail]\n",*argv); exit(1); break; } } signal(SIGINT, onsig); initscr(); #ifdef TRACE noecho(); cbreak(); #endif /* TRACE */ nonl(); bottom = LINES-1; last = COLS-1; #ifdef A_COLOR if (has_colors()) { start_color(); init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK); init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK); init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK); init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK); init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK); init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK); init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK); flavor[0] |= COLOR_PAIR(COLOR_GREEN) | A_BOLD; flavor[1] |= COLOR_PAIR(COLOR_RED) | A_BOLD; flavor[2] |= COLOR_PAIR(COLOR_CYAN) | A_BOLD; flavor[3] |= COLOR_PAIR(COLOR_WHITE) | A_BOLD; flavor[4] |= COLOR_PAIR(COLOR_MAGENTA) | A_BOLD; flavor[5] |= COLOR_PAIR(COLOR_BLUE) | A_BOLD; flavor[6] |= COLOR_PAIR(COLOR_YELLOW) | A_BOLD; } #endif /* A_COLOR */ ip=(short *)malloc(LINES*COLS*sizeof (short)); for (n=0;n<LINES;) { ref[n++]=ip; ip+=COLS; } for (ip=ref[0],n=LINES*COLS;--n>=0;) *ip++=0; #ifdef BADCORNER /* if addressing the lower right corner doesn't work in your curses */ ref[bottom][last]=1; #endif /* BADCORNER */ for (n=number, w= &worm[0];--n>=0;w++) { w->orientation=w->head=0; if (!(ip=(short *)malloc((length+1)*sizeof (short)))) { fprintf(stderr,"%s: out of memory\n",*argv); exit(1); } w->xpos=ip; for (x=length;--x>=0;) *ip++ = -1; if (!(ip=(short *)malloc((length+1)*sizeof (short)))) { fprintf(stderr,"%s: out of memory\n",*argv); exit(1); } w->ypos=ip; for (y=length;--y>=0;) *ip++ = -1; } if (field) { register char *p; p=field; for (y=bottom;--y>=0;) { for (x=COLS;--x>=0;) { addch((chtype)(*p++)); if (!*p) p=field; } addch('\n'); } } refresh(); napms(100); for (;;) { #ifdef TRACE if (trace_start || trace_end) { if (generation == trace_start) { trace(TRACE_CALLS); getch(); } else if (generation == trace_end) { trace(0); getch(); } if (singlestep && generation > trace_start && generation < trace_end) getch(); generation++; } #endif /* TRACE */ for (n=0,w= &worm[0];n<number;n++,w++) { if ((x=w->xpos[h=w->head])<0) { cursor(x=w->xpos[h]=0,y=w->ypos[h]=bottom); addch(flavor[n % MAXWORMS]); ref[y][x]++; } else y=w->ypos[h]; if (++h==length) h=0; if (w->xpos[w->head=h]>=0) { register int x1, y1; x1=w->xpos[h]; y1=w->ypos[h]; if (--ref[y1][x1]==0) { cursor(x1,y1); addch(trail); } } op= &(x==0 ? (y==0 ? upleft : (y==bottom ? lowleft : left)) : (x==last ? (y==0 ? upright : (y==bottom ? lowright : right)) : (y==0 ? upper : (y==bottom ? lower : normal))))[w->orientation]; switch (op->nopts) { case 0: refresh(); endwin(); exit(0); case 1: w->orientation=op->opts[0]; break; default: w->orientation=op->opts[(int)(ranf()*(float)op->nopts)]; } cursor(x+=xinc[w->orientation], y+=yinc[w->orientation]); if (y < 0 ) y = 0; addch(flavor[n % MAXWORMS]); ref[w->ypos[h]=y][w->xpos[h]=x]++; } napms(100); refresh(); } }
void MedeaSpAgentObserver::selectRankProp() { if(_wm->_genomesList.size() != 0) { int idBest = 0; if(_wm->_genomesList.size() == 1) { idBest = (*_wm->_genomesList.begin()).first; } else { double minimumExpected = MedeaSpSharedData::gMinimumExpectedValue; double maximumExpected = MedeaSpSharedData::gMaximumExpectedValue; //reverse the map to sort by fitness. Will put lowest first std::multimap<double,int> sortedFitness = flip_map(_wm->_fitnessList); std::map<int, double> f; int rank = 0; for (std::multimap<double, int >::iterator it = sortedFitness.begin(); it != sortedFitness.end() ; it++) { f[(*it).second] = ( maximumExpected - (maximumExpected - minimumExpected) * (rank / ( (double)_wm->_fitnessList.size() - 1.0) )) / (double)_wm->_fitnessList.size() ; rank ++; } double sum1 = 0.0; for( std::map<int, double>::iterator it = f.begin() ; it != f.end() ; it ++) { sum1 += f[(*it).first]; } double sum2 = 0.0; std::map<int, double> p; for( std::map<int, double>::iterator it = f.begin() ; it != f.end() ; it ++) { if (f[(*it).first] > 0.0) { sum2 += f[(*it).first]; p[(*it).first] = sum2/sum1; } } double r = ranf(); //Proportional selection std::map<int, double >::iterator it = p.begin() ; while( (r > p[(*it).first]) && (it != p.end())) { it ++; } idBest = (*it).first ; _wm->_currentGenome =_wm->_genomesList[idBest]; if ( MedeaSpSharedData::gDynamicSigma == true ) { mutateWithBouncingBounds(_wm->_sigmaList[idBest]); } else { mutateWithBouncingBounds(-1.00); } _wm->setNewGenomeStatus(true); _wm->setFatherId(idBest); //gLogFile << gWorld->getIterations() << " : " << _wm->_agentId + 1000 * _wm->getDateOfBirth() << " take " << _wm->getFatherId()<<std::endl; if (_wm->_agentId == 1 && gVerbose) // debug std::cout << " Sigma is " << _wm->_sigmaList[idBest] << "." << std::endl; _wm->_genomesList.clear(); _wm->_fitnessList.clear(); } } }
void MedeaSpAgentObserver::selectFitnessProp() { if(_wm->_genomesList.size() != 0) { double sum1 = 0.0; for( std::map<int, std::vector<double> >::iterator it = _wm->_genomesList.begin() ; it != _wm->_genomesList.end() ; it ++) { sum1 += _wm->_fitnessList[(*it).first]; } int selected = 0; //if one has a fitness higher than 0 do the computations for proportional selection if (sum1 > 0) { double sum2 = 0.0; std::map<int, double> p; for( std::map<int, std::vector<double> >::iterator it = _wm->_genomesList.begin() ; it != _wm->_genomesList.end() ; it ++) { if (_wm->_fitnessList[(*it).first] > 0.0) { sum2 += _wm->_fitnessList[(*it).first]; p[(*it).first] = sum2/sum1; } } double r = ranf(); //Proportional selection std::map<int, double >::iterator it = p.begin() ; while( (r > p[(*it).first]) && (it != p.end())) { it ++; } selected = (*it).first ; std::cerr<<"okok"<<std::endl; } else { int randomIndex = rand()%_wm->_genomesList.size(); std::map<int, std::vector<double> >::iterator it = _wm->_genomesList.begin(); while (randomIndex != 0 ) { it ++; randomIndex --; } selected = (*it).first; } //_currentGenome = _wm->_genomesList[selected]; int idBest = selected; _wm->_currentGenome =_wm->_genomesList[selected]; if ( MedeaSpSharedData::gDynamicSigma == true ) { mutateWithBouncingBounds(_wm->_sigmaList[idBest]); } else { mutateWithBouncingBounds(-1.00); } _wm->setNewGenomeStatus(true); _wm->setFatherId(idBest); //gLogFile << gWorld->getIterations() << " : " << _wm->_agentId + 1000 * _wm->getDateOfBirth() << " take " << _wm->getFatherId()<<std::endl; if (_wm->_agentId == 1 && gVerbose) // debug std::cout << " Sigma is " << _wm->_sigmaList[idBest] << "." << std::endl; _wm->_genomesList.clear(); _wm->_fitnessList.clear(); } }