Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

43.9 KB
Raw Blame
/*
**	Command & Conquer Generals Zero Hour(tm)
**	Copyright 2025 Electronic Arts Inc.
**
**	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, 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 General Public License for more details.
**
**	You should have received a copy of the GNU General Public License
**	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
// Modified 2025-2026 by Olcay Seygan for Zero Hour Reforged; see the git history.

////////////////////////////////////////////////////////////////////////////////
//																																						//
//  (c) 2001-2003 Electronic Arts Inc.																				//
//																																						//
////////////////////////////////////////////////////////////////////////////////

// FILE: ControlBarCommandProcessing.cpp //////////////////////////////////////////////////////////
// Author: Colin Day, March 2002
// Desc:   This file contain just the method responsible for processing the actual command
//				 clicks from the window controls in the UI
///////////////////////////////////////////////////////////////////////////////////////////////////

// USER INCLUDES //////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h"	// This must go first in EVERY cpp file int the GameEngine

#include "Common/BuildAssistant.h"
#include "Common/Money.h"
#include "Common/Player.h"
#include "Common/PlayerList.h"
#include "Common/Science.h"
#include "Common/SpecialPower.h"
#include "Common/ThingTemplate.h"
#include "Common/Upgrade.h"
#include "Common/PlayerTemplate.h"

#include "GameClient/CommandXlat.h"
#include "GameClient/ControlBar.h"
#include "GameClient/Drawable.h"
#include "GameClient/Eva.h"
#include "GameClient/GameClient.h"
#include "GameClient/GadgetPushButton.h"
#include "GameClient/GameWindow.h"
#include "GameClient/GameWindowManager.h"
#include "GameClient/InGameUI.h"
#include "GameClient/Keyboard.h"
#include "GameClient/AnimateWindowManager.h"

#include "GameLogic/AI.h"
#include "GameLogic/GameLogic.h"
#include "GameLogic/Object.h"
#include "GameLogic/Module/ProductionUpdate.h"


#ifdef _INTERNAL
// for occasional debugging...
//#pragma optimize("", off)
//#pragma MESSAGE("************************************** WARNING, optimization disabled for debugging purposes")
#endif


//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
struct SelectObjectsInfo
{
	const ThingTemplate *thingTemplate;
	GameMessage *msg;
};

//-------------------------------------------------------------------------------------------------

void selectObjectOfType( Object* obj, void* selectObjectsInfo )
{
	SelectObjectsInfo *soInfo = (SelectObjectsInfo*)selectObjectsInfo;
	if( !obj || !soInfo )
	{
		return;
	}

	//Do the templates match?
	if( obj->getTemplate()->isEquivalentTo( soInfo->thingTemplate ) )
	{
		//Okay, then add it to the selected group.
		soInfo->msg->appendObjectIDArgument( obj->getID() );

		Drawable *draw = obj->getDrawable();
		if( draw )
		{
			TheInGameUI->selectDrawable( draw );
		}
	}
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------
/** How many upgrades this frame has already handed to this building.
	*
	* The tally is thrown away as soon as the logic frame changes, because from the next frame on the
	* buildings' own production queues carry the same information and are the better source. */
//-------------------------------------------------------------------------------------------------
Int ControlBar::upgradesSpreadTo( ObjectID id )
{
	if( m_upgradeSpreadFrame != TheGameLogic->getFrame() )
	{
		m_upgradeSpreadFrame = TheGameLogic->getFrame();
		m_upgradeSpreadEntries = 0;
	}

	for( Int i = 0; i < m_upgradeSpreadEntries; i++ )
		if( m_upgradeSpreadID[ i ] == id )
			return m_upgradeSpreadCount[ i ];

	return 0;
}

//-------------------------------------------------------------------------------------------------
/** Record that one more upgrade went to this building on this frame. */
//-------------------------------------------------------------------------------------------------
void ControlBar::noteUpgradeSpread( ObjectID id )
{
	upgradesSpreadTo( id );			// rolls the tally over if the frame changed

	for( Int i = 0; i < m_upgradeSpreadEntries; i++ )
	{
		if( m_upgradeSpreadID[ i ] == id )
		{
			m_upgradeSpreadCount[ i ]++;
			return;
		}
	}

	if( m_upgradeSpreadEntries < UPGRADE_SPREAD_MAX )
	{
		m_upgradeSpreadID[ m_upgradeSpreadEntries ] = id;
		m_upgradeSpreadCount[ m_upgradeSpreadEntries ] = 1;
		m_upgradeSpreadEntries++;
	}
}

//-------------------------------------------------------------------------------------------------
/** Of the selected buildings that can research this player upgrade, the one with the shortest
	* queue - counting what earlier clicks on this same frame already sent it.
	*
	* A player upgrade is researched once for the whole player, so unlike an object upgrade it goes
	* to exactly one building. The bar used to always name the object it was drawn from, so clicking
	* three upgrades with four barracks selected stacked all three on one barracks while the other
	* three stood idle. Ties keep selection order, so the same click on the same selection always
	* picks the same building. */
