Import from neverball-1.4.0.tar.gz
[neverball-archive.git] / putt / course.c
blobf613844a3e6ba8b5e7bf0fa032d1049af5e8aeee
1 /*
2 * Copyright (C) 2003 Robert Kooima
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.
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.
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include "config.h"
20 #include "course.h"
21 #include "hole.h"
23 /*---------------------------------------------------------------------------*/
25 struct course
27 char holes[MAXSTR];
29 char shot[MAXSTR];
30 char desc[MAXSTR];
33 static int course_state = 0;
35 static int course;
36 static int count;
38 static struct course course_v[MAXCRS];
40 /*---------------------------------------------------------------------------*/
42 void course_init()
44 FILE *fin;
46 if (course_state)
47 course_free();
49 count = 0;
51 if ((fin = fopen(config_data(COURSE_FILE), "r")))
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))
58 char *q = course_v[count].desc + strlen(course_v[count].desc) - 1;
60 if (*q == '\n') *q = 0;
62 count++;
65 fclose(fin);
67 course_state = 1;
71 int course_exists(int i)
73 return (0 <= i && i < count);
76 int course_count(void)
78 return count;
81 void course_goto(int i)
83 hole_init(course_v[i].holes);
84 course = i;
87 int course_curr(void)
89 return course;
92 void course_free(void)
94 hole_free();
95 course_state = 0;
98 void course_rand(void)
100 course_goto(rand() % count);
101 hole_goto(rand() % curr_count(), 4);
104 /*---------------------------------------------------------------------------*/
106 const char *course_desc(int i)
108 return course_exists(i) ? course_v[i].desc : "";
111 const char *course_shot(int i)
113 return course_exists(i) ? course_v[i].shot : course_v[0].shot;
116 /*---------------------------------------------------------------------------*/