tropicdome/MyPetBattle — pull request #30 by joevt (2013-08-09 05:26:10+00:00)
Title: New team matching options and miscellaneous fixes

These changes apply to issue #7, issue #27, and issue #19. I think it may also address issue #28 and issue #11. There are changes that may apply to issue #18.

I think the team matching method might be superior to that given in issue #29 or at least a viable alternative.

New options:

Leveling
1) WT match levels
-	Start level
-	Direction

Pre-Combat
1) Include non-rares
2) Randomize
3) Minimum count to keep of a level

Combat
4) Auto-equip Safari Hat

Fixes:

Rearranged the controls in the configuration and the UI (left justify
the "Capture common" CheckButton, added explicit line feeds to
CheckButton items to wrap the text more evenly between two lines,
removed explicit line feeds from tooltips, etc.)

Fixed an error that occurred when pressing the Escape key in one of the
text fields in the UI after it was empty.

GetPetInfoByIndex and GetPetInfoByPetID may return stagnent information
so I added setPetLevel and getPetLevel to ensure the returned level is
correct. setPetLevel is called when a pet changes level during pet
battle.
To do:
Verify that GetPetInfoByIndex and GetPetInfoByPetID return the same
level.
Test level changes that occur when a pet changes quality.

Displaying the level for each pet slot in the UI is now done separately
from setting each pet icon. Before, the level was updated only if the
pet changed.

The pet XP gained calculation is wrong when the pet changes level.
Instead of doing the calculation, I get the XP gained from an event.

The pet team can't be set during login (pet info is not cached yet) so
additional attempts are made later until the team can be set.

Pets are healed only if there is a useable pet that does not have full
health.

Combat changes:

Added round number to MPB messages.

Added return calls so that only one action per round is attempted.

Added code to make sure the forfeiter doesn't slaughter the winner's
low level pets or accidentally kills the last enemy pet.

Fixed the "Would be Casting" debug message so that it actually
calculates the spell that would be casted.

Misc notes and changes:

The sync message is now prefixed with "s" to distinguish it from other
messages we may want to add in the future.

Added states and state flags for state machine (MyPetBattleState).
Moved state processing to the timer function (MPB_onUpdate). How it
works: Events change states; States toggle flags; Flags cover a range
of states. A state may be processed immediately, or the next time the
the timer function is called (several times a second). The timer
function does not process states immediately, unless the new state
follows the old state in the code.

Added some timers in addition to the existing sync timer and one second
timer. They are defined as global variables so you may adjust them
while WoW is running.

1)
MPB_timerSetTeamCountdown = 0
MPB_timerSetTeamInterval = 1
Used for sending team messages to the win-trading partner. Team
messages are sent every second until both sides acknowledge the other's
team message. The team message is described in setTeam. It is 94
characters long and prefixed with the letter "m".

2)
MPB_timerStartPVPMatchingCountdown = 0
MPB_timerStartPVPMatchingAfterSetTeamInterval = 2
MPB_timerStartPVPMatchingInterval = 5
Used for pvp queueing attempts. The 2 second interval occurs after
setting a team to ensure the team is loaded properly. The 5 second
interval occurs after the first queue attempt which generally shouldn't
fail.

3)
MPB_timerAcceptPVPMatchmakingCountdown = 0
MPB_timerAcceptPVPMatchmakingInterval = 5
Used for accepting a pvp match. Generally, accepting a pvp match
shouldn't fail, but if it does, then we'll wait 5 seconds to accept
another match or queue again.

4)
MPB_timerBattleCloseCountdown = 0
MPB_timerBattleCloseInterval = 5
Used after a battle is won. The match is still considered in progress
while this is counting down so that we don't attempt to set a new team
or queue for another pvp match until the normal wow UI is properly
loaded (5 seconds).

The new configuration options are stored in a global tabled called MPB.
A table is used so that only one variable name needs to be added to the
.toc.

I use a table global variable called "MyPetBattleVars" to hold pet list
calculations, statistics, and other information in one place so that
the user can inspect them for debugging purposes.

Appended " and true or false" to the getting of CheckButton values to
convert them to boolean.

Added player XP gains statistics. Use the following command to dump the
statistics:
"/run MyPetBattle:dumpXPGains()". You may pass a number representing
the chat window (e.g. 2 for Combat Log).

You can verify the Safari Hat feature is working properly be enabling
"/mypetbattle debug" and watching for the PLAYER_EQUIPMENT_CHANGED
event. It should occur after pet XP is awarded and before player XP is
awarded.

Set Team changes:

The setTeam function has been rewritten to allow for more options. Team
creation when not doing team level matching behaves much as before
except with the new options applied.

Sorting functions are used to sort pet candidates. Sorting functions
are constructed by passing a list of fields to sort by to
composeSortFunction.

A loopPets function is used to loop through possible pet candidates. It
filters pet quality if non-rare pets are not allowed. It also excludes
pets that are already used. A handler function is used to add
additional fields to each pet record and to add the pet record to a
list of candidate pets for sorting. For example, the countPets function
counts pets that can battle and usable rare and non-rare pets (must
have health > than threshold).

When matching team levels with the enemy the following occurs:

1) A leader is determined (enemyIsLeader). This is the person that is
not forfeiting. If both players are forfeiting then the party leader is
the leader. The leader determines the starting level. Both the player
and the enemy configuration determine the valid pet level range.

2) If the other player has a locked pet then the player will need a pet
of the same level if the player doesn't already have a locked pet of
the same level (neededLevels, commonLevels, neededLevelsEnemy). The
HandleLockedPets function searches for the best level matches for the
neededLevels and neededLevelsEnemy lists. Chosen levels for each team
have their count reduced in that team's list.

3) Choose pets for the remaining slots using the remaining team list
counts for the player and the enemy. Player and enemy pet must be
within 1 level to be considered. The level of the pet must be in the
valid pet level range. As a last resort, a pet of level 25 can be
chosen (this is ok since the pet can't gain a level if it's part of the
winning team).