Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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  
  Getting stuck on A* + how to get better performance. + targetting ghosts  (Read 605 times)
0 Members and 1 Guest are viewing this topic.
Offline roseslayer

Junior Member


Medals: 1



« Posted 2013-08-28 12:40:21 »

Hi there JGO, it's me again.

I am working on a small AI script from the villager that has the woodcutter job. But sometimes it is attacking air/ghosts and I guess also targetting them. I don't know how this is happening. Most of the times it works, some times he is stuck because my A* pathfinding isn't that good, nor positioning of the Villager.

Screeny: (The grey plate with the sword on it is that he is attacking, because I couldn't show you the animation in a pic).


(this one's positioning is really terrible. I guess it is a small variable that is wrong(My cutprogram likes to add a white border Tongue).


Codes
Fighting:
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  
public void fighting() {
      if(x < fightTargetCon.x){
         movingDir = 3;
      } else {
         movingDir = 2;
      }
      if(fightTargetCon.isDestroyed()){
         wonFight = true;
         isFighting = false;
      } else if(attackSpeedTimer >= attackSpeed){
         attackSpeedTimer = 0;
           Random hitChance = new Random();
            if(hitChance.nextInt(100) <= accuracy-1){
               yourHitHit = true;
               hitMissed = false;
               yourHitIconShowTimer = 0;
               Random randomCriticalChance = new Random();
               if(randomCriticalChance.nextInt(100) <= criticalChance -1){
                  attackDamageModifier = 2;
                  criticalHit = true;
               } else {
                  attackDamageModifier = 1;
                  criticalHit = false;
               }
               Random damageChance = new Random();    
               fightTargetCon.healthChange((minAttackDamage + damageChance.nextInt(maxAttackDamage-minAttackDamage)) * attackDamageModifier, false);
            } else {
               hitMissed = true;
               yourHitHit = false;
               yourHitIconShowTimer = 0;
            }
      } else {
         attackSpeedTimer++;
      }
   }


The AI (searching for the tree, walking to it and then targetting + attacking):
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  
public void working() {
      if(job == "VILLAGER"){

      } else if(job == "WOODCUTTER"){
         if(!wasLooking && !isMovingOnPath && !wasDoingjob){ //searching for trees.
           int eX = 0; int eY = 0; int lowestXY = -1;
            boolean negativeX = false; boolean negativeY = false;
            for (Entity e : level.getEntities()){
               if (e instanceof ConstructionTree){
                 
                  if(x <= e.x && e.x-x <= jobRadius){
                     eX = e.x-x;
                     negativeX = true;
                  } else if(x > e.x  && x-e.x <= jobRadius){
                     negativeX = false;
                     eX = x-e.x;
                  }
                  if(y <= e.y && e.y-y <= jobRadius){
                     negativeY = false;
                     eY = e.y-y;
                  } else if(y > e.y && y-e.y <= jobRadius){
                     negativeY = true;
                     eY = y-e.y;
                  }
                  if(lowestXY != -1 && lowestXY > eX + eY || lowestXY == -1 && eX + eY != 0){
                     targetCon = (Construction)e;
                     lowestXY = eX + eY;
                     if(!negativeX){
                        jobX = x-eX + 8;
                     } else {
                        jobX = x+eX - 8;
                     }
                     if(!negativeY){
                        jobY = y+eY - 8;
                     } else {
                        jobY = y-eY - 8;
                     }
                  }
               }
            }
            wasLooking = true;
            if(lowestXY != -1){
               findPath(new Point(this.x >> 3, this.y >> 3), new Point((jobX >> 3), (jobY) >> 3));
            }
         } else if(!wasDoingjob && !isMovingOnPath && wasLooking){ //Targetting trees
           fightTargetCon = targetCon;
            isFighting = true;
            if(wonFight == true){
               wonFight = false;
               isFighting = false;
               wasDoingjob = true;
               findPath(new Point(this.x >> 3, this.y >> 3), house);
            }
         } else if(wasDoingjob && wasLooking && !isMovingOnPath){ //Waiting in his home
           home.occupied = true;
            isInsideBuilding = true;
            if(jobTimer > 400){
               jobTimer = 0;
               home.occupied = false;
               isInsideBuilding = false;
               wasLooking = false;
               wasDoingjob = false;
            } else {
               jobTimer++;
            }
         }
      }
   }


NOTE:
-There is probably nothing wrong with the attackingscript.
-I think it is a small bug/wrongplaced variable as many times it is working.
-Better performance things are always welcome!
-Ask for thing u want to know.

Already thanks.
-RoseSlayer

Fundamentum W.I.P.
Offline roseslayer

Junior Member


Medals: 1



« Reply #1 - Posted 2013-08-28 17:29:06 »

I fixed it by just removing the booleans, still don't know why but already thaught it was a small failure.

Fundamentum W.I.P.
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #2 - Posted 2013-08-28 17:34:44 »

That code frightens me. You've got names that end in "ing" and how many variables? How do you debug that? Sad

Also, if your path finding code gets the character stuck, you did not implement A*.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline roseslayer

Junior Member


Medals: 1



« Reply #3 - Posted 2013-08-28 17:52:32 »

That code frightens me. You've got names that end in "ing" and how many variables? How do you debug that? Sad

Also, if your path finding code gets the character stuck, you did not implement A*.

I implemented A*. the only problem I've is easy to fix is.
And I am cleaning my scripts over time:

Search code:
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  
if(!wasLooking && !isMovingOnPath && !wasDoingjob){ //Looking for trees.
           int eX = 0; int eY = 0; int lowestXY = workRadius * workRadius;
            for (Entity e : level.getEntities()){
               if (e instanceof ConstructionTree){
                   if(e.x >> 3 >= (home.x >> 3) - workRadius && e.x >> 3 <= (home.x >> 3) + workRadius){
                      if(e.y >> 3 >= (home.y >> 3) - workRadius && e.y >> 3 <= (home.y >> 3) + workRadius){
                         Game.level.alterTile((e.x >> 3) - 1, e.y >> 3, Tile.POSIONGRASS);
                         if(x <= e.x)
                                 eX = e.x-x;
                         else
                                 eX = x-e.x;
                         if(y <= e.y)
                            eY = e.y-y;
                         else
                                 eY = y-e.y;
                         if(lowestXY > eX + eY){
                                 targetCon = (Construction)e;
                                 lowestXY = eX + eY;
                                 if(x <= e.x)
                                    jobX = x+eX - 8;
                                 else
                                    jobX = x-eX + 8;
                                 if(y <= e.y)
                                    jobY = y+eY - 8;
                                 else
                                    jobY = y-eY - 8;
                         }
                      }
                   }
               }
            }
            if(lowestXY != workRadius * workRadius){
               wasLooking = true;
               findPath(new Point(this.x >> 3, this.y >> 3), new Point(jobX >> 3, jobY >> 3));
            }
         }


Fundamentum W.I.P.
Offline lcass
« Reply #4 - Posted 2013-08-28 19:07:40 »

WELP your code makes me have strokes randomly but here is what I think YOU yes YOU should do to make it simpler for yourself and also stop their little computer brains bleeding everytime they try to find a tree.

Make a treemap array , when you generate a tree you add its spawn x and spawn y to the map maybe even just make it a boolean array. Then  use a trial and error type pathfinding system to get to the tree.
Offline Several Kilo-Bytes

Senior Member


Medals: 11



« Reply #5 - Posted 2013-08-28 23:07:30 »

Your naming shows a lack of familiarity with Java. Most methods are in the form set<Property>, get<Property>, is<Boolean Result>, to<Alternative Type>, and <imperative verb>. Such as getColor, setColor, isVisble, toString, sort, etc. Your code just looks like a lump of if statements and variable assignments. There is no self documenting naming, no separation of concerns, and no modularity.

That is not A*. Where is the open list? the closed list? The heuristic? The cost function? A* cannot get stuck on a uniform grid and will tell you if there is no path to a goal.
Offline roseslayer

Junior Member


Medals: 1



« Reply #6 - Posted 2013-08-30 17:34:07 »

Make a treemap array , when you generate a tree you add its spawn x and spawn y to the map maybe even just make it a boolean array. Then  use a trial and error type pathfinding system to get to the tree.
That is a really good idea, going to do that for sure. the performance will be better!

Your naming shows a lack of familiarity with Java. Most methods are in the form set<Property>, get<Property>, is<Boolean Result>, to<Alternative Type>, and <imperative verb>. Such as getColor, setColor, isVisble, toString, sort, etc. Your code just looks like a lump of if statements and variable assignments. There is no self documenting naming, no separation of concerns, and no modularity.
I was used to programm (when I was younger, 5-3 years ago), to use GML (Game-maker language). I am still using the same naming and never thaught of changing it.

That is not A*. Where is the open list? the closed list? The heuristic? The cost function? A* cannot get stuck on a uniform grid and will tell you if there is no path to a goal.
The A* pathfinding was in an other topic and it is really in-efficient. If you wish to view it:
Pastebin.

The problem where this code will get stuck most times is the path it is returning.
and in the Villager tick() there is just a small code walking to the path array.


Fundamentum W.I.P.
Offline lcass
« Reply #7 - Posted 2013-08-30 17:51:34 »

Listen im currently working on pathfinding ais I like to call dumbpath , smartpath ,objectivepath. Dumbpath is for use with things like zombies they just run directly towards an x,y   smartpath takes into account objects and chooses a random direction to go to avoid the object.    objectivepath will be worked on for later use at is will develop its own personal knowladge map of collosions as it goes through.
Offline roseslayer

Junior Member


Medals: 1



« Reply #8 - Posted 2013-08-30 18:56:13 »

Listen im currently working on pathfinding ais I like to call dumbpath , smartpath ,objectivepath. Dumbpath is for use with things like zombies they just run directly towards an x,y   smartpath takes into account objects and chooses a random direction to go to avoid the object.    objectivepath will be worked on for later use at is will develop its own personal knowladge map of collosions as it goes through.

I've listened, but not sure how this can help me. I will never use dumbpaths in my game, as it looks weird and unprofessional. I am trying to always use the objectivepath. But I failed on how to find the G and returning the paht so my path is now more likely a smartpath but not the fastest because I am not calculating the G (correctly).

Fundamentum W.I.P.
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.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

ctomni231 (48 views)
2014-07-18 06:55:21

Zero Volt (44 views)
2014-07-17 23:47:54

danieldean (35 views)
2014-07-17 23:41:23

MustardPeter (38 views)
2014-07-16 23:30:00

Cero (53 views)
2014-07-16 00:42:17

Riven (52 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!