Skip to content

Commit

Permalink
Prepend all function with GFA_ and feature tags
Browse files Browse the repository at this point in the history
  • Loading branch information
szapp committed Aug 8, 2017
1 parent 2caf9a8 commit a888774
Show file tree
Hide file tree
Showing 29 changed files with 412 additions and 366 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ manually.
const int MENU_ID_GFA = 7; // Next available Y-spot in the menu
```

7. Finally initialize free aim by adding the line `freeAim_Init();` in to the function `INIT_GLOBAL()` in
7. Finally initialize free aim by adding the line `GFA_Init();` in to the function `INIT_GLOBAL()` in
`_work\data\Scripts\Content\Story\Startup.d` somewhere after Ikarus and LeGo.

> Again: The [setup](http://github.com/szapp/g2freeAim/releases/latest) will perform all these steps for you.
Expand Down
64 changes: 32 additions & 32 deletions _work/data/Scripts/Content/freeAim/_intern/activate.d
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
/*
* Update internal settings when turning free aim on/off in the options menu. Settings include focus ranges/angles,
* camera angles along with resetting some other settings. The constant GFA_ACTIVE will be updated accordingly.
* This function is called from freeAimIsActive() nearly every frame.
* This function is called from GFA_IsActive() nearly every frame.
*/
func void freeAimUpdateSettings(var int on) {
func void GFA_UpdateSettings(var int on) {
if ((GFA_ACTIVE > 0) == on) {
return; // No change necessary
};
Expand Down Expand Up @@ -62,7 +62,7 @@ func void freeAimUpdateSettings(var int on) {
MEM_WriteString(zString_CamModMagic, "CAMMODMAGIC");

// Reset to collision behavior of projectiles on NPCs to default (does not affect Gothic 1)
freeAimSetCollisionWithNPC(0);
GFA_CC_SetProjectileCollisionWithNpc(0);

};
GFA_ACTIVE = !GFA_ACTIVE;
Expand All @@ -73,59 +73,59 @@ func void freeAimUpdateSettings(var int on) {
* This function updates the settings when free aiming or the Gothic 2 controls are enabled or disabled. It is called
* every time when the Gothic settings are updated (after leaving the game menu), as well as during loading and level
* changes. The function hooks cGameManager::ApplySomeSettings() at the very end (after all other menu settings!).
* The constant GFA_ACTIVE is modified in the subsequent function freeAimUpdateSettings().
* The constant GFA_ACTIVE is modified in the subsequent function GFA_UpdateSettings().
*/
func void freeAimUpdateStatus() {
func void GFA_UpdateStatus() {
// Check if g2freeAim is enabled and mouse controls are enabled
if (!STR_ToInt(MEM_GetGothOpt("FREEAIM", "enabled"))) || (!MEM_ReadInt(zCInput_Win32__s_mouseEnabled)) {
// Disable if previously enabled
freeAimUpdateSettings(0);
freeAimDisableAutoTurn(0);
freeAimSetCameraMode_G1(0);
freeAimUpdateSettingsG2Ctrl(0);
GFA_UpdateSettings(0);
GFA_DisableAutoTurning(0);
GFA_SetCameraModes(0);
GFA_UpdateSettingsG2Ctrl(0);

} else {
// Enable if previously disabled
freeAimUpdateSettings(1);
GFA_UpdateSettings(1);

if (GOTHIC_BASE_VERSION == 2) {
freeAimUpdateSettingsG2Ctrl(!MEM_ReadInt(oCGame__s_bUseOldControls)); // G1 controls = 0, G2 controls = 1
GFA_UpdateSettingsG2Ctrl(!MEM_ReadInt(oCGame__s_bUseOldControls)); // G1 controls = 0, G2 controls = 1
};
};
};


/*
* This function is called nearly every frame by freeAimManualRotation(), providing the mouse is enabled, to check
* This function is called nearly every frame by GFA_TurnPlayerModel(), providing the mouse is enabled, to check
* whether free aiming is active (player in either magic or ranged combat). It sets the constant GFA_ACTIVE
* accordingly:
* 1 if not active (not currently aiming)
* 5 if currently aiming in ranged fight mode (FMODE_FAR)
* 7 if currently aiming in magic fight mode with free aiming supported spell (FMODE_MAGIC)
*
* GFA_ACTIVE is prior set to 0 in freeAimUpdateStatus() if free aiming is disabled.
* GFA_ACTIVE is prior set to 0 in GFA_UpdateStatus() if free aiming is disabled.
*
* Different checks are performed in performance-favoring order (exiting the function as early as possible) to set the
* constant, which is subsequently used in a lot of functions to determine the state of free aiming.
*/
func void freeAimIsActive() {
func void GFA_IsActive() {
if (!GFA_ACTIVE) {
return;
};

// Check if currently in a menu or in a dialog
if (MEM_Game.pause_screen) || (!InfoManager_HasFinished()) {
freeAimSetCameraMode_G1(0);
freeAimDisableAutoTurn(0);
GFA_SetCameraModes(0);
GFA_DisableAutoTurning(0);
GFA_ACTIVE = 1;
return;
};

// Before anything else, check if player is in magic or ranged fight mode
var oCNpc her; her = Hlp_GetNpc(hero);
if (her.fmode < FMODE_FAR) {
freeAimSetCameraMode_G1(0);
freeAimDisableAutoTurn(0);
GFA_SetCameraModes(0);
GFA_DisableAutoTurning(0);
GFA_ACTIVE = 1;
return;
};
Expand Down Expand Up @@ -157,56 +157,56 @@ func void freeAimIsActive() {
if (her.fmode == FMODE_MAGIC) {
// Check if free aiming for spells is disabled
if (!GFA_SPELLS) {
freeAimDisableAutoTurn(0);
freeAimSetCameraMode_G1(0);
GFA_DisableAutoTurning(0);
GFA_SetCameraModes(0);
GFA_ACTIVE = 1;
return;
};

// Gothic 1 does not differentiate between camera modes. Force/overwrite all to free aiming mode
freeAimSetCameraMode_G1(1);
GFA_SetCameraModes(1);

// Gothic 1 controls require action key to be pressed/held
if (GOTHIC_BASE_VERSION == 2) {
if (MEM_ReadInt(oCGame__s_bUseOldControls)) && (!keyPressed) {
freeAimDisableAutoTurn(0);
GFA_DisableAutoTurning(0);
GFA_ACTIVE = 1;
return;
};
} else if (!keyPressed) {
freeAimDisableAutoTurn(0);
GFA_DisableAutoTurning(0);
GFA_ACTIVE = 1;
return;
};

// Check if active spell supports free aiming
var C_Spell spell; spell = freeAimGetActiveSpellInst(hero);
if (!freeAimSpellEligible(spell)) {
var C_Spell spell; spell = GFA_GetActiveSpellInst(hero);
if (!GFA_IsSpellEligible(spell)) {
// Reset ranged focus collection
Focus_Magic.npc_azi = 45.0;
Focus_Magic.item_prio = -1;
freeAimDisableAutoTurn(0);
GFA_DisableAutoTurning(0);
GFA_ACTIVE = 1;
return;
} else {
// Spell uses free aiming: Set stricter focus collection
Focus_Magic.npc_azi = 15.0;
Focus_Magic.item_prio = 0;
freeAimDisableAutoTurn(1);
GFA_DisableAutoTurning(1);
GFA_ACTIVE = FMODE_MAGIC;
};

} else if (her.fmode >= FMODE_FAR) { // Greater or equal: Crossbow has different fight mode!
// Check if free aiming for ranged combat is disabled
if (!GFA_RANGED) {
freeAimDisableAutoTurn(0);
freeAimSetCameraMode_G1(0);
GFA_DisableAutoTurning(0);
GFA_SetCameraModes(0);
GFA_ACTIVE = 1;
return;
};

// Gothic 1 does not differentiate between camera modes. Force/overwrite all to free aiming mode
freeAimSetCameraMode_G1(1);
GFA_SetCameraModes(1);

if (GOTHIC_BASE_VERSION == 2) {
// Set internally whether the aiming key is held or not (only if using Gothic 2 controls)
Expand All @@ -217,11 +217,11 @@ func void freeAimIsActive() {

// Check if aiming key is not pressed/held
if (!keyPressed) {
freeAimDisableAutoTurn(0);
GFA_DisableAutoTurning(0);
GFA_ACTIVE = 1;
return;
} else {
freeAimDisableAutoTurn(1);
GFA_DisableAutoTurning(1);
GFA_ACTIVE = FMODE_FAR; // Do not differentiate between bow and crossbow
};

Expand Down
2 changes: 1 addition & 1 deletion _work/data/Scripts/Content/freeAim/_intern/aimRay.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* the interval in milliseconds in which the trace ray is recomputed. The upper bound is 500ms, which already introduces
* a slight lag in the focus collection and reticle size (if applicable). A recommended value is below 50ms.
*/
func int freeAimRay(var int distance, var int focusType, var int vobPtr, var int posPtr, var int distPtr,
func int GFA_AimRay(var int distance, var int focusType, var int vobPtr, var int posPtr, var int distPtr,
var int trueDistPtr) {

// Only run full trace ray machinery every so often (see GFA_AimRayInterval) to allow weaker machines to run this
Expand Down
75 changes: 40 additions & 35 deletions _work/data/Scripts/Content/freeAim/_intern/aimVob.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@


/*
* Attach a visual FX to the aim vob. This function is never used internally, but is useful for spells that visualize
* the aim vob. An example is the spell blink.
* Detach the visual FX from the aim vob. This function should go hand in hand with attaching a visual FX: If you attach
* an FX, you should make sure to remove the FX, when it is no longer needed. Some precautions are already taken from
* the side of g2freeAim and this function is called on every weapon change, specifically in GFA_CleanUpAiming().
*/
func void freeAimAttachFX(var string effectInst) {
func void GFA_AimVobDetachFX() {
if (!GFA_AimVobHasFX) {
// This check increases performance (at least for Gothic 1)
return;
};

// Retrieve vob by name
var int vobPtr; vobPtr = MEM_SearchVobByName("AIMVOB");
if (!vobPtr) {
Expand All @@ -36,73 +42,72 @@ func void freeAimAttachFX(var string effectInst) {
if (GOTHIC_BASE_VERSION == 1) {
// In Gothic 1 there are no item effects
var C_Item vob; vob = _^(vobPtr);
Wld_PlayEffect(effectInst, vob, vob, 0, 0, 0, FALSE);
Wld_StopEffect_Ext("", vob, 0, TRUE); // Remove all effects "from" the aim vob
Wld_StopEffect_Ext("", 0, vob, TRUE); // Remove all effects "to" the aim vob
} else {
// Much nicer in Gothic 2

// Set the FX property
MEM_WriteString(vobPtr+oCItem_effect_offset, effectInst);

// Start the FX
// Remove item FX immediately
const int call = 0;
if (CALL_Begin(call)) {
CALL__thiscall(_@(vobPtr), oCItem__InsertEffect);
CALL__thiscall(_@(vobPtr), oCItem__RemoveEffect);
call = CALL_End();
};

// Clear the FX property
MEM_WriteString(vobPtr+oCItem_effect_offset, "");
};

GFA_AimVobHasFX = 0;
};


/*
* Detach the visual FX from the aim vob. This function should go hand in hand with attaching a visual FX: If you attach
* an FX, you should make sure to remove the FX, when it is no longer needed. Some precautions are already taken from
* the side of g2freeAim and this function is called on every weapon change, specifically in freeAimManageReticle().
* Attach a visual FX to the aim vob. This function is never used internally, but is useful for spells that visualize
* the aim vob. An example is the spell blink.
*/
func void freeAimDetachFX() {
func void GFA_AimVobAttachFX(var string effectInst) {
// Retrieve vob by name
var int vobPtr; vobPtr = MEM_SearchVobByName("AIMVOB");
if (!vobPtr) {
return;
};

if (GFA_AimVobHasFX) {
GFA_AimVobDetachFX();
};

if (GOTHIC_BASE_VERSION == 1) {
// In Gothic 1 there are no item effects
var C_Item vob; vob = _^(vobPtr);
Wld_StopEffect_Ext("", vob, 0, TRUE); // Remove all effects "from" the aim vob
Wld_StopEffect_Ext("", 0, vob, TRUE); // Remove all effects "to" the aim vob
Wld_PlayEffect(effectInst, vob, vob, 0, 0, 0, FALSE);
} else {
// Much easier in Gothic 2
if (Hlp_StrCmp(MEM_ReadString(vobPtr+oCItem_effect_offset), "")) {
// If there is no FX, no action is necessary
return;
};
// Gothic 2: Set the oCItem FX property
MEM_WriteString(vobPtr+oCItem_effect_offset, effectInst);

// Remove FX immediately
// Start the FX
const int call = 0;
if (CALL_Begin(call)) {
CALL__thiscall(_@(vobPtr), oCItem__RemoveEffect);
CALL__thiscall(_@(vobPtr), oCItem__InsertEffect);
call = CALL_End();
};

// Clear the FX property
MEM_WriteString(vobPtr+oCItem_effect_offset, "");
};

GFA_AimVobHasFX = 1;
};


/*
* Manipulate the position of the aim vob (only for spells). This function is called from freeAimSetupAimVob() and only
* Manipulate the position of the aim vob (only for spells). This function is called from GFA_SetupAimVob() and only
* works for spells, as they might incorporate the aim vob into the spell's mechanics or visuals. An example is the
* spell blink, which shifts the aim vob away from walls. To do this, adjust the config: freeAimShiftAimVob().
* spell blink, which shifts the aim vob away from walls. To do this, adjust the config: GFA_ShiftAimVob().
*/
func void freeAimManipulateAimVobPos(var int posPtr) {
func void GFA_AimVobManipulatePos(var int posPtr) {
var int spell; spell = Npc_GetActiveSpell(hero);
if (spell == -1) {
return;
};

// Check whether aim vob should be shifted
var int shifted; shifted = freeAimShiftAimVob(spell);
var int shifted; shifted = GFA_ShiftAimVob(spell);

if (shifted) {
shifted = mkf(shifted); // Amount to shift the aim vob along the out vector of the camera
Expand All @@ -124,13 +129,13 @@ func void freeAimManipulateAimVobPos(var int posPtr) {
* the aim vob and to reposition it. Manipulating the aim vob from outside of free aiming SHOULD NOT BE DONE. This is
* an internal mechanic and it should not be touched.
*/
func int freeAimSetupAimVob(var int posPtr) {
func int GFA_SetupAimVob(var int posPtr) {
// Retrieve vob by name
var int vobPtr; vobPtr = MEM_SearchVobByName("AIMVOB");

// Create vob if it does not exit
if (!vobPtr) {
MEM_Info("freeAimSetupAimVob: Creating aim vob."); // Should be printed only once ever (each world)
MEM_Info("GFA_SetupAimVob: Creating aim vob."); // Should be printed only once ever (each world)

// This actually allocates the memory, so no need to care about freeing
CALL__cdecl(oCItem___CreateNewInstance);
Expand All @@ -155,8 +160,8 @@ func int freeAimSetupAimVob(var int posPtr) {
var oCNpc her; her = Hlp_GetNpc(hero);
MEM_CopyBytes(_@(her)+zCVob_trafoObjToWorld_offset, vobPtr+zCVob_trafoObjToWorld_offset, sizeof_zMAT4);

// Additionally shift the vob (for certain spells, adjust in freeAimShiftAimVob())
freeAimManipulateAimVobPos(posPtr);
// Additionally shift the vob (for certain spells, adjust in GFA_ShiftAimVob())
GFA_AimVobManipulatePos(posPtr);

// Reposition the vob
const int call = 0;
Expand Down

0 comments on commit a888774

Please sign in to comment.