Week 6
Creating Units (Advanced) - Hooman Never Told You What Happened To Your Cargo Trucks...
Time to cover some stuff I told you not to worry about. This week's topics:
-Creating (and keeping track of) special units
-Putting cargo in Cargo Trucks
-Intro to AIProc
-Using AIProc to do things with special units
Remember way back in week 1 when I first taught you how to create units?
Unit x; // Don't worry about this for now.
TethysGame::CreateUnit(x, mapCommandCenter, LOCATION(64, 64), 0, mapNone, 0);
See the part I told you not to worry about? Well, now it's time to take a closer look at it.
Unit handles allow us to keep track of special units (if we want to). This is useful for AI and special mission objectives. We've already been using a generic unit handle (x) to create non-special units, but it's time to move into the big leagues. Here are some examples:
Unit Unit1; // From now on, we will use Unit1 as our generic unit, rather than "x", which could be anything really.
Unit advLab; // A special Advanced Lab we want to remember because an objective requires the player to scan it with a Scout.
Unit AI_VF1; // An AI-controlled Vehicle Factory. We will need to keep track of it so the AI can produce new units with it.
Unit MustLive; // We need to keep track of this unit and fail the mission if it gets destroyed.
Now let's create all of those.
TethysGame::CreateUnit(Unit1, mapCommandCenter, ...);
TethysGame::CreateUnit(Unit1, mapGuardPost, ...);
TethysGame::CreateUnit(advLab, mapAdvancedLab, ...);
TethysGame::CreateUnit(AI_VF1, mapVehicleFactory, ...);
TethysGame::CreateUnit(MustLive, mapVehicleFactory, ...);
TethysGame::CreateUnit(Unit1, mapTokamak, ...);
See? Not so hard. Now let's do something with those special units.
//Use the special Advanced Lab in a trigger:
int InitProc()
{
// Create a trigger that fires if Player 1 scans the special Lab with a Scout
CreateSpecialTarget(1, 1, advLab, mapScout, "LabScanned");
...
}
SCRIPT_API void LabScanned()
{
// stuff would happen here
}
I might as well explain how CreateSpecialTarget works.
CreateSpecialTarget(Enabled/Disabled, One-time Only, Unit Handle, Triggering Unit, "Trigger Function");
I should only have to explain Unit Handle and Triggering Unit.
Unit Handle: The unit that needs to be scanned.
Trigger Unit: The type of unit that needs to scan the special unit.
//This Works But Isn't Very Useful
SCRIPT_API void BuildMoreVehicles()
{
AI_VF1.DoDevelop(mapScout);
}
//Checking if a unit is still alive
void AIProc()
{
if (MustLive.IsLive() == 0)
{
// Create a trigger that will always evaluate to true so the player fails the mission
}
}
We'll discuss AIProc a bit more down below.
One important thing I forgot to mention is that any special units should be declared globally (put them near the top of your code file,
not in InitProc or any other function).
char MapName[] = "on6_01.map";
char LevelDesc[] = "6P, LoS, 'Some Name'";
char TechtreeName[] = "MULTITEK.TXT";
SDescBlock DescBlock = { MultiLastOneStanding, 6, 12, 0 };
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hinstDLL);
}
return TRUE;
}
// Special Unit Handles
Unit Special1;
Unit Special2;
...
int InitProc()
{
Unit Unit1;
...
}
Next up, I'll show you how you can put cargo in Cargo Trucks.
// Let's create a truck with 1000 units of food.
TethysGame::CreateUnit(Unit1, mapCargoTruck, LOCATION(40+31, 30-1), 0, mapNone, 0);
Unit1.DoSetLights(1);
Unit1.SetTruckCargo(truckFood, 1000);
Easy, right?
Valid Cargo List:
truckEmpty // No cargo.
truckFood // Food
truckCommonOre // Common Ore
truckRareOre // Rare Ore
truckCommonMetal // Common Metal
truckRareMetal // Rare Metal
truckCommonRubble // Common Rubble
truckRareRubble // Rare Rubble
truckSpaceport // Starship module/satellite
truckGarbage // Wreckage
Note that for truckSpaceport, the cargo amount determines the starship component/satellite loaded into the truck.
//Starship parts listed by Map ID.
mapEDWARDSatellite = 88
mapSolarSatellite = 89
mapIonDriveModule = 90
mapFusionDriveModule = 91
mapCommandModule = 92
mapFuelingSystems = 93
mapHabitatRing = 94
mapSensorPackage = 95
mapSkydock = 96
mapStasisSystems = 97
mapOrbitalPackage = 98
mapPhoenixModule = 99
mapRareMetalsCargo = 100
mapCommonMetalsCargo = 101
mapFoodCargo = 102
mapEvacuationModule = 103
mapChildrenModule = 104
The same thing applies for truckGarbage, except replace "starship part" with "tech the wreckage gives you". You'll have to look in the techtree file you're using, and set the amount to the approparite tech ID.
BONUS LESSON: More stuff with units.
Here's some more things you can do to your units:
Structures:Unit1.DoIdle(); // Idles a structure
Unit1.DoUnIdle(); // Activates a structure if it's operational requirements are met
Unit1.DoInfect(); // Forces structure to "Disabled - Infected" status. Can only be used in maps with only one player (that player count includes AIs!)
Vehicles:Unit1.DoAttack(Special Unit); // Attacks the designated unit
Unit1.DoMove(LOCATION(X,Y) ); // Moves to the designated location
Unit1.DoStop(); // Stops the unit
Unit1.DoSelfDestruct(); // Forces the unit to self-destruct
Both:Unit1.DoTransfer(Recipient); // Trades the vehicle or structure to the designated player
Unit1.DoDeath(); // Kills the unit
Unit1.SetDamage(value); // Remove specified amount of HP from the unit. Note that this can result in a structure or unit having negative HP if you're not careful!
Unit1.ClearSpecialTarget(); // You know that green outline that appears around some special units? This gets rid of that. Use it in the response to a Special Target Trigger.
If you want to put units in a Garage at game start-up, go
here.
Hmm... That's odd. I feel like I've forgotten something... Oh well.
Now let's take a look at AIProc.