File: cup.c

package info (click to toggle)
asciijump 0.0.6-4
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 688 kB
  • ctags: 455
  • sloc: sh: 2,908; ansic: 2,772; makefile: 203
file content (208 lines) | stat: -rw-r--r-- 4,083 bytes parent folder | download | duplicates (3)
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
/* :: ascii-jump project
   :: $Id: cup.c,v 1.1.1.1 2003/02/28 10:05:04 loth Exp $
   
   :: copyright (c) 2003 :: grzegorz moskal, g.moskal@opengruop.org
   :: license gnu gpl v 2						*/

#define CUP_C
#include "cup.h"

struct object **jumpers_tab, *jumpers;
struct hill **hills_tab;

int nhills, njumpers = 8;

int cup_status;
static int set_hill = 1;
static int double_hill = 0;

static int current_jumper_no;
static int current_hill_no;

static struct widget *scr_result;
static struct widget *win_result;
static struct widget *menu_result;

static char *names[] = {
"player",
"next player",
"next ^2 player",
"next ^3 player",
"erykah badu",
"charlie parker",
"thom yorke",
"bjork",
NULL
};

static void jumpers_init()
{
	struct object *last = NULL, *jmp;
	int i = 0;
	
	for (; i < njumpers; i++) {
		jmp = jumper_init(hills, skin, (i < 8) ? names[i] : "foo");
		if (i < 4)
			jmp->level = 0;
		if (jumpers == NULL) 
			jumpers = last = jmp;
		else {
			last->next = jmp; 
			SWITCH(last);
		}
	}
	
	jumpers_tab = xmalloc(sizeof(struct object *)*njumpers);
	hills_tab = xmalloc(sizeof(struct hill *)*nhills);
}

void cup_init()
{
	scr_result = screen_add();
	jumpers_init();
	scrcup_init();
}

void cup_refresh(void)
{
	int i = njumpers-1;

	current_jumper_no = 0;
	current_hill_no = 0;
	
	for (; i >= 0; i--) 
		if (jumpers_tab[i]) {
			jumper_flush(jumpers_tab[i], hills);
			jumpers_tab[i]->points = 0;
		}

	cup_menurefresh();
}

static void cup_menurefresh_last()
{
	int uw = sl_screen_width/10, i, place = 1;
	char *text = NULL;
	int len = 0, tmplen = 0;
	
	win_result = window_add(scr_result, 
		" world cup _w_i_n_n_e_r_s ", 5, 3, uw*6, 11);
	menuobj_add(menu_add(win_result, 2, 8, uw*6-2, 1, 0),
		"<<<", 0,  scrmain_show, 0);

	for (i = njumpers-1; i >= 0; i--)  {
		if (jumpers_tab[i] == 0)
			continue;
			
		if (text)
			tmplen = strlen(text);
			
		len += strlen(jumpers_tab[i]->name) + 20;
		text = xrealloc(text, sizeof(char) * len);
		sprintf(text+tmplen, "  %d. %.1f  %s\n", place,
			jumpers_tab[i]->points, jumpers_tab[i]->name);
		if (place++ > 5)
			break;
	}
	
	textbox_add(win_result, text);
	xfree(text);	
	
	scr_result->kids = scr_result->current = win_result;
	
	cup_status = MENU;
	
	sl_cls();
}

static void cup_menurefresh()
{
	struct widget *mo;
	char *name;
	int i;

	win_result = window_add(scr_result, "results", 3, 1,
		WIDTH-6, HEIGHT-2);
	menuobj_add(menu_add(win_result,1, 
	HEIGHT-4, WIDTH-9, 1,0), ">>>", 0,  cup_gamerefresh, 0);
	menu_result = menu_add(win_result, 1, 2, WIDTH-9, HEIGHT-2, 0);
	menu_result->selectable = 0;
	
	jumper_tabsort(jumpers_tab, njumpers);
	
	for (i = njumpers-1; i >= 0; i--) {
		if (jumpers_tab[i] == 0)
			continue;
			
		name = xmalloc(sizeof(char) * (strlen(jumpers_tab[i]->name) + 30));
		sprintf(name, "%s  --  %.1f(%.1f)", 
			jumpers_tab[i]->name, jumpers_tab[i]->points, jumpers_tab[i]->last_points);
		mo = menuobj_add(menu_result, name, *name, NULL, 0);
		
		if (i == current_jumper_no)
			menu_result->current = mo;
		xfree(name);
			
	}
		
	scr_result->kids = scr_result->current = win_result;
	cup_status = MENU;
	status = CUP;
	sl_cls();
}

static void cup_gamerefresh(void)
{
	cup_status = GAME;
	sl_cls();
}

static void cup_service(void)
{
	if (current_hill_no < nhills && HILL) {
		int i = njumpers-1;
		for (; i >= 0; i--) 
			if (jumpers_tab[i])
				jumper_flush(jumpers_tab[i], HILL);
					
		jumper_tabsort(jumpers_tab, njumpers);
		set_hill = 0;			
		if (++double_hill >= 2) {
			current_hill_no++;
			double_hill = 0;
		} else 
			hill_intro(HILL);
	} else {
		cup_menurefresh_last();
		set_hill = 0;
	}
}

void cup(int key)
{
	
	switch (cup_status) {
	case MENU:
		if (current_jumper_no == 0 && set_hill) 
			cup_service();
			
		scr_result->draw(scr_result);
		scr_result->key(scr_result, key);
		break;
	
	case GAME:
		if (JUMPER->level)
			jumper_cpujump(JUMPER);
		else if (jumper_service(JUMPER, key)) {
			game_draw(JUMPER);	
			break;
			
		}
		
		if (++current_jumper_no >= njumpers || JUMPER == 0) {
			current_jumper_no = 0;
			set_hill = 1;
			cup_menurefresh();
		}
	}
}