summaryrefslogtreecommitdiff
path: root/jeu-test/Lemmini/0.84/src/Game/ReplayStream.java
blob: ead4561b96b5d89bbf9191b9e2392276a509c522 (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package Game;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/*
 * Copyright 2009 Volker Oth
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Handle replays.
 * @author Volker Oth
 */
public class ReplayStream {
	// event types
	final static int ASSIGN_SKILL = 0;
	final static int MOVE_XPOS = 1;
	final static int SELECT_SKILL = 2;
	final static int SET_RELEASE_RATE = 3;
	final static int NUKE = 4;

	private ArrayList<ReplayEvent> events;
	private int replayIndex;

	/**
	 * Constructor.
	 */
	public ReplayStream() {
		events = new ArrayList<ReplayEvent>(); // <events>
		replayIndex = 0;
	}

	/**
	 * Rewind replay to start position.
	 */
	public void rewind() {
		replayIndex = 0;
	}

	/**
	 * Get next replay event
	 * @param ctr frame counter
	 * @return replay event
	 */
	public ReplayEvent getNext(final int ctr) {
		if (replayIndex >= events.size())
			return null;
		ReplayEvent r = events.get(replayIndex);
		/* Note: there can be multiple replay events for one frame.
		 * return the next stored event if was stored for a frame
		 * smaller or equal to the given frame counter.
		 */
		if (ctr >= r.frameCtr) {
			replayIndex++;
			return r;
		}
		return null; /* no more events for this frame */
	}

	/**
	 * Clear the replay buffer.
	 */
	public void clear() {
		events.clear();
	}

	/**
	 * Clear the replay buffer from a certain frame counter.
	 * @param ctr frame counter
	 */
	public void clearFrom(final int ctr) {
		/* Note: there can be multiple replay events for one frame. */
		for (int i=events.size()-1; i>0; i--) {
			ReplayEvent r = events.get(i);
			if (r.frameCtr > ctr // clearly behind ctr -> erase
					|| r.frameCtr == ctr && i > replayIndex) // equal to ctr, but after replayIndex -> erase
				events.remove(i);
			else break;
		}
		replayIndex = 0;
	}

	/**
	 * Load replay buffer from file.
	 * @param fname file name
	 * @return replay information
	 */
	public ReplayLevelInfo load(final String fname) {
		try {
			ArrayList<ReplayEvent> ev = new ArrayList<ReplayEvent>();
			BufferedReader f = new BufferedReader(new FileReader(fname));
			String line = f.readLine();
			if (!line.equals("#REPLAY"))
				return null;
			// read level info
			line = f.readLine();
			String e[] = line.split(",");
			for (int j=0; j<e.length; j++)
				e[j] = e[j].trim();
			ReplayLevelInfo rli = new ReplayLevelInfo();
			if (e[0].charAt(0) != '#')
				return null;
			rli.setLevelPack(e[0].substring(1));
			rli.setDiffLevel(Integer.parseInt(e[1]));
			rli.setLvlNumber(Integer.parseInt(e[2]));
			// read events
			while ( (line=f.readLine()) != null) {
				e = line.split(",");
				int i[] = new int[e.length];
				for (int j=0; j<e.length; j++)
					i[j] = Integer.parseInt(e[j].trim());

				switch (i[1] /* type*/) {
					case ASSIGN_SKILL:
						ev.add(new ReplayAssignSkillEvent(i[0], Lemming.Type.get(i[2]), i[3]));
						break;
					case MOVE_XPOS:
						ev.add(new ReplayMoveXPosEvent(i[0], i[2]));
						break;
					case SELECT_SKILL:
						ev.add(new ReplaySelectSkillEvent(i[0], Lemming.Type.get(i[2])));
						break;
					case SET_RELEASE_RATE:
						ev.add(new ReplayReleaseRateEvent(i[0], i[2]));
						break;
					case NUKE:
						ev.add(new ReplayEvent(i[0], NUKE));
						break;
					default:
						return null;
				}
			}
			f.close();
			events = ev;
			return rli;
		} catch(FileNotFoundException e) {
			return null;
		}
		catch(IOException e) {
			return null;
		}
		catch(NumberFormatException e) {
			return null;
		}
		catch(ArrayIndexOutOfBoundsException e) {
			return null;
		}
	}

	/**
	 * Store replay info in a file.
	 * @param fname file name
	 * @return true if save ok, false otherwise
	 */
	public boolean save(final String fname) {
		try {
			FileWriter f = new FileWriter(new File(fname));
			f.write("#REPLAY\n");
			LevelPack lp = GameController.getCurLevelPack();
			f.write("#"+lp.getName()+", "+GameController.getCurDiffLevel()+", "+GameController.getCurLevelNumber()+"\n");
			for (int i=0; i < events.size(); i++) {
				ReplayEvent r = events.get(i);
				f.write(r.toString()+"\n"); // will use toString of the correct child object
			}
			f.close();

			return true;
		} catch(FileNotFoundException e) {
			return false;
		}
		catch(IOException e) {
			return false;
		}
	}

	/**
	 * Add a NUKE event (all lemmings nuked).
	 * @param ctr frame counter
	 */
	public void addNukeEvent(final int ctr) {
		ReplayEvent event = new ReplayEvent(ctr, NUKE);
		events.add(event);
	}

	/**
	 * Add ASSIGN_SKILL event (one lemming was assigned a skill).
	 * @param ctr frame counter
	 * @param skill skill assigned
	 * @param lemming Lemming the skill was assigned to
	 */
	public void addAssignSkillEvent(final int ctr, final Lemming.Type skill, final int lemming ) {
		ReplayAssignSkillEvent event = new ReplayAssignSkillEvent(ctr, skill, lemming);
		events.add(event);
	}

	/**
	 * Add SELECT_SKILL event (skill selection button was pressed).
	 * @param ctr frame counter
	 * @param skill skill selected
	 */
	public void addSelectSkillEvent(final int ctr, final Lemming.Type skill) {

		ReplaySelectSkillEvent event = new ReplaySelectSkillEvent(ctr, skill);
		events.add(event);
	}

	/**
	 * Add MOVE_XPOS event (screen moved left/right).
	 * @param ctr frame counter
	 * @param xPos new screen position
	 */
	public void addXPosEvent(final int ctr, final int xPos ) {
		ReplayMoveXPosEvent event = new ReplayMoveXPosEvent(ctr, xPos);
		events.add(event);
	}

	/**
	 * Add SET_RELEASE_RATE event (release rate was changed).
	 * @param ctr frame counter
	 * @param releaserate new release rate
	 */
	public void addReleaseRateEvent(final int ctr, final int releaserate ) {
		ReplayReleaseRateEvent event = new ReplayReleaseRateEvent(ctr, releaserate);
		events.add(event);
	}
}

/**
 * Storage class for one replay event.
 * @author Volker Oth
 */
class ReplayEvent {
	/** frame counter */
	int frameCtr;
	/** event type */
	int type;

	/**
	 * Constructor
	 * @param ctr frame counter
	 * @param t type
	 */
	public ReplayEvent(final int ctr, final int t) {
		frameCtr = ctr;
		type = t;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ""+frameCtr+", "+type;
	}
}

/**
 * Storage class for ASSIGN_SKILL event
 * @author Volker Oth
 */
class ReplayAssignSkillEvent extends ReplayEvent {
	/** skill */
	Lemming.Type skill;
	/** Lemming */
	int lemming;

	/**
	 * Skill assigned
	 * @param ctr Frame counter
	 * @param s skill selected
	 * @param lem lemming no. that the skill was assigned
	 */
	public ReplayAssignSkillEvent(final int ctr, final Lemming.Type s, final int lem) {
		super(ctr, ReplayStream.ASSIGN_SKILL);
		skill = s;
		lemming = lem;
	}

	/* (non-Javadoc)
	 * @see Game.ReplayEvent#toString()
	 */
	@Override
	public String toString() {
		return super.toString()+", "+skill.ordinal()+", "+lemming;
	}
}

/**
 * Storage class for SELECT_SKILL event.
 * @author Volker Oth
 */
class ReplaySelectSkillEvent extends ReplayEvent {
	Lemming.Type skill;

	/**
	 * Skill selected
	 * @param ctr Frame counter
	 * @param s skill selected
	 */
	public ReplaySelectSkillEvent(final int ctr, final Lemming.Type s) {
		super(ctr, ReplayStream.SELECT_SKILL);
		skill = s;
	}

	/* (non-Javadoc)
	 * @see Game.ReplayEvent#toString()
	 */
	@Override
	public String toString() {
		return super.toString()+", "+skill.ordinal();
	}
}

/**
 * Storage class for MOVE_XPOS event.
 * @author Volker Oth
 */
class ReplayMoveXPosEvent extends ReplayEvent {
	/** screen x position */
	int xPos;

	/**
	 * Screen X position changed event
	 * @param ctr Frame counter
	 * @param x release x position
	 */
	public ReplayMoveXPosEvent(final int ctr, final int x) {
		super(ctr, ReplayStream.MOVE_XPOS);
		xPos = x;
	}

	/* (non-Javadoc)
	 * @see Game.ReplayEvent#toString()
	 */
	@Override
	public String toString() {
		return super.toString()+", "+xPos;
	}
}

/**
 * Storage class for SET_RELEASE_RATE event.
 * @author Volker Oth
 */
class ReplayReleaseRateEvent extends ReplayEvent {
	int releaseRate;

	/**
	 * Release Rate changed event
	 * @param ctr Frame counter
	 * @param rate release rate value
	 */
	public ReplayReleaseRateEvent(final int ctr, final int rate) {
		super(ctr, ReplayStream.SET_RELEASE_RATE);
		releaseRate = rate;
	}

	/* (non-Javadoc)
	 * @see Game.ReplayEvent#toString()
	 */
	@Override
	public String toString() {
		return super.toString()+", "+releaseRate;
	}
}