Advertisement
JademusSreg

Damage Tracker

Feb 4th, 2012
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.63 KB | None | 0 0
  1. //================================================================
  2. // DAMAGE TRACKER
  3. // by JademusSreg
  4. //----------------------------------------------------------------
  5. // Requires: Time (by grim001)
  6. // Debug utility to track the damage output of players, unit types
  7. // and effect types and outputs the stats to a logfile, to assist
  8. // in balancing gameplay content.
  9. //----------------------------------------------------------------
  10. // API
  11. //
  12. // void DmgTrack_Start ()
  13. //     Enables the damage event trigger and stores the time at
  14. //     which the tracking begins.
  15. //
  16. // void DmgTrack_Output ()
  17. //     Outputs the damage stats to a logfile defined inside the
  18. //     initialization function DmgTrack_Init().
  19. //
  20. // void DmgTrack_Clear ()
  21. //     Deletes all the DmgTrack entries from the data table.
  22. //
  23. // void DmgTrack_Finished ()
  24. //     Disables the damage event trigger, calls DmgTrack_Output()
  25. //     to log the stats, then calls DmgTrack_Clear to empty the
  26. //     data table.
  27. //
  28. //================================================================
  29. // Data
  30. struct DmgTrackData
  31. {
  32.     string tableKey;
  33.     int debugType;
  34.     int precision;
  35.     // Text
  36.     text entryToken;
  37.     text valueToken;
  38.     text dpgsToken;
  39.     text dprsToken;
  40.     text elapsedGameTime;
  41.     text elapsedRealTime;
  42.     text damageStats;
  43.     // Triggers
  44.     trigger damageTrigger;
  45.     trigger chatTrigger;
  46.     // Counters
  47.     fixed gameStart;
  48.     fixed realStart;
  49. };
  50. DmgTrackData DmgTrack;
  51. //================================================================
  52. // Functions
  53. void DmgTrack_Start ()
  54. {
  55.     TriggerEnable(DmgTrack.damageTrigger,true);
  56.     DmgTrack.gameStart = GetTime(c_timeGame);
  57.     DmgTrack.realStart = GetTime(c_timeReal);
  58. }
  59. void DmgTrack_Output ()
  60. {
  61.     // Local variables
  62.     fixed elapsedGameTime = GetTime(c_timeGame)-DmgTrack.gameStart;
  63.     fixed elapsedRealTime = GetTime(c_timeReal)-DmgTrack.realStart;
  64.     int count = DataTableValueCount(true);
  65.     string key;
  66.     fixed value;
  67.     text message;
  68.     // Output elapsed game and real time.
  69.     TriggerDebugOutput(DmgTrack.debugType,TextReplaceWord(DmgTrack.elapsedGameTime,DmgTrack.valueToken,FixedToText(elapsedGameTime,DmgTrack.precision),1,true),false);
  70.     TriggerDebugOutput(DmgTrack.debugType,TextReplaceWord(DmgTrack.elapsedRealTime,DmgTrack.valueToken,FixedToText(elapsedRealTime,DmgTrack.precision),1,true),false);
  71.     // Iterate through the entire goddamn data table.
  72.     while (count > 0)
  73.     {
  74.         // If the first word of the entry name doesn't match the
  75.         // data table prefix defined in DmgTrack.tableKey, skip
  76.         // the entry.
  77.         key = DataTableValueName(true,count);
  78.         if ((StringWord(key,1)+" ") == DmgTrack.tableKey)
  79.         {
  80.             // Get the value before altering the key.
  81.             value = DataTableGetFixed(true,key);
  82.             // Replace the DmgTrack prefix from the key.
  83.             key = StringReplaceWord(key,DmgTrack.tableKey,null,1,true);
  84.             // Replace the entry token with the key.
  85.             message = TextReplaceWord(DmgTrack.damageStats,DmgTrack.entryToken,StringToText(key),1,true);
  86.             // Replace the value token with the value.
  87.             message = TextReplaceWord(message,DmgTrack.valueToken,FixedToText(value,DmgTrack.precision),1,true);
  88.             // Replace the dpgs token with damage per game second value.
  89.             message = TextReplaceWord(message,DmgTrack.dpgsToken,FixedToText((value/elapsedGameTime),DmgTrack.precision),1,true);
  90.             // Replace the dprs token with damage per real second value.
  91.             message = TextReplaceWord(message,DmgTrack.dprsToken,FixedToText((value/elapsedRealTime),DmgTrack.precision),1,true);
  92.             // Output the message to the logfile.
  93.             TriggerDebugOutput(DmgTrack.debugType,message,false);
  94.         }
  95.         count-=1;
  96.     }
  97. }
  98. void DmgTrack_Clear ()
  99. {
  100.     // Local variables
  101.     int count = DataTableValueCount(true);
  102.     string key;
  103.     // Iterate through the entire goddamn data table.
  104.     while (count > 0)
  105.     {
  106.         // If the first word of the entry name doesn't match the
  107.         // data table prefix defined in DmgTrack.tableKey, skip
  108.         // the entry.
  109.         key = DataTableValueName(true,count);
  110.         if ((StringWord(key,1)+" ") == DmgTrack.tableKey)
  111.         {
  112.             // Remove the entry from the data table.
  113.             DataTableValueRemove(true,key);
  114.         }
  115.         count-=1;
  116.     }
  117. }
  118. void DmgTrack_Finish ()
  119. {
  120.     TriggerEnable(DmgTrack.damageTrigger,false);
  121.     DmgTrack_Output();
  122.     DmgTrack_Clear();
  123. }
  124. //================================================================
  125. // Actions
  126. bool DmgTrack_Damage_Action (bool testConditions, bool runActions)
  127. {
  128.     // Local variables
  129.     unit source = EventUnitDamageSourceUnit();
  130.     int player = UnitGetOwner(source);
  131.     string effect = EventUnitDamageEffect();
  132.     fixed damage = EventUnitDamageAmount();
  133.     string playerKey;
  134.     string unitKey;
  135.     string effectKey;
  136.     // If player is Player 16, use a semantic nameKey. Else, use
  137.     // the player int to concatenate the key.
  138.     if (player == c_maxPlayers) { playerKey = DmgTrack.tableKey+"Null Player"; }
  139.     else { playerKey = DmgTrack.tableKey+"Player "+IntToString(player); }
  140.     // If source is null, use a semantic unitKey. Else, use the
  141.     // unit type to concatenate the key.
  142.     if (source == null) { unitKey = playerKey+" : "+"Null Unit"; }
  143.     else { unitKey = playerKey+" : "+UnitGetType(source); }
  144.     // If effect is null, use a semantic effectKey. Else, use the
  145.     // effect type to concatenate the key.
  146.     if (effect == null) { effectKey = unitKey+" : "+"Null Effect"; }
  147.     else { effectKey = unitKey+" : "+effect; }
  148.     // Store damage for the player in the data table.
  149.     if (DataTableValueExists(true,playerKey)) { DataTableSetFixed(true,playerKey,DataTableGetFixed(true,playerKey)+damage); }
  150.     else { DataTableSetFixed(true,playerKey,damage); }
  151.     // Store damage for the player : unit type in the data table.
  152.     if (DataTableValueExists(true,unitKey)) { DataTableSetFixed(true,unitKey,DataTableGetFixed(true,unitKey)+damage); }
  153.     else { DataTableSetFixed(true,unitKey,damage); }
  154.     // Store damage for the player : unit type : effect type in the data table.
  155.     if (DataTableValueExists(true,effectKey)) { DataTableSetFixed(true,effectKey,DataTableGetFixed(true,effectKey)+damage); }
  156.     else { DataTableSetFixed(true,effectKey,damage); }
  157.     return true;
  158. }
  159. bool DmgTrack_Chat_Action (bool testConditions, bool runActions)
  160. {
  161.     string command = StringCase(EventChatMessage(false),false);
  162.     if ((command == "~dmgtrack start") && !TriggerIsEnabled(DmgTrack.damageTrigger)) { DmgTrack_Start(); }
  163.     else if ((command == "~dmgtrack output") && TriggerIsEnabled(DmgTrack.damageTrigger)) { DmgTrack_Output(); }
  164.     else if ((command == "~dmgtrack stop") && TriggerIsEnabled(DmgTrack.damageTrigger)) { DmgTrack_Finish(); }
  165.     return true;
  166. }
  167. //================================================================
  168. // Initialization
  169. void DmgTrack_Init ()
  170. {
  171.     // Define the key prefixed to DmgTrack data table entries.
  172.     DmgTrack.tableKey = "DmgTrack ";
  173.     // Configure debug type and output file.
  174.     DmgTrack.debugType = 4;
  175.     TriggerDebugSetTypeFile(DmgTrack.debugType,"DamageTrackerOutput.txt");
  176.     // Define the decimal precision for the output values.
  177.     DmgTrack.precision = 3;
  178.     // Define the text segments used to format output.
  179.     DmgTrack.entryToken = StringToText("%Entry%");
  180.     DmgTrack.valueToken = StringToText("%Value%");
  181.     DmgTrack.dpgsToken = StringToText("%DPGS%");
  182.     DmgTrack.dprsToken = StringToText("%DPRS%");
  183.     DmgTrack.elapsedGameTime = StringToText("Elapsed game time:  ")+DmgTrack.valueToken+StringToText(" seconds");
  184.     DmgTrack.elapsedRealTime = StringToText("Elapsed real time:  ")+DmgTrack.valueToken+StringToText(" seconds");
  185.     DmgTrack.damageStats = StringToText("[")+DmgTrack.entryToken+StringToText("] Total damage: ")+DmgTrack.valueToken+StringToText("  |  DPS (game): ")+DmgTrack.dpgsToken+StringToText("  |  DPS (real): ")+DmgTrack.dprsToken;
  186.     // Create and register the damage detection trigger.
  187.     DmgTrack.damageTrigger = TriggerCreate("DmgTrack_Damage_Action");
  188.     TriggerAddEventUnitDamaged(DmgTrack.damageTrigger,null,c_unitDamageTypeAny,c_unitDamageEither,null);
  189.     TriggerEnable(DmgTrack.damageTrigger,false);
  190.     // Create and register the chat command trigger.
  191.     DmgTrack.chatTrigger = TriggerCreate("DmgTrack_Chat_Action");
  192.     TriggerAddEventChatMessage(DmgTrack.chatTrigger,c_playerAny,null,false);
  193.     TriggerEnable(DmgTrack.chatTrigger,false); // Comment out this line to enable the chat commands by default.
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement