Advertisement
Guest User

Tile Scoring

a guest
Nov 19th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.61 KB | None | 0 0
  1. // Enemy AI
  2.  
  3. // ListTileScoring
  4. // To contain id of every tile in level
  5.  
  6. // GridTileScoring
  7. // x = Map data structure, containing:
  8. // tile id, tile x and y, any occupant, whether it can be traversed or not, and if so its move cost,
  9. // tile type, status, alternated list of abilities and targets, and score
  10. // y = Score
  11.  
  12. ListTileScoring = ds_list_create();
  13. var GridTileScoring = ds_grid_create(instance_number(oTile),2);
  14. //var GridTileScoring = ds_grid_create(2,instance_number(oTile));
  15.  
  16. ListTileMaps = ds_list_create();
  17.  
  18. // Work through list, building a map for each tile of all details and options
  19. // Add the tile's map and tile's score to GridTileScoring
  20. var ListPos = 0;
  21.  
  22. for (CheckX = 0; CheckX < oGenRoom.GridWidth; CheckX = CheckX + 1) {
  23.  
  24. // Looping through for as long as CheckY is less than GridHeight:
  25. for (CheckY = 0; CheckY < oGenRoom.GridHeight; CheckY = CheckY + 1) {
  26.  
  27. // Establish map and variables
  28. var MapTileScoring = ds_map_create();
  29.  
  30. //var TileID; // This is object_index, not a simple number like other map IDs
  31. var TileGridX; // oGenRoom.Grid GridX
  32. var TileGridY; // oGenRoom.Grid GridX
  33. var TileOccupant; // Occupant id, or noone
  34. var TilePassable; // Boolean
  35. var TileMoveCost; // Number value, real
  36. var TileType; // Grass, water, etc
  37. var TileHeight; // Integer, real
  38. var TileEffect; // Burning, etc, or noone
  39. //var TileListActions; // Alternated (divisible by 2) list of abilities and targets
  40. //var TileScore; // Score for movement, according to all of the above
  41.  
  42. TileID = oGenRoom.Grid[# CheckX, CheckY];
  43. TileGridX = oGenRoom.Grid[# CheckX, CheckY].GridX;
  44. TileGridY = oGenRoom.Grid[# CheckX, CheckY].GridY;
  45. TileOccupant = oGenRoom.Grid[# CheckX, CheckY].Occupant;
  46. TilePassable = oGenRoom.Grid[# CheckX, CheckY].Passable;
  47. TileMoveCost = oGenRoom.Grid[# CheckX, CheckY].MoveCost;
  48. TileType = oGenRoom.Grid[# CheckX, CheckY].TileType;
  49. TileHeight = oGenRoom.Grid[# CheckX, CheckY].Height;
  50. TileEffect = oGenRoom.Grid[# CheckX, CheckY].Effect;
  51. TileListActions = ds_list_create();
  52. TileScore = scrTileScore(oGenRoom.Grid[# CheckX, CheckY],0);
  53.  
  54. with oGenRoom.Grid[# CheckX, CheckY] {
  55. TileScore = other.TileScore;
  56. }
  57.  
  58. // Add variables to map
  59. ds_map_add(MapTileScoring, "TileID", TileID);
  60. ds_map_add(MapTileScoring, "TileGridX", TileGridX);
  61. ds_map_add(MapTileScoring, "TileGridY", TileGridY);
  62. ds_map_add(MapTileScoring, "TileOccupant", TileOccupant);
  63. ds_map_add(MapTileScoring, "TilePassable", TilePassable);
  64. ds_map_add(MapTileScoring, "TileMoveCost", TileMoveCost);
  65. ds_map_add(MapTileScoring, "TileType", TileType);
  66. ds_map_add(MapTileScoring, "TileHeight", TileHeight);
  67. ds_map_add(MapTileScoring, "TileEffect", TileEffect);
  68. ds_map_add(MapTileScoring, "TileListActions", TileListActions);
  69. ds_map_add(MapTileScoring, "TileScore", TileScore);
  70.  
  71. // Add map to list and grid, add score to grid
  72. ds_list_add(ListTileMaps,MapTileScoring);
  73.  
  74. ds_grid_add(GridTileScoring,ListPos,0,MapTileScoring);
  75. ds_grid_add(GridTileScoring,ListPos,1,TileScore);
  76.  
  77. ListPos = ListPos + 1;
  78.  
  79. }
  80.  
  81. CheckY = 0;
  82.  
  83. //ListPos = ListPos + 1;
  84.  
  85. }
  86.  
  87. // Once all tiles are in GridTileScoring, sort grid by score
  88. ds_grid_sort(GridTileScoring,1,false);
  89.  
  90. // Apply margin dependent on enemy capability to top # of options, and choose one
  91. // **** DISABLED FOR TESTING ****
  92. // Set size of list of best options to choose from based on enemy difficulty (ie modifier minus 0 to 5, 5 being smartest enemy)
  93. // This many options from the top down are added to a list (so long as they exist)
  94. // Then desired tile is chosen by random number, 0 to this list's length - 1
  95. /**///
  96.  
  97. /*
  98. var DifficultyModifier = 1;
  99.  
  100. ListBestTiles = ds_list_create();
  101.  
  102. var i;
  103. for (i = 0; i < DifficultyModifier; i = i + 1) {
  104.  
  105. // Delete tiles with TileCurrentScore = false from list
  106.  
  107. // Store map
  108. var BestTileMapCurrent = ds_grid_get(GridTileScoring, 0, i);
  109.  
  110. // Pull tile id from map, add to ListBestTiles
  111. ds_list_add(ListBestTiles,BestTileMapCurrent[? "TileID"]);
  112.  
  113. }
  114.  
  115.  
  116. DesiredTileID = ds_list_find_value(ListBestTiles,irandom_range(0,ds_list_size(ListBestTiles)-1));
  117. var DesiredTile;
  118.  
  119. // When DesiredTile is chosen, find its map in ListTileScoring
  120. var TileIndex = scrFindTile(DesiredTileID);
  121.  
  122. // Once found...
  123. if (TileIndex > -1) {
  124.  
  125. var data = ds_list_find_value(ListTileMaps,TileIndex);
  126.  
  127. DesiredTile = oGenRoom.Grid[# ds_map_find_value(data, "TileGridX"), ds_map_find_value(data,"TileGridY")];
  128. }
  129.  
  130. */
  131.  
  132. // Store desired tile id
  133. var DesiredTileMap = ds_grid_get(GridTileScoring,0,0);
  134. //var DesiredTileMap = GridTileScoring[# 0, 0];
  135.  
  136. //var DesiredTile = oGenRoom.Grid[# Selected.UICharacterNumber,Selected.UICharacterNumber];
  137. //var DesiredTile = oGenRoom.Grid[# ds_map_find_value(DesiredTileMap,"TileGridX"),ds_map_find_value(DesiredTileMap,"TileGridY")];
  138. var DesiredTile = ds_map_find_value(DesiredTileMap,"TileID");
  139.  
  140. // From map, pull TileListActions
  141. DesiredTileListActions = ds_list_create();
  142.  
  143. // If there are abilities and targets listed...
  144. if !ds_list_empty(ds_map_find_value(DesiredTileMap, "TileListActions")) {
  145.  
  146. ds_list_copy(DesiredTileListActions,ds_map_find_value(DesiredTileMap, "TileListActions"));
  147.  
  148. // Choose an even number for ability to perform, + 1 is the target of that ability
  149. var DTLAI = irandom_range(0,ds_list_size(DesiredTileListActions));
  150.  
  151. // Even number -- DTLAI = ability name
  152. // DTLAI + 1 = target character
  153. if DTLAI mod 2 == 0 {
  154. AbilityQueued = ds_list_find_value(DesiredTileListActions,DTLAI);
  155. var DesiredTarget = ds_list_find_value(DesiredTileListActions,DTLAI + 1);
  156. TargetAttackTile = DesiredTarget.TileCurrent;
  157. }
  158. // Odd number -- target
  159. else if DTLAI mod 2 == 1 {
  160. AbilityQueued = ds_list_find_value(DesiredTileListActions,DTLAI - 1);
  161. var DesiredTarget = ds_list_find_value(DesiredTileListActions,DTLAI);
  162. TargetAttackTile = DesiredTarget.TileCurrent;
  163. }
  164.  
  165. }
  166.  
  167. // Else if there are no abilities and targets listed...
  168. else {
  169. TargetAttackTile = noone;
  170. AbilityQueued = noone;
  171. }
  172.  
  173. // Set TargetAttackTile
  174. //TargetAttackTile = ds_list_find_index(DesiredTileListActions,oGenRoom.Grid[# DesiredTarget.GridX, DesiredTarget.GridY]);
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. // Queue action and move to tile as necessary
  183.  
  184. // Generate list of tiles from those within our single move range
  185. scrMovementRangeAI(oGenRoom.Grid[#Selected.GridX,Selected.GridY],Selected.Move,Selected.Actions);
  186.  
  187. // If desired tile is within movement range...
  188. if DesiredTile.MoveTile == true {
  189.  
  190. // Build path of movement with priority queue
  191. //Current = TargetMoveTile;
  192. Current = DesiredTile;
  193. Path = ds_priority_create();
  194. with Selected {
  195. StackPath = ds_stack_create();
  196. ds_stack_push(StackPath,other.Current);
  197. }
  198.  
  199. // Add current tile to the queue
  200. ds_priority_add(Path, Current, Current.GScore);
  201.  
  202. // Step through each tile from path parent to path parent, connecting the path
  203. // until the first tile in the path has a link, or "PathParent"
  204. while (Current.PathParent != noone) {
  205.  
  206. // Add PathParent tile to queue. Priority = PathParent's GScore
  207. ds_priority_add(Path,Current.PathParent,Current.PathParent.GScore);
  208.  
  209. // Set Current to Current's PathParent, then repeat
  210. Current = Current.PathParent;
  211.  
  212. // Add each tile to the path stack
  213. with Selected {
  214. ds_stack_push(StackPath,other.Current);
  215. }
  216.  
  217. }
  218.  
  219. do {
  220. // Delete lowest priority tile (closest to character), store id in Current
  221. Current = ds_priority_delete_min(Path);
  222.  
  223. // Add current tile's sprite coords to character's path
  224. path_add_point(Selected.MovementPath, Current.x, Current.y, 100);
  225. //path_add_point(Selected.MovementPath, Current.x, (Current.y - Current.HeightOffset), 100);
  226.  
  227. }
  228.  
  229. until
  230. ds_priority_empty(Path);
  231.  
  232. // Destroy queue
  233. ds_priority_destroy(Path);
  234.  
  235. // If destination tile is not the same as old tile...
  236. if oGenRoom.Grid[#Current.GridX,Current.GridY] != oGenRoom.Grid[#Selected.GridX,Selected.GridY] {
  237.  
  238. // Reduce Selected's actions according to distance
  239. // Blue into yellow - 2 Actions spent
  240. if Current.GScore > Selected.Move {
  241. Selected.Actions = Selected.Actions - 2;
  242. Selected.AbilityQueued = noone;
  243. Selected.TargetAttackTile = noone;
  244. }
  245.  
  246. // Blue into blue - 1 Action spent
  247. else if Current.GScore <= Selected.Move {
  248. Selected.Actions = Selected.Actions - 1;
  249. // Set ability, even if "noone"
  250. Selected.AbilityQueued = AbilityQueued;
  251. // Set target tile, even if "noone"
  252. //if ds_list_empty(ArrayTileScoring[ArrayCheck,2]) == false {
  253. Selected.TargetAttackTile = TargetAttackTile;
  254. }
  255.  
  256. // Clear previous tile of occupant
  257. oGenRoom.Grid[#Selected.GridX,Selected.GridY].Occupant = noone;
  258.  
  259. // Update character's grid coords with chosen tile's coords
  260. Selected.GridX = Current.GridX;
  261. Selected.GridY = Current.GridY;
  262.  
  263. // Update destination tile's status for arrival
  264. Current.Occupant = Selected;
  265.  
  266. // Add destination tile to end of stack
  267. with Selected {
  268. ds_stack_push(StackPath,other.Current);
  269. }
  270.  
  271. // Send character on path to destination
  272. Selected.State = "BeginPath";
  273.  
  274. }
  275.  
  276. // Else skip straight to BeginAbility state
  277. else {
  278. // Set ability, even if "noone"
  279. Selected.AbilityQueued = AbilityQueued;
  280. // Set target tile, even if "noone"
  281. if TargetAttackTile != noone {
  282. Selected.TargetAttackTile = TargetAttackTile;
  283. }
  284. else {
  285. Selected.AbilityQueued = noone;
  286. Selected.TargetAttackTile = noone;
  287. }
  288. Selected.State = "BeginAbility";
  289. }
  290.  
  291. }
  292.  
  293.  
  294. // If desired tile is not within movement range, move to closest move tile to desired tile
  295. else {
  296. // Placeholder end turn
  297. Selected.AbilityQueued = noone;
  298. Selected.TargetAttackTile = noone;
  299. Selected.State = "BeginAbility";
  300. }
  301.  
  302.  
  303.  
  304. // Destroy data structures
  305. ds_list_destroy(ListTileScoring);
  306. ds_grid_destroy(GridTileScoring);
  307. ds_list_destroy(ListTileMaps);
  308. //ds_list_destroy(ListBestTiles);
  309. ds_list_destroy(DesiredTileListActions);
  310.  
  311. // End turn
  312.  
  313.  
  314.  
  315.  
  316. //// NEW ////
  317.  
  318. // Add all tiles on level to grid
  319. // One column to hold a map with all tile details, second to hold the tile's score
  320. // Work through every tile, perform checks, apply score
  321. // Sort grid by second column, the score
  322. // Choose a tile at top of grid/list based on difficulty modifier
  323. // If chosen tile has an associated ability and target, queue them
  324. // At this point store a esired queued action, or noone;
  325.  
  326. // Tile scoring factors (unmodified by individual traits):
  327.  
  328. // Actions that can be taken (enemies hit, allies not hit, allies buffed)
  329. // Occupant (allied, enemy, obstacle, objective)
  330. // Occupant adjacent (allied, enemy, obstacle, objective)
  331. // No hazard
  332. // No hazard adjacent
  333. // Difficulty of terrain
  334. // Proximity/distance to allies, enemies
  335. // Height difference
  336. // Height
  337.  
  338. // End turn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement