-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathCondition.java
More file actions
147 lines (134 loc) · 4.22 KB
/
Copy pathCondition.java
File metadata and controls
147 lines (134 loc) · 4.22 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Copyright (c) 2001-2025 Mathew A. Nelson and Robocode contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* https://robocode.sourceforge.io/license/epl-v10.html
*/
package robocode;
/**
* Condition is used to define custom {@link AdvancedRobot#waitFor(Condition)
* waitFor(Condition)} and custom events for an {@link AdvancedRobot}. The code
* below is taken from the sample robot named {@code sample.Target}. See the
* {@code sample/Target.java} for details.
* <pre>
* addCustomEvent(
* new Condition("triggerhit") {
* public boolean test() {
* return (getEnergy() <= trigger);
* };
* }
* );
* </pre>
* You should note that by extending Condition this way, you are actually
* creating an inner class -- so if you distribute your robot, there will be
* multiple class files. (i.e. {@code Target$1.class})
*
* @see AdvancedRobot#waitFor(Condition)
* @see AdvancedRobot#addCustomEvent(Condition)
* @see AdvancedRobot#removeCustomEvent(Condition)
* @see AdvancedRobot#onCustomEvent(CustomEvent)
*
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
* @author Nathaniel Troutman (contributor)
*/
public abstract class Condition {
/**
* The priority of this condition. Defaults to 80.
*/
public int priority = 80;
/**
* The name of this condition.
*/
public String name;
/**
* Creates a new, unnamed Condition with the default priority, which is 80.
*/
public Condition() {}
/**
* Creates a new Condition with the specified name, and default priority,
* which is 80.
*
* @param name the name for the new Condition
*/
public Condition(String name) {
this.name = name;
}
/**
* Creates a new Condition with the specified name and priority.
* A condition priority is a value from 0 - 99. The higher value, the
* higher priority. The default priority is 80.
*
* @param name the name for the new condition
* @param priority the priority of the new condition
*/
public Condition(String name, int priority) {
this.name = name;
if (priority < 0) {
System.out.println("SYSTEM: Priority must be between 0 and 99.");
System.out.println("SYSTEM: Priority for condition " + name + " will be 0.");
priority = 0;
} else if (priority > 99) {
System.out.println("SYSTEM: Priority must be between 0 and 99.");
System.out.println("SYSTEM: Priority for condition " + name + " will be 99.");
priority = 99;
}
this.priority = priority;
}
/**
* Returns the name of this condition.
*
* @return the name of this condition
*/
public String getName() {
return (name != null) ? name : getClass().getName();
}
/**
* Returns the priority of this condition.
* A condition priority is a value from 0 - 99. The higher value, the
* higher priority. The default priority is 80.
*
* @return the priority of this condition
*/
public final int getPriority() {
return priority;
}
/**
* Sets the name of this condition.
*
* @param newName the new name of this condition
*/
public void setName(String newName) {
name = newName;
}
/**
* Sets the priority of this condition.
* A condition priority is a value from 0 - 99. The higher value, the
* higher priority. The default priority is 80.
*
* @param newPriority the new priority of this condition.
*/
public void setPriority(int newPriority) {
priority = newPriority;
}
/**
* Overriding the test() method is the point of a Condition.
* The game will call your test() function, and take action if it returns
* {@code true}. This is valid for both {@link AdvancedRobot#waitFor} and
* {@link AdvancedRobot#addCustomEvent}.
* <p>
* You may not take any actions inside of test().
*
* @return {@code true} if the condition has been met, {@code false}
* otherwise.
*/
public abstract boolean test();
/**
* Called by the system in order to clean up references to internal objects.
*
* @since 1.4.3
*/
public void cleanup() {/* Do nothing: Should be overridden by sub-classes to perform needed
clean up to ensure that there are NO circular references */}
}