1 package cz.cuni.amis.pogamut.defcon.communication.worldview;
2
3 import javabot.PogamutJBotSupport;
4 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
5 import cz.cuni.amis.pogamut.defcon.base3d.worldview.object.DefConLocation;
6 import cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.flags.BasicFlag;
7 import cz.cuni.amis.pogamut.defcon.communication.worldview.modules.grid.flags.IFlagChecker;
8
9
10
11
12
13
14
15 public abstract class AbstractMapSource implements IFlagChecker {
16
17 public static final double STEP = 1d;
18
19 @Override
20 public DefConLocation traceFromTo(DefConLocation start, DefConLocation end,
21 BasicFlag flag) {
22
23 Location direction = end.sub(start);
24 Location step = direction.getNormalized().scale(STEP);
25 DefConLocation working = new DefConLocation(end);
26
27 while (direction.dot(working.sub(start)) > 0) {
28 if (hasFlag(working, flag))
29 return working;
30
31 working = new DefConLocation(working.getX() - step.getX(), working.getY() - step.getY());
32 }
33
34 return hasFlag(start, flag) ? start : null;
35 }
36
37 @Override
38 public DefConLocation traceFromTo(DefConLocation start, DefConLocation end,
39 BasicFlag flag,
40 double distance) {
41
42 Location direction = end.sub(start);
43 Location step = direction.getNormalized().scale(STEP);
44
45 DefConLocation working = new DefConLocation(end);
46 DefConLocation lastLocation = new DefConLocation();
47 DefConLocation okLocation = null;
48
49
50 distance *= distance;
51
52
53
54
55
56 while (direction.dot(working.sub(start)) > 0) {
57 if (hasFlag(working, flag)) {
58
59 lastLocation = new DefConLocation(working);
60
61 if (okLocation == null) {
62 okLocation = new DefConLocation(lastLocation);
63 }
64 } else {
65 okLocation = null;
66 }
67
68 if (okLocation != null) {
69 double dist = okLocation.getDistanceSquare(working);
70
71
72 if (dist > distance) {
73 return new DefConLocation(lastLocation);
74 }
75 }
76
77 working = new DefConLocation(working.getX() - step.getX(), working.getY() - step.getY());
78
79
80
81 }
82
83 if (okLocation != null)
84 return okLocation;
85
86 if (!hasFlag(start, flag))
87 PogamutJBotSupport
88 .writeToConsole("TraceFromTo: Flag start invalid " + start);
89 return hasFlag(start, flag) ? start : null;
90 }
91 }