bonus ball for bonus levels. Fix #56
[neverball] / putt / course.c
1 /*   
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERPUTT is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "config.h"
20 #include "course.h"
21 #include "hole.h"
22
23 /*---------------------------------------------------------------------------*/
24
25 struct course
26 {
27     char holes[MAXSTR];
28
29     char shot[MAXSTR];
30     char desc[MAXSTR];
31 };
32
33 static int course_state = 0;
34
35 static int course;
36 static int count;
37
38 static struct course course_v[MAXCRS];
39
40 /*---------------------------------------------------------------------------*/
41
42 void course_init()
43 {
44     FILE *fin;
45
46     if (course_state)
47         course_free();
48
49     count = 0;
50
51     if ((fin = fopen(config_data(COURSE_FILE), "r")))
52     {
53         while (fscanf(fin, "%s %s\n",
54                       course_v[count].holes,
55                       course_v[count].shot) == 2 &&
56                 fgets(course_v[count].desc, MAXSTR, fin))
57         {
58             char *q = course_v[count].desc + strlen(course_v[count].desc) - 1;
59
60             if (*q == '\n') *q = 0;
61
62             count++;
63         }
64
65         fclose(fin);
66
67         course_state = 1;
68     }
69 }
70
71 int course_exists(int i)
72 {
73     return (0 <= i && i < count);
74 }
75
76 int course_count(void)
77 {
78     return count;
79 }
80
81 void course_goto(int i)
82 {
83     hole_init(course_v[i].holes);
84     course = i;
85 }
86
87 int course_curr(void)
88 {
89     return course;
90 }
91
92 void course_free(void)
93 {
94     hole_free();
95     course_state = 0;
96 }
97
98 void course_rand(void)
99 {
100     course_goto(rand() % count);
101     hole_goto(rand() % curr_count(), 4);
102 }
103
104 /*---------------------------------------------------------------------------*/
105
106 const char *course_desc(int i)
107 {
108     return course_exists(i) ? course_v[i].desc : "";
109 }
110
111 const char *course_shot(int i)
112 {
113     return course_exists(i) ? course_v[i].shot : course_v[0].shot;
114 }
115
116 /*---------------------------------------------------------------------------*/