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 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
// ____ _ __
// / __ )____ _____ | | / /___ ___________
// / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
// / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
// /_____/\____/____/ |__/|__/\__,_/_/ /____/
//
// A futuristic real-time strategy game.
// This file is part of Bos Wars.
//
/**@name commands.cpp - Global command handler - network support. */
//
// (c) Copyright 2000-2007 by Lutz Sammer, Andreas Arens, and Jimmy Salmon.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; only version 2 of the License.
//
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include "stratagus.h"
#include "commands.h"
#include "unit.h"
#include "unit_manager.h"
#include "unittype.h"
#include "actions.h"
#include "network.h"
#include "spells.h"
#include "replay.h"
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Send command: Unit stop.
**
** @param unit pointer to unit.
*/
void SendCommandStopUnit(CUnit *unit)
{
if (!IsNetworkGame()) {
CommandLog("stop", unit, FlushCommands, -1, -1, NoUnitP, NULL, -1);
CommandStopUnit(unit);
} else {
NetworkSendCommand(MessageCommandStop, unit, 0, 0, NoUnitP, 0, FlushCommands);
}
}
/**
** Send command: Unit stand ground.
**
** @param unit pointer to unit.
** @param flush Flag flush all pending commands.
*/
void SendCommandStandGround(CUnit *unit, int flush)
{
if (!IsNetworkGame()) {
CommandLog("stand-ground", unit, flush, -1, -1, NoUnitP, NULL, -1);
CommandStandGround(unit, flush);
} else {
NetworkSendCommand(MessageCommandStand, unit, 0, 0, NoUnitP, 0, flush);
}
}
/**
** Send command: Follow unit to position.
**
** @param unit pointer to unit.
** @param dest follow this unit.
** @param flush Flag flush all pending commands.
*/
void SendCommandFollow(CUnit *unit, CUnit *dest, int flush)
{
if (!IsNetworkGame()) {
CommandLog("follow", unit, flush, -1, -1, dest, NULL, -1);
CommandFollow(unit, dest, flush);
} else {
NetworkSendCommand(MessageCommandFollow, unit, 0, 0, dest, 0, flush);
}
}
/**
** Send command: Move unit to position.
**
** @param unit pointer to unit.
** @param x X map tile position to move to.
** @param y Y map tile position to move to.
** @param flush Flag flush all pending commands.
*/
void SendCommandMove(CUnit *unit, int x, int y, int flush)
{
if (!IsNetworkGame()) {
CommandLog("move", unit, flush, x, y, NoUnitP, NULL, -1);
CommandMove(unit, x, y, flush);
} else {
NetworkSendCommand(MessageCommandMove, unit, x, y, NoUnitP, 0, flush);
}
}
/**
** Send command: Unit repair.
**
** @param unit Pointer to unit.
** @param x X map tile position to repair.
** @param y Y map tile position to repair.
** @param dest Unit to be repaired.
** @param flush Flag flush all pending commands.
*/
void SendCommandRepair(CUnit *unit, int x, int y, CUnit *dest, int flush)
{
if (!IsNetworkGame()) {
CommandLog("repair", unit, flush, x, y, dest, NULL, -1);
CommandRepair(unit, x, y, dest, flush);
} else {
NetworkSendCommand(MessageCommandRepair, unit, x, y, dest, 0, flush);
}
}
/**
** Send command: Unit auto repair.
**
** @param unit pointer to unit.
** @param on 1 for auto repair on, 0 for off.
*/
void SendCommandAutoRepair(CUnit *unit, int on)
{
if (!IsNetworkGame()) {
CommandLog("auto-repair", unit, FlushCommands, on, -1, NoUnitP,
NULL, 0);
CommandAutoRepair(unit, on);
} else {
NetworkSendCommand(MessageCommandAutoRepair,
unit, on, -1, NoUnitP, NULL, FlushCommands);
}
}
/**
** Send command: Unit attack unit or at position.
**
** @param unit pointer to unit.
** @param x X map tile position to attack.
** @param y Y map tile position to attack.
** @param attack or !=NoUnitP unit to be attacked.
** @param flush Flag flush all pending commands.
*/
void SendCommandAttack(CUnit *unit, int x, int y, CUnit *attack, int flush)
{
if (!IsNetworkGame()) {
CommandLog("attack", unit, flush, x, y, attack, NULL, -1);
CommandAttack(unit, x, y, attack, flush);
} else {
NetworkSendCommand(MessageCommandAttack, unit, x, y, attack, 0, flush);
}
}
/**
** Send command: Unit attack ground.
**
** @param unit pointer to unit.
** @param x X map tile position to fire on.
** @param y Y map tile position to fire on.
** @param flush Flag flush all pending commands.
*/
void SendCommandAttackGround(CUnit *unit, int x, int y, int flush)
{
if (!IsNetworkGame()) {
CommandLog("attack-ground", unit, flush, x, y, NoUnitP, NULL, -1);
CommandAttackGround(unit, x, y, flush);
} else {
NetworkSendCommand(MessageCommandGround, unit, x, y, NoUnitP, 0, flush);
}
}
/**
** Send command: Unit patrol between current and position.
**
** @param unit pointer to unit.
** @param x X map tile position to patrol between.
** @param y Y map tile position to patrol between.
** @param flush Flag flush all pending commands.
*/
void SendCommandPatrol(CUnit *unit, int x, int y, int flush)
{
if (!IsNetworkGame()) {
CommandLog("patrol", unit, flush, x, y, NoUnitP, NULL, -1);
CommandPatrolUnit(unit, x, y, flush);
} else {
NetworkSendCommand(MessageCommandPatrol, unit, x, y, NoUnitP, 0, flush);
}
}
/**
** Send command: Unit board unit.
**
** @param unit pointer to unit.
** @param x X map tile position (unused).
** @param y Y map tile position (unused).
** @param dest Destination to be boarded.
** @param flush Flag flush all pending commands.
*/
void SendCommandBoard(CUnit *unit, int x, int y, CUnit *dest, int flush)
{
if (!IsNetworkGame()) {
CommandLog("board", unit, flush, x, y, dest, NULL, -1);
CommandBoard(unit, dest, flush);
} else {
NetworkSendCommand(MessageCommandBoard, unit, x, y, dest, 0, flush);
}
}
/**
** Send command: Unit unload unit.
**
** @param unit pointer to unit.
** @param x X map tile position of unload.
** @param y Y map tile position of unload.
** @param what Passagier to be unloaded.
** @param flush Flag flush all pending commands.
*/
void SendCommandUnload(CUnit *unit, int x, int y, CUnit *what, int flush)
{
if (!IsNetworkGame()) {
CommandLog("unload", unit, flush, x, y, what, NULL, -1);
CommandUnload(unit, x, y, what, flush);
} else {
NetworkSendCommand(MessageCommandUnload, unit, x, y, what, 0, flush);
}
}
/**
** Send command: Unit builds building at position.
**
** @param unit pointer to unit.
** @param x X map tile position of construction.
** @param y Y map tile position of construction.
** @param what pointer to unit-type of the building.
** @param flush Flag flush all pending commands.
*/
void SendCommandBuildBuilding(CUnit *unit, int x, int y,
CUnitType *what, int flush)
{
if (!IsNetworkGame()) {
CommandLog("build", unit, flush, x, y, NoUnitP, what->Ident.c_str(), -1);
CommandBuildBuilding(unit, x, y, what, flush);
} else {
NetworkSendCommand(MessageCommandBuild, unit, x, y, NoUnitP, what, flush);
}
}
/**
** Send command: Cancel this building construction.
**
** @param unit pointer to unit.
*/
void SendCommandDismiss(CUnit *unit)
{
// FIXME: currently unit and worker are same?
if (!IsNetworkGame()) {
CommandLog("dismiss", unit, FlushCommands, -1, -1, NULL, NULL, -1);
CommandDismiss(unit);
} else {
NetworkSendCommand(MessageCommandDismiss, unit, 0, 0, NULL, 0,
FlushCommands);
}
}
/**
** Send command: Unit harvest resources
**
** @param unit pointer to unit.
** @param dest pointer to destination (oil-platform,gold mine).
** @param flush Flag flush all pending commands.
*/
void SendCommandResource(CUnit *unit, CUnit *dest, int flush)
{
if (!IsNetworkGame()) {
CommandLog("resource", unit, flush, -1, -1, dest, NULL, -1);
CommandResource(unit, dest, flush);
} else {
NetworkSendCommand(MessageCommandResource, unit, 0, 0, dest, 0, flush);
}
}
/**
** Send command: Building/unit train new unit.
**
** @param unit pointer to unit.
** @param what pointer to unit-type of the unit to be trained.
** @param flush Flag flush all pending commands.
*/
void SendCommandTrainUnit(CUnit *unit, CUnitType *what, int flush)
{
if (!IsNetworkGame()) {
CommandLog("train", unit, flush, -1, -1, NoUnitP, what->Ident.c_str(), -1);
CommandTrainUnit(unit, what, flush);
} else {
NetworkSendCommand(MessageCommandTrain, unit, 0, 0, NoUnitP, what, flush);
}
}
/**
** Send command: Cancel training.
**
** @param unit Pointer to unit.
** @param slot Slot of training queue to cancel.
** @param type Unit-type of unit to cancel.
*/
void SendCommandCancelTraining(CUnit *unit, int slot, const CUnitType *type)
{
if (!IsNetworkGame()) {
CommandLog("cancel-train", unit, FlushCommands, -1, -1, NoUnitP,
type ? type->Ident.c_str() : NULL, slot);
CommandCancelTraining(unit, slot, type);
} else {
NetworkSendCommand(MessageCommandCancelTrain, unit, slot, 0, NoUnitP,
type, FlushCommands);
}
}
/**
** Send command: Unit spell cast on position/unit.
**
** @param unit pointer to unit.
** @param x X map tile position where to cast spell.
** @param y Y map tile position where to cast spell.
** @param dest Cast spell on unit (if exist).
** @param spellid Spell type id.
** @param flush Flag flush all pending commands.
*/
void SendCommandSpellCast(CUnit *unit, int x, int y, CUnit *dest, int spellid,
int flush)
{
if (!IsNetworkGame()) {
CommandLog("spell-cast", unit, flush, x, y, dest, NULL, spellid);
CommandSpellCast(unit, x, y, dest, SpellTypeTable[spellid], flush);
} else {
NetworkSendCommand(MessageCommandSpellCast + spellid,
unit, x, y, dest, NULL, flush);
}
}
/**
** Send command: Unit auto spell cast.
**
** @param unit pointer to unit.
** @param spellid Spell type id.
** @param on 1 for auto cast on, 0 for off.
*/
void SendCommandAutoSpellCast(CUnit *unit, int spellid, int on)
{
if (!IsNetworkGame()) {
CommandLog("auto-spell-cast", unit, FlushCommands, on, -1, NoUnitP,
NULL, spellid);
CommandAutoSpellCast(unit, spellid, on);
} else {
NetworkSendCommand(MessageCommandSpellCast + spellid,
unit, on, -1, NoUnitP, NULL, FlushCommands);
}
}
/**
** Send command: Diplomacy changed.
**
** @param player Player which changes his state.
** @param state New diplomacy state.
** @param opponent Opponent.
*/
void SendCommandDiplomacy(int player, int state, int opponent)
{
if (!IsNetworkGame()) {
switch (state) {
case DiplomacyNeutral:
CommandLog("diplomacy", NoUnitP, 0, player, opponent,
NoUnitP, "neutral", -1);
break;
case DiplomacyAllied:
CommandLog("diplomacy", NoUnitP, 0, player, opponent,
NoUnitP, "allied", -1);
break;
case DiplomacyEnemy:
CommandLog("diplomacy", NoUnitP, 0, player, opponent,
NoUnitP, "enemy", -1);
break;
case DiplomacyCrazy:
CommandLog("diplomacy", NoUnitP, 0, player, opponent,
NoUnitP, "crazy", -1);
break;
}
CommandDiplomacy(player, state, opponent);
} else {
NetworkSendExtendedCommand(ExtendedMessageDiplomacy,
-1, player, state, opponent, 0);
}
}
/**
** Send command: Shared vision changed.
**
** @param player Player which changes his state.
** @param state New shared vision state.
** @param opponent Opponent.
*/
void SendCommandSharedVision(int player, bool state, int opponent)
{
if (!IsNetworkGame()) {
if (state == false) {
CommandLog("shared-vision", NoUnitP, 0, player, opponent,
NoUnitP, "0", -1);
} else {
CommandLog("shared-vision", NoUnitP, 0, player, opponent,
NoUnitP, "1", -1);
}
CommandSharedVision(player, state, opponent);
} else {
NetworkSendExtendedCommand(ExtendedMessageSharedVision,
-1, player, state, opponent, 0);
}
}
//@}
//----------------------------------------------------------------------------
// Parse the message, from the network.
//----------------------------------------------------------------------------
/**@name parse */
//@{
/**
** Parse a command (from network).
**
** @param msgnr Network message type
** @param unum Unit number (slot) that receive the command.
** @param x optional X map position.
** @param y optional y map position.
** @param dstnr optional destination unit.
*/
void ParseCommand(unsigned char msgnr, UnitRef unum,
unsigned short x, unsigned short y, UnitRef dstnr)
{
CUnit *unit;
CUnit *dest;
int id;
int status;
Assert(unum < UnitSlotFree);
unit = UnitSlots[unum];
Assert(unit);
//
// Check if unit is already killed?
//
if (unit->Destroyed) {
DebugPrint(" destroyed unit skipping %d\n" _C_ UnitNumber(unit));
return;
}
Assert(unit->Type);
status = (msgnr & 0x80) >> 7;
// Note: destroyed destination unit is handled by the action routines.
switch (msgnr & 0x7F) {
case MessageSync:
return;
case MessageQuit:
return;
case MessageChat:
return;
case MessageCommandStop:
CommandLog("stop", unit, FlushCommands, -1, -1, NoUnitP, NULL, -1);
CommandStopUnit(unit);
break;
case MessageCommandStand:
CommandLog("stand-ground", unit, status, -1, -1, NoUnitP, NULL, -1);
CommandStandGround(unit, status);
break;
case MessageCommandFollow:
dest = NoUnitP;
if (dstnr != (unsigned short)0xFFFF) {
dest = UnitSlots[dstnr];
Assert(dest && dest->Type);
}
CommandLog("follow", unit, status, -1, -1, dest, NULL, -1);
CommandFollow(unit, dest, status);
break;
case MessageCommandMove:
CommandLog("move", unit, status, x, y, NoUnitP, NULL, -1);
CommandMove(unit, x, y, status);
break;
case MessageCommandRepair:
dest = NoUnitP;
if (dstnr != (unsigned short)0xFFFF) {
dest = UnitSlots[dstnr];
Assert(dest && dest->Type);
}
CommandLog("repair", unit, status, x, y, dest, NULL, -1);
CommandRepair(unit, x, y, dest, status);
break;
case MessageCommandAutoRepair:
CommandLog("auto-repair", unit, status, x, y, NoUnitP, NULL, 0);
CommandAutoRepair(unit, x);
break;
case MessageCommandAttack:
dest = NoUnitP;
if (dstnr != (unsigned short)0xFFFF) {
dest = UnitSlots[dstnr];
Assert(dest && dest->Type);
}
CommandLog("attack", unit, status, x, y, dest, NULL, -1);
CommandAttack(unit, x, y, dest, status);
break;
case MessageCommandGround:
CommandLog("attack-ground", unit, status, x, y, NoUnitP, NULL, -1);
CommandAttackGround(unit, x, y, status);
break;
case MessageCommandPatrol:
CommandLog("patrol", unit, status, x, y, NoUnitP, NULL, -1);
CommandPatrolUnit(unit, x, y, status);
break;
case MessageCommandBoard:
dest = NoUnitP;
if (dstnr != (unsigned short)0xFFFF) {
dest = UnitSlots[dstnr];
Assert(dest && dest->Type);
}
CommandLog("board", unit, status, x, y, dest, NULL, -1);
CommandBoard(unit, dest, status);
break;
case MessageCommandUnload:
dest = NoUnitP;
if (dstnr != (unsigned short)0xFFFF) {
dest = UnitSlots[dstnr];
Assert(dest && dest->Type);
}
CommandLog("unload", unit, status, x, y, dest, NULL, -1);
CommandUnload(unit, x, y, dest, status);
break;
case MessageCommandBuild:
CommandLog("build", unit, status, x, y, NoUnitP, UnitTypes[dstnr]->Ident.c_str(),
-1);
CommandBuildBuilding(unit, x, y, UnitTypes[dstnr], status);
break;
case MessageCommandDismiss:
CommandLog("dismiss", unit, FlushCommands, -1, -1, NULL, NULL, -1);
CommandDismiss(unit);
break;
case MessageCommandResource:
dest = NoUnitP;
if (dstnr != (unsigned short)0xFFFF) {
dest = UnitSlots[dstnr];
Assert(dest && dest->Type);
}
CommandLog("resource", unit, status, -1, -1, dest, NULL, -1);
CommandResource(unit, dest, status);
break;
case MessageCommandTrain:
CommandLog("train", unit, status, -1, -1, NoUnitP,
UnitTypes[dstnr]->Ident.c_str(), -1);
CommandTrainUnit(unit, UnitTypes[dstnr], status);
break;
case MessageCommandCancelTrain:
// We need (short)x for the last slot -1
if (dstnr != (unsigned short)0xFFFF) {
CommandLog("cancel-train", unit, FlushCommands, -1, -1, NoUnitP,
UnitTypes[dstnr]->Ident.c_str(), (short)x);
CommandCancelTraining(unit, (short)x, UnitTypes[dstnr]);
} else {
CommandLog("cancel-train", unit, FlushCommands, -1, -1, NoUnitP,
NULL, (short)x);
CommandCancelTraining(unit, (short)x, NULL);
}
break;
default:
id = (msgnr&0x7f) - MessageCommandSpellCast;
if (y != (unsigned short)0xFFFF) {
dest = NoUnitP;
if (dstnr != (unsigned short)0xFFFF) {
dest = UnitSlots[dstnr];
Assert(dest && dest->Type);
}
CommandLog("spell-cast", unit, status, x, y, dest, NULL, id);
CommandSpellCast(unit, x, y, dest, SpellTypeTable[id], status);
} else {
CommandLog("auto-spell-cast", unit, status, x, -1, NoUnitP, NULL, id);
CommandAutoSpellCast(unit, id, x);
}
break;
}
}
/**
** Parse an extended command (from network).
**
** @param type Network extended message type
** @param status Bit 7 of message type
** @param arg1 Messe argument 1
** @param arg2 Messe argument 2
** @param arg3 Messe argument 3
** @param arg4 Messe argument 4
*/
void ParseExtendedCommand(unsigned char type, int status,
unsigned char arg1, unsigned short arg2, unsigned short arg3,
unsigned short arg4)
{
// Note: destroyed units are handled by the action routines.
switch (type) {
case ExtendedMessageDiplomacy:
switch (arg3) {
case DiplomacyNeutral:
CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
NoUnitP, "neutral", -1);
break;
case DiplomacyAllied:
CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
NoUnitP, "allied", -1);
break;
case DiplomacyEnemy:
CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
NoUnitP, "enemy", -1);
break;
case DiplomacyCrazy:
CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
NoUnitP, "crazy", -1);
break;
}
CommandDiplomacy(arg2, arg3, arg4);
break;
case ExtendedMessageSharedVision:
if (arg3 == 0) {
CommandLog("shared-vision", NoUnitP, 0, arg2, arg4,
NoUnitP, "0", -1);
} else {
CommandLog("shared-vision", NoUnitP, 0, arg2, arg4,
NoUnitP, "1", -1);
}
CommandSharedVision(arg2, arg3 ? true : false, arg4);
break;
default:
DebugPrint("Unknown extended message %u/%s %u %u %u %u\n" _C_
type _C_ status ? "flush" : "-" _C_
arg1 _C_ arg2 _C_ arg3 _C_ arg4);
break;
}
}
//@}
|