summaryrefslogtreecommitdiff
path: root/noncore/games/kpacman/pacman.cpp
blob: 82524b42e31cac2c060c6bfa77f58b98f75a5c07 (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
#include "pacman.h"

Pacman::Pacman(Board *b)
{
    board = b;
    setDemo(FALSE);
    setAlive(0);
    actualPosition = lastPosition = OUT;
    mouthPosition = 0;
    lastPix = 0;
    maxPixmaps = 0;
}

void Pacman::setMaxPixmaps(int max)
{
    if (actualDirection == X && lastPix >= 0) {
        actualDirection = lastPix / (maxPixmaps/4);
        if (max < maxPixmaps)
            mouthPosition = 0;
        else
            mouthPosition = lastPix % (maxPixmaps/4);
        maxPixmaps = max;       

        lastPix = pix();

        actualDirection = X;
    } else
        maxPixmaps = max;
}

void Pacman::setAlive(int ticks)
{
    actualState = alive;
    pauseDuration = ticks;
    pause = 0;
}

void Pacman::setPosition(int pos)
{
    board->reset(lastPosition, pacman);
    actualPosition = lastPosition = pos;
    board->set(actualPosition, pacman);
    mouthPosition = 0;
}

void Pacman::setDirection(int dir, bool forced)
{
    if (forced ||
        board->isWay(actualPosition, dir, empty) ||
        board->isWay(actualPosition, dir, tunnel)) {
        if (dir == X)
            lastPix = pix();
        actualDirection = dir;
        nextDirection = X;
    } else
        nextDirection = dir;
}

void Pacman::setDemo(bool yes)
{
    demo = yes;
}

pacmanState Pacman::state()
{
    return actualState;
}

int Pacman::position()
{
    return actualPosition;
}

int Pacman::direction()
{
    return actualDirection;
}

bool Pacman::move()
{
    if (pause-- > 0) 
        return FALSE;
    else
        pause = pauseDuration;

    if (actualDirection == X || actualPosition == OUT)
        return FALSE;

    lastPosition = actualPosition;

    if (demo) {
        int d = actualDirection;

        do                              // try new direction, but not the opposite
            d = rand() % 4;             // direction, to prevent hectic movement.
        while (d == board->turn(actualDirection));

        while (!board->isWay(actualPosition, d, empty) &&
               !board->isWay(actualPosition, d, tunnel)) {
            if (d != actualDirection)   // if new actualDirection is not possible,
                d = actualDirection;    // try current actualDirection first.
            else
                d = rand() % 4;
        }

        actualDirection = d;
        actualPosition = board->move(actualPosition, actualDirection);

    } else {

        if (nextDirection != X)
            if (board->isWay(actualPosition, nextDirection, empty) ||
                board->isWay(actualPosition, nextDirection, tunnel)) {
                actualDirection = nextDirection;
                nextDirection = X;      
            }

        if (board->isWay(actualPosition, actualDirection, empty) ||
            board->isWay(actualPosition, actualDirection, tunnel))
            actualPosition = board->move(actualPosition, actualDirection);
    }

    if (actualPosition != lastPosition) {
        board->reset(lastPosition, pacman);
        board->set(actualPosition, pacman);

        if (++mouthPosition >= (maxPixmaps/4))
            mouthPosition = 0;
    }
    return TRUE;
}

int Pacman::pix()
{
    if (actualPosition != OUT && maxPixmaps > 0)
        switch (actualDirection) {
            case N : return ((maxPixmaps/4)*0)+mouthPosition;
            case E : return ((maxPixmaps/4)*1)+mouthPosition;
            case S : return ((maxPixmaps/4)*2)+mouthPosition;
            case W : return ((maxPixmaps/4)*3)+mouthPosition;
            case X : return lastPix;
        }

    return -1;
}