changeset 789:83a58d185d3c

stru176 & OnTimer
author Nomad
date Sun, 24 Mar 2013 23:15:33 +0200
parents b3e08a496778
children 1e2be0f5eb30
files mm7_3.cpp mm7_data.h stru176.h
diffstat 3 files changed, 81 insertions(+), 97 deletions(-) [+]
line wrap: on
line diff
--- a/mm7_3.cpp	Sun Mar 24 21:15:01 2013 +0200
+++ b/mm7_3.cpp	Sun Mar 24 23:15:33 2013 +0200
@@ -11973,8 +11973,8 @@
 
 			v6 = ((unsigned short)_evt->v12 << 8) + _evt->v11;
 
-			v3->field_C = ((unsigned short)_evt->v12 << 8) + _evt->v11;
-			v3->field_E = ((unsigned short)_evt->v12 << 8) + _evt->v11;
+			v3->time_left_to_fire = ((unsigned short)_evt->v12 << 8) + _evt->v11;
+            v3->fire_interval = ((unsigned short)_evt->v12 << 8) + _evt->v11;
 			if (v3->timer_evt_type  == EVENT_OnLongTimer && !(short)v6 )
 			{
 				if ( v20 )
@@ -11988,7 +11988,7 @@
 					if ( v3->field_10 )
 					{
 						++dword_5B65C8_timers_count;
-						v3->field_0_time = 0;
+                        v3->next_fire_time = 0;
 						continue;
 					}
 				}
@@ -11997,7 +11997,7 @@
 					v26 != 0 || !v20)
 				{
 					++dword_5B65C8_timers_count;
-					v3->field_0_time = 0;
+					v3->next_fire_time = 0;
 					continue;
 				}
 			}
@@ -12052,7 +12052,7 @@
 						}
 					}
 				}
-				v3->field_0_time = (signed __int64)((double)((seconds
+                v3->next_fire_time = (signed __int64)((double)((seconds
 					+ 60 * minutes
 					+ 3600 * hours
 					+ 0x93A80 * weeks
@@ -13522,95 +13522,79 @@
 }
 
 //----- (00448B67) --------------------------------------------------------
-void __thiscall OnTimer(int a1)
-{
-  int v1; // ebp@1
-  unsigned int v2; // ebx@4
-  unsigned int v3; // edi@4
-  char *v4; // esi@6
-  __int16 v5; // ax@8
-  int v6; // ecx@10
-  unsigned int v7; // ebp@12
-  int v8; // eax@16
-  int v9; // eax@19
-  unsigned __int64 v10; // qax@20
-  int v11; // ecx@24
-  int v12; // [sp+Ch] [bp-10h]@5
+void OnTimer(int a1)
+{
+  //int v1; // ebp@1
+  //unsigned int v2; // ebx@4
+  //unsigned int v3; // edi@4
+  //char *v4; // esi@6
+  //__int16 v5; // ax@8
+  //int v6; // ecx@10
+  //unsigned int v7; // ebp@12
+  //int v8; // eax@16
+  //int v9; // eax@19
+  //unsigned __int64 v10; // qax@20
+  //int v11; // ecx@24
+  //int v12; // [sp+Ch] [bp-10h]@5
   unsigned int v13; // [sp+10h] [bp-Ch]@4
-
-  v1 = 0;
-  if ( !pEventTimer->bPaused )
-  {
-    if ( a1 )
-      dword_5773C0 = 0;
-    v3 = HIDWORD(pParty->uTimePlayed);
-    v2 = LODWORD(pParty->uTimePlayed);
-    v13 = (signed __int64)(pParty->uTimePlayed - _5773B8_event_timer) / 128;
-    if ( v13 )
-    {
-      _5773B8_event_timer = pParty->uTimePlayed;
-      v12 = 0;
-      if ( dword_5B65C8_timers_count > 0 )
-      {
-        v4 = (char *)&array_5B5928_timers[0].field_C;
-        while ( 1 )
-        {
-          v5 = *(short *)v4;
-          if ( *(short *)v4 != (short)v1 )
-            break;
-          v7 = *((int *)v4 - 2);
-          if ( v7 <= v3 && (v7 < v3 || *((int *)v4 - 3) <= v2) )
-          {
-            if ( *((short *)v4 + 2) )
-            {
-              v8 = 29030400;
-            }
-            else
-            {
-              if ( *((short *)v4 + 3) )
-              {
-                v8 = 2419200;
-              }
-              else
-              {
-                v9 = -(*((short *)v4 + 4) != 0);
-                v8 = (v9 & 0x7E900) + 86400;
-              }
-            }
-            v10 = v7 + *((int *)v4 - 3) + (signed __int64)((double)((signed __int64)v8 << 7) * 0.033333335);//v10 = __PAIR__(v7, *((int *)v4 - 3)) + (signed __int64)((double)((signed __int64)v8 << 7) * 0.033333335);
-            *((int *)v4 - 3) = v10;
-            *((int *)v4 - 2) = HIDWORD(v10);
-            if ( HIDWORD(v10) <= v3 && (HIDWORD(v10) < v3 || *((int *)v4 - 3) < v2) )
-            {
-              *((int *)v4 - 3) = v2;
-              *((int *)v4 - 2) = v3;
-            }
-            v11 = *((short *)v4 - 2);
-            start_event_seq_number = *((short *)v4 - 1);
-            EventProcessor(v11, 0, 1);
-            start_event_seq_number = 0;
-LABEL_25:
-            v3 = HIDWORD(pParty->uTimePlayed);
-            v2 = LODWORD(pParty->uTimePlayed);
-          }
-LABEL_26:
-          ++v12;
-          v4 += 32;
-          if ( v12 >= dword_5B65C8_timers_count )
-            return;
-          v1 = 0;
-        }
-        if ( v13 < v5 )
-        {
-          *(short *)v4 = v5 - v13;
-          goto LABEL_26;
-        }
-        v6 = *((short *)v4 - 2);
-        *(short *)v4 = *((short *)v4 + 1);
-        start_event_seq_number = *((short *)v4 - 1);
-        EventProcessor(v6, 0, 1);
-        start_event_seq_number = v1;
-        goto LABEL_25;
+  
+  if (pEventTimer->bPaused)
+    return;
+  
+  if (a1)
+    dword_5773C0 = 0;
+  
+  v13 = (signed __int64)(pParty->uTimePlayed - _5773B8_event_timer) / 128;
+  if (!v13)
+    return;
+
+  //uint _v2v3 = pParty->uTimePlayed;
+  //v3 = HIDWORD(pParty->uTimePlayed);
+  //v2 = LODWORD(pParty->uTimePlayed);
+
+  _5773B8_event_timer = pParty->uTimePlayed;
+
+  for (uint i = 0; i < dword_5B65C8_timers_count; ++i)
+  {
+        //v4 = (char *)&array_5B5928_timers[0].field_C;
+    auto timer = array_5B5928_timers + i;
+        //while ( 1 )
+        //{
+          //v5 = *(short *)v4;
+    if (timer->time_left_to_fire)
+    {
+      if (v13 < timer->time_left_to_fire)
+        timer->time_left_to_fire -= v13;
+      else
+      {
+        timer->time_left_to_fire = timer->fire_interval;
+
+        start_event_seq_number = timer->timer_evt_seq_num;
+        EventProcessor(timer->timer_evt_ID, 0, 1);
+
+        start_event_seq_number = 0;
+      }
+    }
+    else
+    {
+      if (timer->next_fire_time < pParty->uTimePlayed)
+      {
+        uint next_trigger_time = 1 * 60 * 60 * 24; // 1 day
+        if (timer->field_10)
+          next_trigger_time = 336 * 60 * 60 * 24; // 1 year
+        else if (timer->field_12)
+          next_trigger_time = 28 * 60 * 60 * 24; // 1 month
+        else if (timer->field_14)
+          next_trigger_time = 7 * 60 * 60 * 24; // 1 week
+
+        timer->next_fire_time += (next_trigger_time * 128) / 3.0f;
+        if (timer->next_fire_time < pParty->uTimePlayed) // make sure in wont fire several times in a row is big time interval has lapsed
+          timer->next_fire_time = pParty->uTimePlayed;
+
+        start_event_seq_number = timer->timer_evt_seq_num;
+        EventProcessor(timer->timer_evt_ID, 0, 1);
+
+        start_event_seq_number = 0;
       }
     }
   }
--- a/mm7_data.h	Sun Mar 24 21:15:01 2013 +0200
+++ b/mm7_data.h	Sun Mar 24 23:15:33 2013 +0200
@@ -1857,7 +1857,7 @@
 void __fastcall sub_44892E_set_faces_bit(int sCogNumber, int bit, int on);
 void __fastcall ToggleActorGroupFlag(unsigned int uGroupID, unsigned int uFlag, unsigned int bToggle);
 void __thiscall GameUI_StatusBar_UpdateTimedString(unsigned int bForceHide); // idb
-void __thiscall OnTimer(int a1);
+void OnTimer(int a1);
 void __fastcall sub_448CF4_spawn_monsters(__int16 typeindex, __int16 level, int count, int x, int y, int z, int group, unsigned int uUniqueName);
 void __fastcall sub_448DF8_cast_spell(int spellnum, int rank, int level, int fromx, int fromy, int fromz, int tox, int toy, int toz);
 __int16 __fastcall sub_449A49_door_switch_animation(unsigned int uDoorID, int a2); // idb
--- a/stru176.h	Sun Mar 24 21:15:01 2013 +0200
+++ b/stru176.h	Sun Mar 24 23:15:33 2013 +0200
@@ -5,11 +5,11 @@
 #pragma pack(push, 1)
 struct stru176
 {
-  signed __int64 field_0_time;
+  __int64 next_fire_time;      // timer will either fire event at this time (type 2, field_C == 0)
   __int16 timer_evt_ID;
   __int16 timer_evt_seq_num;
-  __int16 field_C;
-  __int16 field_E;
+  __int16 time_left_to_fire;
+  __int16 fire_interval;       // or fire on these intervals (type 1)
   __int16 field_10;
   __int16 field_12;
   __int16 field_14;