window.onload = initPage;
// Globals
var gs_buffer = '';
const GKN_INPUT_MOVE_KEY = 0;
const GKN_INPUT_ALT_KEY = 1;
const GKN_INPUT_CREATE_KEY = 2;
const GKN_INPUT_OBSTACLE_KEY = 3;
var ga_obstacle_colors_proto = new Array();
ga_obstacle_colors_proto[0] = "white";
ga_obstacle_colors_proto[1] = "gray";
ga_obstacle_colors_proto[2] = "black";
const GKA_OBSTACLE_COLORS = ga_obstacle_colors_proto; // Store in a constant value to avoid changes
var ga_alt_colors = new Array();
ga_alt_colors[0] = "white";
ga_alt_colors[1] = "yellow";
ga_alt_colors[2] = "orange";
ga_alt_colors[3] = "red";
const GKA_ALT_COLORS = ga_alt_colors; // Store in a constant value to avoid changes
var ga_obstacle_verbose = new Array();
ga_obstacle_verbose[0] = "None";
ga_obstacle_verbose[1] = "Half";
ga_obstacle_verbose[2] = "Full";
const GKA_OBSTACLE_VERBOSE = ga_obstacle_verbose;
var gn_input_type = 0;
function initPage() {
const KN_ROW_NUM = 20;
const KN_COL_NUM = 20;
const KCOL_GRID_OUTPUT = document.getElementById("grid");
var s_grid_output = "<table border='2' id='battle_grid'>";
document.getElementById("status").style.display = 'none';
for (var i = 1; i <= KN_ROW_NUM; i++) {
s_grid_output += "<tr>";
for (var j = 1; j <= KN_COL_NUM; j++) {
s_grid_output += "<td posx='"+j+"' posy='"+i+"' title='("+j+","+i+")' altitude='0' obstacle='0' occupant=''></td>";
}
s_grid_output += "</tr>";
}
s_grid_output += "</table>";
KCOL_GRID_OUTPUT.innerHTML = s_grid_output;
// Update the grid with this new information
updateGrid();
var col_cells = document.getElementById("grid").getElementsByTagName("td");
for (var i = 0; i < col_cells.length; i++) {
col_cells[i].onclick=function() {
if (gn_input_type == GKN_INPUT_MOVE_KEY) {
move(this);
}
else if (gn_input_type == GKN_INPUT_ALT_KEY) {
changeAltitude(this);
}
else if (gn_input_type == GKN_INPUT_OBSTACLE_KEY) {
createObstacles(this);
}
else {
alert("There was an error when selecting the input key. Current value: " + gn_input_type);
}
updateGrid();
}
}
return true;
}
function changeInputType() {
var col_inputs = document.forms[0].elements['input_types'];
for (var i = 0; i < col_inputs.length; i++) {
if (col_inputs[i].checked) {
gn_input_type = col_inputs[i].value;
break;
}
}
// Show additional options for inputs
var input_options_output = '';
if (gn_input_type == GKN_INPUT_MOVE_KEY) {
// Show nothing as there are no options
}
else if (gn_input_type == GKN_INPUT_ALT_KEY) {
input_options_output = "<form name='alt_form' id='alt_form'>Altitude: <select name='altitude' id='altitude'>";
for (i = 0; i <= 3; i++) {
input_options_output += "<option value='" + i + "'>" + i + "</option>";
}
input_options_output += "</select></form>";
}
else if (gn_input_type == GKN_INPUT_CREATE_KEY) {
input_options_output = "<form name='create_character' action='#' method='post'>";
input_options_output += "<p><label>First Name: </label> <input type='text' name='first_name' /> <span id='first_name_help'></span></p>";
input_options_output += "<p><label>Last Name: </label> <input type='text' name='last_name' /> <span id='last_name_help'></span></p>";
input_options_output += "<p><label>Position: </label> (x,y) (<input type='text' name='posx' style='width:15px;' />, <input type='text' name='posy' style='width:15px;' />) <span id='posx_help'></span> <span id='posy_help'></span></p>";
input_options_output += "<p><label>Key: </label> <input type='text' name='key' style='width:20px;' /> <span id='key_help'></span></p>";
input_options_output += "<p><input type='button' onclick='createCharacters(); return false;' value='Create Character' /></p>";
input_options_output += "</form>";
}
else if (gn_input_type == GKN_INPUT_OBSTACLE_KEY) {
input_options_output = "<form action='#' method='get' name='obstacle_form'>";
input_options_output += "<input type='radio' name='obstacle' value='0' id='obstacle_none' /> <label for='obstacle_none'>None</label><br />";
input_options_output += "<input type='radio' name='obstacle' value='1' id='obstacle_half' /> <label for='obstacle_half'>Half</label><br />";
input_options_output += "<input type='radio' name='obstacle' value='2' id='obstacle_full' /> <label for='obstacle_full'>Full</label>";
input_options_output += "</form>";
}
document.getElementById("input_options").innerHTML = input_options_output;
return true;
}
function updateGrid() {
var cells = document.getElementById("battle_grid").getElementsByTagName("td");
// Loop through each grid cell
for (var i = 0; i < cells.length; i++) {
var cell = cells[i];
var occupant = cell.getAttribute("occupant");
var altitude = cell.getAttribute("altitude");
var obstacle = cell.getAttribute("obstacle");
// Set the cell contents by occupant attribute
cell.innerHTML = occupant;
// Change the cell's color by its altitude
cell.style.background = GKA_ALT_COLORS[altitude];
// Check if this cell is an obstacle. If yes, override the altitude color change
if (obstacle > 0) {
cell.style.background = GKA_OBSTACLE_COLORS[obstacle];
}
cell.title = "(" + cell.getAttribute("posx") + "," + cell.getAttribute("posy") + ") | Altitude: " + altitude + " | Obstacle: " + GKA_OBSTACLE_VERBOSE[obstacle];
}
return true;
}
function move(e) {
var occupant = e.getAttribute("occupant");
// If the cell is an obstacle, don't let a unit occupy it
if (e.getAttribute("obstacle") > 0) {
alert("Cannot place unit on an obstacle");
return false;
}
// Check if there is a character in the buffer waiting to be moved
if (gs_buffer != '' && occupant == '') {
e.setAttribute("occupant", gs_buffer);
gs_buffer = '';
}
else if (gs_buffer == '' && occupant != '') {
gs_buffer = occupant;
e.setAttribute("occupant", '');
}
else {
alert("This square is occupied or you do not have a unit to move.");
}
return true;
}
function changeAltitude(e) {
var altitude_ddl = document.forms['alt_form'].elements['altitude'];
new_altitude = altitude_ddl.options[altitude.selectedIndex].value;
e.setAttribute("altitude",new_altitude);
return true;
}
function createCharacters() {
var form_els = document.forms['create_character'].elements;
var bol_errors = false;
// Check for empty fields
for (var i = 0; i < form_els.length; i++) {
if (form_els[i].value.length == 0) {
document.getElementById(form_els[i].name + "_help").innerHTML = form_els[i].name + " is invalid.";
bol_errors = true;
}
}
// Check for position within 20,20
if (form_els['posx'].value > 20 && form_els['posx'].value < 1 || form_els['posy'].value > 20 && form_els['posy'].value < 1) {
document.getElementById("posy_help").innerHTML = "Must be between 1 and 20";
bol_errors = true;
}
if (bol_errors) {
return false;
}
var rows = document.getElementById("battle_grid").getElementsByTagName("tr");
for (var y = 1; y <= rows.length; y++) {
var cols = rows[y].getElementsByTagName("td");
for (var x = 1; x <= cols.length; x++) {
if (form_els['posx'].value == x && form_els['posy'].value == y) {
var cells = document.getElementById("battle_grid").getElementsByTagName("td");
var newY = y * 20;
var newX = x;
var cur_cell = newY+newX;
cells[cur_cell].setAttribute("occupant", form_els['key']);
}
}
}
updateGrid();
}
function createObstacles(e) {
var new_obstacle = document.forms['obstacle_form'].elements['obstacle'];
var new_obstacle_value = 0;
for (var i = 0; i < new_obstacle.length; i++) {
if (new_obstacle[i].checked) {
new_obstacle_value = new_obstacle[i].value;
break;
}
}
e.setAttribute("obstacle", new_obstacle_value);
return true;
}