Skip to content

Commit

Permalink
Merge pull request #311 from DragonSlayer62/checktimesincelastcombat-…
Browse files Browse the repository at this point in the history
…function

CheckTimeSinceLastCombat
  • Loading branch information
Xoduz authored Jan 29, 2025
2 parents 05f757f + 1b515ed commit b96f3f3
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 1 deletion.
29 changes: 28 additions & 1 deletion docs/jsdocs.html
Original file line number Diff line number Diff line change
Expand Up @@ -5049,6 +5049,33 @@ <h4>January 9th, 2022</h4>
</div>
</div>

<div class="spoilerWrapper">
<input type="checkbox" id="spoiler_function_combat_CheckTimeSinceLastCombat" />
<label for="spoiler_function_combat_CheckTimeSinceLastCombat">CheckTimeSinceLastCombat <i class="fas fa-angle-down"></i></label>
<div class="spoiler">
<div class="settingsDiv">
<p><span class="hl">Prototype</span></p>
<p><em>CheckTimeSinceLastCombat( pUser, 120 );</em></p>
</div>
<div class="settingsDiv">
<p><span class="hl">Purpose</span></p>
<p>Checks if a character has engaged in combat within a specified time frame (in seconds).</p>
</div>
<div class="settingsDiv">
<p><span class="hl">Example of usage</span></p>
<pre><code class="language-javascript">// Check if the player has been in combat within the last 120 seconds (2 minutes)
if( CheckTimeSinceLastCombat( pUser, 120 ))
{
pUser.SysMessage( "You must wait two minutes after engaging in combat before you can use this item." );
}
else
{
pUser.SysMessage( "You are free to use the item." );
}</code></pre>
</div>
</div>
</div>

</div>
</div> <!-- cd-faq__content -->
</li>
Expand Down Expand Up @@ -17419,4 +17446,4 @@ <h4>Finalize Result Announcement</h4>
<script src="assets/js/main.js"></script>
<script src="assets/js/prism.js"></script>
</body>
</html>
</html>
3 changes: 3 additions & 0 deletions source/Changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
20/10/2024 - Dragon Slayer
Added SetRandomColor("#") to js functions. (Thanks, Xuri)

14/10/2024 - Dragon Slayer
     Added CheckTimeSinceLastCombat that records the time when combat starts as a timestamp(SEFunctions.cpp).

14/10/2024 - Dragon Slayer
    Added the CheckInstaLog JS function to allow checking if a specific location is within an instant logout zone (SEFunctions.cpp). (thanks, Xuri)

Expand Down
43 changes: 43 additions & 0 deletions source/SEFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,49 @@ JSBool SE_CalcMultiFromSer( JSContext *cx, [[maybe_unused]] JSObject *obj, uintN
return JS_TRUE;
}

//o------------------------------------------------------------------------------------------------o
//| Function - SE_CombatTimeCheck()
//o------------------------------------------------------------------------------------------------o
//| Purpose - Checks if a character has engaged in combat within a given time span
//o------------------------------------------------------------------------------------------------o
JSBool SE_CheckTimeSinceLastCombat( JSContext* cx, [[maybe_unused]] JSObject* obj, uintN argc, jsval* argv, jsval* rval )
{
if (argc != 2)
{
ScriptError( cx, "CheckTimeSinceLastCombat: Invalid number of parameters (2)" );
return JS_FALSE;
}

CChar* from = nullptr;
UI32 timespanInSeconds = 0;

if( !JSVAL_IS_OBJECT( argv[0] ) || !( from = ( CChar* )JS_GetPrivate( cx, JSVAL_TO_OBJECT( argv[0] ))))
{
ScriptError( cx, "CheckTimeSinceLastCombat: Invalid first argument (expected CChar)" );
return JS_FALSE;
}

if( !JSVAL_IS_INT( argv[1] ))
{
ScriptError( cx, "CheckTimeSinceLastCombat: Invalid second argument (expected time in seconds)" );
return JS_FALSE;
}

timespanInSeconds = static_cast<UI32>( JSVAL_TO_INT( argv[1] ));

// Use GetGUITimerCurTime() instead of time(nullptr) for consistency
UI32 now = cwmWorldState->GetUICurrentTime();

if(( now - from->GetLastCombatTime() ) < timespanInSeconds )
{
*rval = JSVAL_TRUE;
return JS_TRUE;
}

*rval = JSVAL_FALSE;
return JS_TRUE;
}

