Adding the submarine unit 'type'
This is a 'work in progress'
As with missiles, the 'easy' way to dive your subs is to simply define a new unit 'type' (unit flags are held in a 'long' = 32bit word, so there are plenty 'spare'). You then modify the artwork (units.bmp) to add the 'transpartent' icon to the start of the file (so it's location can be 'hard coded' into the source game.cpp).
Then 'all' that's needed is to modify the game.cpp code (at about line 777) to 'hide' enemy subs at the end of their go
On the first game turn, the first player will see enemy Subs that have been 'pre-deployed' = unless, in CoMET, you pre-deploy subs with the 'transparent' artwork icon Of course, whilst this 'works' fine, it does mean that once deployed in CoMET you can't actually 'see' them :-)
To define the sub 'type' flag, in gamedefs.h, add :-
#define U_SUBMARINE 0x00001000
When the unit definitions file (.usrc) is compiled, units of type = submarine have to be 'built' with the new flag set by makunitset.cpp. After :- if ( key == "type" ) { add :-
else if ( val == "submarine" ) t.AddFlags( U_SUBMARINE );
The final change is to game.cpp source - it has to 'spot' when this players submarine is in water/deepwater and dive it before the next players turn or surface it (so the next player can see their own subs or enemy subs in shallow water)
Start by adding a definition to 'detect' if the unit is of type submarine in /source/comet/unit.h add :-
bool IsSubmarine( void ) const { return (u_flags & U_SUBMARINE) != 0; }
Now the complicated part. A unit game object has a 'u_type' value which is a 'pointer' (*prefix) into the unit type definition array (UnitType) held in computer in memory. These pointers are generated when the map is compiled (by looking up each units name in the compiled units file).
SO - what we need to do is change the u_type pointer from the 'submarine' icon to the 'transparent' icon and back again. This means 'saving' the two pointers generated when the map is created by cfed i.e. adding them to the 'map' (or mission') header. They will then be carried forward into the current game.
Since the transparent icon is always at the start of the icon array, all we need to do is note where the array, when loaded into memory, starts.
At the end of the turn, any submarine of the current player in water/deepwater is dived, all others surfaced. In game.cpp, at about line 777, in the section :- while ( u ) { next = static_cast(u->Next()); add :-
// detect submarines
if ( u->IsSubmarine ) {
// current player, in water/deepwater, dives (i.e. change the u_type for this unit)
if ( ) {
} else {
// else surface
}
}
Now recompile CF and makeunits (which defines the u_type constants)... then and go set up the submarine unit defs and artwork and recompile the units file and that's it !