Subversion Repositories Kolibri OS

Rev

Rev 5232 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5232 Rev 5248
Line 1... Line 1...
1
#include "board.h"
1
#include "board.h"
2
#include "config.h"
2
#include "config.h"
Line -... Line 3...
-
 
3
 
-
 
4
__u8 board_need_config = true;
3
 
5
 
4
rect base_cell = {0};
6
rect base_cell = {0};
Line 5... Line 7...
5
tile null_tile = {0};
7
tile null_tile = {0};
6
 
8
 
Line 56... Line 58...
56
// Random number generator
58
// Random number generator
57
__u32 random_u32(__u32 max);
59
__u32 random_u32(__u32 max);
Line 58... Line 60...
58
 
60
 
59
void board_init(rect* r)
61
void board_init(rect* r)
60
{
-
 
61
    __u32 high = config_load_highscore();
-
 
62
    if (high > board.highscore)
-
 
63
        board.highscore = high;
-
 
64
 
62
{
65
    // seed for random number generator
63
    // seed for random number generator
Line 66... Line 64...
66
    srand(__menuet__getsystemclock());
64
    srand(__menuet__getsystemclock());
67
 
65
 
Line 85... Line 83...
85
    {
83
    {
86
        board.cell_map[i] = position2cell(board_position(i));
84
        board.cell_map[i] = position2cell(board_position(i));
87
        board.tile_map[i] = null_tile;
85
        board.tile_map[i] = null_tile;
88
    }
86
    }
Line -... Line 87...
-
 
87
 
-
 
88
    __u8 loaded = false;
-
 
89
    __u8 empty_config = true;
-
 
90
    if (board_need_config)
-
 
91
    {
-
 
92
        board_need_config = false;
-
 
93
        config_state state = {0};
-
 
94
        loaded = config_load(&state);
-
 
95
        if(loaded)
-
 
96
        {
-
 
97
            board.score = state.score;
-
 
98
            board.highscore = state.highscore;
-
 
99
 
-
 
100
            i = 0;
-
 
101
            for (i = 0; i < BOARD_MAP_SIZE; i++)
-
 
102
            {
-
 
103
                if (state.value_map[i])
-
 
104
                {
-
 
105
                    empty_config = false;
-
 
106
                    board_add_tile(state.value_map[i],i);
-
 
107
                }
-
 
108
            }
-
 
109
        }
-
 
110
    }
-
 
111
 
-
 
112
    if (!loaded || empty_config)
89
 
113
    {
90
    i = 0;
114
        i = 0;
91
    for (i = 0; i < START_COUNT; i++)
115
        for (i = 0; i < START_COUNT; i++)
92
    {
116
        {
93
        board_add_random_tile();
117
            board_add_random_tile();
-
 
118
        }
Line 94... Line 119...
94
    }
119
    }
95
 
120
 
Line 96... Line 121...
96
    board_redraw();
121
    board_redraw();
97
}
122
}
-
 
123
 
-
 
124
void board_delete()
98
 
125
{
-
 
126
    config_state state = {0};
-
 
127
    state.score = board.score;
-
 
128
    state.highscore = board.highscore;
-
 
129
    int i = 0;
-
 
130
    for (i = 0; i < BOARD_MAP_SIZE; i++)
99
void board_delete()
131
        state.value_map[i] = board.tile_map[i].value;
100
{
132
    config_save(&state);
Line 101... Line 133...
101
    config_save_highscore(board.highscore);
133
 
102
    canvas_delete();
134
    canvas_delete();
Line 349... Line 381...
349
    board_update_empty_info();
381
    board_update_empty_info();
350
    if (board.empty_count)
382
    if (board.empty_count)
351
    {
383
    {
352
        __u16 rnd_av = random_u32(board.empty_count);
384
        __u16 rnd_av = random_u32(board.empty_count);
353
        rnd_av = board.empty_index[rnd_av];
385
        rnd_av = board.empty_index[rnd_av];
-
 
386
        __u32 rnd_value = (random_u32(10) < 9) ? 2 : 4;
-
 
387
 
-
 
388
        board_add_tile(rnd_value,rnd_av);
-
 
389
    }
-
 
390
    return board.empty_count;
-
 
391
}
Line -... Line 392...
-
 
392
 
-
 
393
void board_add_tile(__u32 value, __u16 index)
354
 
394
{
355
        tile* av_tile = &board.tile_map[rnd_av];
395
    tile* av_tile = &board.tile_map[index];
Line 356... Line 396...
356
        av_tile->value = (random_u32(10) < 9) ? 2 : 4;
396
    av_tile->value = value;
357
 
397
 
358
        av_tile->animate = true;
398
    av_tile->animate = true;
359
        av_tile->ani_step = ANI_APPEAR_STEP;
399
    av_tile->ani_step = ANI_APPEAR_STEP;
360
        av_tile->transition = position2cell(board_position(rnd_av));
400
    av_tile->transition = position2cell(board_position(index));
361
        av_tile->cell.x = av_tile->transition.x + base_cell.width / 2;
401
    av_tile->cell.x = av_tile->transition.x + base_cell.width / 2;
362
        av_tile->cell.y = av_tile->transition.y + base_cell.height / 2;
402
    av_tile->cell.y = av_tile->transition.y + base_cell.height / 2;
363
        av_tile->cell.width = 0;
403
    av_tile->cell.width = 0;
364
        av_tile->cell.height = 0;
-
 
365
    }
-
 
Line 366... Line 404...
366
    return board.empty_count;
404
    av_tile->cell.height = 0;
367
}
405
}
368
 
406
 
369
__u8 board_has_moves()
407
__u8 board_has_moves()