All pastes #28352 Raw Edit

Unnamed

public text v1 · immutable
#28352 ·published 2005-11-11 12:31 UTC
rendered paste body
% CLASSES.T
/***************************************************************************************
 *                       TURING WARS (Working Title) V 0.0.1                           *
 *                               Class Declarations File                               *
 *      Created By: Josh S                                    Date: November 9, 2005   *
 *                            ICS 3M1 Turing Final Project                             *
 ***************************************************************************************
 *   OK, so this is a first dive into a quasi object-oriented program. It just seems   *
 *   about the only way to have many (definitely upwards of 20) instances of the same  *
 *   kind of thing at one time, without some freaky variables... We did not learn      *
 *   these in ICS3M, however a valuable skill to have leaving high school is indepen-  *
 *   -dent learning. This year r eally let me see different things I thought Turing     *
 *   could never accomplish, so here we go!                                            *
 ***************************************************************************************/
var verdana := Font.New ("verdana:10:bold")
% -------------------------------- BEGIN UNIT DECLARATIONS -----------------------------
class Infantry
    import verdana
    export x, y, col, initialize, load
    var x, y, col : int := 0

    proc initialize (gridX, gridY : int, teamOn : boolean)
        if teamOn = false then
            col := red
        else
            col := blue
        end if
        %drawbox (100 + (gridX * 16), 100 + (gridY * 16), 116 + (gridX * 16), 116 + (gridY * 16), col)
        Font.Draw ("I", 105 + (gridX * 16), 105 + (gridY * 16), verdana, col)
        x := gridX
        y := gridY
    end initialize
    proc load (unitx, unity, side : int)
        col := side
        x := x
        y := y
        Font.Draw ("I", 105 + (unitx * 16), 105 + (unity * 16), verdana, col)

    end load
end Infantry

class Tank
    import verdana
    export x, y, col, initialize, load
    var x, y, col : int := 0

    proc initialize (gridX, gridY : int, teamOn : boolean)
        if teamOn = false then
            col := red
        else
            col := blue
        end if
        %drawfillbox (100 + (gridX * 16), 100 + (gridY * 16), 116 + (gridX * 16), 116 + (gridY * 16), col)
        Font.Draw ("T", 105 + (gridX * 16), 105 + (gridY * 16), verdana, col)
        x := gridX
        y := gridY
    end initialize
    proc load (unitx, unity, side : int)
        col := side
        x := x
        y := y
        Font.Draw ("T", 105 + (unitx * 16), 105 + (unity * 16), verdana, col)
    end load
end Tank

class Artillery
    import verdana
    export x, y, col, initialize, load
    var x, y, col : int := 0

    proc initialize (gridX, gridY : int, teamOn : boolean)
        if teamOn = false then
            col := red
        else
            col := blue
        end if
        %drawfillbox (100 + (gridX * 16), 100 + (gridY * 16), 116 + (gridX * 16), 116 + (gridY * 16), col)
        Font.Draw ("A", 105 + (gridX * 16), 105 + (gridY * 16), verdana, col)
        x := gridX
        y := gridY
    end initialize
    proc load (unitx, unity, side : int)
        col := side
        x := x
        y := y
        Font.Draw ("A", 105 + (unitx * 16), 105 + (unity * 16), verdana, col)
    end load
end Artillery

% -------------- TURING WARS.T
View.Set ("graphics:max,max")

include "classes.t"

% Declare the terrain constants
const CITY := 1
const GRASS := 2
const FOREST := 3
const MOUNTAIN := 4
const ROAD := 5
const SHALLOW := 6
const WATER := 7
const SHORE := 8

var terrainTypes : array 1 .. 8 of int := init (42, 48, 2, 66, 15, 53, 55, 91)
var units : array 1 .. 100, 1 .. 100 of string                             % The array which stores where all the units are
var infantry : array 1 .. 100 of pointer to Infantry
var tank : array 1 .. 100 of pointer to Tank
var artillery : array 1 .. 100 of pointer to Artillery
var unitToAdd, infCount, tankCount, artyCount : int := 1
var map : array 1 .. 100, 1 .. 100 of int
var mx, my, mb, terrType : int := 1
var teamOn : boolean := false

% Initialize the Unit Classes -> Josh
for i : 1 .. 100
    new Infantry, infantry (i)
    new Tank, tank (i)
    new Artillery, artillery (i)
end for
% ********************* COMMON PROCEDURES AND FUNCTIONS *************************
var addTerr : boolean := false

function pixToGridX (mx : int) : int % -> The next 4 were done by both
    result (mx - 100) div 16
end pixToGridX

function pixToGridY (my : int) : int
    result (my - 100) div 16
end pixToGridY

function gridToPixX (gridX : int) : int
    result 100 + gridX * 16
end gridToPixX

function gridToPixY (gridY : int) : int
    result 100 + gridY * 16
end gridToPixY

function findSpaceInString (word : string) : int % -> Josh
    for i : 1 .. length (word)
        if word (i) = " " then
            result i
        end if
    end for
    result 0
end findSpaceInString

function isPassable (gridX, gridY : int) : boolean
    var terrtype : int
    terrtype := map (gridX, gridY)
    if terrtype = SHALLOW or terrtype = WATER then
        result false
    else
        result true
    end if
end isPassable

proc updateTile (tileX, tileY, terrType : int)         % -> Josh
    drawfillbox (100 + tileX * 16, 100 + tileY * 16, 116 + tileX * 16, 116 + tileY * 16, terrainTypes (map (tileX, tileY)))
end updateTile

proc initializeMap     % -> Josh
    for i : 1 .. 100
        for j : 1 .. 100
            map (i, j) := 2
            units (i, j) := ""
        end for
    end for
end initializeMap

proc drawMap     % -> Josh
    for i : 1 .. 40
        for j : 1 .. 40
            drawfillbox (100 + i * 16, 100 + j * 16, 100 + i * 16 + 16, 100 + j * 16 + 16, terrainTypes (map (i, j)))
        end for
    end for
end drawMap

% ********************* MAP EDITOR PROCEDURES AND FUNCTIONS *************************

proc initializeEditor
    % The Save Button
    drawbox (50, 250, 100, 275, black)
    drawfill (51, 251, red, black)
    Font.Draw ("SAVE", 58, 258, verdana, white)

    % The Load Button
    drawbox (50, 280, 100, 305, black)
    drawfill (51, 281, red, black)
    Font.Draw ("LOAD", 58, 288, verdana, white)

    var unitnames : array 1 .. 3 of string := init ("Inf.", "Tank", "Art.")
    Draw.Text ("Units", 125, 90, verdana, black)
    drawbox (65, 75, 95, 100, black)
    drawfill (66, 76, red, black)
    drawbox (65, 30, 95, 55, black)
    drawfill (66, 31, blue, black)
    if teamOn = false then
        drawbox (64, 29, 96, 56, white)
        drawbox (64, 74, 96, 101, green)
    else
        drawbox (64, 73, 96, 101, white)
        drawbox (64, 29, 96, 56, green)
    end if
    for u : 1 .. 3
        drawbox (130 + (u * 35), 55, 155 + (u * 35), 80, black)
        Draw.Text (unitnames (u), 130 + (u * 35), 40, verdana, black)
    end for
    % This first bit was done by Mike. Following done by Josh, with help from Mike
    for i : 1 .. 2
        for j : 1 .. 4
            if i = 1 then
                drawfillbox (25 + (i * 25), 100 + (j * 25), 50 + (i * 25), 125 + (j * 25), terrainTypes (j))
            else
                drawfillbox (25 + (i * 25), 100 + (j * 25), 50 + (i * 25), 125 + (j * 25), terrainTypes (4 + j))
            end if
        end for
    end for
end initializeEditor

proc changeTerrType (mx, my, terrType : int)
    map (pixToGridX (mx), pixToGridY (my)) := terrType
end changeTerrType

proc saveMap
    var filename : string := ""
    var output : int
    locate (1, 1)
    put "Enter a name: " ..
    get filename
    open : output, "maps\\" + filename + ".map", put
    for i : 1 .. 40
        for j : 1 .. 40
            put : output, map (i, j), " " ..
        end for
    end for
    for i : 1 .. 20
        if infantry (i) -> x > 0 then
            put : output, "i", infantry (i) -> col, " ", infantry (i) -> x, " ", infantry (i) -> y
        end if
        if tank (i) -> x > 0 then
            put : output, "t", tank (i) -> col, " ", tank (i) -> x, " ", tank (i) -> y
        end if
        if artillery (i) -> x > 0 then
            put : output, "a", artillery (i) -> col, " ", artillery (i) -> x, " ", artillery (i) -> y
        end if
    end for
    close : output
    locate (1, 1)
    put "Map Saving Completed."
end saveMap

proc loadMap
    var filename : string := ""
    var input : int
    var Char : string (1)
    var character : string
    var temp, temp2, unitX, unitY, side : int := 0
    locate (1, 1)
    put "Enter a name: " ..
    get filename
    open : input, "maps\\" + filename + ".map", get

    if Error.Last = eNoError then
        for i : 1 .. 40
            for j : 1 .. 40
                get : input, Char
                if strintok (Char) = false and Char not= " " then
                    put "Data file corrupted! Tile (", i, ",", j, ") returned to Default Type"
                elsif strintok (Char) then
                    map (i, j) := strint (Char)
                end if
            end for
        end for
        drawMap

        loop
            exit when eof (input)
            get : input, character : *
            if length (character) > 6 then              % Evil little region of code, took me 20min!
                side := strint (character (2))
                temp := findSpaceInString (character)
                temp2 := temp + findSpaceInString (character (temp + 1 .. *))
                unitX := strint (character (temp + 1 .. temp2 - 1))
                unitY := strint (character (temp2 + 1 .. *))
            end if
            if character (1) = "i" then
                infantry (infCount) -> load (unitX, unitY, side)
                infCount += 1
            elsif character (1) = "t" then
                tank (tankCount) -> load (unitX, unitY, side)
                tankCount += 1
            elsif character (1) = "a" then
                artillery (artyCount) -> load (unitX, unitY, side)
                artyCount += 1
            end if
        end loop
        close : input
    else
        locate (1, 1)
        put "File not Found."
    end if
end loadMap

proc mapEditor          % Created by both
    initializeMap
    initializeEditor
    drawMap

    loop
        mousewhere (mx, my, mb)
        locate (1, 1)
        put mx, " ", my ..
        locate (2, 1)
        if teamOn = false then
            put "RED TEAM (Human)" ..
        else
            put "BLUE TEAM (Computer)" ..
        end if

        if mx >= 65 and mx <= 95 then
            if my >= 75 and my <= 100 and mb = 1 then
                teamOn := false
            elsif my >= 30 and my <= 55 and mb = 1 then
                teamOn := true
            end if
        end if

        % Choose units to add
        if my > 54 and my < 80 and mb = 1 then
            if mx > 165 and mx < 190 then
                unitToAdd := 1
                addTerr := false
            elsif mx > 200 and mx < 225 then
                unitToAdd := 2
                addTerr := false
            elsif mx > 235 and mx < 260 then
                unitToAdd := 3
                addTerr := false
            end if
        end if

        % Add New Units to the Map
        if mx > 116 and my > 115 and mx < 758 and my < 756 and mb = 1 and length (units (pixToGridX (mx), pixToGridY (my))) = 0 and isPassable (pixToGridX (mx), pixToGridY (my))
                and addTerr = false then
            if unitToAdd = 1 then
                new Infantry, infantry (infCount)
                infantry (infCount) -> initialize (pixToGridX (mx), pixToGridY (my), teamOn)
                units (pixToGridX (mx), pixToGridY (my)) := "i" + intstr (infCount)
                infCount += 1
                delay (50)
            elsif unitToAdd = 2 then
                new Tank, tank (tankCount)
                tank (tankCount) -> initialize (pixToGridX (mx), pixToGridY (my), teamOn)
                units (pixToGridX (mx), pixToGridY (my)) := "t" + intstr (tankCount)
                tankCount += 1
                delay (50)
            elsif unitToAdd = 3 then
                new Artillery, artillery (artyCount)
                artillery (artyCount) -> initialize (pixToGridX (mx), pixToGridY (my), teamOn)
                units (pixToGridX (mx), pixToGridY (my)) := "a" + intstr (artyCount)
                artyCount += 1
                delay (50)
            end if
        end if

        % Choose the terrain type to add
        if mx > 50 and my > 125 and mx < 100 and my < 225 and mb = 1 then
            if mx < 75 then
                terrType := 1 + (my - 125) div 25
                addTerr := true
            else
                terrType := 5 + ((my - 125) div 25)
                addTerr := true
            end if
        end if
        if mx > 116 and my > 115 and mx < 758 and my < 756 and mb = 1 and addTerr = true then
            changeTerrType (mx, my, terrType)
            updateTile (pixToGridX (mx), pixToGridY (my), map (pixToGridX (mx), pixToGridY (my)))
        end if
        if mx > 50 and my > 250 and mx < 100 and my < 275 and mb = 1 then
            saveMap
        elsif mx > 50 and my > 280 and mx < 100 and my < 305 and mb = 1 then
            loadMap
        end if

        % Update display
        if addTerr = false then
            if unitToAdd = 1 then
                put "INFANTRY   " ..
            elsif unitToAdd = 2 then
                put "TANKS      " ..
            elsif unitToAdd = 3 then
                put "ARTILLERY  " ..
            end if
        elsif addTerr = true then
            if terrType = 1 then
                put "CITY    " ..
            elsif terrType = 2 then
                put "GRASS     " ..
            elsif terrType = 3 then
                put "FOREST    " ..
            elsif terrType = 4 then
                put "MOUNTAIN    " ..
            elsif terrType = 5 then
                put "ROAD    " ..
            elsif terrType = 6 then
                put "SHALLOWS    " ..
            elsif terrType = 7 then
                put "WATER    " ..
            elsif terrType = 8 then
                put "SAND    " ..
            end if
        end if
    end loop

end mapEditor

mapEditor
% BEACHHEAD MAP (FOR TESTING)
4 4 4 4 4 4 4 4 4 4 4 3 3 2 2 2 5 2 2 2 2 2 2 3 3 4 3 4 4 4 4 4 4 4 4 4 4 2 2 2 4 4 4 4 4 4 4 4 4 4 3 3 3 2 2 2 5 2 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 4 4 2 2 2 4 4 4 4 4 4 4 4 3 3 3 3 2 2 2 5 5 2 2 2 2 3 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 2 2 2 2 4 4 4 4 4 4 3 3 3 3 2 2 2 2 5 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 2 2 2 2 3 4 4 4 3 3 3 3 3 2 2 2 2 5 5 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 2 2 2 2 2 3 3 3 3 3 3 3 2 2 2 2 2 5 2 2 2 2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