Ejemplo n.º 1
0
/* This function means that the robot has nothing better to do.
   If there are hostile players in the game, it will try to get
   as close to them as it can, while staying in its own space.
   Otherwise, it will head to the center of its own space.
*/
void go_home(struct Enemy *ebuf)
{
    int x, y;
    double dx, dy;
    struct player *j;

    if (ebuf == 0) {  /* No enemies */
/*	if (debug)
	    ERROR(1,( "%d) No enemies\n", me->p_no));*/
	x = center[me->p_team].x;
	y = center[me->p_team].y;
    }
    else {	/* Let's get near him */
	j = &players[ebuf->e_info];
	x = j->p_x;
	y = j->p_y;
	switch (me->p_team) {
	    case FED:
		if (x > (GWIDTH/2) - 5000)
		    x = (GWIDTH/2) - 5000;
		if (y < (GWIDTH/2) + 5000)
		    y = (GWIDTH/2) + 5000;
		break;
	    case ROM:
		if (x > (GWIDTH/2) - 5000)
		    x = (GWIDTH/2) - 5000;
		if (y > (GWIDTH/2) - 5000)
		    y = (GWIDTH/2) - 5000;
		break;
	    case KLI:
		if (x < (GWIDTH/2) + 5000)
		    x = (GWIDTH/2) + 5000;
		if (y > (GWIDTH/2) - 5000)
		    y = (GWIDTH/2) - 5000;
		break;
	    case ORI:
		if (x < (GWIDTH/2) + 5000)
		    x = (GWIDTH/2) + 5000;
		if (y < (GWIDTH/2) + 5000)
		    y = (GWIDTH/2) + 5000;
		break;
	}
    }
/*    if (debug)
	ERROR(1,( "%d) moving towards (%d/%d)\n",
	    me->p_no, x, y));*/

    /* Note that I've decided that robots should never stop moving.
    ** It makes them too easy to kill
    */

    me->p_desdir = getcourse(x, y);
    if (angdist(me->p_desdir, me->p_dir) > 64)
	set_speed(dogslow);
    else if (me->p_etemp > 900)		/* 90% of 1000 */
	set_speed(runslow);
    else {
	dx = x - me->p_x;
	dy = y - me->p_y;
	set_speed(((int) hypot(dx, dy) / 5000) + 3); /* was missing (int) 5/1/92 TC */
    }
    if (! cloaker) cloak_off();
}
Ejemplo n.º 2
0
int make_wu_headers(std::vector<dr2_compact_block_t> &tapebuffer, telescope_id
tel, std::vector<workunit> &wuheader) {
  int procid=getpid();
  double receiver_freq;
  int bandno;
  FILE *tmpfile;
  char tmpstr[256];
  char buf[64];
  static const receiver_config &r(rcvr);
  static const settings &s(splitter_settings);
  bool group_is_vlar;

  if (!strncmp(s.splitter_cfg->data_type,"encoded",
     std::min(static_cast<size_t>(7),sizeof(s.splitter_cfg->data_type)))) {
    noencode=0;
  } else {
      noencode=1;
  }

  tapebuffer[0].header.samplerate*=1e+6;
  seconds sample_time(1.0/tapebuffer[0].header.samplerate);
  seti_time start_time(tapebuffer[0].header.data_time
           -tapebuffer[0].data.size()*0.5*sample_time);
  seti_time end_time(tapebuffer[tapebuffer.size()-1].header.data_time);


  workunit_grp wugrp;
  sprintf(wugrp.name,"%s.%ld.%d.%d.%d",
    tapebuffer[0].header.name,
    procid, 
    tapebuffer[0].header.dataseq,
    tel-AO_430,
    s.id);
  wugrp.receiver_cfg=r; 
  wugrp.recorder_cfg=s.recorder_cfg; 
  wugrp.splitter_cfg=s.splitter_cfg; 
  wugrp.analysis_cfg=s.analysis_cfg; 

  wugrp.data_desc.nsamples=NSAMPLES;
  wugrp.data_desc.true_angle_range=0;
  coordinate_t start_coord(cmap_interp(coord_history,start_time));
  coordinate_t end_coord(cmap_interp(coord_history,end_time));
  wugrp.data_desc.start_ra=start_coord.ra;
  wugrp.data_desc.end_ra=end_coord.ra;
  wugrp.data_desc.start_dec=start_coord.dec;
  wugrp.data_desc.end_dec=end_coord.dec;
  coordinate_t last_coord=start_coord;
  double sample_rate=tapebuffer[0].header.samplerate/NSTRIPS;

  // find the bracketing entries in the coordinate history
  std::map<seti_time,coordinate_t>::iterator above(coord_history.upper_bound(end_time));
  std::map<seti_time,coordinate_t>::iterator below(coord_history.lower_bound(start_time));
  std::map<seti_time,coordinate_t>::iterator p;
  if (above==coord_history.begin()) {
    above++;
  }
  if (below==coord_history.end()) {
    below=above;
    below--;
  }
  if (above==below) {
    below--;
  }
  // Calculate the angular distance the beam has traveled
  for (p=below;p!=above;p++)
  {
    wugrp.data_desc.true_angle_range+=angdist(last_coord,p->second);
    last_coord=p->second;
  }
  wugrp.data_desc.true_angle_range+=angdist(last_coord,end_coord);
  if (wugrp.data_desc.true_angle_range==0) wugrp.data_desc.true_angle_range=1e-10;
  // Calculate the number of unique signals that could be found in a workunit.
  // We will use these numbers to calculate thresholds.
  double numgauss=2.36368e+08/std::min(wugrp.data_desc.true_angle_range,10.0);
  double numpulse=std::min(4.52067e+10/std::min(wugrp.data_desc.true_angle_range,10.0),2.00382e+11);
  double numtrip=std::min(3.25215e+12/std::min(wugrp.data_desc.true_angle_range,10.0),1.44774e+13);

  // check for VLAR workunits
  if (wugrp.data_desc.true_angle_range < 0.12) {
    group_is_vlar=true;
  } else {
    group_is_vlar=false;
  }
    

  // if (useanalysiscfgid > 0) { 
  //   log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,"Re-reading analysis cfg id: %d (set by user):\n",useanalysiscfgid);
  //   s.analysis_cfg = useanalysiscfgid;
  //   }

  // Calculate a unique key to describe this analysis config.
  long keyuniq=floor(std::min(wugrp.data_desc.true_angle_range*100,1000.0)+0.5)+
    s.analysis_cfg.id*1024;
  if ((keyuniq>((s.analysis_cfg.id+1)*1024)) ||(keyuniq<(s.analysis_cfg.id)*1024)) {
     log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Invalid keyuniq value!\n");
     log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"%d %d %f\n",keyuniq,s.analysis_cfg.id,wugrp.data_desc.true_angle_range);
     exit(1);
  }

  keyuniq*=-1;
  long save_keyuniq=keyuniq;
  splitter_settings.analysis_cfg=wugrp.analysis_cfg;
  sprintf(tmpstr,"where keyuniq=%d",keyuniq); 
  // Check if we've already done this analysis_config...
  // Fetch through splitter_settings, since it's alias (s) is const.
  splitter_settings.analysis_cfg.id=0;
  splitter_settings.analysis_cfg->fetch(tmpstr);

  if (s.analysis_cfg->id==0) {
    if (keyuniq != save_keyuniq) {
      log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"keyuniq value changed!\n");
      exit(1);
    }

    // If not calculate the thresholds based upon the input analysis_config
    // Triplets are distributed exponentially...
    wugrp.analysis_cfg->triplet_thresh+=(log(numtrip)-29.0652);

    // Gaussians are based upon chisqr...
    double p_gauss=lcgf(32.0,wugrp.analysis_cfg->gauss_null_chi_sq_thresh*32.0);
    p_gauss-=(log(numgauss)-19.5358);
    wugrp.analysis_cfg->gauss_null_chi_sq_thresh=invert_lcgf(p_gauss,32,1e-4)*0.03125;

    // Pulses thresholds are log of the probability
    wugrp.analysis_cfg->pulse_thresh+=(log(numpulse)-24.7894);

    wugrp.analysis_cfg->keyuniq=keyuniq;
    wugrp.analysis_cfg->insert();
  } else {
    wugrp.analysis_cfg=s.analysis_cfg;
  }

  strlcpy(wugrp.data_desc.time_recorded,
      short_jd_string(start_time.jd().uval()),
      sizeof(wugrp.data_desc.time_recorded));
  wugrp.data_desc.time_recorded_jd=start_time.jd().uval();
  wugrp.data_desc.coords.clear();

  wugrp.data_desc.coords.push_back(start_coord);  
  for (p=below;p!=above;p++)
  {
     wugrp.data_desc.coords.push_back(p->second);  
  }
  wugrp.data_desc.coords.push_back(end_coord);

  wugrp.tape_info->id=0;
  sprintf(buf,"%d",tel-AO_ALFA_0_0);
  wugrp.tape_info->fetch(std::string("where name=\'")+tapebuffer[0].header.name+"\' and beam="+buf);
  wugrp.tape_info->start_time=tapebuffer[0].header.data_time.jd().uval();
  wugrp.tape_info->last_block_time=wugrp.tape_info->start_time;
  wugrp.tape_info->last_block_done=tapebuffer[0].header.dataseq;
  wugrp.tape_info->beam=tel-AO_ALFA_0_0;

  if (!nodb) {
    if (wugrp.tape_info.id) {
      if (!(wugrp.tape_info->update())) {
        char buf[1024];
        log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"%s",sql_error_message());
	exit(1);
      }
    } else {
      strlcpy(wugrp.tape_info->name,tapebuffer[0].header.name,sizeof(wugrp.tape_info->name));
      wugrp.tape_info->insert();
    }
  }

  if (!nodb) {
    sqlint8_t wgid;
    if ((wgid=wugrp.insert())<=0) {
      log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Workunit_grp insert failed\nwgid=%d\nSQLCODE=%d\nLAST_NON_ZERO_SQLCODE=%d\n",wgid,sql_error_code(),sql_last_error_code());
      exit( 1 );
    }
    wugrp.id=wgid;
  }
  int i; 
  wu_database_id.resize(NSTRIPS);
  bin_data.resize(NSTRIPS);
  wuheader.resize(NSTRIPS);
  for (i=0;i<NSTRIPS;i++) {
    bin_data[i].clear();
    wuheader[i].group_info=wugrp;
    wuheader[i].group_info.id=wugrp.id;
    sprintf(wuheader[i].name,"%s.%ld.%d.%ld.%d.%d",tapebuffer[0].header.name,
       procid, tapebuffer[0].header.dataseq,
       tel-AO_430,s.id,i);
    if (group_is_vlar) {
      strlcat(wuheader[i].name,".vlar",sizeof(wuheader[i].name));
    }
    wuheader[i].subband_desc.sample_rate=tapebuffer[0].header.samplerate/NSTRIPS;
 
    receiver_freq=tapebuffer[0].header.sky_freq;

    bandno=((i+NSTRIPS/2)%NSTRIPS)-NSTRIPS/2;

    wuheader[i].subband_desc.base=receiver_freq+
	       (double)(bandno)*wuheader[i].subband_desc.sample_rate;
    wuheader[i].subband_desc.center=receiver_freq+wuheader[i].subband_desc.sample_rate*NSTRIPS*((double)IFFT_LEN*bandno/FFT_LEN+(double)IFFT_LEN/(2*FFT_LEN)-1.0/(2*FFT_LEN));
    wuheader[i].subband_desc.number=i;

    if (!nodb ) {
      if (!(wu_database_id[i]=wuheader[i].id=wuheader[i].insert())) {
        log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Database error in make_wu_headers()\n");
        exit(EXIT_FAILURE);
      }
    }
	
    sprintf(tmpstr,"./wu_inbox/%s",wuheader[i].name);
    if ((tmpfile=fopen(tmpstr,"w"))) {
      fprintf(tmpfile,"<workunit>\n");
      fprintf(tmpfile,wuheader[i].print_xml().c_str());
      fclose(tmpfile);
    } else {
      log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Unable to open file ./wu_inbox/%s, errno=%d\n",wuheader[i].name,errno);
      exit(1);
    }
    bin_data[i].reserve(wuheaders[i].group_info->recorder_cfg->bits_per_sample*
       wuheaders[i].group_info->data_desc.nsamples/8);
  }
  return(1);
}
Ejemplo n.º 3
0
void rmove()
{
    register struct player *j;
    register int i;
    register int burst;
    register int numHits, tDir;
    int		avDir;
    extern struct Enemy *get_nearest();
    struct Enemy *enemy_buf;
    struct player *enemy = NULL;
    static int	roboclock = 0;
    static int	avoid[2] = { -32, 32 };
    int no_cloak;
    char towhom[MSG_LEN];
    int timer;
    static int lastTorpped = 0; /* when we last fired a torp 4/13/92 TC */

    roboclock++;

    /* keep ghostbuster away */
    me->p_ghostbuster = 0;

    /* Check that I'm alive */
    if (me->p_status == PEXPLODE) {
        if (debug) ERROR(1,("Robot: Augh! exploding.\n"));
	return;
    }
    else if (me->p_status == PDEAD) {
	if (me->p_ntorp > 0)
               return;
	if (debug) ERROR(1,("Robot: done exploding and torps are gone.\n"));
	exitRobot();
	return;
    }


    timer=0;
    for (i = 0, j = &players[i]; i < (MAXPLAYER - TESTERS); i++, j++) {
	if ((j->p_status != PFREE) && !(j->p_flags & PFROBOT))
	    timer=1;
    }
    if (!timer && !sticky) {
	exitRobot();
	return;
    }

    /* if I'm a Terminator, quit if he quits, and quit if he dies and */
    /* I'm not "sticky" (-s) */

    if (target >= 0) {
	if (players[target].p_status == PFREE) { /* he went away */
	    me->p_status = PEXPLODE;	    
	    return;
	}
	if ((!sticky) && (players[target].p_status != PALIVE)) { /* he died */
	    me->p_status = PEXPLODE;
	    return;
	}
    }

    /* If it's been BOREDOM_TIME updates since we fired a torp, become
       hostile to all races, if we aren't already, and if we're not
       a practice robot (intended for guardian bots). 4/13/92 TC */

    if ((roboclock - lastTorpped > BOREDOM_TIME) && (!practice) && (!hostile) &&
        (me->p_team != 0 && !quiet)) {
        messAll(me->p_no,roboname,"I'm bored.");
        hostile++;
        declare_war(ALLTEAM, 0);
    }

    /* Our first priority is to phaser plasma torps in nearby vicinity... */
    /* If we fire, we aren't allowed to cloak... */
    no_cloak = phaser_plasmas();

    /* Find an enemy */

    enemy_buf = get_nearest();

    if ((enemy_buf != NULL) && (enemy_buf != NOENEMY)) {	/* Someone to kill */
	enemy = &players[enemy_buf->e_info];
	if (((random() % messfuse) == 0) &&
	    (hypot((double) me->p_x-enemy->p_x, (double) me->p_y-enemy->p_y) < 20000.0)) {
	    /* change 5/10/21 TC ...neut robots don't message */
	    messfuse = MESSFUSEVAL;
	    if (me->p_team != 0 && !quiet) {
		sprintf(towhom, " %s->%s",
			players[me->p_no].p_mapchars,
			players[enemy->p_no].p_mapchars);
		pmessage2(enemy->p_no, MINDIV, towhom, me->p_no, "%s",
			robo_message(enemy));
	    }
	    else if (target >= 0 && !quiet) {
		messAll(me->p_no,roboname,"%s",termie_message(enemy));
	    }
	}
	else
	    if (--messfuse == 0)
		messfuse = 1;
	timer = 0;
/*	if (debug)
	    ERROR(1,( "%d) noticed %d\n", me->p_no, enemy_buf->e_info));*/
    }
    else if (enemy_buf == NOENEMY) { /* no more players. wait 1 minute. */
	if (do_repair()) {
	    return;
	}
	go_home(0);
/*	if (debug)
	    ERROR(1,( "%d) No players in game.\n", me->p_no));*/
	return;
    }
    else if (enemy_buf == 0) {	 /* no one hostile */
/*	if (debug)
	    ERROR(1,( "%d) No hostile players in game.\n", me->p_no));*/
	if (do_repair()) {
	    return;
	}
	go_home(0);
	timer = 0;
	return;
    }

/* Note a bug in this algorithm:
** Once someone dies, he is forgotten.  This makes robots particularly easy
**  to kill on a suicide run, where you aim to where you think he will turn 
**  as you die.  Once dead, the robot will ignore you and all of your 
**  active torpedoes!
**/

/* Algorithm:
** We have an enemy.
** First priority: shoot at target in range.
** Second: Dodge torps and plasma torps.
** Third: Get away if we are damaged.
** Fourth: repair.
** Fifth: attack.
*/

/*
** If we are a practice robot, we will do all but the second.  One
** will be modified to shoot poorly and not use phasers.
**/
    /* Fire weapons!!! */
    /*
    ** get_nearest() has already determined if torpedoes and phasers
    ** will hit.  It has also determined the courses which torps and
    ** phasers should be fired.  If so we will go ahead and shoot here.
    ** We will lose repair and cloaking for the rest of this interrupt.
    ** if we fire here.
    */

    if (practice) {
	no_cloak = 1;
	if (enemy_buf->e_flags & E_TSHOT) {
/*	    if (debug)
		ERROR(1,( "%d) firing torps\n", me->p_no));*/
	    for (burst = 0; (burst < 3) && (me->p_ntorp < MAXTORP); burst++) {
		ntorp(enemy_buf->e_tcourse, TWOBBLE | TOWNERSAFE | TDETTEAMSAFE | TPRACTICE);
	    }
	}
    }
    else {
	if (enemy_buf->e_flags & E_TSHOT) {
/*	    if (debug)
		ERROR(1,( "%d) firing torps\n", me->p_no));*/
	    for (burst = 0; (burst < 2) && (me->p_ntorp < MAXTORP); burst++) {
		repair_off();
		if (! cloaker) cloak_off();
		ntorp(enemy_buf->e_tcourse, TWOBBLE | TOWNERSAFE | TDETTEAMSAFE);
		no_cloak++;
                lastTorpped = roboclock; /* record time of firing 4/13/92 TC */
	    }
	}
	if (enemy_buf->e_flags & E_PSHOT) {
/*	    if (debug)
		ERROR(1,( "%d) phaser firing\n", me->p_no));*/
	    no_cloak++;
	    repair_off();
	    if (! cloaker) cloak_off();
	    phaser(enemy_buf->e_course);
	}
    }

    /* auto pressor 7/27/91 TC */
    /* tractor/pressor rewritten on 5/1/92... glitches galore :-| TC */

    /* whoa, too close for comfort, or he's tractoring me, or
       headed in for me, or I'm hurt */

    /* a little tuning -- 0.8 on phrange and +/- 90 degrees in for pressor */

    /* pressor_player(-1); this didn't do anything before, so we'll let
       the pressors disengage by themselves 5/1/92 TC */
    if (enemy_buf->e_flags & E_TRACT) {	/* if pressorable */
	if (((enemy_buf->e_dist < 0.8 * enemy_buf->e_phrange) &&
	     (angdist(enemy_buf->e_edir, enemy_buf->e_course) > 64)) ||
	    (isTractoringMe(enemy_buf)) ||
	    (me->p_damage > 0)) {
	    if (!(enemy->p_flags & PFCLOAK)) {
		  if (debug)
		    ERROR(1,( "%d) pressoring %d\n", me->p_no,
			    enemy_buf->e_info));
		  pressor_player(enemy->p_no);
		  no_cloak++;
		  repair_off();
		  if (!cloaker) cloak_off();
	    }
	}
    }

    /* auto tractor 7/31/91 TC */

    /* tractor if not pressoring and... */

    /* tractor if: in range, not too close, and not headed +/- 90 degrees */
    /* of me, and I'm not hurt */
    
    if ((!(me->p_flags & PFPRESS)) &&
	(enemy_buf->e_flags & E_TRACT) &&
	(angdist(enemy_buf->e_edir, enemy_buf->e_course) < 64) &&
	(enemy_buf->e_dist > 0.7 * enemy_buf->e_phrange)) {
	if (!(me->p_flags & PFTRACT)) {
	    if (debug)
		ERROR(1,( "%d) tractoring %d\n", me->p_no,
			enemy_buf->e_info));
	    tractor_player(enemy->p_no);
	    no_cloak++;
	}
    }	
    else
	tractor_player(-1);	/* otherwise don't tractor */

    /* Avoid torps */
    /*
    ** This section of code allows robots to avoid torps.
    ** Within a specific range they will check to see if
    ** any of the 'closest' enemies torps will hit them.
    ** If so, they will evade for four updates.
    ** Evading is all they will do for this round, other than shooting.
    */

    if (!practice) {
	if ((enemy->p_ntorp < 5)) {
	    if ((enemy_buf->e_dist < 15000) || (avoidTime > 0)) {
		numHits = projectDamage(enemy->p_no, &avDir);
		if (debug) {
		    ERROR(1,( "%d hits expected from %d from dir = %d\n",
			    numHits, enemy->p_no, avDir));
		}
		if (numHits == 0) {
		    if (--avoidTime > 0) {	/* we may still be avoiding */
			if (angdist(me->p_desdir, me->p_dir) > 64)
			    me->p_desspeed = dogslow;
			else
			    me->p_desspeed = dogfast;
			return;
		    }
		} else {
		    /*
		     * Actually avoid Torps
		     */ 
		    avoidTime = AVOID_TIME;
		    tDir = avDir - me->p_dir;
		    /* put into 0->255 range */
		    tDir = NORMALIZE(tDir);
		    if (debug)
			ERROR(1,( "mydir = %d avDir = %d tDir = %d q = %d\n",
			    me->p_dir, avDir, tDir, tDir / 64));
		    switch (tDir / 64) {
		    case 0:
		    case 1:
			    set_course(NORMALIZE(avDir + 64));
			    break;
		    case 2:
		    case 3:
			    set_course(NORMALIZE(avDir - 64));
			    break;
		    }
		    if (!no_cloak)
			cloak_on();

		    if (angdist(me->p_desdir, me->p_dir) > 64)
			me->p_desspeed = dogslow;
		    else
			me->p_desspeed = dogfast;
			
		    shield_up();
		    detothers();	/* hmm */
		    if (debug)
			ERROR(1,( "evading to dir = %d\n", me->p_desdir));
		    return;
		}
	    }
	}

	/*
	** Trying another scheme.
	** Robot will keep track of the number of torps a player has
	** launched.  If they are greater than say four, the robot will
	** veer off immediately.  Seems more humanlike to me.
	*/

	else if (enemy_buf->e_dist < 15000) {
	    if (--avoidTime > 0) {	/* we may still be avoiding */
		if (angdist(me->p_desdir, me->p_dir) > 64)
		    me->p_desspeed = dogslow;
		else
		    me->p_desspeed = dogfast;
		return;
	    }
	    if (random() % 2) {
		me->p_desdir = NORMALIZE(enemy_buf->e_course - 64);
		avoidTime = AVOID_TIME;
	    }
	    else {
		me->p_desdir = NORMALIZE(enemy_buf->e_course + 64);
		avoidTime = AVOID_TIME;
	    }
	    if (angdist(me->p_desdir, me->p_dir) > 64)
		me->p_desspeed = dogslow;
	    else
		me->p_desspeed = dogfast;
	    shield_up();
	    return;
	}
    }
	    
    /* Run away */
    /*
    ** The robot has taken damage.  He will now attempt to run away from
    ** the closest player.  This obviously won't do him any good if there
    ** is another player in the direction he wants to go.
    ** Note that the robot will not run away if he dodged torps, above.
    ** The robot will lower his shields in hopes of repairing some damage.
    */

#define STARTDELTA 5000		/* ships appear +/- delta of home planet */

    if (me->p_damage > 0 && enemy_buf->e_dist < 13000) {
	if (me->p_etemp > 900)		/* 90% of 1000 */
	    me->p_desspeed = runslow;
	else
	    me->p_desspeed = runfast;
	if (!no_cloak)
	    cloak_on();
	repair_off();
	shield_down();
	set_course(enemy_buf->e_course - 128);
	if (debug)
	    ERROR(1,( "%d(%d)(%d/%d) running from %c%d %16s damage (%d/%d) dist %d\n",
		me->p_no,
		(int) me->p_kills,
		me->p_damage,
		me->p_shield,
		teamlet[enemy->p_team],
		enemy->p_no,
		enemy->p_login,
		enemy->p_damage,
		enemy->p_shield,
		enemy_buf->e_dist));
	return;
    }

    /* Repair if necessary (we are safe) */
    /*
    ** The robot is safely away from players.  It can now repair in peace.
    ** It will try to do so now.
    */

    if (do_repair()) {
	return;
    }

    /* Attack. */
    /*
    ** The robot has nothing to do.  It will check and see if the nearest
    ** enemy fits any of its criterion for attack.  If it does, the robot
    ** will speed in and deliver a punishing blow.  (Well, maybe)
    */

    if ((enemy_buf->e_flags & E_INTRUDER) || (enemy_buf->e_dist < 15000)
	|| (hostile)) {
	if ((!no_cloak) && (enemy_buf->e_dist < 10000))
	    cloak_on();
	shield_up();
/*	if (debug)
	    ERROR(1,( "%d(%d)(%d/%d) attacking %c%d %16s damage (%d/%d) dist %d\n",
		me->p_no,
		(int) me->p_kills,
		me->p_damage,
		me->p_shield,
		teamlet[enemy->p_team],
		enemy->p_no,
		enemy->p_login,
		enemy->p_damage,
		enemy->p_shield,
		enemy_buf->e_dist));*/

	if (enemy_buf->e_dist < 15000) {
	    set_course(enemy_buf->e_course + 
		    avoid[(roboclock / AVOID_CLICKS) % SIZEOF(avoid)]);
	    if (angdist(me->p_desdir, me->p_dir) > 64)
		set_speed(closeslow);
	    else
		set_speed(closefast);
	}
	else {
	    me->p_desdir = enemy_buf->e_course;
	    if (angdist(me->p_desdir, me->p_dir) > 64)
		set_speed(closeslow);
	    if (target >= 0)	/* 7/27/91 TC */
		set_speed(12);
	    else if (me->p_etemp > 900)		/* 90% of 1000 */
		set_speed(runslow);
	    else
		set_speed(runfast);
	}
    }
    else {
	go_home(enemy_buf);
    }
}
static void
movesnake(void)
{
    register int i, px, py;
    register struct player *j;
    register struct torp *k /* , *prev = thead */ ;
    unsigned char tc;
    struct player *tr;
    static
    int     dir = 8;
    int     ok;

    if ((s_clock % PLAYERCHECK) == 0) {
	/* every x seconds make sure there's people in the game */
	ok = 0;
	defenders[FED] = defenders[ROM] = defenders[KLI] = defenders[ORI] = 0;
	for (i = 0, j = players; i < MAXPLAYER; i++, j++) {
	    if ((j->p_status != PFREE) && !(j->p_flags & PFROBOT)) {
		ok = 1;
		defenders[j->p_team]++;
	    }
	}
	if (!ok)
	    exitSnake(0);
    }
    if (patrol && (target == -1) && (s_clock % BOMBCHECK) == 0) {
	target = bombcheck(team1, team2);
    }
    if ((s_clock % (4 + (lrand48() % 4))) == 0)
	dir = -dir;

    thead->t_dir += dir + rrnd(8);

    if (target > -1) {
	tr = &players[target];
	if (tr->p_status == PALIVE) {
	    int     nd, td;
	    tc = getcourse(tr->p_x, tr->p_y, thead->t_x, thead->t_y);
	    nd = angdist(thead->t_dir, tc);
	    if (nd > 8) {
		td = tc + nd;
		if (td == thead->t_dir)
		    thead->t_dir -= 8;
		else
		    thead->t_dir += 8;
	    }
	}
    }
    if (target == -1) {
	if (!patrol)
	    check_tboundary(ALLTEAM, &thead->t_dir, 8, thead->t_x, thead->t_y);
	else
	    check_tboundary(team1 | team2, &thead->t_dir, 8, thead->t_x, thead->t_y);
    }
    lx = thead->t_x;
    ly = thead->t_y;

    if (!plan_guard) {
	/* NOTE: we aren't letting the daemon move the torp head */
	thead->t_x += (double) TORPSEP *Cos[thead->t_dir];
	thead->t_y += (double) TORPSEP *Sin[thead->t_dir];
    }
    for (j = perfs[0]; j; j = (j == perfs[1] ? NULL : perfs[1])) {
	if (plan_guard) {
	    int     temp;
	    thead = &torps[j->p_no * MAXTORP];
	    if (j == perfs[0])
		temp = planet1;
	    else
		temp = planet2;
	    lx = thead->t_x;
	    ly = thead->t_y;
	    thead->t_x = planets[temp].pl_x + Cos[255 - plan_count] * PLANRAD;
	    thead->t_y = planets[temp].pl_y + Sin[255 - plan_count] * PLANRAD;
	}
	for (i = j->p_no * MAXTORP, k = &torps[i];
	     i < j->p_no * MAXTORP + length; i++, k++) {

	    if (k->t_status == TFREE)
		/* got exploded.  x & y location will remain however */
		snake_torp(k, i, j);

	    if (k == thead)
		continue;

	    px = k->t_x;
	    py = k->t_y;

	    k->t_x = lx;
	    k->t_y = ly;

	    lx = px;
	    ly = py;
	}
    }
    if (plan_guard) {
	thead = &torps[perfs[0]->p_no * MAXTORP];
	plan_count = (plan_count + PLANRATE) % 256;
    }
    lx = thead->t_x;
    ly = thead->t_y;
    doeyes();
}
Ejemplo n.º 5
0
intrupt()
{
   static long     lastread;
   static int      prevread;
   int		   cr_time;

   /* handle torp firing */
   do_torps();
   _tcheck = 0;
keep_reading:
	;
#ifdef ATM
   if (readFromServer(pollmode)) {	/* should be 0 */
#else
   if (readFromServer()) {
#endif
      cr_time = mtime(0);

      _cycletime = cr_time - prevread;
      _serverdelay = ((double)_cycletime/100.);
      _udnonsync++;

      /* can't be less than 100 ms */
      if(_serverdelay < 1.) _serverdelay = 1.;

      if (DEBUG & DEBUG_SERVER) {
	 printf("cycletime = %dms\n", _cycletime);
	 printf("wait for input = %dms\n", cr_time - _waiting_for_input);
      }
      prevread = cr_time;

      if(_state.borg_detect)
	 borg_detect();
   } else {
      /*
       * We haven't heard from server for 3 secs... Strategy:  send a
       * useless packet to "ping" server.
       */
      /* If server is dead, just give up */
      if (isServerDead())
         exitRobot(0);
      if(!pollmode){
	 int	now = time(NULL)-3;
	 mprintf("sending wakeup packet at %s", ctime(&now));
	 sendWarReq(me->p_hostile);
      }
   }
   if (me->p_status == POUTFIT) {
      death();
   }
   if(me->p_status != PALIVE)
      goto keep_reading;
}

/* borg detect */

borg_detect()
{
   register Player		*p;
   register struct player	*j;
   int				last_speedc, last_dirc;
   register			i;

   for(i=0, p=_state.players; i < MAXPLAYER; i++,p++){
      if(!p->p || (p->p->p_status != PALIVE) || p->invisible)
	 continue;
      
      j = p->p;

      /* check for simultaneous torp & phaser but in different directions.
	 -- Very common borg maneuver */
      if(_udnonsync - p->tfire_t < 2){
	 if(p->tfire_t == p->pfire_t){
	    if(p->tfire_dir > -1){
	       if(angdist(p->tfire_dir, p->pfire_dir) > 2){
		  /*
		  printf("%s BORG %d\n", j->p_mapchars, 
		     angdist(p->tfire_dir, p->pfire_dir));
		  printf("current p: %d\n", p->bp1);
		  */
		  p->bp1 ++;
	       }
	    }
	 }
      }
      /* check for recent change in direction far away from current torp
	 or phaser fire */
      if(j->p_speed > 1){
	 if(_udnonsync - p->tfire_t < 2){
	    if(_udnonsync - p->turn_t < 2){
	       if(angdist(j->p_dir, p->tfire_t) > 64){
		  /*
		  printf("%s tfire BORG %d\n", j->p_mapchars,
		     angdist(j->p_dir, p->tfire_t));
		  */
		  p->bp2 ++;
	       }
	    }
	 }
	 if(_udnonsync - p->pfire_t < 2){
	    if(_udnonsync - p->turn_t < 2){
	       if(angdist(j->p_dir, p->pfire_t) > 64){
		  /*
		  printf("%s pfire BORG %d\n", j->p_mapchars,
		     angdist(j->p_dir, p->pfire_t));
		  */
		  p->bp3 ++;
	       }
	    }
	 }
      }
      if(p->bdc < 1.0)
	 p->borg_probability = 0.0;
      else
	 p->borg_probability = (double)(p->bp1 * 10 + p->bp2 + p->bp3)/
	    (double)p->bdc;
   }
}

/* maybe later */
#ifdef nodef
      if(_udcounter - p->tfire_t < 3){
	 
	 last_speedc = p->tfire_t - p->speed_t;
	 /* fired, then observed speed change within 5 cycles */
	 if(last_speedc < 5 && p->tfire_dir > -1){

	    /* player direction and torp fire direction greater than 64 */
	    if(angdist(j->p_dir, p->tfire_dir) > 64){
	       mprintf("speed detect\n");
	       p->borg_probability ++;
	    }
	 }
	 last_dirc = p->tfire_t - p->turn_t;
	 /* fired, then observed direction change within 5 cycles */
	 if(last_dirc < 5 && p->tfire_dir > -1){

	    /* player direction and torp fire direction greater than 64 */
	    if(angdist(j->p_dir, p->tfire_dir) > 64){
	       mprintf("direction detect\n");
	       p->borg_probability ++;
	    }
	 }
      }
Ejemplo n.º 6
0
int make_wu_headers(tapeheader_t tapeheader[],workunit wuheader[],
                    buffer_pos_t *start_of_wu) {
  int procid=getpid();
  int i,j,startframe=start_of_wu->frame;
  double receiver_freq;
  int bandno;
  SCOPE_STRING *lastpos;
  FILE *tmpfile;
  char tmpstr[256];
  char buf[64];
  static int HaveConfigTable=0;
  static ReceiverConfig_t ReceiverConfig;   
  static receiver_config r;
  static settings s;

  if(!HaveConfigTable) {
    sprintf(buf,"where s4_id=%d",gregorian?AOGREG_1420:AO_1420);
    r.fetch(std::string(buf));
    ReceiverConfig.ReceiverID=r.s4_id;
    strlcpy(ReceiverConfig.ReceiverName,r.name,
           sizeof(ReceiverConfig.ReceiverName));
    ReceiverConfig.Latitude=r.latitude;
    ReceiverConfig.Longitude=r.longitude;
    ReceiverConfig.WLongitude=-r.longitude;
    ReceiverConfig.Elevation=r.elevation;
    ReceiverConfig.Diameter=r.diameter;
    ReceiverConfig.BeamWidth=r.beam_width;
    ReceiverConfig.CenterFreq=r.center_freq;
    ReceiverConfig.AzOrientation=r.az_orientation;
    for (i=0;i<(sizeof(ReceiverConfig.ZenCorrCoeff)/sizeof(ReceiverConfig.ZenCorrCoeff[0]));i++) {
      ReceiverConfig.ZenCorrCoeff[i]=r.zen_corr_coeff[i];
      ReceiverConfig.AzCorrCoeff[i]=r.az_corr_coeff[i];
    }
    HaveConfigTable=1;
  }
  sprintf(buf,"where active=%d",app.id);
  if (!s.fetch(std::string(buf))) {
    log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Unable to find active settings for app.id=%d\n",app.id);
    exit(1);
  }
  if (s.receiver_cfg->id != r.id) {
    log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Receiver config does not match settings (%d != %d)\n",s.receiver_cfg->id, r.id);
    exit(1);
  }
  s.recorder_cfg->fetch();
  s.splitter_cfg->fetch();
  s.analysis_cfg->fetch();
  if (!strncmp(s.splitter_cfg->data_type,"encoded",
     std::min(static_cast<size_t>(7),sizeof(s.splitter_cfg->data_type)))) {
    noencode=0;
  } else {
      noencode=1;
  }

  workunit_grp wugrp;
  sprintf(wugrp.name,"%s.%ld.%d.%ld.%d",tapeheader[startframe].name,
    procid, 
    (current_record-TAPE_RECORDS_IN_BUFFER)*8+startframe,
    start_of_wu->byte,s.id);
  wugrp.receiver_cfg=r;
  wugrp.recorder_cfg=s.recorder_cfg;
  wugrp.splitter_cfg=s.splitter_cfg;
  wugrp.analysis_cfg=s.analysis_cfg;

  wugrp.data_desc.start_ra=tapeheader[startframe+1].telstr.ra;
  wugrp.data_desc.start_dec=tapeheader[startframe+1].telstr.dec;
  wugrp.data_desc.end_ra=tapeheader[startframe+TAPE_FRAMES_PER_WU].telstr.ra;
  wugrp.data_desc.end_dec=tapeheader[startframe+TAPE_FRAMES_PER_WU].telstr.dec;
  wugrp.data_desc.nsamples=NSAMPLES;
  wugrp.data_desc.true_angle_range=0;
  {
    double sample_rate=tapeheader[startframe].samplerate/NSTRIPS;
    /* startframe+1 contains the first valid RA and Dec */
    TIME st=tapeheader[startframe+1].telstr.st;
    TIME et=tapeheader[startframe+TAPE_FRAMES_PER_WU].telstr.st;
    double diff=(et-st).jd*86400.0;
    for (j=2;j<TAPE_FRAMES_PER_WU;j++) {
      wugrp.data_desc.true_angle_range+=angdist(tapeheader[startframe+j-1].telstr,tapeheader[startframe+j].telstr);
    }
    wugrp.data_desc.true_angle_range*=(double)wugrp.data_desc.nsamples/(double)sample_rate/diff;
    if (wugrp.data_desc.true_angle_range==0) wugrp.data_desc.true_angle_range=1e-10;
  }
  // Calculate the number of unique signals that could be found in a workunit.
  // We will use these numbers to calculate thresholds.
  double numgauss=2.36368e+08/wugrp.data_desc.true_angle_range;
  double numpulse=std::min(4.52067e+10/wugrp.data_desc.true_angle_range,2.00382e+11);
  double numtrip=std::min(3.25215e+12/wugrp.data_desc.true_angle_range,1.44774e+13);

  

  // Calculate a unique key to describe this analysis config.
  long keyuniq=floor(std::min(wugrp.data_desc.true_angle_range*100,1000.0)+0.5)+
    s.analysis_cfg.id*1024.0;
  if ((keyuniq>(13*1024)) ||(keyuniq<12*1024)) {
     log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Invalid keyuniq value!\n");
     log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"%d %d %f\n",keyuniq,s.analysis_cfg.id,wugrp.data_desc.true_angle_range);
     exit(1);
  }

  keyuniq*=-1;
  long save_keyuniq=keyuniq;
  s.analysis_cfg=wugrp.analysis_cfg;
  sprintf(tmpstr,"where keyuniq=%d",keyuniq); 
  // Check if we've already done this analysis_config...
  s.analysis_cfg.id=0;
  s.analysis_cfg->fetch(tmpstr);

  if (s.analysis_cfg->id==0) {
    if (keyuniq != save_keyuniq) {
      log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"keyuniq value changed!\n");
      exit(1);
    }

    // If not calculate the thresholds based upon the input analysis_config
    // Triplets are distributed exponentially...
    wugrp.analysis_cfg->triplet_thresh+=(log(numtrip)-29.0652);

    // Gaussians are based upon chisqr...
    double p_gauss=lcgf(32.0,wugrp.analysis_cfg->gauss_null_chi_sq_thresh*32.0);
    p_gauss-=(log(numgauss)-19.5358);
    wugrp.analysis_cfg->gauss_null_chi_sq_thresh=invert_lcgf(p_gauss,32,1e-4)*0.03125;

    // Pulses thresholds are log of the probability
    wugrp.analysis_cfg->pulse_thresh+=(log(numpulse)-24.7894);

    wugrp.analysis_cfg->keyuniq=keyuniq;
    wugrp.analysis_cfg->insert();
  } else {
    wugrp.analysis_cfg=s.analysis_cfg;
  }

  strlcpy(wugrp.data_desc.time_recorded,
      short_jd_string(tapeheader[startframe+1].telstr.st.jd),
      sizeof(wugrp.data_desc.time_recorded));
  wugrp.data_desc.time_recorded_jd=tapeheader[startframe+1].telstr.st.jd;

  lastpos=&(tapeheader[startframe].telstr);
  coordinate_t tmpcoord;
  tmpcoord.time=tapeheader[startframe].telstr.st.jd;
  tmpcoord.ra=tapeheader[startframe].telstr.ra;
  tmpcoord.dec=tapeheader[startframe].telstr.dec;
  wugrp.data_desc.coords.push_back(tmpcoord);  

  for (j=1;j<TAPE_FRAMES_PER_WU;j++) {
    if ((tapeheader[startframe+j].telstr.st.jd-lastpos->st.jd) > (1.0/86400.0)) 
    {
      lastpos=&(tapeheader[startframe+j].telstr);
      tmpcoord.time=tapeheader[startframe+j].telstr.st.jd;
      tmpcoord.ra=tapeheader[startframe+j].telstr.ra;
      tmpcoord.dec=tapeheader[startframe+j].telstr.dec;
      wugrp.data_desc.coords.push_back(tmpcoord);  
    }
  }

  wugrp.tape_info->id=0;
  wugrp.tape_info->fetch(std::string("where name=\'")+tapeheader[startframe].name+"\'");
  wugrp.tape_info->start_time=tapeheader[startframe].st.jd;
  wugrp.tape_info->last_block_time=tapeheader[startframe].st.jd;
  wugrp.tape_info->last_block_done=tapeheader[startframe].frameseq;

  if (!nodb) {
    if (wugrp.tape_info.id) {
      if (!(wugrp.tape_info->update())) {
        char buf[1024];
        log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"%s",sql_error_message());
	exit(1);
      }
    } else {
      strlcpy(wugrp.tape_info->name,tapeheader[startframe].name,sizeof(wugrp.tape_info->name));
      wugrp.tape_info->insert();
    }
  }

  if (!nodb) wugrp.insert();
  
  for (i=0;i<NSTRIPS;i++) {
    bin_data[i].clear();
    wuheader[i].group_info=wugrp;
    sprintf(wuheader[i].name,"%s.%ld.%d.%ld.%d.%d",tapeheader[startframe].name,
       procid, (current_record-TAPE_RECORDS_IN_BUFFER)*8+startframe,
       start_of_wu->byte,s.id,i);
    wuheader[i].subband_desc.sample_rate=tapeheader[startframe].samplerate/NSTRIPS;
 
    receiver_freq=tapeheader[startframe].centerfreq;

    bandno=((i+NSTRIPS/2)%NSTRIPS)-NSTRIPS/2;

    wuheader[i].subband_desc.base=receiver_freq+
	       (double)(bandno)*wuheader[i].subband_desc.sample_rate;
    wuheader[i].subband_desc.center=receiver_freq+wuheader[i].subband_desc.sample_rate*NSTRIPS*((double)IFFT_LEN*bandno/FFT_LEN+(double)IFFT_LEN/(2*FFT_LEN)-1.0/(2*FFT_LEN));
    wuheader[i].subband_desc.number=i;

    if (!nodb ) {
      if (!(wu_database_id[i]=wuheader[i].insert())) {
        log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Database error in make_wu_headers()\n");
        exit(EXIT_FAILURE);
      }
    }
	
    sprintf(tmpstr,"./wu_inbox/%s",wuheader[i].name);
    if ((tmpfile=fopen(tmpstr,"w"))) {
      fprintf(tmpfile,"<workunit>\n");
      fprintf(tmpfile,wuheader[i].print_xml().c_str());
      fclose(tmpfile);
    } else {
      log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,"Unable to open file ./wu_inbox/%s, errno=%d\n",wuheader[i].name,errno);
      exit(1);
    }
    bin_data[i].reserve(wuheaders[i].group_info->recorder_cfg->bits_per_sample*
       wuheaders[i].group_info->data_desc.nsamples/8);
  }
  return(1);
}
Ejemplo n.º 7
0
double angdist(const SCOPE_STRING &a, const SCOPE_STRING &b) {
  return angdist(a.ra*15,a.dec,b.ra*15,b.dec);
}