/// This is where the magic happens; program your plug-in's core behavior here.

#include "game_hooks.h"
#include <graphics/graphics.h>
#include <SCBW/api.h>
#include "../psi_field.h"
#include <cstdio>


/*
/////// "target unit" HELPER functions ----------------
const u32 Func_Sub4EB900 = 0x004EB900;
bool function_004EB900(CUnit* unit, CUnit* target) {

	static Bool32 bPreResult;

	__asm {
		PUSHAD
		MOV EAX, target
		MOV ECX, unit
		CALL Func_Sub4EB900
		MOV bPreResult, EAX
		POPAD
	}

	return (bPreResult != 0);

}

;

const u32 Func__moveToTarget = 0x004EB980;
bool orderToMoveToTarget(CUnit* unit, CUnit* target) {

	static Bool32 bPreResult;
  
	__asm {
		PUSHAD
		MOV EAX, target
		MOV ECX, unit
		CALL Func__moveToTarget
		MOV bPreResult, EAX
		POPAD
	}

	return bPreResult != 0;

}
/////// ----------------------------
*/




namespace hooks {

/// This hook is called every frame; most of your plugin's logic goes here.
bool nextFrame() {

	if (!scbw::isGamePaused()) { //If the game is not paused

		scbw::setInGameLoopState(true); //Needed for scbw::random() to work
		graphics::resetAllGraphics();
		hooks::updatePsiFieldProviders();
    
		//This block is executed once every game.
		if (*elapsedTimeFrames == 0) {
			//Write your code here
			/////// off -------  scbw::printText(PLUGIN_NAME ": Hello, world!");
		}

		//Loop through all visible units in the game.
		for (CUnit *unit = *firstVisibleUnit; unit; unit = unit->link.next) {
			//Write your code here
			
			/////// OWN -------
			// Schleich-Modus (Siege Mode) Tarnung
			if(unit->id == UnitId::tom_kazansky) {
				if(unit->sprite->mainGraphic->animation == IscriptAnimation::SpecialState2) {
						//unit->status &= ~UnitStatus::CloakingForFree; //disable the flag if set
						unit->setSecondaryOrder(OrderId::Decloak);
				}
				if(unit->sprite->mainGraphic->animation == IscriptAnimation::Init) {
					//if(unit->sprite->mainGraphic->frameIndex == 360) {
						unit->status |= UnitStatus::CloakingForFree;
						unit->setSecondaryOrder(OrderId::Cloak);
					//}
				}
			}
			
			// Resotration Heal
			if(unit->mainOrderId == OrderId::Restoration) {
				if(unit->mainOrderState == 2) {
				   //CUnit* target = unit->orderTarget.unit;
				   //target->setHp(target->hitPoints + 2550);
				   unit->orderTarget.unit->setHp(unit->orderTarget.unit->hitPoints + 2550);
				}
		   }
			
			/////// -------
		}

		scbw::setInGameLoopState(false);
		
	}
  
	return true;
  
}

bool gameOn() {
	return true;
}

bool gameEnd() {
	return true;
}

} //hooks
