aboutsummaryrefslogtreecommitdiffstats
path: root/Sources/game/main.swift
blob: f0b5b179be68e35fda983cde6534ac369c7f81bc (plain) (blame)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import Foundation

var texts = [
    "0": [
        "toShow": "Hello dear player, this will be simple game, make your choices wisely as it is possible to die. Have a nice day. Click enter to play.",
        "next": "1",
        "where": [:],
    ],
]
texts["1"] = [
        "toShow": "You're next to small lake, you don't remember where you are, you have strong feeling that you have a mission and need to go to the nearest castle to find mage, luckily for you there's a sign pointing you with probably good direction. Go to the \n[1] castle, \n[2] stay here, \n[3] kill yourself, because of a very deep pain that you have always felt",
        "next": "1",
        "where": [
            "1": "4",
            "2": "3",
            "3": "2",
        ],
    ]
texts["2"] = [
        "toShow": "You killed yourself. We could count that as a win I guess. <enter>",
        "next": "-2",
        "where": [:],
    ]
texts["3"] = [
        "toShow": "You can stay here, please feel like home. \n[1] Stay for a bit longer, \n[2] go to the castle",
        "next": "1",
        "where": [
            "1": "3",
            "2": "4",
        ],
    ]
texts["4"] = [
        "toShow": "On your way to the castle a small witch alike monster crossed your way and made you fight him. It wasn't hard at all. <enter>",
        "next": "5",
        "where": [:],
    ]
texts["5"] = [
        "toShow": "Finally, you've found the castle, you were almost there, but guard at the entrance said that he would never ever let somebody like you in. You have to find another way in. Look for it \n[1] left to the main entrance or \n[2] right",
        "next": "1",
        "where": [
            "1": "8",
            "2": "6",
        ],
    ]
texts["6"] = [
        "toShow": "You found a small cabin that looks like witch hut, \n[1]explore it or \n[2]go back",
        "next": "1",
        "where": [
            "1": "30",
            "2": "5",
        ],
    ]
texts["7"] = [
        "toShow": "You are waiting for the miracle, \n[1] wait more or \n[2] go back",
        "next": "1",
        "where": [
            "1": "7",
            "2": "5",
        ],
    ]
texts["8"] = [
        "toShow": "You have found sewage, big enough that theoratically you could go through it. \n[1] Try going through or\n[2] go back ",
        "next": "1",
        "where": [
            "1": "9",
            "2": "5",
        ],
    ]
texts["9"] = [
        "toShow": "You are in city! Maybe you smell like sh*t but you are in the city. You realised that it doesn't mean anything if you can't find a wizard and you don't know where he is. <enter>",
        "next": "10",
        "where": [:],
    ]
texts["10"] = [
        "toShow": "[1] Ask about the mage in tavern, \n[2] look for magic tower in rich part of the city, \n[3] try screaming very loud, \n[4] try to remind yourself where he had to be",
        "next": "1",
        "where": [
            "1": "11",
            "2": "12",
            "3": "14",
            "4": "13",
        ],
    ]
texts["11"] = [
        "toShow": "People in tavern tells you that magic doesn't exist and you should drink something because you're talking nonsense. \n[1] Drink beer and go back outside, \n[2] go back outside",
        "next": "1",
        "where": [
            "1": "10",
            "2": "10",
        ],
    ]
texts["12"] = [
        "toShow": "There are no towers in the whole city, not even one small church tower, what a strange city you thought. Go back <enter>",
        "next": "10",
        "where": [:],
    ]
texts["13"] = [
        "toShow": "You couldn't remind yourself anything, you realised that you dont know your name, or the name of your mother. Go back <enter>",
        "next": "10",
        "where": [:],
    ]
texts["14"] = [
        "toShow": "You started screaming and next you remember is lying down on the floor in big old house. Was I teleported here? Did I drink too much? Look around and try to find out where you are <enter>",
        "next": "15",
        "where": [:],
    ]
texts["15"] = [
        "toShow": "[1] Go through big old wooden doors, \n[2] search the wardrobe, \n[3] inspect the ceiling",
        "next": "1",
        "where": [
            "1": "18",
            "2": "17",
            "3": "16",
        ],
    ]
texts["16"] = [
        "toShow": "That's very good ceiling, even you're house doesn't have such a good one. It's strange that you remember your house but not your name. You should probably try looking somewhere else <enter>",
        "next": "15",
        "where": [:],
    ]
texts["17"] = [
        "toShow": "Wardrobe only has big old red hat. You decided to put it on, just because. You should probably try looking somewhere else <enter>",
        "next": "31",
        "where": [:],
    ]
texts["18"] = [
        "toShow": "You found yourself in the middle of the ocean on remote island. So this what you get for pissing of the wizard or drinking too much. There's no hope from now on. \n[1] You can kill yourself or \n[2] wait till the death will come to you",
        "next": "1",
        "where": [
            "1": "2",
            "2": "19",
        ],
    ]
texts["19"] = [
        "toShow": "This is the end, maybe death can't find me here and I will be eternal? There's only one way to find out <enter>",
        "next": "20",
        "where": [:],
    ]
texts["20"] = [
        "toShow": "Wait",
        "next": "20",
        "where": [:],
    ]
texts["30"] = [
        "toShow": "Cabin looks very comfortable from the inside, you see some witch things that maybe will do something good for you. \n[1]Try wearing a witch hat or \n[2]try witch potion",
        "next": "1",
        "where": [
            "1": "31",
            "2": "32",
        ],
    ]
texts["31"] = [
        "toShow": "Hat seems pretty normal at first, but then you start to see only pink, and then you stop feeling the floor, and then you see completely new ideal world made up of cheese. To be continued... <enter>",
        "next": "-2",
        "where": [:],
    ]
texts["32"] = [
        "toShow": "Potion tasted very well, but now you can see through walls and you can hear people thoughts. \n[1]Go to city to try your new skills or \n[2]stay here and study yourself",
        "next": "1",
        "where": [
            "1": "34",
            "2": "33",
        ],
    ]
texts["33"] = [
        "toShow": "You started thinking about fundamental questions about universe and you know answers to each one, you become so intelligent that you merge with the universe. {I would count that as I win}",
        "next": "-2",
        "where": [:],
    ]
texts["34"] = [
        "toShow": "On your way to the city you see every living creature with small white border around it, unknowingly you start flying. You come to the guard and tell him that you see his wife with another man in the bed. He starts crying and runs to home, you fly to the king and tell him that this is now your kingdom. You became king and ruled those lands forever. {That's definitely a win}",
        "next": "-2",
        "where": [:],
    ]

@discardableResult
func shell(_ args: String...) -> Int32 {
    let task = Process()
    task.launchPath = "/usr/bin/env"
    task.arguments = args
    do {
    try task.run()
    } catch {}
    task.waitUntilExit()
    return task.terminationStatus
}

struct Paragraph {
    var textToShow = ""
    var id = ""
    var next = ""
    var whereD: [String:String] = [:]

    init(textToShow: String = "",  id: String = "",  next: String = "-1", whereD: Any = [:]) {
        self.textToShow = textToShow
        self.id = id
        if next != "-1" {
         self.next = next
        }
        self.whereD = whereD as! [String:String]
    }

    func play() {
        shell("clear")
        var path = "/home/maks/school/mobileApps/mendela/kappalepeli/Sources/game/imgs/"
        // path += self.id + ".txt"
        path += self.id + ".tpng"
        do {
            print(try NSString(contentsOfFile: path, encoding: 4))
        } catch {}
        print(self.textToShow)
        let choice = readLine()!
        var n = "-1"
        if self.textToShow.contains("[") {
            n = self.whereD[choice] ?? "-1"
        } else {
            n = self.next
        }
        if n != "-1" {
            play_next(n)
        } else if n == "-2" {
            exit(0)
        } else {
            print("Invalid choice")            
            exit(1)
        }
    }
}
var ps: [Paragraph] = []
func play_next(_ id: String) {
    for p in ps {
        if p.id == id {
            p.play()
        } 
    }
}
for (k, v) in texts {
    let p = Paragraph(textToShow: v["toShow"]! as! String, id: k, next: v["next"]! as! String, whereD: v["where"]!)
    ps.append(p)
}
ps.first(where: {$0.id == "0"})!.play()