aboutsummaryrefslogtreecommitdiff
path: root/src/pong.c
blob: 198a253fcca4314e5f7fe77a2751dfaa4fd525ed (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
/* Copyright (c) 2009, Thorsten Toepper
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of Thorsten Toepper nor the
 *   names of contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Thorsten Toepper ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Thorsten Toepper BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <time.h>
#include <ncurses.h>


struct player_data {
    unsigned int x;
    unsigned int y;
    unsigned int score;
    bool ai;
};

struct ball_data {
    unsigned int x;
    unsigned int y;
    bool mv_left;
    bool mv_right;
    bool mv_up;
    bool mv_down;
};

struct game_data {
    unsigned int max_field_x; /* field size x */
    unsigned int max_field_y; /* field size y */
    struct player_data *p1; /* Player 1 */
    struct player_data *p2; /* Player 2 */
    struct ball_data *ball; /* Ball */
};


/* Called in the main loop to verify the size
 * of the current game field and if it changed
 * to update the size.
 */
bool check_field_size(struct game_data *gd) {
    unsigned int y, x; /* temporary data */
    if (gd == NULL) {
        return FALSE;
    }

    /* Get current size of the terminal */
    getmaxyx(stdscr, y, x); 
    
    x--;
    y--;

    /* If size changed, update the data and return FALSE so
     * it's easy to check if the data changed.
     */
    if ((gd->max_field_y != y) || (gd->max_field_x != x)) {
        gd->max_field_x = x;
        gd->max_field_y = y;
        return FALSE;
    }
    return TRUE;
}


/* Allocates memory for every item and
 * fills them with the initial data.
 */
struct game_data *init_game_data(void) {
    struct game_data *gd;

    /* Allocate memory for every item */
    gd = malloc(sizeof(struct game_data));
    if (gd == NULL) {
        return NULL;
    }
  
    gd->p1 = malloc(sizeof(struct player_data));
    if (gd->p1 == NULL) {
        free(gd);
        return NULL;
    }

    gd->p2 = malloc(sizeof(struct player_data));
    if (gd->p2 == NULL) {
        free(gd->p1);
        free(gd);
        return NULL;
    }

    gd->ball = malloc(sizeof(struct ball_data));
    if (gd->ball == NULL) {
        free(gd->p2);
        free(gd->p1);
        free(gd);
        return NULL;
    }

    /* Fill items with the default data */
    check_field_size(gd);

    gd->p1->x = 0;
    gd->p1->y = gd->max_field_y / 2;
    gd->p1->score = 0;
    gd->p1->ai = FALSE;

    gd->p2->x = gd->max_field_x;
    gd->p2->y = gd->max_field_y / 2;
    gd->p2->score = 0;
    gd->p2->ai = TRUE;

    gd->ball->x = gd->max_field_x / 2;
    gd->ball->y = (gd->max_field_y-1) / 2; /* w/o statusbar */
    gd->ball->mv_left = FALSE;
    gd->ball->mv_right = FALSE;
    gd->ball->mv_up = FALSE;
    gd->ball->mv_down = FALSE;

    return gd;
}

/* Controls the movement of the ball */
void ball_movement(struct game_data *gd) {
    if (gd == NULL) {
        return;
    }
    
    /* Clear current position of the ball */
    mvaddch(gd->ball->y, gd->ball->x, ' ');
    
    if (gd->ball->x == 1) {
        /* Does it hit p1s pad? */
        if ((gd->ball->y > gd->p1->y-2) &&
                (gd->ball->y < gd->p1->y+2)) {
            gd->ball->mv_left = FALSE;
            gd->ball->mv_right = TRUE;
            gd->ball->mv_up = FALSE;
            gd->ball->mv_down = FALSE;

            if ((gd->ball->y > gd->p1->y-2) &&
                    (gd->ball-y < gd->p1->y)) {
                gd->ball->mv_up = TRUE;
            }
            if ((gd->ball->y > gd->p1->y) &&
                    (gd->ball-y < gd->p1->y+2)) {
                gd->ball->mv_down = TRUE;
            }
        }
    }

    if (gd->ball->x == gd->max_field_x-1) {
        /* Does it hit p2s pad? */
        if ((gd->ball->y > gd->p2->y-2) &&
                (gd->ball->y < gd->p2->y+2)) {
            gd->ball->mv_left = TRUE;
            gd->ball->mv_right = FALSE;
            gd->ball->mv_up = FALSE;
            gd->ball->mv_down = FALSE;

            if ((gd->ball->y > gd->p2->y-2) &&
                    (gd->ball-y < gd->p2->y)) {
                gd->ball->mv_up = TRUE;
            }
            if ((gd->ball->y > gd->p2->y) &&
                    (gd->ball-y < gd->p2->y+2)) {
                gd->ball->mv_down = TRUE;
            }
        }
    }

    /* Check if it hits the top of the terminal */
    if (gd->ball->y == 0) {
        gd->ball->mv_up = FALSE;
        gd->ball->mv_down = TRUE;
    }
    /* Check if it hits the statusbar */
    else if (gd->ball->y == gd->max_field_y-1) {
        gd->ball->mv_down = FALSE;
        gd->ball->mv_up = TRUE;
    }


    /* Check if p2 scores and if, reset location to the front of
     * p1s pad launching the ball will be handled elsewhere.
     */
    if (gd->ball->x == 0) {
        gd->p2->score +=1 ;

        gd->ball->x = 1;
        gd->ball->y = (gd->max_field_y-1) / 2;
        gd->ball->mv_left = FALSE;
        gd->ball->mv_right = FALSE;
        gd->ball->mv_up = FALSE;
        gd->ball->mv_down = FALSE;
    }
    /* Check if p1 scores and if, reset the location to the front of
     * p2s pad, launching the ball will be handled elsewhere.
     */
    if (gd->ball->x == gd->max_field_x) {
        gd->p1->score +=1 ;

        gd->ball->x = gd->max_field_x-1;
        gd->ball->y = (gd->max_field_y-1) / 2;
        gd->ball->mv_left = FALSE;
        gd->ball->mv_right = FALSE;
        gd->ball->mv_up = FALSE;
        gd->ball->mv_down = FALSE;
    }
 
    /* Movement of the ball */
    if (gd->ball->mv_left == TRUE) {
        gd->ball->x--;
    }
    else if (gd->ball->mv_right == TRUE) {
        gd->ball->x++;
    }
    if (gd->ball->mv_up == TRUE) {
        gd->ball->y--;
    }
    else if (gd->ball->mv_down == TRUE) {
        gd->ball->y++;
    }
    mvaddch(gd->ball->y, gd->ball->x, '0');
}

void quit(void) {
    endwin();
}


int main(void) {
    struct game_data *game; /* contains all our data */
    
    game = init_game_data();

    return EXIT_SUCCESS;
}