Posted By: Helghast
Tactical RPG Grid setup - 01/24/11 10:19
Roxas had asked previously how to calculate grid movement for a tactical game (link to thread: Tactical RPG Grid Calculation ) . I took an hour to write a pretty dynamic system for that, and thought people could make use of it.
it's just a multi-dimensional array that calculates distance cost from any grid cell based on a given diameter, but it's a good start for any tactics game.
the code clamps around the edges properly.
you can use the mouse position to set a position to calculate from, and use the scrollwheel to increase/decrease the movement radius.
I hope someone can make use of it, or atleast learn from it
regards,
it's just a multi-dimensional array that calculates distance cost from any grid cell based on a given diameter, but it's a good start for any tactics game.
the code clamps around the edges properly.
you can use the mouse position to set a position to calculate from, and use the scrollwheel to increase/decrease the movement radius.
Code:
#include <acknex.h> #include <default.c> #define GRID_size 10 #define GRID_cellsize 32 var GridMoveSquares = 3; // stores size of movement diameter var GridArray[GRID_size][GRID_size]; // stores size of grid // visual debug representation variables var current_row = 3; var current_col = 1; // function that resets every GRID value function resetGrid() { var X_index = 0; var Y_index = 0; for(X_index=0; X_index<GRID_size; X_index++) { for(Y_index=0; Y_index<GRID_size; Y_index++) { GridArray[X_index][Y_index] = 0; } } } // function that visually represents the GRID data function drawGridData() { var column_index = 0; var row_index = 0; for(column_index=0; column_index<GRID_size; column_index++) { for(row_index=0; row_index<GRID_size; row_index++) { if(GridArray[column_index][row_index] == 0) { draw_text(str_for_num(NULL, GridArray[column_index][row_index]), GRID_cellsize*column_index, GRID_cellsize*row_index, vector(255,255,255)); } else { draw_text(str_for_num(NULL, GridArray[column_index][row_index]), GRID_cellsize*column_index, GRID_cellsize*row_index, vector(0,0,255)); } } } } // actually calculate the cells that can be moved on function setMoveGrid(var row, var col) { var column_index = 0; var row_index = 0; var clampedIndex; resetGrid(); // reset for(column_index=GridMoveSquares; column_index>=0; column_index--) { // fill right hand side of column clampedIndex = clamp(col+column_index, 0, GRID_size-1); GridArray[row][clampedIndex] = column_index; for(row_index=GridMoveSquares; row_index>=column_index; row_index--) { GridArray[clamp(row+(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index; GridArray[clamp(row-(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index; } // fill left hand side of column clampedIndex = clamp(col-column_index, 0, GRID_size-1); GridArray[row][clampedIndex] = column_index; for(row_index=GridMoveSquares; row_index>=column_index; row_index--) { GridArray[clamp(row+(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index; GridArray[clamp(row-(row_index-column_index), 0, GRID_size-1)][clampedIndex] = row_index; } } } function getMouseInput() { current_row = clamp(integer(abs(mouse_pos.x) / GRID_cellsize), 0, GRID_size-1); current_col = clamp(integer(abs(mouse_pos.y) / GRID_cellsize), 0, GRID_size-1); GridMoveSquares += (mickey.z/120); GridMoveSquares = clamp(GridMoveSquares, 0, GRID_size); } function main() { mouse_mode = 4; resetGrid(); // reset GRID while(1) { getMouseInput(); setMoveGrid(current_row,current_col); // calculate new GRID data drawGridData(); // debug draw grid wait(1); } }
I hope someone can make use of it, or atleast learn from it
regards,