Keith1039 / Pygame-RPG

A repository of me trying to make an RPG with a custom game engine with Pygame added in as support.
0 stars 0 forks source link

Pygame-RPG 19.4 Upgrading damage calculations #67

Closed Keith1039 closed 2 months ago

Keith1039 commented 2 months ago

Pygame-RPG 19.4 Upgrading damage calculations

This commit focuses on adding new functionality to the Move.py file in order to create more unique moves. The original idea was to add a math engine to the game that can evaluate valid mathematical expressions but since that is not possible without introducing the mother of all vulnerabilities, eval(), into this game, this idea had to be shelved.

Instead, the functionality was implemented by using special symbols that will be parsed and evaluated. As a result of these changes, we assume that each damage calculation formula that isn't empty string, can be split into 3 pieces. The main stat, the operation ("+" or "*") and the value to be parsed. To help with this, new helper functions were added to the Move.py file (not Move class methods), these methods are isfloat(), replace_all_stats_with_values(), look_for_special_symbols, parse_special_symbols() and apply_symbol().

isfloat(): This function takes in a string and tries to convert it to a float. If it succeeds with no error, True is returned otherwise we return false.

replace_all_stats_with_values(): This function takes in a string and an entity object. It then replaces all mentions of a stat (ex Str for strength stat) and replaces them with the Entity's string value. For example, the string "Str + 1" will be converted to "1 + 1" assuming the Entity object's strength stat is 1. To ensure that this function is consistent with new changes, it replaces valid stats referenced in the Entity objects attribute dictionary. This means that when new stats are added, they will be accounted for in this function.

look_for_special_symbols(): This function takes in a string and returns whether it has a valid special symbol. This function is a work in progress. Currently, there is a list of special symbols that the function specifically looks. If they are present, the function returns true, otherwise it returns false. Once it finds at least one special symbol, the function will return true.

parse_special_symbols(): This function takes in a string and an Entity object and returns a float number. There are two cases for this function, the first case is if the string given to it is a valid number (this is determined by the isfloat() function). If this is the case, the function returns the float number that the string represents. If the string isn't however, that implies that the string possesses a special symbol. A special symbol is often accompanied by a numeric multiplier. To handle the symbol, both needs to be parsed out (if there is no explicit numeric multiplier, we assume that it is 1 and we assume that the entire string is the special symbol). To obtain the multiplier and the symbol, we loop every character until we reach the first non-numeric character, then we extract the two sides of the string. This function then returns the result of the apply_symbol() function.

apply_symbol(): This function takes in an integer, a string and an Entity object. using an if statement, the value of the special symbol is determined and is multiplied by the given multiplier. The value is then returned.

As a result of these changes, parse_damage_calculation() was changed. This function now assumes that all valid damage formulas are "" or in the format (stat [* or +] value). If the damage formula is empty string, 0 is returned, otherwise, the string is to be parsed. Firstly, look_for_special_symbols is used to verify that there are any special symbols. Next, the string is split into 3 segments. If there were no special symbols, replace_all_stat_with_values() is used and the string array is refreshed. Next, the stat variable is set, if there were special symbols, the stat value is set differently. Next, the function gets the value by using the parse_special_value() function. Finally, we obtain the damage value for the move by verifying the operation that is meant to be done (multiplication or addition) and returns the rounded damage value.

The new moves Desperado and Experienced Strike were added to the move list to take advantage of the new functionality.

PR checklist