Debian Codegraph @

engine/network/commands.cpp

raw
1 // ____ _ __
2 // / __ )____ _____ | | / /___ ___________
3 // / __ / __ \/ ___/ | | /| / / __ `/ ___/ ___/
4 // / /_/ / /_/ (__ ) | |/ |/ / /_/ / / (__ )
5 // /_____/\____/____/ |__/|__/\__,_/_/ /____/
6 //
7 // A futuristic real-time strategy game.
8 // This file is part of Bos Wars.
9 //
10 /**@name commands.cpp - Global command handler - network support. */
11 //
12 // (c) Copyright 2000-2007 by Lutz Sammer, Andreas Arens, and Jimmy Salmon.
13 //
14 // This program is free software; you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation; only version 2 of the License.
17 //
18 // This program is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with this program; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
26 // 02111-1307, USA.
27 //
28
29 //@{
30
31 /*----------------------------------------------------------------------------
32 -- Includes
33 ----------------------------------------------------------------------------*/
34
35 #include "stratagus.h"
36 #include "commands.h"
37 #include "unit.h"
38 #include "unit_manager.h"
39 #include "unittype.h"
40 #include "actions.h"
41 #include "network.h"
42 #include "spells.h"
43 #include "replay.h"
44
45 /*----------------------------------------------------------------------------
46 -- Functions
47 ----------------------------------------------------------------------------*/
48
49 /**
50 ** Send command: Unit stop.
51 **
52 ** @param unit pointer to unit.
53 */
54 void SendCommandStopUnit(CUnit *unit)
55 {
56 if (!IsNetworkGame()) {
57 CommandLog("stop", unit, FlushCommands, -1, -1, NoUnitP, NULL, -1);
58 CommandStopUnit(unit);
59 } else {
60 NetworkSendCommand(MessageCommandStop, unit, 0, 0, NoUnitP, 0, FlushCommands);
61 }
62 }
63
64 /**
65 ** Send command: Unit stand ground.
66 **
67 ** @param unit pointer to unit.
68 ** @param flush Flag flush all pending commands.
69 */
70 void SendCommandStandGround(CUnit *unit, int flush)
71 {
72 if (!IsNetworkGame()) {
73 CommandLog("stand-ground", unit, flush, -1, -1, NoUnitP, NULL, -1);
74 CommandStandGround(unit, flush);
75 } else {
76 NetworkSendCommand(MessageCommandStand, unit, 0, 0, NoUnitP, 0, flush);
77 }
78 }
79
80 /**
81 ** Send command: Follow unit to position.
82 **
83 ** @param unit pointer to unit.
84 ** @param dest follow this unit.
85 ** @param flush Flag flush all pending commands.
86 */
87 void SendCommandFollow(CUnit *unit, CUnit *dest, int flush)
88 {
89 if (!IsNetworkGame()) {
90 CommandLog("follow", unit, flush, -1, -1, dest, NULL, -1);
91 CommandFollow(unit, dest, flush);
92 } else {
93 NetworkSendCommand(MessageCommandFollow, unit, 0, 0, dest, 0, flush);
94 }
95 }
96
97 /**
98 ** Send command: Move unit to position.
99 **
100 ** @param unit pointer to unit.
101 ** @param x X map tile position to move to.
102 ** @param y Y map tile position to move to.
103 ** @param flush Flag flush all pending commands.
104 */
105 void SendCommandMove(CUnit *unit, int x, int y, int flush)
106 {
107 if (!IsNetworkGame()) {
108 CommandLog("move", unit, flush, x, y, NoUnitP, NULL, -1);
109 CommandMove(unit, x, y, flush);
110 } else {
111 NetworkSendCommand(MessageCommandMove, unit, x, y, NoUnitP, 0, flush);
112 }
113 }
114
115 /**
116 ** Send command: Unit repair.
117 **
118 ** @param unit Pointer to unit.
119 ** @param x X map tile position to repair.
120 ** @param y Y map tile position to repair.
121 ** @param dest Unit to be repaired.
122 ** @param flush Flag flush all pending commands.
123 */
124 void SendCommandRepair(CUnit *unit, int x, int y, CUnit *dest, int flush)
125 {
126 if (!IsNetworkGame()) {
127 CommandLog("repair", unit, flush, x, y, dest, NULL, -1);
128 CommandRepair(unit, x, y, dest, flush);
129 } else {
130 NetworkSendCommand(MessageCommandRepair, unit, x, y, dest, 0, flush);
131 }
132 }
133
134 /**
135 ** Send command: Unit auto repair.
136 **
137 ** @param unit pointer to unit.
138 ** @param on 1 for auto repair on, 0 for off.
139 */
140 void SendCommandAutoRepair(CUnit *unit, int on)
141 {
142 if (!IsNetworkGame()) {
143 CommandLog("auto-repair", unit, FlushCommands, on, -1, NoUnitP,
144 NULL, 0);
145 CommandAutoRepair(unit, on);
146 } else {
147 NetworkSendCommand(MessageCommandAutoRepair,
148 unit, on, -1, NoUnitP, NULL, FlushCommands);
149 }
150 }
151
152 /**
153 ** Send command: Unit attack unit or at position.
154 **
155 ** @param unit pointer to unit.
156 ** @param x X map tile position to attack.
157 ** @param y Y map tile position to attack.
158 ** @param attack or !=NoUnitP unit to be attacked.
159 ** @param flush Flag flush all pending commands.
160 */
161 void SendCommandAttack(CUnit *unit, int x, int y, CUnit *attack, int flush)
162 {
163 if (!IsNetworkGame()) {
164 CommandLog("attack", unit, flush, x, y, attack, NULL, -1);
165 CommandAttack(unit, x, y, attack, flush);
166 } else {
167 NetworkSendCommand(MessageCommandAttack, unit, x, y, attack, 0, flush);
168 }
169 }
170
171 /**
172 ** Send command: Unit attack ground.
173 **
174 ** @param unit pointer to unit.
175 ** @param x X map tile position to fire on.
176 ** @param y Y map tile position to fire on.
177 ** @param flush Flag flush all pending commands.
178 */
179 void SendCommandAttackGround(CUnit *unit, int x, int y, int flush)
180 {
181 if (!IsNetworkGame()) {
182 CommandLog("attack-ground", unit, flush, x, y, NoUnitP, NULL, -1);
183 CommandAttackGround(unit, x, y, flush);
184 } else {
185 NetworkSendCommand(MessageCommandGround, unit, x, y, NoUnitP, 0, flush);
186 }
187 }
188
189 /**
190 ** Send command: Unit patrol between current and position.
191 **
192 ** @param unit pointer to unit.
193 ** @param x X map tile position to patrol between.
194 ** @param y Y map tile position to patrol between.
195 ** @param flush Flag flush all pending commands.
196 */
197 void SendCommandPatrol(CUnit *unit, int x, int y, int flush)
198 {
199 if (!IsNetworkGame()) {
200 CommandLog("patrol", unit, flush, x, y, NoUnitP, NULL, -1);
201 CommandPatrolUnit(unit, x, y, flush);
202 } else {
203 NetworkSendCommand(MessageCommandPatrol, unit, x, y, NoUnitP, 0, flush);
204 }
205 }
206
207 /**
208 ** Send command: Unit board unit.
209 **
210 ** @param unit pointer to unit.
211 ** @param x X map tile position (unused).
212 ** @param y Y map tile position (unused).
213 ** @param dest Destination to be boarded.
214 ** @param flush Flag flush all pending commands.
215 */
216 void SendCommandBoard(CUnit *unit, int x, int y, CUnit *dest, int flush)
217 {
218 if (!IsNetworkGame()) {
219 CommandLog("board", unit, flush, x, y, dest, NULL, -1);
220 CommandBoard(unit, dest, flush);
221 } else {
222 NetworkSendCommand(MessageCommandBoard, unit, x, y, dest, 0, flush);
223 }
224 }
225
226 /**
227 ** Send command: Unit unload unit.
228 **
229 ** @param unit pointer to unit.
230 ** @param x X map tile position of unload.
231 ** @param y Y map tile position of unload.
232 ** @param what Passagier to be unloaded.
233 ** @param flush Flag flush all pending commands.
234 */
235 void SendCommandUnload(CUnit *unit, int x, int y, CUnit *what, int flush)
236 {
237 if (!IsNetworkGame()) {
238 CommandLog("unload", unit, flush, x, y, what, NULL, -1);
239 CommandUnload(unit, x, y, what, flush);
240 } else {
241 NetworkSendCommand(MessageCommandUnload, unit, x, y, what, 0, flush);
242 }
243 }
244
245 /**
246 ** Send command: Unit builds building at position.
247 **
248 ** @param unit pointer to unit.
249 ** @param x X map tile position of construction.
250 ** @param y Y map tile position of construction.
251 ** @param what pointer to unit-type of the building.
252 ** @param flush Flag flush all pending commands.
253 */
254 void SendCommandBuildBuilding(CUnit *unit, int x, int y,
255 CUnitType *what, int flush)
256 {
257 if (!IsNetworkGame()) {
258 CommandLog("build", unit, flush, x, y, NoUnitP, what->Ident.c_str(), -1);
259 CommandBuildBuilding(unit, x, y, what, flush);
260 } else {
261 NetworkSendCommand(MessageCommandBuild, unit, x, y, NoUnitP, what, flush);
262 }
263 }
264
265 /**
266 ** Send command: Cancel this building construction.
267 **
268 ** @param unit pointer to unit.
269 */
270 void SendCommandDismiss(CUnit *unit)
271 {
272 // FIXME: currently unit and worker are same?
273 if (!IsNetworkGame()) {
274 CommandLog("dismiss", unit, FlushCommands, -1, -1, NULL, NULL, -1);
275 CommandDismiss(unit);
276 } else {
277 NetworkSendCommand(MessageCommandDismiss, unit, 0, 0, NULL, 0,
278 FlushCommands);
279 }
280 }
281
282 /**
283 ** Send command: Unit harvest resources
284 **
285 ** @param unit pointer to unit.
286 ** @param dest pointer to destination (oil-platform,gold mine).
287 ** @param flush Flag flush all pending commands.
288 */
289 void SendCommandResource(CUnit *unit, CUnit *dest, int flush)
290 {
291 if (!IsNetworkGame()) {
292 CommandLog("resource", unit, flush, -1, -1, dest, NULL, -1);
293 CommandResource(unit, dest, flush);
294 } else {
295 NetworkSendCommand(MessageCommandResource, unit, 0, 0, dest, 0, flush);
296 }
297 }
298
299 /**
300 ** Send command: Building/unit train new unit.
301 **
302 ** @param unit pointer to unit.
303 ** @param what pointer to unit-type of the unit to be trained.
304 ** @param flush Flag flush all pending commands.
305 */
306 void SendCommandTrainUnit(CUnit *unit, CUnitType *what, int flush)
307 {
308 if (!IsNetworkGame()) {
309 CommandLog("train", unit, flush, -1, -1, NoUnitP, what->Ident.c_str(), -1);
310 CommandTrainUnit(unit, what, flush);
311 } else {
312 NetworkSendCommand(MessageCommandTrain, unit, 0, 0, NoUnitP, what, flush);
313 }
314 }
315
316 /**
317 ** Send command: Cancel training.
318 **
319 ** @param unit Pointer to unit.
320 ** @param slot Slot of training queue to cancel.
321 ** @param type Unit-type of unit to cancel.
322 */
323 void SendCommandCancelTraining(CUnit *unit, int slot, const CUnitType *type)
324 {
325 if (!IsNetworkGame()) {
326 CommandLog("cancel-train", unit, FlushCommands, -1, -1, NoUnitP,
327 type ? type->Ident.c_str() : NULL, slot);
328 CommandCancelTraining(unit, slot, type);
329 } else {
330 NetworkSendCommand(MessageCommandCancelTrain, unit, slot, 0, NoUnitP,
331 type, FlushCommands);
332 }
333 }
334
335 /**
336 ** Send command: Unit spell cast on position/unit.
337 **
338 ** @param unit pointer to unit.
339 ** @param x X map tile position where to cast spell.
340 ** @param y Y map tile position where to cast spell.
341 ** @param dest Cast spell on unit (if exist).
342 ** @param spellid Spell type id.
343 ** @param flush Flag flush all pending commands.
344 */
345 void SendCommandSpellCast(CUnit *unit, int x, int y, CUnit *dest, int spellid,
346 int flush)
347 {
348 if (!IsNetworkGame()) {
349 CommandLog("spell-cast", unit, flush, x, y, dest, NULL, spellid);
350 CommandSpellCast(unit, x, y, dest, SpellTypeTable[spellid], flush);
351 } else {
352 NetworkSendCommand(MessageCommandSpellCast + spellid,
353 unit, x, y, dest, NULL, flush);
354 }
355 }
356
357 /**
358 ** Send command: Unit auto spell cast.
359 **
360 ** @param unit pointer to unit.
361 ** @param spellid Spell type id.
362 ** @param on 1 for auto cast on, 0 for off.
363 */
364 void SendCommandAutoSpellCast(CUnit *unit, int spellid, int on)
365 {
366 if (!IsNetworkGame()) {
367 CommandLog("auto-spell-cast", unit, FlushCommands, on, -1, NoUnitP,
368 NULL, spellid);
369 CommandAutoSpellCast(unit, spellid, on);
370 } else {
371 NetworkSendCommand(MessageCommandSpellCast + spellid,
372 unit, on, -1, NoUnitP, NULL, FlushCommands);
373 }
374 }
375
376 /**
377 ** Send command: Diplomacy changed.
378 **
379 ** @param player Player which changes his state.
380 ** @param state New diplomacy state.
381 ** @param opponent Opponent.
382 */
383 void SendCommandDiplomacy(int player, int state, int opponent)
384 {
385 if (!IsNetworkGame()) {
386 switch (state) {
387 case DiplomacyNeutral:
388 CommandLog("diplomacy", NoUnitP, 0, player, opponent,
389 NoUnitP, "neutral", -1);
390 break;
391 case DiplomacyAllied:
392 CommandLog("diplomacy", NoUnitP, 0, player, opponent,
393 NoUnitP, "allied", -1);
394 break;
395 case DiplomacyEnemy:
396 CommandLog("diplomacy", NoUnitP, 0, player, opponent,
397 NoUnitP, "enemy", -1);
398 break;
399 case DiplomacyCrazy:
400 CommandLog("diplomacy", NoUnitP, 0, player, opponent,
401 NoUnitP, "crazy", -1);
402 break;
403 }
404 CommandDiplomacy(player, state, opponent);
405 } else {
406 NetworkSendExtendedCommand(ExtendedMessageDiplomacy,
407 -1, player, state, opponent, 0);
408 }
409 }
410
411 /**
412 ** Send command: Shared vision changed.
413 **
414 ** @param player Player which changes his state.
415 ** @param state New shared vision state.
416 ** @param opponent Opponent.
417 */
418 void SendCommandSharedVision(int player, bool state, int opponent)
419 {
420 if (!IsNetworkGame()) {
421 if (state == false) {
422 CommandLog("shared-vision", NoUnitP, 0, player, opponent,
423 NoUnitP, "0", -1);
424 } else {
425 CommandLog("shared-vision", NoUnitP, 0, player, opponent,
426 NoUnitP, "1", -1);
427 }
428 CommandSharedVision(player, state, opponent);
429 } else {
430 NetworkSendExtendedCommand(ExtendedMessageSharedVision,
431 -1, player, state, opponent, 0);
432 }
433 }
434
435 //@}
436
437 //----------------------------------------------------------------------------
438 // Parse the message, from the network.
439 //----------------------------------------------------------------------------
440
441 /**@name parse */
442 //@{
443
444 /**
445 ** Parse a command (from network).
446 **
447 ** @param msgnr Network message type
448 ** @param unum Unit number (slot) that receive the command.
449 ** @param x optional X map position.
450 ** @param y optional y map position.
451 ** @param dstnr optional destination unit.
452 */
453 void ParseCommand(unsigned char msgnr, UnitRef unum,
454 unsigned short x, unsigned short y, UnitRef dstnr)
455 {
456 CUnit *unit;
457 CUnit *dest;
458 int id;
459 int status;
460
461 Assert(unum < UnitSlotFree);
462 unit = UnitSlots[unum];
463 Assert(unit);
464
465 //
466 // Check if unit is already killed?
467 //
468 if (unit->Destroyed) {
469 DebugPrint(" destroyed unit skipping %d\n" _C_ UnitNumber(unit));
470 return;
471 }
472
473 Assert(unit->Type);
474
475 status = (msgnr & 0x80) >> 7;
476
477 // Note: destroyed destination unit is handled by the action routines.
478
479 switch (msgnr & 0x7F) {
480 case MessageSync:
481 return;
482 case MessageQuit:
483 return;
484 case MessageChat:
485 return;
486
487 case MessageCommandStop:
488 CommandLog("stop", unit, FlushCommands, -1, -1, NoUnitP, NULL, -1);
489 CommandStopUnit(unit);
490 break;
491 case MessageCommandStand:
492 CommandLog("stand-ground", unit, status, -1, -1, NoUnitP, NULL, -1);
493 CommandStandGround(unit, status);
494 break;
495 case MessageCommandFollow:
496 dest = NoUnitP;
497 if (dstnr != (unsigned short)0xFFFF) {
498 dest = UnitSlots[dstnr];
499 Assert(dest && dest->Type);
500 }
501 CommandLog("follow", unit, status, -1, -1, dest, NULL, -1);
502 CommandFollow(unit, dest, status);
503 break;
504 case MessageCommandMove:
505 CommandLog("move", unit, status, x, y, NoUnitP, NULL, -1);
506 CommandMove(unit, x, y, status);
507 break;
508 case MessageCommandRepair:
509 dest = NoUnitP;
510 if (dstnr != (unsigned short)0xFFFF) {
511 dest = UnitSlots[dstnr];
512 Assert(dest && dest->Type);
513 }
514 CommandLog("repair", unit, status, x, y, dest, NULL, -1);
515 CommandRepair(unit, x, y, dest, status);
516 break;
517 case MessageCommandAutoRepair:
518 CommandLog("auto-repair", unit, status, x, y, NoUnitP, NULL, 0);
519 CommandAutoRepair(unit, x);
520 break;
521 case MessageCommandAttack:
522 dest = NoUnitP;
523 if (dstnr != (unsigned short)0xFFFF) {
524 dest = UnitSlots[dstnr];
525 Assert(dest && dest->Type);
526 }
527 CommandLog("attack", unit, status, x, y, dest, NULL, -1);
528 CommandAttack(unit, x, y, dest, status);
529 break;
530 case MessageCommandGround:
531 CommandLog("attack-ground", unit, status, x, y, NoUnitP, NULL, -1);
532 CommandAttackGround(unit, x, y, status);
533 break;
534 case MessageCommandPatrol:
535 CommandLog("patrol", unit, status, x, y, NoUnitP, NULL, -1);
536 CommandPatrolUnit(unit, x, y, status);
537 break;
538 case MessageCommandBoard:
539 dest = NoUnitP;
540 if (dstnr != (unsigned short)0xFFFF) {
541 dest = UnitSlots[dstnr];
542 Assert(dest && dest->Type);
543 }
544 CommandLog("board", unit, status, x, y, dest, NULL, -1);
545 CommandBoard(unit, dest, status);
546 break;
547 case MessageCommandUnload:
548 dest = NoUnitP;
549 if (dstnr != (unsigned short)0xFFFF) {
550 dest = UnitSlots[dstnr];
551 Assert(dest && dest->Type);
552 }
553 CommandLog("unload", unit, status, x, y, dest, NULL, -1);
554 CommandUnload(unit, x, y, dest, status);
555 break;
556 case MessageCommandBuild:
557 CommandLog("build", unit, status, x, y, NoUnitP, UnitTypes[dstnr]->Ident.c_str(),
558 -1);
559 CommandBuildBuilding(unit, x, y, UnitTypes[dstnr], status);
560 break;
561 case MessageCommandDismiss:
562 CommandLog("dismiss", unit, FlushCommands, -1, -1, NULL, NULL, -1);
563 CommandDismiss(unit);
564 break;
565 case MessageCommandResource:
566 dest = NoUnitP;
567 if (dstnr != (unsigned short)0xFFFF) {
568 dest = UnitSlots[dstnr];
569 Assert(dest && dest->Type);
570 }
571 CommandLog("resource", unit, status, -1, -1, dest, NULL, -1);
572 CommandResource(unit, dest, status);
573 break;
574 case MessageCommandTrain:
575 CommandLog("train", unit, status, -1, -1, NoUnitP,
576 UnitTypes[dstnr]->Ident.c_str(), -1);
577 CommandTrainUnit(unit, UnitTypes[dstnr], status);
578 break;
579 case MessageCommandCancelTrain:
580 // We need (short)x for the last slot -1
581 if (dstnr != (unsigned short)0xFFFF) {
582 CommandLog("cancel-train", unit, FlushCommands, -1, -1, NoUnitP,
583 UnitTypes[dstnr]->Ident.c_str(), (short)x);
584 CommandCancelTraining(unit, (short)x, UnitTypes[dstnr]);
585 } else {
586 CommandLog("cancel-train", unit, FlushCommands, -1, -1, NoUnitP,
587 NULL, (short)x);
588 CommandCancelTraining(unit, (short)x, NULL);
589 }
590 break;
591 default:
592 id = (msgnr&0x7f) - MessageCommandSpellCast;
593 if (y != (unsigned short)0xFFFF) {
594 dest = NoUnitP;
595 if (dstnr != (unsigned short)0xFFFF) {
596 dest = UnitSlots[dstnr];
597 Assert(dest && dest->Type);
598 }
599 CommandLog("spell-cast", unit, status, x, y, dest, NULL, id);
600 CommandSpellCast(unit, x, y, dest, SpellTypeTable[id], status);
601 } else {
602 CommandLog("auto-spell-cast", unit, status, x, -1, NoUnitP, NULL, id);
603 CommandAutoSpellCast(unit, id, x);
604 }
605 break;
606 }
607 }
608
609 /**
610 ** Parse an extended command (from network).
611 **
612 ** @param type Network extended message type
613 ** @param status Bit 7 of message type
614 ** @param arg1 Messe argument 1
615 ** @param arg2 Messe argument 2
616 ** @param arg3 Messe argument 3
617 ** @param arg4 Messe argument 4
618 */
619 void ParseExtendedCommand(unsigned char type, int status,
620 unsigned char arg1, unsigned short arg2, unsigned short arg3,
621 unsigned short arg4)
622 {
623 // Note: destroyed units are handled by the action routines.
624
625 switch (type) {
626 case ExtendedMessageDiplomacy:
627 switch (arg3) {
628 case DiplomacyNeutral:
629 CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
630 NoUnitP, "neutral", -1);
631 break;
632 case DiplomacyAllied:
633 CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
634 NoUnitP, "allied", -1);
635 break;
636 case DiplomacyEnemy:
637 CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
638 NoUnitP, "enemy", -1);
639 break;
640 case DiplomacyCrazy:
641 CommandLog("diplomacy", NoUnitP, 0, arg2, arg4,
642 NoUnitP, "crazy", -1);
643 break;
644 }
645 CommandDiplomacy(arg2, arg3, arg4);
646 break;
647 case ExtendedMessageSharedVision:
648 if (arg3 == 0) {
649 CommandLog("shared-vision", NoUnitP, 0, arg2, arg4,
650 NoUnitP, "0", -1);
651 } else {
652 CommandLog("shared-vision", NoUnitP, 0, arg2, arg4,
653 NoUnitP, "1", -1);
654 }
655 CommandSharedVision(arg2, arg3 ? true : false, arg4);
656 break;
657 default:
658 DebugPrint("Unknown extended message %u/%s %u %u %u %u\n" _C_
659 type _C_ status ? "flush" : "-" _C_
660 arg1 _C_ arg2 _C_ arg3 _C_ arg4);
661 break;
662 }
663 }
664
665 //@}