From: barton@latcs1.lat.OZ.AU (Douglas P Barton)
Newsgroups: rec.games.xtank.play
Subject: Another Program
Date: 20 Aug 92 23:20:28 GMT
Organization: What ORGANISATION???  This mess is total chaos!!!



Due to interest I have decided to post a program I use in some of my tanks...

it is mostly useful for replenishing and it aims too, but only in a simple
manner...

Must acknowledge that this is hacked code from the distributions so ack's go
to all those guys...

included is a nice tank with which to use this program...


<--------------program
                      /**************************************\
                     *       A simple computer assisted       *
                     *       aim/replenish program            *
                      \**************************************/

#include <xtanklib.h>
#include <math.h>


static void main();		/* forward reference */

Prog_desc doug_prog = {
    "doug",
    "doug",			/* or dizzy2 */
    "A hands-off approach to aiming all weapons.  Computer assisted shooting.  Modifications/Butchery:                                                              Doug Barton barton@latcs1.oz.au                                            Dougal Beilby beilby@latcs1.oz.au",
    "Robert Potter (Original Author)",
    DOES_REPLENISH,
    3,
    main
};


/* all my global data */
typedef struct {
    Vehicle_info me;		/* where I am */
    Boolean have_target;	/* whether I have someone to attack */
    Vehicle_info target;	/* who I'm going to attack */
    Weapon_info wep[MAX_WEAPONS];
    int num_weapons;
} All;


static void look_around(allp)
    All *allp;
{
    get_self(&allp->me);		/* find out where I am */
    allp->have_target = get_closest_enemy(&allp->target);
}

/************************************
*
* This is where both shooting and replenishing may take place...
*
* If I have a target, aim at him.
* If I am stationary on an AMMO landmark, then replenish ammo
* 
*************************************/
static void do_something(allp)
    All *allp;
{
  Vehicle_info me;

    me=allp->me;

    /* If a bad guy's around, attack him */
    if (allp->have_target)
    {
        int dx, dy, tx, ty, i;
        float time;
        Vehicle_info t,Rel;
        float theta, enemy_v_theta, evel, edist, aim_angle;
        
        t=allp->target;

/* relative location */
        dx = t.loc.x - me.loc.x;
        dy = t.loc.y - me.loc.y;

/* relative speed in X and Y directions */
        Rel.xspeed = t.xspeed - me.xspeed;
        Rel.yspeed = t.yspeed - me.yspeed;

/*   Not used at the moment
        evel = HYPOT(Rel.xspeed,Rel.yspeed);
*/

/* distance to enemy from me */
        edist = HYPOT(dx,dy);

/* if I have weapons then I should aim them at the enemy's likely position */
        if (num_weapons()>0)
        {
          time = 2 * edist / (float) allp->wep[0].ammo_speed;

/* if the enemy is close aim at him as he will not be able to avoid the shots */
          if (edist>30 /* && evel*time<edist */ )
          {
          tx=dx + (int) (Rel.xspeed * time/allp->wep[0].ammo_speed);
          ty=dy + (int) (Rel.yspeed * time/allp->wep[0].ammo_speed);
          }
          else
            {
            tx=dx;
            ty=dy;
            }
  
          aim_all_turrets(tx, ty);
        }
    }

/* if I am on an AMMO landmark and not moving */
    if (me.xspeed==0 && me.yspeed==0
             && landmark(me.loc.grid_x,me.loc.grid_y)==AMMO)
    {
      int i=0;
      int FINISHED=0;
      int num_weps;

      num_weps=allp->num_weapons;

/* replenish AMMO until at max_ammo for each weapon
                        OR an enemy turns up
                        OR i begin moving
*/
      do
      {
        get_self(&me);
        for(i=0;i<num_weps;i++)
          if (weapon_ammo(i)==allp->wep[i].max_ammo)
          {
            turn_on_weapon(i);
            FINISHED++;
          }
          else if (weapon_ammo(i)<allp->wep[i].max_ammo)
               {
                 turn_off_weapon(i);
                 FINISHED--;
               }
        done();
      } while (FINISHED<num_weps && me.xspeed==0 && me.yspeed==0
               && !get_closest_enemy(&allp->target));

      for(i=num_weps-1;i>=0;i--)
        turn_on_weapon(i);
    }
}


static void cleanup(allp)
    All *allp;
{
    /* free all dynamically allocated memory */
    free((char *)allp);
}

static void give_up()
{
    /* I don't like my vehicle */
    printf("doug: giving up.\n");
    while (1) done();
}


static void main()
{
    All *allp;
    int i;

    /* allocate the All structure dynamically, to save stack space */
    allp = (All *) calloc(1, sizeof(*allp));

    /* arrange for the All structure to get free()ed when I die */
    set_cleanup_func(cleanup, (void *) allp);

    allp->num_weapons=num_weapons();

    if (get_weapon(0, &allp->wep[0]) == BAD_VALUE) {
      give_up();
    }
    else for(i=1;i<allp->num_weapons;i++)
           get_weapon(i,&allp->wep[i]);

    while (1) {
      look_around(allp);
      do_something(allp);
      done();
    }
}



<-------------Vehicle
Vehicle: testing
Designer: HellRaiser
Body: Marauder
Engine: Fuel Cell
W#0: T1 Acid Sprayer
W#1: T1 Acid Sprayer
W#2: T1 Acid Sprayer
W#3: T2 Acid Sprayer
W#4: T2 Acid Sprayer
W#5: T2 Acid Sprayer
Armor Type: Composite
Front: 200
Back: 130
Left: 140
Right: 140
Top: 180
Bottom: 0
Special: Console
Special: Mapper
Special: Ram Plate
Special: New Radar
Special: Tac Link
Heat sinks: 0
Suspension: Active
Treads: Hover
Bumpers: Retro

-- 

Douglas Barton - barton@latcs1.oz.au
		"My ,son.  Take for thyself another world,
 		 for that which I leave thee is too small..."
Newsgroups: rec.games.xtank.play