diff --git a/sc2/src/sc2code/battle.c b/sc2/src/sc2code/battle.c index 5d0550cc7..099c1d80e 100644 --- a/sc2/src/sc2code/battle.c +++ b/sc2/src/sc2code/battle.c @@ -38,7 +38,9 @@ QUEUE disp_q; -SIZE battle_counter; +BYTE battle_counter[NUM_SIDES]; + // The number of ships still available for battle to each side. + // A ship that has warped out is no longer available. BOOLEAN instantVictory; size_t battleInputOrder[NUM_SIDES]; // Indices of the sides in the order their input is processed. @@ -79,7 +81,7 @@ DoRunAway (STARSHIP *StarShipPtr) { extern void flee_preprocess (ELEMENT *); - battle_counter -= MAKE_WORD (1, 0); + battle_counter[0]--; ElementPtr->turn_wait = 3; ElementPtr->thrust_wait = MAKE_BYTE (4, 0); @@ -353,7 +355,7 @@ selectAllShips (SIZE num_ships) // On network play, what is the top player to one party may be // the bottom player the other. To keep both sides synchronised // the order is always the same. - SIZE order[2]; + SIZE order[NUM_PLAYERS]; if (num_ships == 1) { // HyperSpace in full game. @@ -393,7 +395,7 @@ selectAllShips (SIZE num_ships) { size_t i; - for (i = 0; i < 2; i++) + for (i = 0; i < NUM_PLAYERS; i++) { if (!GetNextStarShip (NULL, order[i])) return FALSE; @@ -430,8 +432,9 @@ Battle (void) if (instantVictory) { - num_ships = 0; // no ships were harmed in the making of this battle - battle_counter = 1; // a winner is you! + num_ships = 0; + battle_counter[0] = 1; + battle_counter[1] = 0; instantVictory = FALSE; } @@ -440,10 +443,8 @@ Battle (void) BATTLE_STATE bs; GLOBAL (CurrentActivity) |= IN_BATTLE; - battle_counter = MAKE_WORD ( - CountLinks (&race_q[0]), - CountLinks (&race_q[1]) - ); + battle_counter[0] = CountLinks (&race_q[0]); + battle_counter[1] = CountLinks (&race_q[1]); setupBattleInputOrder (); #ifdef NETPLAY diff --git a/sc2/src/sc2code/battle.h b/sc2/src/sc2code/battle.h index 39f33a4fd..f9cd28de4 100644 --- a/sc2/src/sc2code/battle.h +++ b/sc2/src/sc2code/battle.h @@ -18,6 +18,8 @@ #include "libs/compiler.h" #include "displist.h" +#include "init.h" + // For NUM_SIDES typedef struct battlestate_struct { BOOLEAN (*InputFunc) (struct battlestate_struct *pInputState); @@ -27,7 +29,7 @@ typedef struct battlestate_struct { } BATTLE_STATE; extern QUEUE disp_q; -extern SIZE battle_counter; +extern BYTE battle_counter[NUM_SIDES]; extern BOOLEAN instantVictory; #ifdef NETPLAY typedef DWORD BattleFrameCounter; diff --git a/sc2/src/sc2code/element.h b/sc2/src/sc2code/element.h index b1d918c9a..8d176bcce 100644 --- a/sc2/src/sc2code/element.h +++ b/sc2/src/sc2code/element.h @@ -47,6 +47,9 @@ typedef QUEUE_HANDLE HELEMENT; #define GOOD_GUY (1 << 0) #define BAD_GUY (1 << 1) #define PLAYER_SHIP (1 << 2) + // The ELEMENT is a player controlable ship, and not some bullet, + // crew, asteroid, fighter, etc. This does not mean that the ship + // is actually controlled by a human; it may be a computer. #define APPEARING (1 << 3) #define DISAPPEARING (1 << 4) @@ -60,6 +63,7 @@ typedef QUEUE_HANDLE HELEMENT; #define FINITE_LIFE (1 << 10) #define PRE_PROCESS (1 << 11) + // PreProcess() is to be called for the ELEMENT. #define POST_PROCESS (1 << 12) #define IGNORE_VELOCITY (1 << 13) @@ -146,7 +150,13 @@ extern PRIMITIVE DisplayArray[MAX_DISPLAY_PRIMS]; #define GRAVITY_MASS(m) ((m) > MAX_SHIP_MASS * 10) #define GRAVITY_THRESHOLD (COUNT)255 -#define WHICH_SIDE(f) (((f) & BAD_GUY) >> 1) +static inline BYTE +ElementFlagsSide (ELEMENT_FLAGS flags) +{ + return (BYTE) ((flags & BAD_GUY) >> 1); +} +#define WHICH_SIDE(flags) ElementFlagsSide (flags) + #define OBJECT_CLOAKED(eptr) \ (GetPrimType (&GLOBAL (DisplayArray[(eptr)->PrimIndex])) >= NUM_PRIMS \ || (GetPrimType (&GLOBAL (DisplayArray[(eptr)->PrimIndex])) == STAMPFILL_PRIM \ diff --git a/sc2/src/sc2code/encount.c b/sc2/src/sc2code/encount.c index 1e3f33c97..fe62f04a3 100644 --- a/sc2/src/sc2code/encount.c +++ b/sc2/src/sc2code/encount.c @@ -462,7 +462,7 @@ UninitEncounter (void) SET_GAME_STATE (BOMB_CARRIER, 0); VictoryState = ( - HIBYTE (battle_counter) || !LOBYTE (battle_counter) + battle_counter[1] || !battle_counter[0] || GET_GAME_STATE (URQUAN_PROTECTING_SAMATRA) ) ? 0 : 1; diff --git a/sc2/src/sc2code/pickmele.c b/sc2/src/sc2code/pickmele.c index f51fcadfd..e04dbc519 100644 --- a/sc2/src/sc2code/pickmele.c +++ b/sc2/src/sc2code/pickmele.c @@ -389,7 +389,7 @@ GetMeleeStarShip (STARSHIP *LastStarShipPtr, COUNT which_player) DrawStamp (&s); - if (LOBYTE (battle_counter) == 0 || HIBYTE (battle_counter) == 0) + if (battle_counter[0] == 0 || battle_counter[1] == 0) { // One side is out of ships. Game over. DWORD TimeOut; @@ -457,10 +457,7 @@ GetMeleeStarShip (STARSHIP *LastStarShipPtr, COUNT which_player) FlushColorXForms (); } - if (which_player == 0) - ships_left = LOBYTE (battle_counter); - else - ships_left = HIBYTE (battle_counter); + ships_left = battle_counter[which_player]; gmstate.flash_rect.extent.width = (ICON_WIDTH + 2); gmstate.flash_rect.extent.height = (ICON_HEIGHT + 2); diff --git a/sc2/src/sc2code/pickship.c b/sc2/src/sc2code/pickship.c index d9830fa23..100e582cf 100644 --- a/sc2/src/sc2code/pickship.c +++ b/sc2/src/sc2code/pickship.c @@ -114,8 +114,10 @@ DoPickBattleShip (MENU_STATE *pMS) ChangeSelection: if (pMS->first_item.x == (NUM_PICK_SHIP_COLUMNS >> 1)) { - pMS->flash_rect0.corner.x = pMS->flash_rect1.corner.x - 2 + FLAGSHIP_X_OFFS; - pMS->flash_rect0.corner.y = pMS->flash_rect1.corner.y - 2 + FLAGSHIP_Y_OFFS; + pMS->flash_rect0.corner.x = + pMS->flash_rect1.corner.x - 2 + FLAGSHIP_X_OFFS; + pMS->flash_rect0.corner.y = + pMS->flash_rect1.corner.y - 2 + FLAGSHIP_Y_OFFS; pMS->flash_rect0.extent.width = FLAGSHIP_WIDTH + 4; pMS->flash_rect0.extent.height = FLAGSHIP_HEIGHT + 4; @@ -248,9 +250,10 @@ GetArmadaStarShip (void) CONTEXT OldContext; HSTARSHIP hBattleShip; - if (HIBYTE (battle_counter) == 0) + if (battle_counter[1] == 0) { - return (0); + // No opponents left. + return 0; } // MenuSounds = CaptureSound (LoadSound (MENU_SOUNDS)); @@ -285,7 +288,7 @@ OldContext = SetContext (SpaceContext); if (hBattleShip) { if (hBattleShip == GetTailLink (&race_q[0])) - battle_counter = MAKE_WORD (1, HIBYTE (battle_counter)); + battle_counter[0] = 1; WaitForSoundEnd (0); } @@ -312,14 +315,19 @@ GetEncounterStarShip (STARSHIP *LastStarShipPtr, COUNT which_player) } else { + // Full game. HSTARSHIP hStarShip; STARSHIP *SPtr; SHIP_FRAGMENT *FragPtr; if (LastStarShipPtr == 0) { - if (which_player == 0 && LOBYTE (battle_counter) > 1) + // First time picking a ship. + if (which_player == 0 && battle_counter[0] > 1) + { + // The player in a full game, still having a ship left. hBattleShip = GetArmadaStarShip (); + } else { hBattleShip = GetHeadLink (&race_q[which_player]); @@ -331,7 +339,7 @@ GetEncounterStarShip (STARSHIP *LastStarShipPtr, COUNT which_player) if (FragPtr->ShipInfo.crew_level == INFINITE_FLEET) { // Infinite number of ships. - battle_counter += MAKE_WORD (0, 1); + battle_counter[1]++; } UnlockStarShip (&GLOBAL (npc_built_ship_q), hStarShip); } @@ -373,7 +381,7 @@ GetEncounterStarShip (STARSHIP *LastStarShipPtr, COUNT which_player) SPtr->cur_status_flags = 1 << which_player; SPtr->captains_name_index = PickCaptainName (); - battle_counter += MAKE_WORD (0, 1); + battle_counter[1]++; } UnlockStarShip (pQueue, hStarShip); break; @@ -384,19 +392,26 @@ GetEncounterStarShip (STARSHIP *LastStarShipPtr, COUNT which_player) if (which_player == 0) { - if (LOBYTE (battle_counter)) + // Player in a full game. + if (battle_counter[0]) hBattleShip = GetArmadaStarShip (); else /* last ship was flagship */ { #define RUN_AWAY_FUEL_COST (5 * FUEL_TANK_SCALE) hBattleShip = 0; if (LastStarShipPtr->special_counter == 0) - /* died in the line of duty */ + { + /* Died in the line of duty */ GLOBAL_SIS (CrewEnlisted) = (COUNT)~0; - else if (GLOBAL_SIS (FuelOnBoard) > RUN_AWAY_FUEL_COST) - GLOBAL_SIS (FuelOnBoard) -= RUN_AWAY_FUEL_COST; + } else - GLOBAL_SIS (FuelOnBoard) = 0; + { + // Player ran away. + if (GLOBAL_SIS (FuelOnBoard) > RUN_AWAY_FUEL_COST) + GLOBAL_SIS (FuelOnBoard) -= RUN_AWAY_FUEL_COST; + else + GLOBAL_SIS (FuelOnBoard) = 0; + } } } } diff --git a/sc2/src/sc2code/process.c b/sc2/src/sc2code/process.c index 92bb2dd26..d0fb6ea2e 100644 --- a/sc2/src/sc2code/process.c +++ b/sc2/src/sc2code/process.c @@ -619,7 +619,7 @@ static VIEW_STATE PreProcessQueue (SIZE *pscroll_x, SIZE *pscroll_y) { SIZE min_reduction, max_reduction; - COUNT num_ships; + COUNT sides_active; POINT Origin; HELEMENT hElement; COUNT ships_alive; @@ -627,8 +627,8 @@ PreProcessQueue (SIZE *pscroll_x, SIZE *pscroll_y) #ifdef KDEBUG log_add (log_Debug, "PreProcess:"); #endif - num_ships = (LOBYTE (battle_counter) ? 1 : 0) - + (HIBYTE (battle_counter) ? 1 : 0); + sides_active = (battle_counter[0] ? 1 : 0) + + (battle_counter[1] ? 1 : 0); if (optMeleeScale == TFB_SCALE_STEP) min_reduction = max_reduction = MAX_VIS_REDUCTION + 1; @@ -673,7 +673,7 @@ PreProcessQueue (SIZE *pscroll_x, SIZE *pscroll_y) dy = DISPLAY_ALIGN (ElementPtr->next.location.y) - Origin.y; dy = WRAP_DELTA_Y (dy); - if (num_ships <= 2 || !(ElementPtr->state_flags & BAD_GUY)) + if (sides_active <= 2 || !(ElementPtr->state_flags & BAD_GUY)) { Origin.x = DISPLAY_ALIGN (Origin.x + (dx >> 1)); Origin.y = DISPLAY_ALIGN (Origin.y + (dy >> 1)); @@ -1061,6 +1061,8 @@ RedrawQueue (BOOLEAN clear) DisplayLinks = MakeLinks (END_OF_LIST, END_OF_LIST); } +// Set the hTarget field to 0 for all elements in the display list that +// have hTarget set to ElementPtr. void Untarget (ELEMENT *ElementPtr) { diff --git a/sc2/src/sc2code/ships/arilou/arilou.c b/sc2/src/sc2code/ships/arilou/arilou.c index c6956e5d9..26e8ff4b8 100644 --- a/sc2/src/sc2code/ships/arilou/arilou.c +++ b/sc2/src/sc2code/ships/arilou/arilou.c @@ -202,6 +202,7 @@ arilou_preprocess (ELEMENT *ElementPtr) && StarShipPtr->special_counter == 0 && DeltaEnergy (ElementPtr, -SPECIAL_ENERGY_COST)) { + /* Special key is pressed; start teleport */ #define HYPER_LIFE 5 ZeroVelocityComponents (&ElementPtr->velocity); StarShipPtr->cur_status_flags &= @@ -239,6 +240,7 @@ arilou_preprocess (ELEMENT *ElementPtr) if ((life_span = ElementPtr->life_span) == NORMAL_LIFE) { + /* Ending teleport */ ElementPtr->state_flags &= ~(NONSOLID | FINITE_LIFE); ElementPtr->state_flags |= APPEARING; ElementPtr->current.image.farray = @@ -252,6 +254,7 @@ arilou_preprocess (ELEMENT *ElementPtr) } else { + /* Teleporting in progress */ --life_span; if (life_span != 2) { diff --git a/sc2/src/sc2code/tactrans.c b/sc2/src/sc2code/tactrans.c index 4aec39f4a..1675f95e8 100644 --- a/sc2/src/sc2code/tactrans.c +++ b/sc2/src/sc2code/tactrans.c @@ -374,8 +374,7 @@ UnbatchGraphics (); if (RestartMusic) BattleSong (TRUE); } - else if (LOBYTE (battle_counter) == 0 - || HIBYTE (battle_counter) == 0) + else if (battle_counter[0] == 0 || battle_counter[1] == 0) { // One player is out of ships. The battle is over. GLOBAL (CurrentActivity) &= ~IN_BATTLE; @@ -489,10 +488,8 @@ ship_death (ELEMENT *ShipPtr) if (ShipPtr->mass_points <= MAX_SHIP_MASS) { - if (WHICH_SIDE (ShipPtr->state_flags)) - battle_counter -= MAKE_WORD (0, 1); - else - battle_counter -= MAKE_WORD (1, 0); + COUNT side = ElementFlagsSide (ShipPtr->state_flags); + battle_counter[side]--; } VictoriousStarShipPtr = 0;