-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit.java
More file actions
187 lines (186 loc) · 4.67 KB
/
Copy pathUnit.java
File metadata and controls
187 lines (186 loc) · 4.67 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
public class Unit implements Comparable<Unit>{
public int xloc;
public int yloc;
public int nextTick;
public int speed;
public int damage;
public int range;
public int health;
public int id;
public Team team;
public Map imap;
public PathNode[][] pathing;
public Unit(Team t, int i){
team = t;
id = i;
nextTick = 0;
}
/*
public Unit(Team t, int currhp){
team = t;
health = currhp;
}*/
public void setIMap(Map map){
imap = map;
}
// MAY WANT TO CHANGE NEXT 2 METHODS TO SPACE? MAY CHANGE EVERYTHING ELSE TO COORDS
public void move(int x, int y){
imap.moveUnit(this, x, y);
setLoc(x, y);
}
public void setLoc(int x, int y){
xloc = x;
yloc = y;
}
public void attack(Unit t){
imap.attack(this, t);
}
public void defend(int x){
health -= x;
if(health <= 0){
die();
}
}
public void die(){
imap.removeUnit(this);
imap.units.remove(this.id);
imap.next.remove(this);
team.members.remove(this.id);
xloc = -1;
yloc = -1;
}
public void act(){
if(team.command() == 0){
runBasic();
}
}
public int getDistance(Space space){
return Math.abs(space.xloc-xloc) + Math.abs(space.yloc-yloc);
}
/* THIS ALLOWS FOR DIAGONAL MOVES
public int getDistance(Space space){
int xdist = Math.abs(space.xloc - xloc);
int ydist = Math.abs(space.yloc - yloc);
if(xdist > ydist)
return xdist;
return ydist;
}
*/
public void runBasic(){
Unit u = findNearestEnemy();
if(isInRange(u)){
attack(u);
nextTick += speed;
} else {
Space s = findPathTo(imap.board[u.xloc][u.yloc]);
if(s.xloc == xloc && s.yloc == yloc){
nextTick = imap.next.peek().nextTick+1;
} else {
move(s.xloc,s.yloc);
nextTick += imap.board[xloc][yloc].terr.movecost*speed;
System.out.println(nextTick);
}
}
}
public boolean isInRange(Unit u){
if(getDistance(imap.board[u.xloc][u.yloc]) <= range){
return true;
}
return false;
}
public Unit findNearestEnemy(){
Unit near = null;
int min = Integer.MAX_VALUE;
for(Unit u:imap.units.values()){
int dist = getDistance(imap.board[u.xloc][u.yloc]);
if(dist < min && u.team != team){
near = u;
min = dist;
}
}
return near;
}
public Space findPathTo(Space s){
pathing = new PathNode[imap.board.length][imap.board[0].length];
pathing[xloc][yloc] = new PathNode(0, null);
Space curr = imap.board[xloc][yloc];
while(curr.getDistance(s) > 1){
Space[] neighbors = imap.findNeighbors(curr);
int blocked = 0;
for(Space test: neighbors){
if(test != null && !test.occ){
if(pathing[test.xloc][test.yloc] != null){
int moveCost = pathing[curr.xloc][curr.yloc].cost + test.terr.getMoveCost();
if(pathing[test.xloc][test.yloc] != null && moveCost < pathing[test.xloc][test.yloc].cost){
pathing[test.xloc][test.yloc] = new PathNode((pathing[curr.xloc][curr.yloc].cost + test.terr.getMoveCost()), curr);
}
} else {
pathing[test.xloc][test.yloc] = new PathNode((pathing[curr.xloc][curr.yloc].cost + test.terr.getMoveCost()), curr);
}
} else {
blocked++;
}
}
if(blocked == 4){
return imap.board[xloc][yloc];
}
pathing[curr.xloc][curr.yloc].visit();
int min = Integer.MAX_VALUE;
int minx = 0;
int miny = 0;
for(int i = 0; i < pathing.length; i++){
for(int j = 0; j < pathing[0].length; j++){
if(pathing[i][j] != null && !pathing[i][j].visited && pathing[i][j].cost < min){
minx = i;
miny = j;
min = pathing[i][j].cost;
}
}
}
curr = imap.board[minx][miny];
//printlayer();
}
while(!pathing[curr.xloc][curr.yloc].prev.equals(imap.board[xloc][yloc])){
curr = pathing[curr.xloc][curr.yloc].prev;
}
return curr;
}
public void printlayer(){
System.out.print(" ");
for(int i = 0; i < pathing.length; i++){
System.out.print(i);
for(int j = ((Integer)i).toString().length(); j < 3; j++){
System.out.print(" ");
}
}
System.out.println();
for(int i = 0; i < pathing.length; i++){
System.out.print(i);
for(int j = ((Integer)i).toString().length(); j < 2; j++){
System.out.print(" ");
}
for(int j = 0; j < pathing[i].length; j++){
if(pathing[i][j] != null){
System.out.print(" " + pathing[i][j].cost + " ");
} else {
System.out.print(" X ");
}
}
System.out.println();
}
System.out.println();
}
@Override
public int compareTo(Unit o) {
if(nextTick == o.nextTick){
return id - o.id;
}
return nextTick-o.nextTick;
}
public String toString(){
return "";
}
public String printID(){
return id + " " + nextTick;
}
}