//o------------------------------------------------------------------------------------------------o
//| Function - SE_CalcCharFromSer()
//o------------------------------------------------------------------------------------------------o
Expand Down
1 change: 1 addition & 0 deletions source/SEFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ SEngineFunc SE_CalcCharFromSer; // ***
SEngineFunc SE_CalcItemFromSer; // ***
SEngineFunc SE_CalcMultiFromSer; // ***

SEngineFunc SE_CheckTimeSinceLastCombat;
SEngineFunc SE_CheckInstaLog;

SEngineFunc SE_MakeItem; // ***
Expand Down
15 changes: 15 additions & 0 deletions source/cChar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5950,6 +5950,21 @@ void CChar::LastMoveTime( UI32 newValue )
lastMoveTime = newValue;
}

//o------------------------------------------------------------------------------------------------o
//| Function - CChar::GetLastCombatTime()
//| CChar::SetLastCombatTime()
//o------------------------------------------------------------------------------------------------o
//| Purpose - Gets/Sets timestamp for when player last combat
//o------------------------------------------------------------------------------------------------o
UI32 CChar::GetLastCombatTime() const
{
return lastCombatTime;
}
void CChar::SetLastCombatTime( UI32 newValue )
{
lastCombatTime = newValue;
}

//o------------------------------------------------------------------------------------------------o
//| Function - CChar::GetLastOn()
//| CChar::SetLastOn()
Expand Down
4 changes: 4 additions & 0 deletions source/cChar.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class CChar : public CBaseObject
UI08 PoisonStrength;
BodyType bodyType;
UI32 lastMoveTime; // Timestamp for when character moved last
UI32 lastCombatTime; // Timestamp for when character combat last
UI16 npcGuild; // ID of NPC guild character is in (0=no NPC guild)

SKILLVAL baseskill[ALLSKILLS]; // Base skills without stat modifiers
Expand Down Expand Up @@ -841,6 +842,9 @@ class CChar : public CBaseObject
UI32 LastMoveTime( void ) const;
void LastMoveTime( UI32 newValue );

UI32 GetLastCombatTime() const;
void SetLastCombatTime(UI32 newValue);


CChar * GetTrackingTarget( void ) const;
CChar * GetTrackingTargets( UI08 targetNum ) const;
Expand Down
1 change: 1 addition & 0 deletions source/cScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static JSFunctionSpec my_functions[] =
{ "CalcCharFromSer", SE_CalcCharFromSer, 1, 0, 0 },
{ "CalcItemFromSer", SE_CalcItemFromSer, 1, 0, 0 },
{ "CalcMultiFromSer", SE_CalcMultiFromSer, 1, 0, 0 },
{ "CheckTimeSinceLastCombat", SE_CheckTimeSinceLastCombat,2, 0, 0 },
{ "CheckInstaLog", SE_CheckInstaLog, 4, 0, 0 },
{ "GetHour", SE_GetHour, 0, 0, 0 },
{ "GetMinute", SE_GetMinute, 0, 0, 0 },
Expand Down
7 changes: 7 additions & 0 deletions source/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ bool CHandleCombat::StartAttack( CChar *cAttack, CChar *cTarget )
}
}
}
// Record the time when combat starts as a timestamp
cAttack->SetLastCombatTime( cwmWorldState->GetUICurrentTime() );
cTarget->SetLastCombatTime( cwmWorldState->GetUICurrentTime() );
return true;
}

Expand Down Expand Up @@ -580,6 +583,10 @@ void CHandleCombat::AttackTarget( CChar *cAttack, CChar *cTarget )
if( !StartAttack( cAttack, cTarget )) // Is the char allowed to initiate combat with the target?
return;

// Update last combat time for both the attacker and the target
cAttack->SetLastCombatTime( cwmWorldState->GetUICurrentTime() );
cTarget->SetLastCombatTime( cwmWorldState->GetUICurrentTime() );

if( cAttack->CheckAggressorFlag( cTarget->GetSerial() ))
{
// Send attacker message to all nearby players
Expand Down

0 comments on commit b96f3f3

Please sign in to comment.