Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Excerpt

Enable or disable the mini map and design mode depending on the presence of a HQ.

 

Summary

Under normal game rules a HQ is required to enable the Design Mode (droid design tool) and mini map.

If the HQ gets destroyed, both those features must be disabled. If a HQ gets build, they are re-enabled.

The code to do this is usually stored in a function so that it can be called from three key events:

  • eventGameInit() – when the game starts, as there might be a HQ present after player initialisation (for example a HQ pre-placed on the map at design time, and the baseType setting retained it).
  • eventStructureBuilt() – in case an HQ has been built
  • eventDestroyed() – in case the HQ has been destroyed

Human Player Only

The rules.js script is responsible for performing this check. Unlike the other things it does, this check needs to be limited to the localhost human player (defined by the 'selectedPlayer' or 'me' global, depending on how you want to do it).

Example Code

Assuming the player won't be using the Debug Menu to switch to other players, you can use me for your player checks...

Code Block
themeRDark
languagejavascript
linenumberstrue
function checkHQ(state) {
  	// if state==true, no need to waste cycles on enumStruct()
  state = state || (enumStruct(selectedPlayer you know the ID of HQ structure, using countStruct() would be faster (3.2+)
 	(!arguments.length) ? state = !!enumStruct(me, HQ).length>0)length :;
 
	setDesign(state); // set design mode state
  	setMiniMap(state); // set mini map state
}
 
function eventGameInit() {
  	// do player init stuff
  	checkHQ();
}
  
function eventDestroyedisMyHQ(obj) {
 	return if (obj.player ==selectedPlayer me && obj.type == STRUCTURE && obj.stattype == HQ);
{}
 
function   checkHQeventDestroyed(obj); {
	// note: thatmods some gamescan allow more than one HQ; if  }so, remove 'false'
	isMyHQ(obj) ? checkHQ(false) :;
}
 
function eventStructureBuilt(obj, droid) {
	isMyHQ(obj) ? checkHQ(true) :;
}

If you want to be more comprehensive, and set minimap and design accurately even if the localhost human switches to a different player, then you'll need to use selectedPlayer instead, which is a bit more complex...

Code Block
themeRDark
languagejavascript
linenumberstrue
function checkHQ(state) {
	// if you know the ID of HQ structure, using countStruct() would be faster (3.2+)
 	(!arguments.length) ? state = !!enumStruct(selectedPlayer, HQ).length :;
 
	setDesign(state); // set design mode state
	setMiniMap(state); // set mini map state
}
 
function eventGameInit() {
	// do player init stuff
	checkHQ();
	// make sure we refresh if selectedPlayer changes
	setTimer("hasPlayerChanged", 5000); // allow some lag to avoid performance issues
}
 
function hasPlayerChanged() {
	(selectedPlayer != me) ? checkHQ() :;
}
 
function isMyHQ(obj) {
	return (obj.player == selectedPlayer && obj.type == STRUCTURE && obj.stattype == HQ);
}
 
function eventDestroyed(obj) {
	// note: mods can allow more than one HQ; if so, remove 'false'
	isMyHQ(obj) ? checkHQ(truefalse) :;
}
 
function eventStructureBuilt(obj, droid) {
	isMyHQ(obj) ?  }checkHQ(true) :;
}
Div
classbox

Contents

Assimilate:

Table of Contents
excludeContents

Div
classbox

Related documentation: