summaryrefslogtreecommitdiffstats
path: root/apps/plugins/jackpot.c
blob: b0a6265311070aa5abb073818bf671789b744aee (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
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
/***************************************************************************
 *           __________        __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                   \/     \/     \/    \/         \/
 * $Id$
 *
 * Copyright (C) 2003 Pierre Delore
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
/*
History:
* V1.0: 21/07/03
 First version with a dirty definition of the patterns.
* V1.1: 24/07/03
 Clean definition of the patterns. 
 Init message change
*/
                    
#include "plugin.h"

#ifdef HAVE_LCD_CHARCELLS

PLUGIN_HEADER

/* Jackpot game for the player */

static unsigned char pattern[]={
    0x00, 0x0A, 0x1F, 0x1F, 0x1F, 0x0e, 0x04, /* (+00)Coeur */
    0x00, 0x04, 0x0E, 0x1F, 0x1F, 0x04, 0x0E, /* (+07)Pique */
    0x00, 0x04, 0x0E, 0x1F, 0x0E, 0x04, 0x00, /* (+14)Carreau */
    0x00, 0x15, 0x0E, 0x1F, 0x0E, 0x15, 0x00, /* (+21)Treffle */
    0x03, 0x04, 0x0e, 0x1F, 0x1F, 0x1F, 0x0e, /* (+28)Cerise */
    0x00, 0x04, 0x04, 0x1F, 0x04, 0x0E, 0x1F, /* (+35)Carreau */
    0x04, 0x0E, 0x15, 0x04, 0x0A, 0x0A, 0x11, /* (+42)Homme */
    0x00, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x00, /* (+49)Carre */
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* (+56)Empty */
    0x00, 0x0A, 0x1F, 0x1F, 0x1F, 0x0e, 0x04 /* (+63)Coeur */
};

static unsigned char str[12]; /*Containt the first line*/
static unsigned long h1,h2,h3; /*Handle for the pattern*/

/* here is a global api struct pointer. while not strictly necessary,
   it's nice not to have to pass the api pointer in all function calls
   in the plugin */
static struct plugin_api* rb;

/*Display the first line*/
static void display_first_line(int g)
{
    rb->snprintf(str,sizeof(str),"[   ]$%d",g);
    rb->lcd_puts(0,0,str);

    rb->lcd_putc(1,0, h1);
    rb->lcd_putc(2,0, h2);
    rb->lcd_putc(3,0, h3);
}

/*Call when the program exit*/
static void jackpot_exit(void *parameter)
{
    (void)parameter;
    
    /* Restore the old pattern (i don't find another way to do this. Any
       idea?) */
    rb->lcd_unlock_pattern(h1);
    rb->lcd_unlock_pattern(h2);
    rb->lcd_unlock_pattern(h3);

    /* Clear the screen */
    rb->lcd_clear_display();
    rb->lcd_update();
}


/* this is the plugin entry point */
enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    int i,button,w,j;
    int s[3];
    int n[3];
    int g=20;
    bool exit=false;
    bool go;

    /* if you don't use the parameter, you can do like
       this to avoid the compiler warning about it */
    (void)parameter;

    /* if you are using a global api pointer, don't forget to copy it!
       otherwise you will get lovely "I04: IllInstr" errors... :-) */
    rb = api;
    rb->srand(*rb->current_tick);

    /*Get the pattern handle*/
    h1=rb->lcd_get_locked_pattern();
    h2=rb->lcd_get_locked_pattern();
    h3=rb->lcd_get_locked_pattern();

    /*Init message*/    
    rb->lcd_define_pattern(h1, pattern);
    rb->lcd_define_pattern(h2, pattern+7);
    rb->lcd_define_pattern(h3, pattern+28);

    rb->lcd_puts(0,0,"  Jackpot  ");
    rb->lcd_putc(0,0,h1); rb->lcd_putc(1,0,h2);
    rb->lcd_putc(9,0,h2); rb->lcd_putc(10,0,h1);
    rb->lcd_puts(0,1,"   V1.1    ");
    rb->lcd_putc(1,1,h3); rb->lcd_putc(9,1,h3);
    rb->lcd_update();
    rb->sleep(HZ*2);

    /*First display*/
    rb->lcd_clear_display();
    rb->snprintf(str,sizeof(str),"[   ]$%d",g);
    rb->lcd_puts(0,0,str);
    rb->lcd_puts_scroll(0,1,"PLAY to begin");
    rb->lcd_update();

    /*Empty the event queue*/
    rb->button_clear_queue();

    /* Define the start pattern */
    s[0]=(rb->rand()%9)*7;
    s[1]=(rb->rand()%9)*7;
    s[2]=(rb->rand()%9)*7;

    /*Main loop*/
    while (!exit)
    {
        /*Keyboard loop*/
        go=false;
        while (!go)
        {
            button = rb->button_get(true);
            switch ( button )
            {
                case BUTTON_STOP|BUTTON_REL:
                    exit = true;
                    go = true;
                    break;

                case BUTTON_PLAY|BUTTON_REL:
                    exit = false;
                    if (g>0)
                    {
                        go = true;
                        g--;
                    }
                    break;

                default:
                    if (rb->default_event_handler_ex(button, jackpot_exit,
                                                    NULL) == SYS_USB_CONNECTED)
                        return PLUGIN_USB_CONNECTED;
                    break;
            }
        }

        /*Clear the Second line*/
        rb->lcd_puts_scroll(0,1,"Good luck");
        rb->lcd_update();

        /*GO !!!!*/
        if ( go && !exit )
        {
            /* How many pattern? */
            n[0]=(rb->rand()%15+5)*7;
            n[1]=(rb->rand()%15+5)*7;
            n[2]=(rb->rand()%15+5)*7;

            display_first_line(g);
            /* Jackpot Animation */
            while((n[0]>=0) || (n[1]>=0) || (n[2]>=0))
            {
                if (n[0]>=0)
                    rb->lcd_define_pattern(h1, pattern+s[0]);
                if (n[1]>=0)
                    rb->lcd_define_pattern(h2, pattern+s[1]);
                if (n[2]>=0)
                    rb->lcd_define_pattern(h3, pattern+s[2]);
                rb->sleep(HZ/24);
                rb->lcd_putc(1,0, h1);
                rb->lcd_putc(2,0, h2);
                rb->lcd_putc(3,0, h3);
                rb->lcd_update();
                for(i=0;i<3;i++)
                {
                    if (n[i]>=0)
                    {
                        n[i]--;
                        s[i]++;
                        if (s[i]>=63)
                            s[i]=0;
                    }
                }
            }

            /* You won? */
            s[0]--;
            s[1]--;
            s[2]--;
            w=(s[0]/7)*100+(s[1]/7)*10+(s[2]/7);

            j=0;
            switch (w)
            {
                case 111 :
                    j=20;
                    break;
                case 000 :
                    j=15;
                    break;
                case 333 :
                    j=10;
                    break;
                case 222 :
                    j=8;
                    break;
                case 555 :
                    j=5;
                    break;
                case 777 :
                    j=4;
                    break;
                case 251 :
                    j=4;
                    break;
                case 510 :
                    j=4;
                    break;
                case 686 :
                    j=3;
                    break;
                case 585 :
                    j=3;
                    break;
                case 282 :
                    j=3;
                    break;
                case 484 :
                    j=3;
                    break;
                case 787 :
                    j=2;
                    break;
                case 383 :
                    j=2;
                    break;
                case 80 :
                    j=2;
                    break;
            }
            if (j==0)
            {
                rb->lcd_puts(0,1,"None...");
                if (g<=0)
                    rb->lcd_puts_scroll(0,1,"You lose...STOP to quit");
            }
            else
            {
                g+=j;
                display_first_line(g);
                rb->snprintf(str,sizeof(str),"You win %d$",j);
                rb->lcd_puts(0,1,str);
            }
            rb->lcd_update();
        }
    }

    /* This is the end */
    jackpot_exit(NULL);
    /* Bye */
    return PLUGIN_OK;
}

#endif