JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Autoconf: separate build dir, distclean, dir vars.
[vor.git] / autopilot.c
1 //  Copyright (C) 2007 Jason Woofenden
2 //
3 //  This file is part of VoR.
4 //
5 //  VoR is free software; you can redistribute it and/or modify it
6 //  under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation; either version 2, or (at your option)
8 //  any later version.
9 //
10 //  VoR is distributed in the hope that it will be useful, but
11 //  WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //  General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with VoR; see the file COPYING.  If not, write to the
17 //  Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 //  MA 02111-1307, USA.
19
20
21 // This file has code to generate SDL keypress events
22
23 #include <stdlib.h>
24 #include <SDL.h>
25 #include "mt.h"
26
27 // SETTINGS
28 //
29 // auto pilot waits a random amount of time between zero and this many 1/20ths of a second
30 #define AUTOPILOT_MAX_DELAY 20.0
31
32 #define NUM_KEYS 5
33 SDLKey keysyms[NUM_KEYS] = { SDLK_SPACE, SDLK_LEFT, SDLK_RIGHT, SDLK_UP, SDLK_DOWN};
34 int states[NUM_KEYS] =     { 0,          0,         0,          0,       0,       };
35
36 float next_time;
37 float delay;
38
39 void
40 autopilot_set_timer() {
41         delay = 0;
42         next_time = frnd() * AUTOPILOT_MAX_DELAY;
43 }
44
45 void
46 autopilot_init() {
47         autopilot_set_timer();
48 }
49         
50 void
51 autopilot_fix_keystates(Uint8* table) {
52         int i;
53         for(i = 0; i < NUM_KEYS; ++i) {
54                 table[keysyms[i]] = states[i];
55         }
56 }
57
58 int
59 autopilot_num_down() {
60         int i, num;
61         for(num = 0, i = 0; i < NUM_KEYS; ++i) {
62                 if(states[i]) {
63                         num++;
64                 }
65         }
66
67         return num;
68 }
69
70 void
71 autopilot_send_event() {
72         int down = autopilot_num_down();
73         int nth, i;
74         //SDL_Event event;
75
76         if(down && frnd() < 0.25 + (down / 4.0)) {
77                 //event.type = SDL_KEYUP;
78                 //event.key.state = 0;
79                 nth = (urnd() % down) + 1;
80                 for(i = 0; ; ++i) {
81                         if(states[i]) {
82                                 --nth;
83                         }
84                         if(nth == 0) {
85                                 //event.key.keysym.sym = keysyms[i];
86                                 states[i] = 0;
87                                 break;
88                         }
89                 }
90         } else {
91                 //event.type = SDL_KEYDOWN;
92                 //event.key.state = 1;
93                 nth = (urnd() % (NUM_KEYS - down)) + 1;
94                 for(i = 0; ; ++i) {
95                         if(!states[i]) {
96                                 --nth;
97                         }
98                         if(nth == 0) {
99                                 //event.key.keysym.sym = keysyms[i];
100                                 states[i] = 1;
101                                 break;
102                         }
103                 }
104         }
105
106         //fprintf(stderr, "push event: %i %i\n", event.key.keysym.sym, event.type);
107         //SDL_PushEvent(&event);
108 }
109
110 void
111 autopilot(float ticks) {
112         delay += ticks;
113         if(delay > next_time) {
114                 autopilot_set_timer();
115                 autopilot_send_event();
116         }
117 }