Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Rimscape Combat AI Here  (Read 2643 times)
0 Members and 1 Guest are viewing this topic.
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Posted 2004-12-18 03:52:10 »

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  
      /**
       * @param ship The ship to move
       * @return returns a double representing how many radians away from the desired angle the ship is.  
       * This is to be used for considering when to fire when the destination is a Ship
       */

      public static double[] moveShipInCombat(Ship ship) {
           
            double destX = ship.getTarget().getX();
            double destY = ship.getTarget().getY();

            double directTheta = Math.atan2(
                        destY - ship.getY(),
                        destX - ship.getX());
           
            double distanceSqrd = Point2D.distanceSq(destX, destY, ship.getX(), ship.getY());
            if (Rimscape.DEBUG && Rimscape.gameInProgress && MainScreen.isScreenTarget(ship))
                  System.out.println("moving in combat, distance = " + Math.sqrt(distanceSqrd));
           
            //FIXME: I don't like how this works
           //if (distanceSqrd < Ship.RANGE_SQRD)
           //      directTheta += (chanceRand.nextDouble()*2 - 1)*((double)(Constants.DIFFICULTY_MAX - Rimscape.difficulty)/Constants.DIFFICULTY_DEFAULT);
                       
            double dx = ship.getDx(), dy = ship.getDy();
            double velocityTheta = Math.atan2(dy, dx);
            double offset = circleMod(velocityTheta - directTheta);
     
            if (distanceSqrd < Ship.RANGE_SQRD/9) {
                  ship.accel();
            }
            else if (distanceSqrd < Ship.RANGE_SQRD/6) {
                  ship.reverse();
            }
            else if (distanceSqrd < Ship.RANGE_SQRD/4) {
                //coast
           }
            else if (distanceSqrd < Ship.RANGE_SQRD/2) {
                  ship.decel();
            }
            else {
                  double accel = ship.getStatMax(Ship.STAT_ACCELERATION);
                  double vSqrd = dx*dx + dy*dy;
                  double stuffUnderRadical = 0; //calculated when it's first checked to avoid an unnecessary sqrt
                 
                  //stuffUnderRadical < 0 means that Math.sqrt() check will return NaN
                 // this is because the equation checks for when the velocity will equal 0 if the brakes are hit now (Quadratic regression)
                 // therefore an imaginary root means you never reached 0 and thus didn't reach your destination
                 // it also means that if we stop now we'll stop waaay short of our destination so go further
                 
                  //Math.abs(offset) < THETA_TOLERANCE means don't hit the gas if you're not facing the right direction
                 //however if vSqrd < accel, then this check ^^^ probably doesn't make sense because the velocity
                 //used to create the offset is so small that deriving an angle from it isn't practical
                 
                  if (Math.abs(offset) < THETA_TOLERANCE/2 && (stuffUnderRadical = vSqrd - 2*accel*Math.sqrt(distanceSqrd)) < 0
                                                             //If other checks pass, check finalV < accel
                             || (vSqrd < accel || (2*Math.sqrt(vSqrd) - Math.sqrt(stuffUnderRadical)) < accel*accel)) {
                 
                        if (Rimscape.DEBUG && Rimscape.gameInProgress && MainScreen.isScreenTarget(ship))
                              System.out.println("speeding up to target");
                       
                        if (vSqrd < REALLY_FAST*REALLY_FAST*2) //TODO: new change...test it
                           ship.accel();
                  }
                  else {
                        if (Rimscape.DEBUG && Rimscape.gameInProgress && MainScreen.isScreenTarget(ship))
                              System.out.println("slowing down");
                        ship.decel();
                  }
            }
           
            double desiredTheta;
            if (distanceSqrd < Ship.RANGE_SQRD)
                  desiredTheta = directTheta;
            else
                  desiredTheta = directTheta - (Math.abs(offset) < THETA_TOLERANCE ? offset : 0);
           
            double thetaToGo = circleMod(desiredTheta - ship.getTheta());
           
     
            if (thetaToGo < -ship.getStat(Ship.STAT_ROTATION))
                  ship.turnLeft();
            else if (thetaToGo > ship.getStat(Ship.STAT_ROTATION))
                  ship.turnRight();
            else
                ship.setTheta(desiredTheta);
           
            //return new double[] {thetaToGo, distanceSqrd};
           return new double[] {circleMod(directTheta - ship.getTheta()), distanceSqrd};
      }

There she is Smiley  Hopefully the comments help to illustrate what's going on.  I felt this might be appropriate for the Shared Code forum, but since I'm interested in feedback and suggestions and it's also directly related to AI, I felt this forum was better.

Let me know what you guys think!  If you wanna see it in action, start up Rimscape and piss someone off Wink

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Malohkan

Senior Member




while (true) System.out.println("WOO!!!!");


« Reply #1 - Posted 2004-12-20 03:38:00 »

man it'd be really cool if I could teach my ships to avoid shooting innocent bystanders Smiley

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!