Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //================================================================
- // DAMAGE TRACKER
- // by JademusSreg
- //----------------------------------------------------------------
- // Requires: Time (by grim001)
- // Debug utility to track the damage output of players, unit types
- // and effect types and outputs the stats to a logfile, to assist
- // in balancing gameplay content.
- //----------------------------------------------------------------
- // API
- //
- // void DmgTrack_Start ()
- // Enables the damage event trigger and stores the time at
- // which the tracking begins.
- //
- // void DmgTrack_Output ()
- // Outputs the damage stats to a logfile defined inside the
- // initialization function DmgTrack_Init().
- //
- // void DmgTrack_Clear ()
- // Deletes all the DmgTrack entries from the data table.
- //
- // void DmgTrack_Finished ()
- // Disables the damage event trigger, calls DmgTrack_Output()
- // to log the stats, then calls DmgTrack_Clear to empty the
- // data table.
- //
- //================================================================
- // Data
- struct DmgTrackData
- {
- string tableKey;
- int debugType;
- int precision;
- // Text
- text entryToken;
- text valueToken;
- text dpgsToken;
- text dprsToken;
- text elapsedGameTime;
- text elapsedRealTime;
- text damageStats;
- // Triggers
- trigger damageTrigger;
- trigger chatTrigger;
- // Counters
- fixed gameStart;
- fixed realStart;
- };
- DmgTrackData DmgTrack;
- //================================================================
- // Functions
- void DmgTrack_Start ()
- {
- TriggerEnable(DmgTrack.damageTrigger,true);
- DmgTrack.gameStart = GetTime(c_timeGame);
- DmgTrack.realStart = GetTime(c_timeReal);
- }
- void DmgTrack_Output ()
- {
- // Local variables
- fixed elapsedGameTime = GetTime(c_timeGame)-DmgTrack.gameStart;
- fixed elapsedRealTime = GetTime(c_timeReal)-DmgTrack.realStart;
- int count = DataTableValueCount(true);
- string key;
- fixed value;
- text message;
- // Output elapsed game and real time.
- TriggerDebugOutput(DmgTrack.debugType,TextReplaceWord(DmgTrack.elapsedGameTime,DmgTrack.valueToken,FixedToText(elapsedGameTime,DmgTrack.precision),1,true),false);
- TriggerDebugOutput(DmgTrack.debugType,TextReplaceWord(DmgTrack.elapsedRealTime,DmgTrack.valueToken,FixedToText(elapsedRealTime,DmgTrack.precision),1,true),false);
- // Iterate through the entire goddamn data table.
- while (count > 0)
- {
- // If the first word of the entry name doesn't match the
- // data table prefix defined in DmgTrack.tableKey, skip
- // the entry.
- key = DataTableValueName(true,count);
- if ((StringWord(key,1)+" ") == DmgTrack.tableKey)
- {
- // Get the value before altering the key.
- value = DataTableGetFixed(true,key);
- // Replace the DmgTrack prefix from the key.
- key = StringReplaceWord(key,DmgTrack.tableKey,null,1,true);
- // Replace the entry token with the key.
- message = TextReplaceWord(DmgTrack.damageStats,DmgTrack.entryToken,StringToText(key),1,true);
- // Replace the value token with the value.
- message = TextReplaceWord(message,DmgTrack.valueToken,FixedToText(value,DmgTrack.precision),1,true);
- // Replace the dpgs token with damage per game second value.
- message = TextReplaceWord(message,DmgTrack.dpgsToken,FixedToText((value/elapsedGameTime),DmgTrack.precision),1,true);
- // Replace the dprs token with damage per real second value.
- message = TextReplaceWord(message,DmgTrack.dprsToken,FixedToText((value/elapsedRealTime),DmgTrack.precision),1,true);
- // Output the message to the logfile.
- TriggerDebugOutput(DmgTrack.debugType,message,false);
- }
- count-=1;
- }
- }
- void DmgTrack_Clear ()
- {
- // Local variables
- int count = DataTableValueCount(true);
- string key;
- // Iterate through the entire goddamn data table.
- while (count > 0)
- {
- // If the first word of the entry name doesn't match the
- // data table prefix defined in DmgTrack.tableKey, skip
- // the entry.
- key = DataTableValueName(true,count);
- if ((StringWord(key,1)+" ") == DmgTrack.tableKey)
- {
- // Remove the entry from the data table.
- DataTableValueRemove(true,key);
- }
- count-=1;
- }
- }
- void DmgTrack_Finish ()
- {
- TriggerEnable(DmgTrack.damageTrigger,false);
- DmgTrack_Output();
- DmgTrack_Clear();
- }
- //================================================================
- // Actions
- bool DmgTrack_Damage_Action (bool testConditions, bool runActions)
- {
- // Local variables
- unit source = EventUnitDamageSourceUnit();
- int player = UnitGetOwner(source);
- string effect = EventUnitDamageEffect();
- fixed damage = EventUnitDamageAmount();
- string playerKey;
- string unitKey;
- string effectKey;
- // If player is Player 16, use a semantic nameKey. Else, use
- // the player int to concatenate the key.
- if (player == c_maxPlayers) { playerKey = DmgTrack.tableKey+"Null Player"; }
- else { playerKey = DmgTrack.tableKey+"Player "+IntToString(player); }
- // If source is null, use a semantic unitKey. Else, use the
- // unit type to concatenate the key.
- if (source == null) { unitKey = playerKey+" : "+"Null Unit"; }
- else { unitKey = playerKey+" : "+UnitGetType(source); }
- // If effect is null, use a semantic effectKey. Else, use the
- // effect type to concatenate the key.
- if (effect == null) { effectKey = unitKey+" : "+"Null Effect"; }
- else { effectKey = unitKey+" : "+effect; }
- // Store damage for the player in the data table.
- if (DataTableValueExists(true,playerKey)) { DataTableSetFixed(true,playerKey,DataTableGetFixed(true,playerKey)+damage); }
- else { DataTableSetFixed(true,playerKey,damage); }
- // Store damage for the player : unit type in the data table.
- if (DataTableValueExists(true,unitKey)) { DataTableSetFixed(true,unitKey,DataTableGetFixed(true,unitKey)+damage); }
- else { DataTableSetFixed(true,unitKey,damage); }
- // Store damage for the player : unit type : effect type in the data table.
- if (DataTableValueExists(true,effectKey)) { DataTableSetFixed(true,effectKey,DataTableGetFixed(true,effectKey)+damage); }
- else { DataTableSetFixed(true,effectKey,damage); }
- return true;
- }
- bool DmgTrack_Chat_Action (bool testConditions, bool runActions)
- {
- string command = StringCase(EventChatMessage(false),false);
- if ((command == "~dmgtrack start") && !TriggerIsEnabled(DmgTrack.damageTrigger)) { DmgTrack_Start(); }
- else if ((command == "~dmgtrack output") && TriggerIsEnabled(DmgTrack.damageTrigger)) { DmgTrack_Output(); }
- else if ((command == "~dmgtrack stop") && TriggerIsEnabled(DmgTrack.damageTrigger)) { DmgTrack_Finish(); }
- return true;
- }
- //================================================================
- // Initialization
- void DmgTrack_Init ()
- {
- // Define the key prefixed to DmgTrack data table entries.
- DmgTrack.tableKey = "DmgTrack ";
- // Configure debug type and output file.
- DmgTrack.debugType = 4;
- TriggerDebugSetTypeFile(DmgTrack.debugType,"DamageTrackerOutput.txt");
- // Define the decimal precision for the output values.
- DmgTrack.precision = 3;
- // Define the text segments used to format output.
- DmgTrack.entryToken = StringToText("%Entry%");
- DmgTrack.valueToken = StringToText("%Value%");
- DmgTrack.dpgsToken = StringToText("%DPGS%");
- DmgTrack.dprsToken = StringToText("%DPRS%");
- DmgTrack.elapsedGameTime = StringToText("Elapsed game time: ")+DmgTrack.valueToken+StringToText(" seconds");
- DmgTrack.elapsedRealTime = StringToText("Elapsed real time: ")+DmgTrack.valueToken+StringToText(" seconds");
- DmgTrack.damageStats = StringToText("[")+DmgTrack.entryToken+StringToText("] Total damage: ")+DmgTrack.valueToken+StringToText(" | DPS (game): ")+DmgTrack.dpgsToken+StringToText(" | DPS (real): ")+DmgTrack.dprsToken;
- // Create and register the damage detection trigger.
- DmgTrack.damageTrigger = TriggerCreate("DmgTrack_Damage_Action");
- TriggerAddEventUnitDamaged(DmgTrack.damageTrigger,null,c_unitDamageTypeAny,c_unitDamageEither,null);
- TriggerEnable(DmgTrack.damageTrigger,false);
- // Create and register the chat command trigger.
- DmgTrack.chatTrigger = TriggerCreate("DmgTrack_Chat_Action");
- TriggerAddEventChatMessage(DmgTrack.chatTrigger,c_playerAny,null,false);
- TriggerEnable(DmgTrack.chatTrigger,false); // Comment out this line to enable the chat commands by default.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement