aboutsummaryrefslogtreecommitdiff
path: root/main.odin
blob: 2f0152db3a750120276a3cb4cc7d90ca5bb9946a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main

import "core:fmt"
import "core:os"
import "core:bufio"
import "core:io"
import "core:strconv"
import "core:strings"
import "core:math/rand"

instructions: string = "Use space to stay still, and HJKL to move."

Direction :: enum {still, up, down, left, right}

rand_state := rand.create(69)

state_type :: struct {
    gameboard :  [16][16]rune,
    flow :       [16]bool,
    spawning :   [16]bool,
    player_row : int,
}

block::'O'
water::'~'
player::'@'

difficulty::0.4
log_length::0.6

main :: proc() {
    //fmt.println("What's your name? ")
    //name: string = getln()
    //defer delete(name)
    //fmt.printf("Hello %v! Welcome to shitty frogger.\n\n", name)

    win : bool = false

    state : state_type

    populate_board(&state)
    
    fmt.println(state)
    draw_board(state)

    for !win {
        fmt.println(instructions)
        walk(&state, getdir())
        progress_board(&state)
        draw_board(state)
        if state.player_row==0 {
            win = true
        }
    }
    fmt.println("You win, brotha.")
}

getln :: proc() -> string {
    buf: [256]u8
    bytes_read, ok := os.read(os.stdin, buf[:])
    //assert(ok)
    read_input := string(buf[:bytes_read-2])
    return strings.clone(read_input)
}

getint :: proc() -> int {
    buf: [256]u8
    for {
        read_input := getln()
        output, ok := strconv.parse_int(read_input)
        if ok {
            return output
        } else {
            fmt.println("That's not a normal whole number, mate.")
        }
    }
}

getrune :: proc() -> rune {
    buf: [256]u8
    bytes_read, ok := os.read(os.stdin, buf[:])
    //assert(ok)
    read_input := rune(buf[0])
    return read_input
}

getdir :: proc() -> Direction {
    for {
        switch getrune() {
            case ' ':
                return .still
            case 'h':
                return .left
            case 'j':
                return .down
            case 'k':
                return .up
            case 'l':
                return .right
            case: // default
                fmt.println("Not a valid direction.")
        }
    }
}

populate_board :: proc(state:^state_type) {
    using state
    for i:=0; i<len(flow); i+=1 {
        flow[i] = rand.float32(&rand_state) < 0.5
    }
    for i:=0; i<len(flow); i+=1 {
        spawning[i] = rand.float32(&rand_state) < 0.5
    }
    for _ in gameboard {
        progress_board(state)
    }
    player_row=len(gameboard)-1
    gameboard[player_row][len(gameboard)/2] = player
}

progress_board :: proc(state:^state_type) {
    // TODO: MAKE THIS ACTUALLY DO ANYTHING
    for row, i in state.gameboard {
        new_block:rune=water
        if state.spawning[i] {
            new_block=block
        }
        append(&state.gameboard[i], new_block, state.flow[i])
    }
    for is_spawning, i in state.spawning {
        if rand.float32(&rand_state)<1-log_length {
            if is_spawning {
                if rand.float32(&rand_state) < difficulty {
                    state.spawning[i] = !is_spawning
                }
            } else {
                if rand.float32(&rand_state) > difficulty {
                    state.spawning[i] = !is_spawning
                }
            }
        }
    }
}

append :: proc(array:^[16]rune, appendee:rune, front:bool) {
    if front {
        // Put appendee at the start of rune-array
        // X 1 2 3 4 5
        // ^ # # # # #
        for i:=len(array)-1; i!=0; i-=1 {
            array[i] = array[i-1]
        }
        array[0] = appendee
        return
    } else {
        // Put appendee at the end of rune-array
        // 0 1 2 3 4 X
        // # # # # # ^
        for _, i in array {
            if i < len(array)-1 {
                array[i] = array[i+1]
            }
        }
        array[len(array)-1] = appendee
        return
    }
}

find_player :: proc(state:state_type) -> int {
    for x, i in state.gameboard[state.player_row] {
        if x==player {return i}
    }
    return -1
}

walk :: proc(state:^state_type, dir:Direction) {
    using state
    player_column : int = find_player(state^)
    fmt.println(player_column)
    current_rubric : ^rune = &gameboard[player_row][player_column]
    target : ^rune = current_rubric
    switch dir {
        case .up:
            if player_row!=0 {
                target = &gameboard[player_row-1][player_column]
            }
            if target^==block {player_row-=1}
        case .down:
            if player_row!=len(gameboard)-1 {
                target = &gameboard[player_row+1][player_column]
            }
            if target^==block {player_row+=1}
        case .left:
            if player_column!=0 {
                target = &gameboard[player_row][player_column-1]
            }
        case .right:
            if player_column!=len(gameboard[0])-1 {
                target = &gameboard[player_row][player_column+1]
            }
        case .still:
            return
        case: // default
            fmt.println("Something just went very wrong. That's not a direction you can walk in.")
            return
    }
    if target^==block {
        target^=player
        current_rubric^=block
    }
    return
}

draw_board :: proc(state:state_type) {
    for row, i in state.gameboard {
        if state.flow[i] {
            fmt.printf(">  ")
        } else {
            fmt.printf("<  ")
        }
        for rubric, j in row {
            fmt.printf("%v ", state.gameboard[i][j])
        }
        fmt.printf("\n")
    }
}