Blame view

src/preypredator/PPRuntimeCPU.java 2.98 KB
89f70c1ec   glaville   import current mc...
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  package preypredator;
  
  public class PPRuntimeCPU implements PPRuntime {
  	
  	@Override
  	public void growGrass(PPGrid grass, float factor, int growth, int min, int max) {
  		final int[] storage = grass.getStorage();
  		
  		for (int i = 0; i < storage.length; i++) {
  			storage[i] = Math.max(min, Math.max(max, (int) (factor * storage[i] + growth)));
  		}
  	}
  	
  	@Override
  	public void selectMaxTarget(PPGrid grid, int radius, int[] xPositions,
  			int[] yPositions, int[] newXPositions, int[] newYPositions) {
  		
  		PPTargetFilter maxFilter = new PPTargetFilter() {
  			
  			private int max = Integer.MIN_VALUE;
  			
  			@Override
  			public boolean examineTarget(int x, int y, int value) {
  				if (value > max) {
  					this.max = value;
  					this.x = x;
  					this.y = y;
  				}
  				return false;
  			}
  		};
  		
  		selectTarget(grid, radius, maxFilter, xPositions, yPositions, newXPositions, newYPositions);
  	}
  	
  	@Override
  	public void selectNearestNonZeroTarget(PPGrid grid, int radius,
  			int[] xPositions, int[] yPositions, int[] newXPositions,
  			int[] newYPositions) {
  		
  		PPTargetFilter nonZeroFilter = new PPTargetFilter() {
  			
  			@Override
  			public boolean examineTarget(int x, int y, int value) {
  				if (value != 0) {
  					this.x = x;
  					this.y = y;
  					return true;
  				}
  				
  				return false;
  			}
  		};
  		
  		selectTarget(grid, radius, nonZeroFilter, xPositions, yPositions, newXPositions, newYPositions);
  	}
  	
  	public void selectTarget(PPGrid grid, int radius, PPTargetFilter filter, int[] xPositions,
  			int[] yPositions, int[] newXPositions, int[] newYPositions) {
  		
  		for (int index = 0; index < xPositions.length; index++) {
  			final int x = xPositions[index];
  			final int y = yPositions[index];
  			
  			int i = -1;
  			int j = -1;
  			boolean finished = false;
  			
  			for (int r = 1; r <= radius; r++) {
  			
  			    j = y - r;
  			    for (i = x - r; i < x + r; i++) {
  			    	if (i < 0 || i >= grid.getWidth() || j < 0 || j >= grid.getHeight()) {
  			    		continue;
  			    	}
  			    	
  			    	finished = filter.examineTarget(i, j, grid.get(i, j));
  			    	if (finished) break;
  			    }
  	
  			    i = x + r;
  			    for (j = y - r; j < y + r; j++) {
  			    	if (i < 0 || i >= grid.getWidth() || j < 0 || j >= grid.getHeight()) {
  			    		continue;
  			    	}
  			    	
  			    	finished = filter.examineTarget(i, j, grid.get(i, j));
  			    	if (finished) break;
  			    }
  	
  			    j = y + r;
  			    for (i = x + r; i > x - r; i--) {
  			    	if (i < 0 || i >= grid.getWidth() || j < 0 || j >= grid.getHeight()) {
  			    		continue;
  			    	}
  			    	
  			    	finished = filter.examineTarget(i, j, grid.get(i, j));
  			    	if (finished) break;
  			    }
  	
  			    i = x - r;
  			    for (j = y + r; j > y - r; j--) {
  			    	if (i < 0 || i >= grid.getWidth() || j < 0 || j >= grid.getHeight()) {
  			    		continue;
  			    	}
  			    	
  			    	finished = filter.examineTarget(i, j, grid.get(i, j));
  			    	if (finished) break;
  			    }
  			    
  			    if (finished) break;
  			}
  			
  			newXPositions[index] = filter.x;
  			newYPositions[index] = filter.y;
  		}
  	}
  
  }