//-------------------------------------------------------------------------------------------------
Object *ControlBar::pickUpgradeProducer( const UpgradeTemplate *upgradeT, Object *fallback )
{
	if( upgradeT == NULL )
		return fallback;

	Player *player = ThePlayerList->getLocalPlayer();
	const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();
	if( selected == NULL )
		return fallback;

	Object *best = NULL;
	Int bestLoad = 0;

	for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )
	{
		Object *target = (*it)->getObject();
		if( target == NULL || target->getControllingPlayer() != player )
			continue;

		ProductionUpdateInterface *tpu = target->getProductionUpdateInterface();
		if( tpu == NULL || tpu->canQueueUpgrade( upgradeT ) != CANMAKE_OK )
			continue;

		if( target->canProduceUpgrade( upgradeT ) == FALSE || tpu->isUpgradeInQueue( upgradeT ) == TRUE )
			continue;

		Int load = tpu->getProductionCount() + upgradesSpreadTo( target->getID() );
		DEBUG_LOG(("UpgradeSpread candidate obj %d '%s' queue %d + spread %d = %d\n",
							 (Int)target->getID(), target->getTemplate()->getName().str(),
							 (Int)tpu->getProductionCount(), upgradesSpreadTo( target->getID() ), load));
		if( best == NULL || load < bestLoad )
		{
			best = target;
			bestLoad = load;
		}
	}

	DEBUG_LOG(("UpgradeSpread pick for '%s': obj %d (load %d), fallback obj %d\n",
						 upgradeT->getUpgradeName().str(), best ? (Int)best->getID() : 0, bestLoad,
						 fallback ? (Int)fallback->getID() : 0));

	return best ? best : fallback;
}

//-------------------------------------------------------------------------------------------------
/** Process a button transition message from the window system that should be for one of
	* our GUI commands */
//-------------------------------------------------------------------------------------------------
CBCommandStatus ControlBar::processCommandTransitionUI( GameWindow *control, GadgetGameMessage gadgetMessage )
{
	// sanity, we won't process messages if we have no source object
	if( m_currContext != CB_CONTEXT_MULTI_SELECT &&
			(m_currentSelectedDrawable == NULL || 
			 m_currentSelectedDrawable->getObject() == NULL) )
	{

		if( m_currContext != CB_CONTEXT_NONE && 
				m_currContext != CB_CONTEXT_OBSERVER_INFO && 
				m_currContext != CB_CONTEXT_OBSERVER_LIST)
			switchToContext( CB_CONTEXT_NONE, NULL );
		return CBC_COMMAND_NOT_USED;

	}  // end if

	return CBC_COMMAND_USED;

}

//-------------------------------------------------------------------------------------------------
/** Process a button selected message from the window system that should be for one of
	* our GUI commands */
