summaryrefslogtreecommitdiff
path: root/game/script.rpy
blob: 63c7071523b481893c296c9dafe4e29b3e699e75 (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# The script of the game goes in this file.
# Declare images
#image bg room = Frame("gfx/bg/port.png", 0, 0)
image dialog rebel = "gfx/dialog/rebel.png"

# Declare characters used by this game. The color argument colorizes the
# name of the character.

define e = Character("Player")

label splashscreen:
    centered "{b}{color=#f00}Disclaimer{/color}\n\nBy running this software, you affirm that you have the right to do so.\nNo license is provided for using this demo software.\n\n\
\
\
{color=#f00}This client may contain assets which {u}does not{/u} belongs to it.\n\
If you are the legal owner of an asset, or their legal representant,\n\
And find an asset of yours illegally used, please, fill a DMCA complaint\n\
by sending an email to {a=mailto:dmca@tmw2.org}dmca@tmw2.org{/a}.{/color}\n\
\n\
\n\
No warranties.{/b}{fast}"
    return


# Inform you are now ingame
label start:
    # Begin the initial displays
    # TODO: a fairy :(
    scene black
    show spinner at truecenter

    # Initial configuration
    call prestart

    # Run updater
    $ renpy.invoke_in_thread(GAME_UPDATER)

    # Estabilish the connection to server
    python:
        try:
            renpy.invoke_in_thread(supervisor, persistent.ssl_enabled)
        except:
                # Enter in infinite loop, this will never resolve
                stdout("Unrecoverable error, the program is dead.")
                tr_load=False

        # Block the main thread until the socket connection is done
        while not tr_load:
            sdelay()

    $ stdout("Connection established!")

    # Open game
    scene bg town with Dissolve(0.3)
    pause 0.1
    #show dialog rebel
    $ password=persistent.password
    $ email=""

    # Check if we need to register
    if persistent.password is None:
        call screen welcome
        return

    # Otherwise just login
    jump login

############################################################################
# Login procedures
label login:
    $message=login()
    # TODO=load message
    $ stdout(message)
    python:
        try:
            message=int(message)
            dlcode=copy.copy(message)
        except:
            print "AN ERROR HAPPENED AT LOGIN TIME"
            stdout("Invalid: "+str(message))
            message=-1

    if (message < 5000):
        if (message == ERR_LOGIN_DEFAULT):
            e "Error Code: [message]{fast}\n\nCould not estabilish connection to server."
            jump login
        else:
            e "Error Code: [message]{fast}\n\nCannot login to server. Terminating."
        jump quit

    # Successful login? Update client data and begin routines
    $ stdout("Login successful.")

    # Load game to memory
    python:
        tr_load=False
        renpy.invoke_in_thread(GAME_LOADER)
        #while not tr_load:
        #    sdelay()

    $ persistent.password=password
    $ dlcode-=5000
    #$ ping_routine()
    python:
        try:
            renpy.invoke_in_thread(irc_loop)
        except:
            stdout("IRC disabled")
            pass

    # We're now logged in, load misc data (TODO: What if ERR_ is returned?)
    $ who = -1
    $ inv=get_inventory()
    python:
        for a in inv:
            Player["inv"].append(a)

        # Run AP Timer if needed (FIXME)
        if (Player["ap"] < Player["max_ap"]):
            ApTimer=Timer(360.0, ap_restore)
            ApTimer.start()

    # Remove some temporary variables
    $ del password
    $ stdout("Waiting for game data to be loaded to memory...")

    # Wait until everything is loaded
    python:
        while not tr_memcheck:
            sdelay()

    if (email):
        $ del email
        jump prologue

    # Begin loop
    play music MUSIC_TOWN fadein 0.5
    $ stdout("Game started successfully!")

    # Show news (if they exist and you haven't logged in past day)
    if dlcode:
        if (len(allnews) >= 1):
            call screen show_news
        else:
            $stdout("This server doesn't have a MOTD")
        # TODO: Daily login rewards screen

    jump loop

label loop:
    # Delete predictions, if possible
    $ renpy.free_memory()

    # Maybe we're in combat? If we are, you cannot go to town
    if (Player["status"] >= ST_QUEST):
        window hide None
        $ renpy.call_screen("msgbox", "{b}Battle{/b}\n\nYou are in battle. You'll be brought back to combat.")
        #window show None
        $ Battle=reload_battle()
        # TODO: Handle errors
        if (Battle in [ERR_JSONDECODER]):
            e "Unknown Error: [ERR_JSONDECODER]"
            jump loop
        play music MUSIC_BATTLE.id() fadein 0.5
        jump combat


    $ hud_show()
    menu:
        "Development Options" if config.developer:
            $message="Development login OK"
            menu:
                "Send ping packet":
                    $message=send_packet("ping")
                    pass
                "Redeclare Image":
                    $m=renpy.call_screen("input_box", "Image Name: ")
                    $renpy.image("redeclared", m)
                    "We'll now display Redeclared"
                    show redeclared at truecenter with dissolve
                    pause
                    "So?"
                    hide redeclared with None
                    pass
                "Quest Editor":
                    jump quest_editors
                "List party members":
                    $message=get_party(1)
                "Update client":
                    jump updater_editors
                "Do nothing":
                    pass
        "World Map":
            jump quest_select
        "Manage Party":
            jump party_lobby_enter
        "Show inventory":
            jump inventory
        "Visit tavern":
            jump tavern
        "PUB":
            # TODO this needs its own label
            if persistent.nickname is not None:
                python:
                    while True:
                        _return=renpy.call_screen("pub")
                        if _return != "":
                            res=irc_send(persistent.nickname, _return)
                        else:
                            break
            else:
                "ERROR" "You are not authorized to complete this operation.\n\nERR_NICK"

        "Logout":
            #$message=send_packet("logout")
            jump quit
            #"Server replies:" "[message]"
            #return

    if (message == OFFLINEMSG):
        "Server replies:" "[message]\n\nYou are offline?"
        return

    if (message not in [ERR_OK, OKMSG, True]):
        "Server replies:" "[message]"

    jump loop
    # This ends the game.
    return


label restore:
    # Restore town view
    scene bg town
    #show dialog rebel
    play music MUSIC_TOWN fadein 0.5
    $ hud_show()
    jump loop


label prologue:
    $ hud_story()
    call SQ00001_prologue
    # Automatically begin quest
    $ Battle=loadquest(1)
    $ update_ap(-1)
    play music MUSIC_BATTLE.id() fadein 0.5
    $ renpy.free_memory()
    window hide
    jump combat

    #"Go to Capital, where you'll be formally appointed and receive a mission"
    #"Graduating: You (tactician / whatever) - Auto-Presentation"
    #"You decide it is better to travel to the Capital not alone."
    #"Giulio, graduate as knight."
    #"If you complete the mission at Capital, you'll get a job."
    # --> Mission is derailed if favor of more epic adventure
    #"Reaching Capital is a test on itself, to see if you have what it takes."
    #"You can use Crystals to recruit mercenaries and colleagues."
    # We may want to automatically begin fight 01, and keep important info
    # on story.json (in case you close the app)
    jump restore

label quit:
    $ stdout("Received quit signal!")
    python:
        # Delete ApTimer
        try:
            ApTimer.cancel()
            del ApTimer
        except:
            pass

        # If needed, logout
        try:
            token=Player["token"]
            send_packet("logout")
            del Player
        except:
            pass

        # FIXME
        # If needed, close the socket
        try:
            ws.close(reason="Quit")
        except:
            pass

        # Delete variables
        try:
            del allunitsbase
            del allunits
            del header
            del allnews
            del allstory
            del allworld
            del alltaverns
        except:
            pass

        # Terminate IRC connection
        try:
            irc_kill()
        except NameError:
            pass

    # Data cleaned up
    # Destroy the socket
    $ persistent.ws = None
    $ print("Sockets closed and data flushed!")
    return