1 package SteeringStuff;
2
3 import cz.cuni.amis.pogamut.ut2004.agent.module.sensomotoric.Raycasting;
4 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
5 import cz.cuni.amis.pogamut.ut2004.communication.messages.gbinfomessages.AutoTraceRay;
6 import java.util.HashMap;
7 import java.util.LinkedList;
8 import java.util.concurrent.Future;
9
10
11
12
13
14 public class RaycastingManager {
15
16 private UT2004Bot botself;
17 public Raycasting raycasting;
18
19 private static boolean fastTrace = false;
20 private static boolean floorCorrection = false;
21 private static boolean traceActor = false;
22
23 private HashMap<SteeringType, LinkedList<SteeringRay>> raysMap = new HashMap<SteeringType, LinkedList<SteeringRay>>();
24 private HashMap<SteeringType, IRaysFlagChanged> rayFlagChangedListeners = new HashMap<SteeringType, IRaysFlagChanged>();
25 private HashMap<SteeringType, HashMap<String, Future<AutoTraceRay>>> rayFutures = new HashMap<SteeringType, HashMap<String, Future<AutoTraceRay>>>();
26
27
28 public RaycastingManager(UT2004Bot botself, Raycasting raycasting) {
29 this.botself = botself;
30 this.raycasting = raycasting;
31 }
32
33
34 public void addRayFlagChangedListener(SteeringType type, IRaysFlagChanged listener) {
35 rayFlagChangedListeners.put(type, listener);
36 }
37
38
39 public void notifyRayFlagChangedListeners() {
40 for (IRaysFlagChanged listener : rayFlagChangedListeners.values()) {
41 if (SteeringManager.DEBUG) System.out.println("Flag ray changed. We tell to "+listener);
42 listener.flagRaysChanged();
43 }
44 }
45
46
47 public void addRays(SteeringType type, LinkedList<SteeringRay> rayList, IRaysFlagChanged listener) {
48 raysMap.put(type, rayList);
49 addRayFlagChangedListener(type, listener);
50 prepareRays();
51 }
52
53
54 public void removeRays(SteeringType type) {
55 if (SteeringManager.DEBUG) System.out.println("We remove rays for "+type);
56 raysMap.remove(type);
57 rayFlagChangedListeners.remove(type);
58 prepareRays();
59 }
60
61
62 public HashMap<String, Future<AutoTraceRay>> getMyFutureRays(SteeringType type) {
63 return rayFutures.get(type);
64 }
65
66
67 public boolean raysAreReady() {
68 return raycasting.getAllRaysInitialized().getFlag();
69 }
70
71
72 private void prepareRays() {
73 raycasting.clear();
74 rayFutures.clear();
75
76
77
78 for(SteeringType type : raysMap.keySet()) {
79 LinkedList<SteeringRay> rayList = raysMap.get(type);
80 if (SteeringManager.DEBUG) System.out.println("We prepare rays for "+type);
81 HashMap<String, Future<AutoTraceRay>> fr = new HashMap<String, Future<AutoTraceRay>>();
82 for(SteeringRay ray : rayList) {
83 Future<AutoTraceRay> future = raycasting.createRay(ray.id, ray.direction, ray.length, fastTrace, floorCorrection, traceActor);
84
85 fr.put(ray.id, future);
86 }
87 rayFutures.put(type, fr);
88 }
89
90 raycasting.endRayInitSequence();
91 notifyRayFlagChangedListeners();
92 }
93 }