View Javadoc

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   * Implements common methods to all map sources.
11   * 
12   * @author Radek 'Black_Hand' Pibil
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.setTo(
32  					working.getX() - step.getX(),
33  					working.getY() - step.getY(),
34  					0);
35  
36  		}
37  
38  		return hasFlag(start, flag) ? start : null;
39  	}
40  
41  	@Override
42  	public DefConLocation traceFromTo(DefConLocation start, DefConLocation end,
43  			BasicFlag flag,
44  			double distance) {
45  
46  		Location direction = end.sub(start);
47  		Location step = direction.getNormalized().scale(STEP);
48  
49  		DefConLocation working = new DefConLocation(end);
50  		DefConLocation lastLocation = new DefConLocation();
51  		DefConLocation okLocation = null;
52  
53  		// square distance is easier to compute
54  		distance *= distance;
55  
56  		// PogamutJBotSupport.writeToConsole("direction: " + direction +
57  		// " "
58  		// + step + " " + working + " " + start + " " + end);
59  
60  		while (direction.dot(working.sub(start)) > 0) {
61  			if (hasFlag(working, flag)) {
62  
63  				lastLocation.setTo(working);
64  
65  				if (okLocation == null) {
66  					okLocation = new DefConLocation(lastLocation);
67  				}
68  			} else {
69  				okLocation = null;
70  			}
71  
72  			if (okLocation != null) {
73  				double dist = okLocation.getDistanceSquare(working);
74  				// PogamutJBotSupport.writeToConsole("okLocationdist: "
75  				// + okLocation + " " + Math.sqrt(dist));
76  				if (dist > distance) {
77  					return new DefConLocation(lastLocation);
78  				}
79  			}
80  
81  			working.setTo(
82  					working.getX() - step.getX(),
83  					working.getY() - step.getY());
84  			// PogamutJBotSupport.writeToConsole("direction: " + direction +
85  			// " "
86  			// + step + " " + working + " " + start + " " + end);
87  		}
88  
89  		if (okLocation != null)
90  			return okLocation;
91  
92  		if (!hasFlag(start, flag))
93  			PogamutJBotSupport
94  					.writeToConsole("TraceFromTo: Flag start invalid " + start);
95  		return hasFlag(start, flag) ? start : null;
96  	}
97  }