summaryrefslogtreecommitdiff
path: root/game/player.rpy
blob: 0f7d9ffde8d4d055f5496ba52aa8f02c907e173a (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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
########################################################################################
#     This file is part of Spheres.
#     Copyright (C) 2019  Jesusalva

#     This library is free software; you can redistribute it and/or
#     modify it under the terms of the GNU Lesser General Public
#     License as published by the Free Software Foundation; either
#     version 2.1 of the License, or (at your option) any later version.

#     This library is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#     Lesser General Public License for more details.

#     You should have received a copy of the GNU Lesser General Public
#     License along with this library; if not, write to the Free Software
#     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
########################################################################################
# Player data displays

init -10 python:
    import copy

init python:
    import json
    from threading import Timer

    def inventoryplace():
        global Player
        # Return next free slot index
        try:
            return Player["inv"].index(None)
        except:
            return len(Player["inv"])

    def get_inventory():
        raw=send_packet("get_inv", "")

        pt=json_decode(raw)
        if (pt == ERR_JSONDECODER):
            return ERR_JSONDECODER

        if (pt == FAILUREMSG):
            # TODO: msgbox you're offline, quit
            return ERR_LOGIN_DEFAULT

        print "get_inv(): "+str(pt)
        return pt

    # Function which list all unit id on party
    def party_dupcheck(pid):
        ar=[]
        for unt in Player["party_%s" % pid]:
            ar.append(unt["unit_id"])
        return ar

    def get_party(pid, internal=False):
        raw=send_packet("get_party", str(pid))

        pt=json_decode(raw)
        if (pt == ERR_JSONDECODER):
            return ERR_JSONDECODER

        Player["party_%d" % pid]=pt
        if (internal):
            return False

        allmem=""
        for member in pt:
            # Skip'invalid members
            if member["unit_id"] <= 0:
                continue

            # TODO: show image
            rfc=Player["inv"][member["inv_id"]]

            allmem+="%d★ %s (Lv %d, %d exp)\n" % (allunits[member["unit_id"]]["rare"], allunits[member["unit_id"]]["name"], rfc["level"], rfc["exp"])
            #Player["party_1"].append(member)

        renpy.call_screen("msgbox", "Party %d\n %s" % (pid, allmem))
        return True

    def ap_restore():
        global ApTimer, Player

        Player["ap"]+=1
        if (Player["ap"] < Player["max_ap"]):
            try:
                ApTimer.cancel()
            except:
                pass
            ApTimer=Timer(360.0, ap_restore)
            ApTimer.daemon=True
            ApTimer.start()
        return

    def update_ap(newval):
        global ApTimer, Player

        running=False
        if (Player["ap"] < Player["max_ap"]):
            running=True

        # Update AP
        # TODO: Do we allow over-the-cap AP?
        Player["ap"]+=newval

        # Handle the timer
        if (not running and Player["ap"] < Player["max_ap"]):
            ApTimer=Timer(360.0, ap_restore)
            ApTimer.daemon=True
            ApTimer.start()
        if (running and Player["ap"] >= Player["max_ap"]):
            ApTimer.cancel()

        return

    def set_party(pid, members):
        raw=send_packet("set_party", """{"party_id": %d, "formation": %s}""" % (pid, str(members)))

        pt=json_decode(raw)
        if (pt == ERR_JSONDECODER):
            return ERR_JSONDECODER

        return True

    # Ping function, it keeps the connection alive.
    def ping_routine():
        global Pinger, OFFLINEMSG

        pinged=send_packet_now("ping", legacy=True)

        # Something went wrong
        if pinged != "pong":
            stdout("ERROR, Unexpected reply: %s (expected: pong)" % pinged)
            # Connection is already dead; Why bothering
            if pinged == OFFLINEMSG:
                return
            # TODO: Terminate connection on our end?
            timetowait=60.0
        else:
            timetowait=300.0

        Pinger=Timer(float(timetowait), ping_routine)
        Pinger.daemon=True
        Pinger.start()
        return

    # Techinically a battle function, readjusts a value
    # Rarity affects directly primary stat. We should distribute 140 points
    def readjust_status(rar, job, hp=True):
        newv=50
        # Imbalanced class
        if (job == Job_Swordsman):
            if hp:
                newv+=30+rar
            else:
                newv+=10+(rar/2)
        elif (job == Job_Mage):
            if hp:
                newv+=10+(rar/2)
            else:
                newv+=30+rar
        # Balanced class
        elif (job == Job_Assassin):
            if hp:
                newv+=15+(rar/2)
            else:
                newv+=25+rar
        elif (job == Job_Archer):
            if hp:
                newv+=25+rar
            else:
                newv+=15+(rar/2)
        # All-rounder class, with no steady growth
        elif (job == Job_Gunner):
            if hp:
                newv+=20+(rar/2)
            else:
                newv+=20+(rar/2)

        return newv

    def party_update(pid, pix):
        newindex=renpy.call_screen("inventory", True)

        # We only need inventory id's
        # So make a Party variable with this
        Party=dlist()
        for une in Player["party_%d" % pid]:
            Party.append(une["inv_id"])

        if debug:
            print str(Party)

        # Add unit, but only if no duplicates are found
        # Exception: If it is -1, then we can add
        if newindex != -1:
            dupcheck=party_dupcheck(pid)
            dupid=Player["inv"][newindex]["unit_id"]
            if dupid in dupcheck:
                renpy.notify(_("You cannot add duplicate party members!"))
            else:
                Party[pix]=newindex
        else:
            Party[pix]=newindex

        if debug:
            print str(Party)

        # Set party
        set_party(pid, Party)
        return True

    def evocheck(level, unit):
        # Does an evolved form exist?
        print "evocheck lv %d" % level
        try:
            nu=unit["unit_id"]+1
            #print "nu is: %d" % int(nu)
            next_name=allunits[nu]["name"]
            #print "next name: %s" % next_name
        except:
            return False

        # Level requeriment
        return level == unit["max_level"]

screen inventory(blank=False, filters="True"):
    # window
    # hbox
    vpgrid:
        cols 4
        spacing 5
        draggable True
        mousewheel True
        scrollbars "vertical"
        side_xalign 0.5
        xoffset 15
        #yoffset 45
        #xfill True
        yfill True

        # The close button returns -1 and comes first
        imagebutton:
                if not blank:
                    idle At("gfx/square/back_idle.png", czoom_70)
                    hover At("gfx/square/back_hover.png", czoom_70)
                else:
                    idle At("gfx/square/bg.png", czoom_70)
                action Return(-1)

        for i, item in enumerate(Player["inv"]):
            # We don't care for None items
            if item is not None:
                # Needed because eval :rolling_eyes:
                #$ ir=copy.copy(item["rare"])
                #$ print str(locals())
                python:
                    evl=False
                    #print "---- repr"
                    try:
                        alu=allunits[item["unit_id"]]
                    except:
                        alu={}
                        stdout("ERROR, alu: not defined, index %d" % i)
                    evl=eval(filters, globals(), locals())
                    #print str(evl)
                    #print str(filters)
                    #print str(item)
                    #print str(alu)
                if evl:
                    imagebutton:
                        idle At(Composite(
                                (340, 340),
                                (0, 0), "gfx/square/bg.png",
                                (0, 0), "gfx/square/units/%d.png" % item["unit_id"],
                                (0, 0), "gfx/square/%d.png" % allunits[item["unit_id"]]["rare"],
                            ), czoom_70)
                        action Return(i)
                        #alternate "Show the char data chart"

screen party_main():
    default party = 1
    vbox:
        xalign 0.5
        yalign 0.3
        label _("{size=32}{color=#fff}Party %d{/color}{/size}" % (party))
        null height 20
        hbox:
            for i, item in enumerate(Player["party_%d" % party]):
                imagebutton:
                    if item["unit_id"] > 0:
                        idle At(Composite(
                                (340, 340),
                                (0, 0), "gfx/square/bg.png",
                                (0, 0), "gfx/square/units/%d.png" % item["unit_id"],
                                (0, 0), "gfx/square/%d.png" % allunits[item["unit_id"]]["rare"],
                            ), czoom_70)
                    else:
                        idle At("gfx/square/bg.png", czoom_70)
                    action Return([party, i])
        null height 80
        # The close button returns -1 and comes last (TODO)
        imagebutton:
                idle At("gfx/square/back_idle.png", czoom_75)
                hover At("gfx/square/back_hover.png", czoom_75)
                action Return(-1)


screen upgrade_char():
    vbox:
        xalign 0.5
        yalign 0.3

        # The unit
        hbox:
            add At(Composite(
                        (340, 340),
                        (0, 0), "gfx/square/bg.png",
                        (0, 0), "gfx/square/units/%d.png" % unit["unit_id"],
                        (0, 0), "gfx/square/%d.png" % unit["rare"],
                    ), czoom_70)
            null width 10
            label _("{size=32}{color=#fff}%s{/color}{/size}" % (unit["name"]))

        null height 20
        $i=0
        hbox:
            for ignored in enumerate(material):
                if i < 4:
                    imagebutton:
                        if material[i] >= 0:
                            idle At(Composite(
                                    (340, 340),
                                    (0, 0), "gfx/square/bg.png",
                                    (0, 0), "gfx/square/units/%d.png" % Player["inv"][material[i]]["unit_id"],
                                    (0, 0), "gfx/square/%d.png" % allunits[Player["inv"][material[i]]["unit_id"]]["rare"],
                                ), czoom_70)
                        else:
                            idle At("gfx/square/bg.png", czoom_70)
                        action Return(i)
                    $i+=1

        null height 20
        hbox:
            for ignored in enumerate(material):
                if i < 8:
                    imagebutton:
                        if material[i] >= 0:
                            idle At(Composite(
                                    (340, 340),
                                    (0, 0), "gfx/square/bg.png",
                                    (0, 0), "gfx/square/units/%d.png" % Player["inv"][material[i]]["unit_id"],
                                    (0, 0), "gfx/square/%d.png" % allunits[Player["inv"][material[i]]["unit_id"]]["rare"],
                                ), czoom_70)
                        else:
                            idle At("gfx/square/bg.png", czoom_70)
                        action Return(i)
                    $i+=1

        null height 80
        # The close button returns -1 and comes last (TODO)
        hbox:
            textbutton _("Merge") action Return(-2)
            null width 80
            textbutton _("Leave") action Return(-1)


screen evolve_char():
    vbox:
        xalign 0.5
        yalign 0.3

        # The unit
        hbox:
            add At(Composite(
                        (340, 340),
                        (0, 0), "gfx/square/bg.png",
                        (0, 0), "gfx/square/units/%d.png" % unit["unit_id"],
                        (0, 0), "gfx/square/%d.png" % unit["rare"],
                    ), czoom_70)
            null width 10
            add At("gfx/evol.png", czoom_70)
            null width 10
            add At(Composite(
                        (340, 340),
                        (0, 0), "gfx/square/bg.png",
                        (0, 0), "gfx/square/units/%d.png" % next["unit_id"],
                        (0, 0), "gfx/square/%d.png" % next["rare"],
                    ), czoom_70)
            #label _("{size=32}{color=#fff}%s\n↓\n%s{/color}{/size}" % (unit["name"], next["name"]))

        null height 120
        $i=0
        hbox:
            xalign 0.5
            spacing 150
            for ignored in enumerate(material):
                if i < 2:
                    imagebutton:
                        if material[i] >= 0:
                            idle At(Composite(
                                    (340, 340),
                                    (0, 0), "gfx/square/bg.png",
                                    (0, 0), "gfx/square/units/%d.png" % Player["inv"][material[i]]["unit_id"],
                                    (0, 0), "gfx/square/%d.png" % allunits[Player["inv"][material[i]]["unit_id"]]["rare"],
                                ), czoom_70)
                        else:
                            idle At("gfx/square/bg.png", czoom_70)
                        action Return(i)
                    $i+=1

        null height 80
        # The close button returns -1 and comes last (TODO)
        hbox:
            xalign 0.5
            spacing 80
            textbutton _("Evolve") action [SensitiveIf(material[0]>=0 and material[1]>=0), Return(-2)]
            textbutton _("Cancel") action Return(-1)


screen char_details(un, hpval, akval, idx):
    style_prefix "confirm"

    frame:
        at msgbox_emp

        vbox:
            xalign 0.5
            yalign 0.5
            spacing 30

            label _("{b}%s{/b}\n%s\n\nHP: %d\nATK: %d\nLv %d/%d\nEXP: %d") % (
                                    star_write(un["rare"]), un["name"],
                                    hpval, akval,
                                    Player["inv"][idx]["level"],
                                    un["max_level"],
                                    Player["inv"][idx]["exp"]):
                style "confirm_prompt"
                xalign 0.5

            hbox:
                xalign 0.5
                spacing 100

                textbutton _("Merge") action [SensitiveIf(Player["inv"][idx]["level"] < un["max_level"]), Return(-1)]
                textbutton _("Evolve") action [SensitiveIf(evocheck(Player["inv"][idx]["level"], un)), Return(-2)]

            hbox:
                xalign 0.5
                spacing 100

                textbutton _("Ok") action Return(0)

    ## Right-click and escape answer "no".
    key "game_menu" action Return(0)


# Show inventory button
label inventory:
    play music MUSIC_PARTY.id() fadein 0.5
    $ hud_clear()

    # Try to update inventory
    $ inv=get_inventory()
    python:
        try:
            renpy.call_screen("msgbox", "Error: %d" % int(inv))
        except:
            Player["inv"]=dlist()
            for a in inv:
                Player["inv"].append(a)

label show_inv:
    call screen inventory
    if (_return >= 0):
        $stdout("Selected unit index %d" % _return)
        $un=allunits[Player["inv"][_return]["unit_id"]]
        $show_img("unit_"+str(un["unit_id"]), at_list=[truecenter])
        $hpval=readjust_status(un["rare"], un["job"],
               True)*Player["inv"][_return]["level"]+un["hp"]
        $akval=readjust_status(un["rare"], un["job"],
               False)*Player["inv"][_return]["level"]+un["strength"]

        $ret=renpy.call_screen("char_details", un, hpval, akval, _return)

        # Proccess input
        if ret == -1:
            $who=_return
            call upgrade_pre
        elif ret == -2:
            $who=_return
            call evolve_pre

        $renpy.hide("unit_"+str(un["unit_id"]))
    else:
        jump restore

    jump show_inv


label party_lobby_enter:
    play music MUSIC_PARTY.id() fadein 0.5
    $ hud_clear()

    # Try to update inventory
    $ inv=get_inventory()
    python:
        try:
            renpy.call_screen("msgbox", "Error: %d" % int(inv))
        except:
            Player["inv"]=dlist()
            for a in inv:
                Player["inv"].append(a)

    # FIXME
    $ get_party(1, True)
    jump party_lobby

label party_lobby:

    call screen party_main

    # Return to town
    if _return == -1:
        jump restore

    # Update party index
    $party_update(_return[0], _return[1])
    $ get_party(_return[0], True)

    jump party_lobby

label upgrade_pre:
    # who -> index. Set beforehand
    if who < 0:
        call screen msgbox("Error: Invalid upgrade parameters passed")
        return

    $ unit = allunits[Player["inv"][who]["unit_id"]]
    $ material = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1]

    # Try to update inventory
    $ inv=get_inventory()
    python:
        try:
            renpy.call_screen("msgbox", "Error: %d" % int(inv))
        except:
            Player["inv"]=dlist()
            for a in inv:
                Player["inv"].append(a)

    jump upgrade

label upgrade:
    call screen upgrade_char()
    $message=_return
    if (message == -1):
        $who=-1
        return
        #jump restore
    if (message == -2):
        # TODO
        python:
            mats=list(set(material))
            mats.insert(0, who)
            if -1 in mats:
                mats.remove(-1)
            stdout(str(mats))
            ret=None
            ret=renpy.call_screen("confirm", "Are you sure you want to merge these units?", Return(True), Return(False))
            if ret:
                # TODO: DISPLAY ANIMATION
                message=send_packet("upgrade", str(mats))
                message=json_decode(message)
                try:
                    lv=Player["inv"][who]["level"]
                    narrator("Level up! %d -> %d" % (lv, int(message)+lv))
                except:
                    narrator("An error occured.\n\nError code: %s" % str(message))
                renpy.jump("upgrade_pre")
    else:
        $ cand=renpy.call_screen("inventory", True)
        if cand in material and cand >= 0:
            "This unit is already to be merged!"
        elif cand == who:
            "Cannot merge unit with itself!"
        else:
            # TODO: Check for party
            #"Trying to fuse [cand], must check if in party, duplicate, etc."
            $ material[message]=cand
        $ del cand

    # WIP
    jump upgrade


label evolve_pre:
    # who -> index. Set beforehand
    if who < 0:
        call screen msgbox("Error: Invalid upgrade parameters passed")
        return

    $ unit = allunits[Player["inv"][who]["unit_id"]]
    $ next = allunits[Player["inv"][who]["unit_id"]+1]
    $ material = [-1, -1]

    # Try to update inventory
    $ inv=get_inventory()
    python:
        try:
            renpy.call_screen("msgbox", "Error: %d" % int(inv))
        except:
            Player["inv"]=dlist()
            for a in inv:
                Player["inv"].append(a)

    #$renpy.call_screen("msgbox", "Feature not yet available\n%s" % next["name"])
    $renpy.hide("unit_"+str(unit["unit_id"]))
    jump evolve


label evolve:
    call screen evolve_char()
    $message=_return
    if (message == -1):
        $who=-1
        return
        #jump restore
    if (message == -2):
        # TODO
        python:
            mats=list(set(material))
            mats.insert(0, who)
            stdout(str(mats))
            ret=None
            ret=renpy.call_screen("confirm", "Are you sure you want to evolve this units?\nReagents will be lost forever!", Return(True), Return(False))
            if ret:
                message=send_packet("evolve", str(mats))
                message=json_decode(message)
                if message in ["200", 200]:
                    # TODO: DISPLAY ANIMATION
                    narrator("Evolution SUCCESS!")
                else:
                    narrator("Error code: 101!\n\n%s" % str(message))
                renpy.jump("inventory")
    else:
        $ cand=renpy.call_screen("inventory", True, 'alu["rare"] == %d and\
(\
 (alu["unit_id"] == %s) or\
 (alu["flags"] & UF_EVOMAT and alu["attribute"] == %d) or\
 (alu["flags"] & UF_SUPEREVO) )' % (unit["rare"], unit["unit_id"], unit["attribute"]))
        if cand in material and cand >= 0:
            "This unit is already to be used as material!"
        elif cand == who:
            "That's the unit being evolved!"
        else:
            # TODO: Check for party
            #"Trying to fuse [cand], must check if in party, duplicate, etc."
            $ material[message]=cand
        $ del cand

    # WIP
    jump evolve