-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallbackHelper.java
More file actions
92 lines (85 loc) · 2.85 KB
/
CallbackHelper.java
File metadata and controls
92 lines (85 loc) · 2.85 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
//package Bla1AI;
import com.springrts.ai.oo.clb.*;
import com.springrts.ai.oo.AIFloat3;
import java.lang.Math;
import java.util.ArrayList;
import java.util.List;
/**
* A static class with the callback and all other meathods that should be accessible to all classes
* in Bla1AI
*
* @author Richard Nai
* @version (a version number or a date)
*/
public class CallbackHelper
{
private static OOAICallback engineCallback = null;
/**
* Constructor for objects of class CallbackHelper
*/
public CallbackHelper(OOAICallback callback)
{
try{
engineCallback = callback;
}
catch(Exception ex){
say("Error in callback!");
}
}
public static OOAICallback getCallback(){
return engineCallback;
}
/**
* sends a message to the game
*/
public static void say(String msg){
engineCallback.getGame().sendTextMessage(msg, 0);
}
public static float getDistanceBetween(AIFloat3 loc1, AIFloat3 loc2){
float xDistance = loc1.x - loc2.x;
float yDistance = loc1.y - loc2.y;
float zDistance = loc1.z - loc2.z;
float totalDistanceSquared = xDistance*xDistance + yDistance*yDistance + zDistance*zDistance;
return totalDistanceSquared;
}
public static AIFloat3 randomPointAround(AIFloat3 mid, float distance){
boolean notInRange = true;
while(notInRange){
AIFloat3 answer = mid;
answer.x+= 2*(Math.random()-0.5)*distance;
answer.z+=2*(Math.random()-0.5)*distance;
if(Math.sqrt(getDistanceBetween(mid, answer))<=distance){
return answer;
}
}
return null;
}
public static AIFloat3 randomPointOutside(AIFloat3 mid, float distance){
say("recieved call to randomPointOutSide");
boolean InRange = true;
while(InRange){
AIFloat3 answer = mid;
answer.x+= 4*(Math.random()-0.5)*distance;
answer.z+=4*(Math.random()-0.5)*distance;
if(Math.sqrt(getDistanceBetween(mid, answer))>=distance){
say("Exiting randomPointOutside, returning "+ answer.toString());
return answer;
}
}
return null;
}
public static AIFloat3 randomPoint(){
return new AIFloat3((float)Math.random()*engineCallback.getMap().getWidth()*8, (float)0, (float)Math.random()*engineCallback.getMap().getHeight()*8);
}
public static UnitDef findMatch(List<UnitDef> list, Unit unit){
ArrayList<UnitDef> similarOptions = new ArrayList<UnitDef>();
for(UnitDef def: list){
for(UnitDef uniDef: unit.getDef().getBuildOptions()){
if(def.equals(uniDef)){
similarOptions.add(def);
}
}
}
return similarOptions.get(0);
}
}