//-------------------------------------------------------------------------------------------------
CBCommandStatus ControlBar::processCommandUI( GameWindow *control, 
																							GadgetGameMessage gadgetMessage )
{
	// get the command pointer from the control user data we put in the button
	const CommandButton *commandButton = (const CommandButton *)GadgetButtonGetData(control);
	if( !commandButton )
	{
		DEBUG_CRASH( ("ControlBar::processCommandUI() -- Button activated has no data. Ignoring...") );
		return CBC_COMMAND_NOT_USED;
	}

	// sanity, we won't process messages if we have no source object, 
	// unless we're CB_CONTEXT_PURCHASE_SCIENCE or GUI_COMMAND_SPECIAL_POWER_FROM_SHORTCUT
	if( m_currContext != CB_CONTEXT_MULTI_SELECT &&
			commandButton->getCommandType() != GUI_COMMAND_PURCHASE_SCIENCE &&
			commandButton->getCommandType() != GUI_COMMAND_SPECIAL_POWER_FROM_SHORTCUT &&
			commandButton->getCommandType() != GUI_COMMAND_SPECIAL_POWER_CONSTRUCT_FROM_SHORTCUT &&
			commandButton->getCommandType() != GUI_COMMAND_SELECT_ALL_UNITS_OF_TYPE && 
			(m_currentSelectedDrawable == NULL || m_currentSelectedDrawable->getObject() == NULL) )
	{

		if( m_currContext != CB_CONTEXT_NONE )
			switchToContext( CB_CONTEXT_NONE, NULL );
		return CBC_COMMAND_NOT_USED;

	}  // end if

	// sanity
	if( control == NULL )
		return CBC_COMMAND_NOT_USED;

	// the context sensitive gui only is only made of buttons ... sanity
	if( control->winGetInputFunc() != GadgetPushButtonInput )
		return CBC_COMMAND_NOT_USED;

	
	if( commandButton == NULL )
		return CBC_COMMAND_NOT_USED;

	// if the button is flashing, tell it to stop flashing
	commandButton->setFlashCount(0);
	TheControlBar->setFlash( FALSE );

	if( commandButton->getCommandType() != GUI_COMMAND_EXIT_CONTAINER )
	{
		GadgetButtonSetEnabledImage( control, commandButton->getButtonImage() );
	}
	
	//
	// get the object that is driving the context sensitive UI.  In the multi-select context
	// that is the focused type's representative - except for unit builds, whose handler
	// spreads the batch across every selected factory when obj is left NULL
	//
	Object *obj = NULL;
	if( commandButton->getCommandType() != GUI_COMMAND_PURCHASE_SCIENCE &&
			commandButton->getCommandType() != GUI_COMMAND_SPECIAL_POWER_FROM_SHORTCUT &&
			commandButton->getCommandType() != GUI_COMMAND_SPECIAL_POWER_CONSTRUCT_FROM_SHORTCUT &&
			commandButton->getCommandType() != GUI_COMMAND_SELECT_ALL_UNITS_OF_TYPE )
	{
		if( m_currContext != CB_CONTEXT_MULTI_SELECT )
			obj = m_currentSelectedDrawable->getObject();
		else if( commandButton->getCommandType() != GUI_COMMAND_UNIT_BUILD &&
						 m_currentSelectedDrawable != NULL )
		{
			//
			// the button was offered because *someone* in the focused group can do this
			// (getGroupCommandAvailability); send the order to that one rather than to the
			// representative, which may be the very one that already has the upgrade
			//
			obj = m_currentSelectedDrawable->getObject();
			getGroupCommandAvailability( commandButton, obj, control, &obj );
		}
	}

	//@todo Kris -- Special case code so convoy trucks can detonate nuke trucks -- if other things need this,
	//rethink it.
	if( obj && BitTest( commandButton->getOptions(), SINGLE_USE_COMMAND ) )
	{
		/** @todo Added obj check because Single Use and Multi Select crash when used together, but with this check
			* they just won't work.  When the "rethinking" occurs, this can get fixed.  Right now it is unused.
			* Convoy Truck needs Multi Select so Single Use is turned off, and noone else has it.
		*/

		//Make sure the command button is marked as used if it's a single use command. That way
		//we can never press the button again. This was added specifically for nuke convoy trucks.
		//When you click to detonate the nuke, it takes a few seconds to detonate in order to play
		//a sound. But we want to disable the button after the first click.
		obj->markSingleUseCommandUsed(); //Yeah, an object can only use one single use command...
	}

	TheInGameUI->placeBuildAvailable( NULL, NULL );

	//Play any available unit specific sound for button
	Player *player = ThePlayerList->getLocalPlayer();
	if( player )
	{
		AudioEventRTS sound = *commandButton->getUnitSpecificSound();
		sound.setPlayerIndex( player->getPlayerIndex() );
		TheAudio->addAudioEvent( &sound );
	}

	if( commandButton->getCommandType() == GUI_COMMAND_ATTACK_MOVE )
	{
		// The attack move button arms the same key its hotkey does, rather than becoming a pending
		// GUI command that one click uses up: shift then keeps it armed for a row of points.  It
		// takes the place of whatever command was waiting for a target, as any other button would.
		TheInGameUI->setGUICommand( NULL );
		TheMessageStream->appendMessage( GameMessage::MSG_META_TOGGLE_ATTACKMOVE );
	}
	else if( BitTest( commandButton->getOptions(), COMMAND_OPTION_NEED_TARGET ) )
	{
		if (commandButton->getOptions() & USES_MINE_CLEARING_WEAPONSET)
		{
			TheMessageStream->appendMessage( GameMessage::MSG_SET_MINE_CLEARING_DETAIL );
		}

		//June 06, 2002 -- Major change
		//I've added support for specific context sensitive commands which need targets just like
		//other options may need. When we need a target, the user must move the cursor to a position
		//where he wants the GUI command to take place. Older commands such as napalm strikes or daisy
		//cutter drops simply needed the user to click anywhere he desired. 
		//
		//Now, we have new commands that will only work when the user clicks on valid targets to interact 
		//with. For example, the terrorist can jack a car and convert it into a carbomb, but he has to 
		//click on a valid car. In this case the doCommandOrHint code will determine if the mode is valid 
		//or not and the cursor modes will be set appropriately.
		TheInGameUI->setGUICommand( commandButton );
	}
	else switch( commandButton->getCommandType() )
	{

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_DOZER_CONSTRUCT:
		{

			// sanity
			if( m_currentSelectedDrawable == NULL )
				break;

			//Kris: September 27, 2002
			//Make sure we have enough CASH to build it WHEN we click the button to build it,
			//before actually previewing the purchase, otherwise, cancel altogether.
			const ThingTemplate *whatToBuild = commandButton->getThingTemplate();
			CanMakeType cmt = TheBuildAssistant->canMakeUnit( obj, whatToBuild );
			if (cmt == CANMAKE_NO_MONEY)
			{
				TheEva->setShouldPlay(EVA_InsufficientFunds);
				TheInGameUI->message( "GUI:NotEnoughMoneyToBuild" );
				break;
			} 
			else if (cmt == CANMAKE_QUEUE_FULL)
			{
				TheInGameUI->message( "GUI:ProductionQueueFull" );
				break;
			}
			else if (cmt == CANMAKE_PARKING_PLACES_FULL)
			{
				TheInGameUI->message( "GUI:ParkingPlacesFull" );
				break;
			}
			else if( cmt == CANMAKE_MAXED_OUT_FOR_PLAYER )
			{
				TheInGameUI->message( "GUI:UnitMaxedOut" );
				break;
			} 

			// tell the UI that we want to build something so we get a building at the cursor
			TheInGameUI->placeBuildAvailable( commandButton->getThingTemplate(), m_currentSelectedDrawable );

			break;

		}  // end dozer construct


		case GUI_COMMAND_SPECIAL_POWER_CONSTRUCT_FROM_SHORTCUT:
		{
			//Determine the object that would construct it.
			const SpecialPowerTemplate *spTemplate = commandButton->getSpecialPowerTemplate();
			SpecialPowerType spType = spTemplate->getSpecialPowerType();
			Object* obj = ThePlayerList->getLocalPlayer()->findMostReadyShortcutSpecialPowerOfType( spType );
			if( !obj )
				break;
			Drawable *draw = obj->getDrawable();

			const ThingTemplate *whatToBuild = commandButton->getThingTemplate();

			CanMakeType cmt = TheBuildAssistant->canMakeUnit( obj, whatToBuild );
			if (cmt == CANMAKE_NO_MONEY)
			{
				TheEva->setShouldPlay(EVA_InsufficientFunds);
				TheInGameUI->message( "GUI:NotEnoughMoneyToBuild" );
				break;
			} 
			else if (cmt == CANMAKE_QUEUE_FULL)
			{
				TheInGameUI->message( "GUI:ProductionQueueFull" );
				break;
			}
			else if (cmt == CANMAKE_PARKING_PLACES_FULL)
			{
				TheInGameUI->message( "GUI:ParkingPlacesFull" );
				break;
			}
			else if( cmt == CANMAKE_MAXED_OUT_FOR_PLAYER )
			{
				TheInGameUI->message( "GUI:UnitMaxedOut" );
				break;
			} 

			// tell the UI that we want to build something so we get a building at the cursor
			TheInGameUI->placeBuildAvailable( commandButton->getThingTemplate(), draw );
		
			ProductionUpdateInterface* pu = obj->getProductionUpdateInterface();
			if( pu )
			{
				pu->setSpecialPowerConstructionCommandButton( commandButton );
			}

			break;
		}
		case GUI_COMMAND_SPECIAL_POWER_CONSTRUCT:
		{
			// sanity
			if( m_currentSelectedDrawable == NULL )
				break;

			const ThingTemplate *whatToBuild = commandButton->getThingTemplate();

			CanMakeType cmt = TheBuildAssistant->canMakeUnit( obj, whatToBuild );
			if (cmt == CANMAKE_NO_MONEY)
			{
				TheEva->setShouldPlay(EVA_InsufficientFunds);
				TheInGameUI->message( "GUI:NotEnoughMoneyToBuild" );
				break;
			} 
			else if (cmt == CANMAKE_QUEUE_FULL)
			{
				TheInGameUI->message( "GUI:ProductionQueueFull" );
				break;
			}
			else if (cmt == CANMAKE_PARKING_PLACES_FULL)
			{
				TheInGameUI->message( "GUI:ParkingPlacesFull" );
				break;
			}
			else if( cmt == CANMAKE_MAXED_OUT_FOR_PLAYER )
			{
				TheInGameUI->message( "GUI:UnitMaxedOut" );
				break;
			} 

			// tell the UI that we want to build something so we get a building at the cursor
			TheInGameUI->placeBuildAvailable( commandButton->getThingTemplate(), m_currentSelectedDrawable );

			ProductionUpdateInterface* pu = obj->getProductionUpdateInterface();
			if( pu )
			{
				pu->setSpecialPowerConstructionCommandButton( commandButton );
			}

			break;

		}  // end dozer construct



		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_DOZER_CONSTRUCT_CANCEL:
		{

			// get the object we have selected
			Object *building = obj;
			if( building == NULL )
				break;

			// sanity check, the building must be under our control to cancel construction
			if( building->getControllingPlayer() != ThePlayerList->getLocalPlayer() )
				break;

			// do the message
			TheMessageStream->appendMessage( GameMessage::MSG_DOZER_CANCEL_CONSTRUCT );

			break;

		}  // end cancel dozer construction

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_UNIT_BUILD:
		{
			//
			const ThingTemplate *whatToBuild = commandButton->getThingTemplate();

			// sanity, we must have something to build
			DEBUG_ASSERTCRASH( whatToBuild, ("Undefined BUILD command for object '%s'\n",
												 commandButton->getThingTemplate()->getName().str()) );

			// Ctrl+Alt, on the button's key or a click, puts the unit on repeat or takes it off.
			// The right button already takes units back out of the queue, so it stays that
			if( TheKeyboard->isCtrl() && TheKeyboard->isAlt() )
			{
				toggleRepeatUnit( whatToBuild );
				break;
			}

			// shift, control or both queue a whole batch with one click
			Int wanted = getBuildBatchCount();

			// gather the "factory" objects that could make the thing. With several factories
			// selected (multi-select context) every one that can make it is a candidate, and each
			// unit of the batch goes to the least loaded one, so the work spreads over all of them.
			// A factory already at the nine-deep strip is left out, and with none able to take it
			// the click does nothing: the button is already grey.
			std::vector<Object *> factories;
			std::vector<UnsignedInt> queued;		// real production count plus what this click already gave it
			if( obj )
			{
				ProductionUpdateInterface *opu = obj->getProductionUpdateInterface();
				factories.push_back( obj );
				queued.push_back( opu ? opu->getProductionCount() : 0 );
			}
			else if( m_currContext == CB_CONTEXT_MULTI_SELECT )
			{
				const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();
				for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )
				{
					Object *candidate = (*it)->getObject();
					ProductionUpdateInterface *cpu = candidate ? candidate->getProductionUpdateInterface() : NULL;
					if( cpu == NULL || TheBuildAssistant->canMakeUnit( candidate, whatToBuild ) != CANMAKE_OK )
						continue;
					if( cpu->getProductionCount() >= getQueueCap( cpu ) )
						continue;
					factories.push_back( candidate );
					queued.push_back( cpu->getProductionCount() );
				}
			}
			if( factories.empty() )
				break;

			for( Int built = 0; built < wanted; built++ )
			{
				// least loaded factory that still has a free queue slot takes this one
				Int best = -1;
				for( Int i = 0; i < (Int)factories.size(); i++ )
				{
					ProductionUpdateInterface *fpu = factories[ i ]->getProductionUpdateInterface();
					const UnsignedInt cap = fpu ? getQueueCap( fpu ) : 0;
					if( queued[ i ] >= cap )
						continue;
					if( best < 0 || queued[ i ] < queued[ best ] )
						best = i;
				}
				if( best < 0 )
				{
					if( built == 0 )
						TheInGameUI->message( "GUI:ProductionQueueFull" );
					break;
				}
				Object *factory = factories[ best ];

				CanMakeType cmt = TheBuildAssistant->canMakeUnit(factory, whatToBuild);

				if (cmt == CANMAKE_NO_MONEY)
				{
					TheEva->setShouldPlay(EVA_InsufficientFunds);
					TheInGameUI->message( "GUI:NotEnoughMoneyToBuild" );
					break;
				} 
				else if (cmt == CANMAKE_QUEUE_FULL)
				{
					TheInGameUI->message( "GUI:ProductionQueueFull" );
					break;
				}
				else if (cmt == CANMAKE_PARKING_PLACES_FULL)
				{
					TheInGameUI->message( "GUI:ParkingPlacesFull" );
					break;
				}
				else if( cmt == CANMAKE_MAXED_OUT_FOR_PLAYER )
				{
					TheInGameUI->message( "GUI:UnitMaxedOut" );
					break;
				}
				// a unit on repeat keeps its button clickable after its prerequisite is lost or its
				// factory goes dark (ControlBar::updateContextCommand), so these reach here now. The
				// tooltip already names what is missing; the click just does nothing
				else if( cmt == CANMAKE_NO_PREREQ || cmt == CANMAKE_FACTORY_IS_DISABLED )
				{
					break;
				}
				else if (cmt != CANMAKE_OK)
				{
					DEBUG_ASSERTCRASH( 0, ("Cannot create '%s' because the factory object '%s' returns false for canMakeUnit\n",
																	whatToBuild->getName().str(), 
																	factory->getTemplate()->getName().str()) );
					break;
				}

				// get the production interface from the factory object
				ProductionUpdateInterface *pu = factory->getProductionUpdateInterface();

				// sanity, we can't build things if we can't produce units
				if( pu == NULL )
				{

					DEBUG_ASSERTCRASH( 0, ("Cannot create '%s' because the factory object '%s' is not capable of producting units\n", 
																	whatToBuild->getName().str(), 
																	factory->getTemplate()->getName().str()) );
					break;

				}  // end if

				// create a message to build this thing.  The second argument is kept for the message's
				// shape and is unused: the logic mints the production ID, because minting it here moved
				// the factory's counter on this machine and no other.

				GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_QUEUE_UNIT_CREATE );
				msg->appendIntegerArgument( whatToBuild->getTemplateID() );
				msg->appendIntegerArgument( PRODUCTIONID_INVALID );
				msg->appendObjectIDArgument( factory->getID() );	// which of the selected factories builds it

				//
				// the real production count only catches up once the logic runs these messages, so
				// count it here to keep spreading the rest of the batch
				//
				queued[ best ]++;

			}  // end for, one message per unit of the batch

			break;

		}  // end build unit
		
		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_CANCEL_UNIT_BUILD:
		{
			Int i;

			// find out which index (i) in the queue represents the button clicked
			for( i = 0; i < MAX_BUILD_QUEUE_BUTTONS; i++ )
				if( m_queueData[ i ].control == control )
					break;

			// sanity, control not found
			if( i == MAX_BUILD_QUEUE_BUTTONS )
			{

				DEBUG_ASSERTCRASH( 0, ("Control not found in build queue data\n") );
				break;

			}  // end if

			// sanity
			if( m_queueData[ i ].type != PRODUCTION_UNIT )
				break;

			// the the production ID to cancel
			ProductionID productionIDToCancel = m_queueData[ i ].productionID;

			// get the object that is the producer
			Object *producer = obj;
			if( producer == NULL )
				break;

			// sanity, we must control the producer ... if this isn't true they might be hacking the game
			if( producer->getControllingPlayer() != ThePlayerList->getLocalPlayer() )
				break;

			// send a message to cancel that particular production entry
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_CANCEL_UNIT_CREATE );
			msg->appendIntegerArgument( productionIDToCancel );

			break;

		}  // end cancel unit build

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_PLAYER_UPGRADE:
		{
			const UpgradeTemplate *upgradeT = commandButton->getUpgradeTemplate();
			DEBUG_ASSERTCRASH( upgradeT, ("Undefined upgrade '%s' in player upgrade command\n", "UNKNOWN") );

			// sanity
			if( obj == NULL || upgradeT == NULL )
				break;

			// make sure the player can really make this
			if( TheUpgradeCenter->canAffordUpgrade( ThePlayerList->getLocalPlayer(), upgradeT, TRUE ) == FALSE )
			{
				break;
			}

			//
			// A player upgrade is researched once for the whole player, so it goes to a single
			// building - but with several selected there is no reason for that to be the same
			// building every time. Send it to the one with the shortest queue instead, counting
			// the upgrades earlier clicks on this frame already handed out, which is something
			// the buildings' own queues cannot tell us yet because the messages have not reached
			// the logic. Without this, clicking three upgrades with four barracks selected piled
			// all three onto one barracks and left the other three idle.
			//
			Object *producer = obj;
			DEBUG_LOG(("UpgradeSpread player upgrade '%s' ctx %d selCount %d rep obj %d\n",
								 upgradeT->getUpgradeName().str(), (Int)m_currContext,
								 TheInGameUI ? TheInGameUI->getSelectCount() : -1, (Int)obj->getID()));
			if( m_currContext == CB_CONTEXT_MULTI_SELECT )
				producer = pickUpgradeProducer( upgradeT, obj );

			ProductionUpdateInterface* pu = producer ? producer->getProductionUpdateInterface() : NULL;
			if (pu != NULL)
			{
				CanMakeType cmt = pu->canQueueUpgrade(upgradeT);
				if (cmt == CANMAKE_QUEUE_FULL)
				{
					TheInGameUI->message( "GUI:ProductionQueueFull" );
					break;
				}
			}

			if( producer == NULL )
				break;

			// send the message
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_QUEUE_UPGRADE );
			msg->appendObjectIDArgument( producer->getID() );
			msg->appendIntegerArgument( upgradeT->getUpgradeNameKey() );
			noteUpgradeSpread( producer->getID() );

			break;

		}  // command player upgrade

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_OBJECT_UPGRADE:
		{
			const UpgradeTemplate *upgradeT = commandButton->getUpgradeTemplate();
			DEBUG_ASSERTCRASH( upgradeT, ("Undefined upgrade '%s' in object upgrade command\n", "UNKNOWN") );
			// sanity
			if( upgradeT == NULL )
				break;

			// shift: the unit buys it when its list gets there, and the money is looked at then
			const Bool queued = TheInGameUI->isInWaypointMode();

			//Make sure the player can really make this
			if( !queued && TheUpgradeCenter->canAffordUpgrade( ThePlayerList->getLocalPlayer(), upgradeT, TRUE ) == FALSE )
			{
				//Kris: Disabled because we can get a valid reason for not being able to afford the upgrade!
				//TheInGameUI->message( "upgrade unsupported in commandprocessing." );
				break;
			}

			ProductionUpdateInterface* pu = obj ? obj->getProductionUpdateInterface() : NULL;
			if (pu != NULL)
			{
				CanMakeType cmt = pu->canQueueUpgrade(upgradeT);
				if (cmt == CANMAKE_QUEUE_FULL)
				{
					TheInGameUI->message( "GUI:ProductionQueueFull" );
					break;
				}
			}

			//
			// An object upgrade is bought per building, so with several selected the click is
			// spread over all of them that still want it and that the player can pay for, in
			// selection order. It used to go to one object only - and to the same one every
			// time, since the bar cannot see the queue the previous click just filled until the
			// next logic frame - so a click on four selected barracks upgraded one of them and
			// the other three clicks were silently thrown away by the duplicate guard in
			// ProductionUpdate::queueUpgrade.
			//
			if( m_currContext == CB_CONTEXT_MULTI_SELECT )
			{
				Player *player = ThePlayerList->getLocalPlayer();
				Int purse = player->getMoney()->countMoney();
				Int cost = upgradeT->calcCostToBuild( player );
				Int sent = 0;

				const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();
				for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )
				{
					Object *target = (*it)->getObject();
					if( target == NULL || target->getControllingPlayer() != player )
						continue;

					ProductionUpdateInterface *tpu = target->getProductionUpdateInterface();
					if( tpu == NULL || tpu->canQueueUpgrade( upgradeT ) != CANMAKE_OK )
						continue;

					if( target->hasUpgrade( upgradeT ) == TRUE ||
							target->affectedByUpgrade( upgradeT ) == FALSE ||
							tpu->isUpgradeInQueue( upgradeT ) == TRUE ||
							target->canProduceUpgrade( upgradeT ) == FALSE )
						continue;

					// the logic charges each one as it queues it, so stop at what is in the bank
					if( !queued && cost > 0 && purse < cost )
						break;
					purse -= cost;

					if( queued )
						TheInGameUI->markNextOrderQueued( ORDER_QUEUE_APPEND );
					GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_QUEUE_UPGRADE );
					msg->appendObjectIDArgument( target->getID() );
					msg->appendIntegerArgument( upgradeT->getUpgradeNameKey() );
					sent++;
					DEBUG_LOG(("UpgradeSpread object upgrade '%s' -> obj %d (purse left %d)\n",
										 upgradeT->getUpgradeName().str(), (Int)target->getID(), purse));
				}

				if( sent > 0 )
					break;
				// nobody in the selection could take it; fall through to the single-object path
			}

			ObjectID objID = INVALID_ID;
			if (obj)
				objID = obj->getID();

			// make sure that the this object can actually build the upgrade
			if( obj && (obj->hasUpgrade( upgradeT ) == TRUE || obj->affectedByUpgrade( upgradeT ) == FALSE) )
				break;

			// send the message
			if( queued )
				TheInGameUI->markNextOrderQueued( ORDER_QUEUE_APPEND );
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_QUEUE_UPGRADE );
			msg->appendObjectIDArgument( objID );
			msg->appendIntegerArgument( upgradeT->getUpgradeNameKey() );

			break;

		}  // end object upgrade

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_CANCEL_UPGRADE:
		{
			Int i;

			// find out which index (i) in the queue represents the button clicked
			for( i = 0; i < MAX_BUILD_QUEUE_BUTTONS; i++ )
				if( m_queueData[ i ].control == control )
					break;

			// sanity, control not found
			if( i == MAX_BUILD_QUEUE_BUTTONS )
			{

				DEBUG_ASSERTCRASH( 0, ("Control not found in build queue data\n") );
				break;

			}  // end if

			// sanity
			if( m_queueData[ i ].type != PRODUCTION_UPGRADE )
				break;

			// get the upgrade to cancel
			const UpgradeTemplate *upgradeT = m_queueData[ i ].upgradeToResearch;

			// get producer object (the thing driving our UI)
			Object *producer = obj;

			// sanity
			if( upgradeT == NULL || producer == NULL )
				break;

			// send the message
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_CANCEL_UPGRADE );
			msg->appendIntegerArgument( upgradeT->getUpgradeNameKey() );

			break;

		}  // end cancel upgrade

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_STOP:
		{
			// This message always works on the currently selected team
			TheMessageStream->appendMessage(GameMessage::MSG_DO_STOP);
			break;
		}

		case GUI_COMMAND_SELECT_ALL_UNITS_OF_TYPE:
		{
			Player* localPlayer = ThePlayerList->getLocalPlayer();
			if( !localPlayer )
			{
				break;
			}

			const ThingTemplate *thing = commandButton->getThingTemplate();
			if( !thing )
			{
				break;
			}

			//deselect other units
			TheInGameUI->deselectAllDrawables();

			// create a new group.
			GameMessage *teamMsg = TheMessageStream->appendMessage( GameMessage::MSG_CREATE_SELECTED_GROUP );

			//New group or add to group? Passed in value is true if we are creating a new group.
			teamMsg->appendBooleanArgument( TRUE );

			//Iterate through the player's entire team and select each member that matches the template.
			SelectObjectsInfo info;
			info.thingTemplate = thing;
			info.msg = teamMsg;
			localPlayer->iterateObjects( selectObjectOfType, (void*)&info );

			break;
		}
		
		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_WAYPOINTS:
			break;

		//-------------------------------------------------------------------------------------------------
		case GUI_COMMAND_EXIT_CONTAINER:
		{
			Int i;
			ObjectID objID = INVALID_ID;

			//
			// find the object ID that wants to exit by scanning through the transport data and looking
			// for the matching control button
			//
			// (this used to run to the end of the array without breaking and without initializing
			// objID, so a miss fed garbage to findObjectByID and then wrote one past m_containData)
			for( i = 0; i < MAX_COMMANDS_PER_SET; i++ )
				if( m_containData[ i ].control == control )
				{
					objID = m_containData[ i ].objectID;
					break;
				}

			// get the actual object
			Object *objWantingExit = TheGameLogic->findObjectByID( objID );
			
			// if object is not found remove inventory entry and exit
			if( objWantingExit == NULL )
			{

				//
				// remove from inventory data to avoid future matches ... the inventory update
				// cycle of the UI will repopulate any buttons as the contents of objects
				// change so this is only an edge case that will be visually corrected next frame
				//
				if( i < MAX_COMMANDS_PER_SET )
				{
					m_containData[ i ].control = NULL;
					m_containData[ i ].objectID = INVALID_ID;
				}
				break;  // exit case

			}  // end if

      //what if container is subdued... assert a logic failure, perhaps?

			// send message to exit
			GameMessage *exitMsg = TheMessageStream->appendMessage( GameMessage::MSG_EXIT );
			exitMsg->appendObjectIDArgument( objWantingExit->getID() ); // 0 is the thing inside coming out

			break;

		}  // end transport exit

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_EVACUATE:
		{
			// A vehicle transport asks where: Command_EvacuateAt's cursor takes the spot, and the
			// transport drives or flies there and unloads (AIGroup::groupEvacuate).  Alt held, here or
			// with the button's key (HotKeyTranslator), empties it where it stands, as a building does
			const CommandButton *evacuateAt = TheKeyboard->isAlt() ? NULL : findCommandButton( "Command_EvacuateAt" );
			if( evacuateAt )
			{
				const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();
				for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )
				{
					const Object *transport = (*it)->getObject();
					if( transport && AIGroup::unloadsWhereSent( transport ) )
					{
						TheInGameUI->setGUICommand( evacuateAt );
						return CBC_COMMAND_USED;
					}
				}
			}

			// Cancel GUI command mode.
			TheInGameUI->setGUICommand( NULL );
			pickAndPlayUnitVoiceResponse( TheInGameUI->getAllSelectedDrawables(), GameMessage::MSG_EVACUATE );
			TheMessageStream->appendMessage( GameMessage::MSG_EVACUATE );
			break;
		}  // end evacuate

		// --------------------------------------------------------------------------------------------
		case GUI_COMMAND_EXECUTE_RAILED_TRANSPORT:
		{
			TheMessageStream->appendMessage( GameMessage::MSG_EXECUTE_RAILED_TRANSPORT );
			break;
		}  // end execute railed transport

		// --------------------------------------------------------------------------------------------
		case GUI_COMMAND_HACK_INTERNET:
		{
			pickAndPlayUnitVoiceResponse( TheInGameUI->getAllSelectedDrawables(), GameMessage::MSG_INTERNET_HACK );
			TheMessageStream->appendMessage( GameMessage::MSG_INTERNET_HACK );
			break;
		}

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_SET_RALLY_POINT:
		{
			break;

		}  // end set rally point

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_SELL:
		{

			// command needs no additional data, send the message
			TheMessageStream->appendMessage( GameMessage::MSG_SELL );
			break;

		}  // end sell

		// --------------------------------------------------------------------------------------------
		case GUI_COMMAND_TOGGLE_OVERCHARGE:
		{

			TheMessageStream->appendMessage( GameMessage::MSG_TOGGLE_OVERCHARGE );
			break;

		}  // end overcharge

