Source

Technical Machine / source / clients / pokemon_online / battle.cpp

Full commit
  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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Pokemon Online battle logic
// Copyright (C) 2012 David Stone
//
// This file is part of Technical Machine.
//
// Technical Machine is free software: you can redistribute it and / or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "battle.hpp"

#include <array>
#include <iostream>
#include <string>
#include <vector>

#include "client.hpp"
#include "conversion.hpp"
#include "inmessage.hpp"
#include "outmessage.hpp"

#include "../../team.hpp"

#include "../../pokemon/pokemon.hpp"

namespace technicalmachine {
namespace po {
namespace {

class Todo {
	public:
		explicit Todo (InMessage & in_msg, std::string const & str):
			msg (in_msg) {
			std::cerr << str + '\n';
		}
		explicit Todo (InMessage & in_msg):
			msg (in_msg) {
		}
		~Todo() {
			while (msg.index != msg.buffer.size ())
				std::cerr << '\t' << static_cast<int> (msg.read_byte()) << '\n';
		}
	private:
		InMessage & msg;
};

std::array<int8_t, 7> parse_boosts(InMessage & msg);

}	// unnamed namespace

void Battle::handle_message (Client & client, uint32_t battle_id, uint8_t command, Party const party, InMessage & msg) {
	enum {
		SEND_OUT = 0,
		WITHDRAW = 1,		// "SendBack"
		USE_ATTACK = 2,
		OFFER_CHOICE = 3,
		BEGIN_TURN = 4,
		PP_CHANGE = 5,
		HP_CHANGE = 6,
		KO = 7,
		EFFECTIVENESS = 8,
		MISS = 9,
		CRITICAL_HIT = 10,
		NUMBER_OF_HITS = 11,
		STAT_CHANGE = 12,
		STATUS_CHANGE = 13,
		STATUS_MESSAGE = 14,
		FAILED = 15,
		BATTLE_CHAT = 16,
		MOVE_MESSAGE = 17,
		ITEM_MESSAGE = 18,
		NO_TARGET = 19,
		FLINCH = 20,
		RECOIL = 21,
		WEATHER_MESSAGE = 22,
		STRAIGHT_DAMAGE = 23,
		ABILITY_MESSAGE = 24,
		ABS_STATUS_CHANGE = 25, 
		SUBSTITUTE = 26,
		BATTLE_END = 27,
		BLANK_MESSAGE = 28,
		CANCEL_MOVE = 29,
		CLAUSE = 30,
		DYNAMIC_INFO = 31,
		DYNAMIC_STATS = 32,
		SPECTATING = 33,
		SPECTATOR_CHAT = 34,
		ALREADY_STATUSED = 35,
		TEMPORARY_POKEMON_CHANGE = 36,
		CLOCK_START = 37,
		CLOCK_STOP = 38,
		RATED = 39,
		TIER_SECTION = 40,
		END_MESSAGE = 41,
		POINT_ESTIMATE = 42,
		MAKE_YOUR_CHOICE = 43,
		AVOID = 44,
		REARRANGE_TEAM = 45,
		SPOT_SHIFTS = 46
	};
	switch (command) {
		case BEGIN_TURN:
			parse_begin_turn(msg);
			break;
		case SEND_OUT:
			parse_send_out(msg, party);
			break;
		case WITHDRAW:
			// Do nothing.
			break;
		case USE_ATTACK:
			parse_use_attack(msg, party);
			break;
		case STRAIGHT_DAMAGE:
			parse_straight_damage(msg);
			break;
		case HP_CHANGE:
			parse_hp_change(msg, party);
			break;
		case PP_CHANGE:
			parse_pp_change(msg);
			break;
		case KO: {
			constexpr uint8_t slot = 0;
			handle_fainted (party, slot);
			break;
		}
		case EFFECTIVENESS:
			parse_effectiveness(msg);
			break;
		case AVOID:
		case MISS:
			std::cerr << ((command == AVOID) ? "AVOID\n" : "MISS\n");
			handle_miss(party);
			break;
		case NO_TARGET:
			// "But there was no target..."
			break;
		case FAILED:
			// "But it failed!"
			break;
		case CRITICAL_HIT:
			handle_critical_hit(party);
			break;
		case NUMBER_OF_HITS:
			parse_number_of_hits(msg);
			break;
		case STAT_CHANGE:
			parse_stat_change(msg);
			break;
		case STATUS_MESSAGE:
			parse_status_message(msg);
			break;
		case STATUS_CHANGE:
			parse_status_change(msg);
			break;
		case ABS_STATUS_CHANGE:
			parse_abs_status_change(msg);
			break;
		case ALREADY_STATUSED:
			parse_already_statused(msg);
			break;
		case FLINCH:
			handle_flinch(party);
			break;
		case RECOIL:
			parse_recoil(msg);
			break;
		case WEATHER_MESSAGE:
			parse_weather_message(msg);
			break;
		case ABILITY_MESSAGE:
			parse_ability_message(msg, party);
			break;
		case ITEM_MESSAGE:
			parse_item_message(msg, party);
			break;
		case MOVE_MESSAGE:
			parse_move_message(msg);
			break;
		case SUBSTITUTE:
			parse_substitute(msg);
			break;
		case CLAUSE:
			std::cerr << "CLAUSE\n";
			break;
		case DYNAMIC_INFO:
			parse_dynamic_info(msg);
			break;
		case DYNAMIC_STATS:
			parse_dynamic_stats(msg);
			break;
		case SPECTATING:
			parse_spectating(msg);
			break;
		case TEMPORARY_POKEMON_CHANGE:
			parse_temporary_pokemon_change(msg);
			break;
		case CLOCK_START:
			parse_clock_start(msg);
			break;
		case CLOCK_STOP:
			parse_clock_stop(msg);
			break;
		case RATED:
			handle_rated(client, msg);
			break;
		case TIER_SECTION:
			parse_tier_section(msg);
			break;
		case BLANK_MESSAGE:
			// Apparently this is used to tell the client to print a new line...
			break;
		case BATTLE_CHAT:
		case END_MESSAGE:
			std::cerr << ((command == BATTLE_CHAT) ? "BATTLE_CHAT\n" : "END_MESSAGE\n");
			parse_battle_chat(client, msg);
			break;
		case SPECTATOR_CHAT:
			parse_spectator_chat(client, msg, battle_id);
			break;
		case POINT_ESTIMATE:
			parse_point_estimate(msg);
			break;
		case OFFER_CHOICE:
			parse_offer_choice(client, msg, battle_id);
			break;
		case MAKE_YOUR_CHOICE:
			handle_make_your_choice(client);
			break;
		case CANCEL_MOVE:
			handle_cancel_move();
			break;
		case REARRANGE_TEAM:
			parse_rearrange_team(msg);
			break;
		case SPOT_SHIFTS:
			parse_spot_shifts(msg);
			break;
		case BATTLE_END:
			parse_battle_end(msg);
			break;
		default:
			std::cerr << "Unknown battle message " << command << '\n';
			break;
	}
}

void Battle::parse_begin_turn (InMessage & msg) {
	uint32_t const turn = msg.read_int ();
	handle_begin_turn (turn);
}

void Battle::parse_send_out (InMessage & msg, Party const party) {
	bool const is_silent = msg.read_byte ();
	uint8_t const index = msg.read_byte ();
	uint16_t const species_id = msg.read_short ();
	uint8_t const forme_id = msg.read_byte ();
	Species const species = id_to_species (species_id, forme_id);
	std::string const nickname = msg.read_string ();
	uint8_t const hp_percent = msg.read_byte ();
	uint32_t const full_status = msg.read_int ();
	Gender const gender (id_to_gender (msg.read_byte ()));
	bool const shiny = msg.read_byte ();
	uint8_t const level = msg.read_byte ();
	uint8_t const slot = 0;
	handle_send_out (party, slot, index, nickname, species, gender, level);
	if (is_me(party)) {
		slot_memory_bring_to_front();
	}
}

void Battle::parse_use_attack (InMessage & msg, Party const party) {
	std::cerr << is_me(party) << " USE ATTACK\n";
	uint16_t const attack = msg.read_short ();
	constexpr uint8_t slot = 0;
	handle_use_move (party, slot, id_to_move(attack));
	last_attacker = party;
}

void Battle::parse_straight_damage (InMessage & msg) {
	uint16_t const damage = msg.read_short();
	constexpr uint8_t slot = 0;
	handle_direct_damage(last_attacker.other(), slot, damage);
}

void Battle::parse_hp_change (InMessage & msg, Party const party) {
	uint16_t const remaining_hp = msg.read_short ();
	uint8_t const slot = 0;
	handle_hp_change (party, slot, remaining_hp);
}

void Battle::parse_pp_change (InMessage & msg) {
	uint8_t const move = msg.read_byte ();
	uint8_t const new_pp = msg.read_byte ();
}

void Battle::parse_effectiveness (InMessage & msg) {
	// This matters for Hidden Power. Do nothing with this for now.
	uint8_t const effectiveness = msg.read_byte ();
	// 0, 1, 2, 4, 8, 16
}

void Battle::parse_number_of_hits (InMessage & msg) {
	uint8_t const number = msg.read_byte ();
}

void Battle::parse_stat_change (InMessage & msg) {
	std::cerr << "STAT_CHANGE\n";
	// I think stat may work just as well unsigned.
	int8_t const stat = static_cast<int8_t> (msg.read_byte ());
	int8_t const boost = static_cast<int8_t> (msg.read_byte ());
}

void Battle::parse_status_message (InMessage & msg) {
	Todo const t (msg, "STATUS_MESSAGE");
	int8_t const status = static_cast<int8_t> (msg.read_byte ());
}

void Battle::parse_status_change (InMessage & msg) {
	Todo const t(msg, "STATUS_CHANGE");
	int8_t const status = static_cast<int8_t> (msg.read_byte());
	// bool const multiple_turns = msg.read_byte ();
	// Includes things like confusion
}

void Battle::parse_abs_status_change (InMessage & msg) {
	uint8_t const index = msg.read_byte();
	if (index >= pokemon_per_team()) {
		std::cerr << "Invalid Pokemon index.\n";
		return;
	}
	int8_t const status = static_cast<int8_t> (msg.read_byte());
}

void Battle::parse_already_statused (InMessage & msg) {
	uint8_t const status = msg.read_byte ();
}

void Battle::parse_recoil (InMessage & msg) {
	std::cerr << "RECOIL\n";
	bool const damaging = msg.read_byte ();
	if (damaging) {
		// is hit with recoil
	}
	else {
		// had its energy drained
	}
}

void Battle::parse_weather_message (InMessage & msg) {
	std::cerr << "WEATHER_MESSAGE\n";
	int8_t const wstatus = static_cast<int8_t> (msg.read_byte());
	int8_t const weather_var = static_cast<int8_t> (msg.read_byte());
}

void Battle::parse_ability_message (InMessage & msg, Party const party) {
	uint16_t const ability = msg.read_short ();
	uint8_t const part = msg.read_byte ();
	handle_ability_message(party, battle_id_to_ability(ability, part));
	#if 0
	int8_t const type = msg.read_byte ();
	int8_t const foe = msg.read_byte ();
	int16_t const other = msg.read_short ();
	#endif
}

void Battle::parse_item_message (InMessage & msg, Party const party) {
	uint16_t const item_id = msg.read_short ();
	uint8_t const part = msg.read_byte ();
	handle_item_message(party, battle_id_to_item(item_id, part));
	// int8_t const foe = msg.read_byte ();
	// int16_t const berry = msg.read_short ();
	// int16_t const other = msg.read_short ();
}

void Battle::parse_move_message (InMessage & msg) {
	std::cerr << "MOVE_MESSAGE\n";
	#if 0
	uint16_t const move = msg.read_short ();
	uint8_t const part = msg.read_byte ();
	int8_t const type = msg.read_byte ();
	int8_t const foe = msg.read_byte ();
	int16_t const other = msg.read_short ();
	std::string const q = msg.read_string ();		// No idea what this is
	#endif
}

void Battle::parse_substitute (InMessage & msg) {
	std::cerr << "SUBSTITUTE\n";
	bool const substitute = msg.read_byte ();
}

void Battle::parse_dynamic_info (InMessage & msg) {
	std::array<int8_t, 7> const boosts = parse_boosts(msg);
	enum DynamicFlags {
		Spikes=1,
		SpikesLV2=2,
		SpikesLV3=4,
		StealthRock=8,
		ToxicSpikes=16,
		ToxicSpikesLV2=32
	};
	// I don't think I need any of this
	static_cast<void>(boosts);
	uint8_t const flags = msg.read_byte ();
}

namespace {

std::array<int8_t, 7> parse_boosts(InMessage & msg) {
	std::array<int8_t, 7> boosts;
	for (int8_t & boost : boosts)
		boost = static_cast<int8_t>(msg.read_byte());
	return boosts;
}

}	// unnamed namespace

void Battle::parse_dynamic_stats (InMessage & msg) {
	for (unsigned n = 0; n != 5; ++n)
		int16_t const something = static_cast<int16_t> (msg.read_short());
}

void Battle::parse_spectating (InMessage & msg) {
	bool const joining = msg.read_byte ();
	uint32_t const user_id = msg.read_int ();
}

void Battle::parse_temporary_pokemon_change (InMessage & msg) {
	Todo const t (msg, "TEMPORARY_POKEMON_CHANGE");
	enum {
		TEMP_MOVE = 0,
		DEF_MOVE = 1,
		TEMP_PP = 2,
		TEMP_SPRITE = 3,
		DEFINITE_FORM = 4,
		AESTHETIC_FORM = 5
	};
	uint8_t const code = msg.read_byte ();
	switch (code) {
		case TEMP_MOVE:
		case DEF_MOVE: {
			std::cerr << "MOVE\n";
			int8_t const slot = static_cast<int8_t> (msg.read_byte ());
			break;
		}
		case TEMP_PP: {
			std::cerr << "TEMP_PP\n";
			int8_t const slot = static_cast<int8_t> (msg.read_byte ());
			int8_t const pp = static_cast<int8_t> (msg.read_byte ());
			break;
		}
		case TEMP_SPRITE: {
			std::cerr << "TEMP_SPRITE\n";
			// ???
			break;
		}
		case DEFINITE_FORM: {
			std::cerr << "DEFINITE_FORM\n";
			int8_t const pokemon = static_cast<int8_t> (msg.read_byte ());
			int16_t const form = static_cast<int16_t> (msg.read_short ());
			break;
		}
		case AESTHETIC_FORM: {
			std::cerr << "AESTHETIC_FORM\n";
			int16_t form = static_cast<int16_t> (msg.read_short ());
			break;
		}
		default:
			std::cerr << "Unknown TEMPORARY_POKEMON_CHANGE code: " << code << '\n';
			break;
	}
}

void Battle::parse_clock_start (InMessage & msg) {
	uint16_t const remaining_time = msg.read_short ();
}

void Battle::parse_clock_stop (InMessage & msg) {
	Todo const t (msg, "CLOCK_STOP");
	uint16_t const remaining_time = msg.read_short ();
}

void Battle::handle_rated (Client & client, InMessage & msg) {
	client.write_team();
	bool const rated = msg.read_byte ();
}

void Battle::parse_tier_section (InMessage & msg) {
	std::string const tier = msg.read_string ();
}

void Battle::parse_battle_chat (Client const & client, InMessage & msg) {
	Todo const t (msg);
	std::string const message = msg.read_string();
	client.print_with_time_stamp (std::cout, message);
}

void Battle::parse_spectator_chat (Client const & client, InMessage & msg, uint32_t const battle_id) {
	std::cerr << "SPECTATOR_CHAT\n";
	uint32_t const user_id = msg.read_int ();
	std::string const message = msg.read_string ();
	client.handle_channel_message (battle_id, client.get_user_name (user_id), message);
}

void Battle::parse_point_estimate (InMessage & msg) {
	Todo const t (msg, "POINT_ESTIMATE");
}

void Battle::parse_offer_choice (Client & client, InMessage & msg, uint32_t const battle_id) {
	int8_t const num_slot = static_cast<int8_t> (msg.read_byte());
	bool const can_switch = msg.read_byte();
	bool const can_attack = msg.read_byte();
	std::vector<uint8_t> attacks_allowed(moves_per_pokemon());
	std::generate(std::begin(attacks_allowed), std::end(attacks_allowed), [&]() { return msg.read_byte(); });
	handle_request_action (client, action, battle_id, can_switch, attacks_allowed);
}

void Battle::handle_make_your_choice (Client & client) {
	client.send_message(action);
	action.reset_action_code();
}

void Battle::handle_cancel_move () {
	std::cerr << "CANCEL_MOVE\n";
	// Humans make foolish mistakes. Technical Machine does not.
}

void Battle::parse_rearrange_team (InMessage & msg) {
	Todo const t (msg, "REARRANGE_TEAM");
	for (unsigned n = 0; n != pokemon_per_team(); ++n) {
		uint16_t const species_id = msg.read_short ();
		uint8_t const form_id = msg.read_byte ();
		uint8_t const level = msg.read_byte ();
		uint8_t const gender = msg.read_byte ();
		bool const item = msg.read_byte ();
	}
}

void Battle::parse_spot_shifts (InMessage & msg) {
	std::cerr << "SPOT_SHIFTS\n";
	// Spinda is quite the Pokemon!
	int8_t const s1 = static_cast<int8_t> (msg.read_byte ());
	int8_t const s2 = static_cast<int8_t> (msg.read_byte ());
	bool const silent = msg.read_byte ();
}

void Battle::parse_battle_end (InMessage & msg) {
	// Forfeit, Win, Tie, Close seems to be the four options in order
	uint8_t const result = msg.read_byte ();
}

uint16_t Battle::max_damage_precision () const {
	return 100;
}

}	// namespace po
}	// namespace technicalmachine