The recently added enemy logic (#2) within the tickEnemies function relies on a SQL query that contains several hardcoded numerical values to determine behavior.
Location: index.html > tickEnemies function > SQL UPDATE query with CTEs.
Current Hardcoded Values:
- Distance Thresholds (Squared):
9 (retreat), 25 (circle/approach boundary), 81 (activation range)
- Base Movement Step:
0.1
- Movement Randomization Factors:
0.9, 0.2 (used as 0.9 + random() * 0.2)
- Circle Direction Change Probability:
0.02 (used as random() < 0.02)
Problem:
Embedding these "magic numbers" directly in the SQL string makes it:
- Harder to Tune: Adjusting enemy behavior requires finding and modifying values within a complex SQL query.
- Less Readable: It's not immediately obvious what these numbers represent without carefully reading the surrounding SQL logic.
- Error-Prone: Modifying the SQL string for tuning increases the risk of introducing syntax errors.
Proposed Solutions:
Consider extracting these values out of the SQL string itself:
- JavaScript Constants: Define these parameters as
const variables at the top of the relevant JavaScript scope. Then, use template literals or string concatenation to insert these variables into the SQL query string before execution.
- Pros: Relatively simple, keeps configuration within the code.
- Cons: Still requires a code change and redeployment to adjust values.
settings Table: Add new columns/rows to the existing settings SQL table to store these AI parameters (e.g., enemy_retreat_dist_sq, enemy_move_step, enemy_circle_change_prob). The tickEnemies function would need to query these settings first and then use them in the main AI movement query.
- Pros: More data-driven, potentially allows for easier changes if the settings were loaded dynamically (though maybe overkill here). Keeps game parameters consolidated.
- Cons: Adds a small overhead (querying settings first), slightly more complex implementation.
Goal:
Refactoring these values will improve the maintainability, readability, and ease of tuning for the enemy behavior.
The recently added enemy logic (#2) within the
tickEnemiesfunction relies on a SQL query that contains several hardcoded numerical values to determine behavior.Location:
index.html>tickEnemiesfunction > SQLUPDATEquery with CTEs.Current Hardcoded Values:
9(retreat),25(circle/approach boundary),81(activation range)0.10.9,0.2(used as0.9 + random() * 0.2)0.02(used asrandom() < 0.02)Problem:
Embedding these "magic numbers" directly in the SQL string makes it:
Proposed Solutions:
Consider extracting these values out of the SQL string itself:
constvariables at the top of the relevant JavaScript scope. Then, use template literals or string concatenation to insert these variables into the SQL query string before execution.settingsTable: Add new columns/rows to the existingsettingsSQL table to store these AI parameters (e.g.,enemy_retreat_dist_sq,enemy_move_step,enemy_circle_change_prob). ThetickEnemiesfunction would need to query these settings first and then use them in the main AI movement query.Goal:
Refactoring these values will improve the maintainability, readability, and ease of tuning for the enemy behavior.