#ifdef ALLOW_SURRENDER
		// ------------------------------------------------------------------------------------------------
		case GUI_COMMAND_POW_RETURN_TO_PRISON:
		{

			TheMessageStream->appendMessage( GameMessage::MSG_RETURN_TO_PRISON );
			break;

		}  // end return to prison
#endif

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_BEACON_DELETE:
		{

			break;

		}  // end delete beacon

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_GUARD:
		case GUI_COMMAND_GUARD_WITHOUT_PURSUIT:
		case GUI_COMMAND_GUARD_FLYING_UNITS_ONLY:
		case GUI_COMMAND_COMBATDROP:
		{
			DEBUG_CRASH(("hmm, should never occur"));
		}
		break;

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_SWITCH_WEAPON:
		{
				// command needs no additional data, send the message
				GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_SWITCH_WEAPONS );

				//Play mode change acknowledgement
				PickAndPlayInfo info;
				WeaponSlotType slot = commandButton->getWeaponSlot();
				info.m_weaponSlot = &slot;
				pickAndPlayUnitVoiceResponse( TheInGameUI->getAllSelectedDrawables(), GameMessage::MSG_SWITCH_WEAPONS, &info );

				msg->appendIntegerArgument( commandButton->getWeaponSlot() );
				break;
		}

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_FIRE_WEAPON:
		{
			// command needs no additional data, send the message
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_DO_WEAPON );
			msg->appendIntegerArgument( commandButton->getWeaponSlot() );
			msg->appendIntegerArgument( commandButton->getMaxShotsToFire() );

			break;

		}  // end fire weapon

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_SPECIAL_POWER_FROM_SHORTCUT:
		{
			const SpecialPowerTemplate *spTemplate = commandButton->getSpecialPowerTemplate();
			SpecialPowerType spType = spTemplate->getSpecialPowerType();

			Object* obj = ThePlayerList->getLocalPlayer()->findMostReadyShortcutSpecialPowerOfType( spType );
			if( !obj )
				break;

			// command needs no additional data, send the message
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_DO_SPECIAL_POWER );
			msg->appendIntegerArgument( spTemplate->getID() );
			msg->appendIntegerArgument( commandButton->getOptions() );
			msg->appendObjectIDArgument( obj->getID() );
			break;

		}  // end special weapon

		case GUI_COMMAND_SPECIAL_POWER:
		{
			// shift: the units use it when their list gets there
			if( TheInGameUI->isInWaypointMode() )
				TheInGameUI->markNextOrderQueued( ORDER_QUEUE_APPEND );

			// command needs no additional data, send the message
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_DO_SPECIAL_POWER );
			msg->appendIntegerArgument( commandButton->getSpecialPowerTemplate()->getID() );
			msg->appendIntegerArgument( commandButton->getOptions() );
			msg->appendObjectIDArgument( INVALID_ID );	// no specific source
			break;

		}  // end special weapon

		//---------------------------------------------------------------------------------------------
		case GUI_COMMAND_PURCHASE_SCIENCE:
		{

			// loop through all the sciences on the button and select the one we don't have

			ScienceType	st = SCIENCE_INVALID; 
			Player *player = ThePlayerList->getLocalPlayer();
			for(Int i = 0; i < commandButton->getScienceVec().size(); ++i)
			{
				st = commandButton->getScienceVec()[ i ];
				if(!player->hasScience(st) && TheScienceStore->playerHasPrereqsForScience(player, st) && TheScienceStore->getSciencePurchaseCost(st) <= player->getSciencePurchasePoints())
				{
					break;
				}
			}
			
			if( st == SCIENCE_INVALID)
			{
				switchToContext( CB_CONTEXT_NONE, NULL );
				break;
			}
			
		
			GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_PURCHASE_SCIENCE );
			msg->appendIntegerArgument( st );
			
			markUIDirty();

			break;

		}  // end pick specialized science

		//---------------------------------------------------------------------------------------------
		default:

			DEBUG_ASSERTCRASH( 0, ("Unknown command '%d'\n", commandButton->getCommandType()) );
			return CBC_COMMAND_NOT_USED;

	}  // end switch

	return CBC_COMMAND_USED;

}  // end processCommandUI

