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 raysMap.remove(type);
56 rayFlagChangedListeners.remove(type);
57 prepareRays();
58 }
59
60
61 public HashMap<String, Future<AutoTraceRay>> getMyFutureRays(SteeringType type) {
62 return rayFutures.get(type);
63 }
64
65
66 public boolean raysAreReady() {
67 return raycasting.getAllRaysInitialized().getFlag();
68 }
69
70
71 private void prepareRays() {
72 raycasting.clear();
73 rayFutures.clear();
74
75
76
77 for(SteeringType type : raysMap.keySet()) {
78 LinkedList<SteeringRay> rayList = raysMap.get(type);
79
80 HashMap<String, Future<AutoTraceRay>> fr = new HashMap<String, Future<AutoTraceRay>>();
81 for(SteeringRay ray : rayList) {
82 Future<AutoTraceRay> future = raycasting.createRay(ray.id, ray.direction, ray.length, fastTrace, floorCorrection, traceActor);
83
84 fr.put(ray.id, future);
85 }
86 rayFutures.put(type, fr);
87 }
88
89 raycasting.endRayInitSequence();
90 notifyRayFlagChangedListeners();
91 }
92 }