//-------------------------------------------------------------------------------------------------
/** The button's repeat mark: some selected factory of ours builds 'thing' on repeat. */
//-------------------------------------------------------------------------------------------------
Bool ControlBar::selectionRepeatsUnit( const ThingTemplate *thing )
{
	const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();
	for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )
	{
		Object *candidate = (*it)->getObject();
		if( candidate == NULL || candidate->getControllingPlayer() != ThePlayerList->getLocalPlayer() )
			continue;
		ProductionUpdateInterface *pu = candidate->getProductionUpdateInterface();
		if( pu && pu->isRepeatUnit( thing ) )
			return TRUE;
	}
	return FALSE;
}

//-------------------------------------------------------------------------------------------------
/** Ctrl+Alt on a build button: every selected factory of ours that can make the unit builds it on
	* repeat, or, when any of them already does, every one of them stops.  One message per factory
	* with the state named outright, so a mixed selection comes out all on or all off. */
//-------------------------------------------------------------------------------------------------
void ControlBar::toggleRepeatUnit( const ThingTemplate *thing )
{
	const Bool on = !selectionRepeatsUnit( thing );
	const DrawableList *selected = TheInGameUI->getAllSelectedDrawables();
	for( DrawableListCIt it = selected->begin(); it != selected->end(); ++it )
	{
		Object *candidate = (*it)->getObject();
		if( candidate == NULL || candidate->getControllingPlayer() != ThePlayerList->getLocalPlayer() )
			continue;
		if( candidate->getProductionUpdateInterface() == NULL )
			continue;
		if( on && !TheBuildAssistant->isPossibleToMakeUnit( candidate, thing ) )
			continue;

		GameMessage *msg = TheMessageStream->appendMessage( GameMessage::MSG_SET_REPEAT_UNIT );
		msg->appendIntegerArgument( thing->getTemplateID() );
		msg->appendObjectIDArgument( candidate->getID() );
		msg->appendBooleanArgument( on );
	}
	markUIDirty();
}