2 /****************************************************************************
3 * Copyright (C) 2002 by Leo Khramov
4 * email: leo@xnc.dubna.su
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 ****************************************************************************/
16 // $Id: welltopnine.cxx,v 1.3 2003/02/23 06:33:40 leo Exp $
17 #include "welltopnine.h"
18 #include "wellengine.h"
19 #include "commonfuncs.h"
21 WellTopNine
*default_top_nine
;
23 //===========================================================================
25 /// constructor, set all to zeros and current date
29 player
[0]=date_time
[0]=0;
30 level
=score
=lines
=squares
=play_time
=0;
31 mixed
=rotation
=next_piece
=false;
35 //===========================================================================
36 /// global get_next_token(char* from, char* to)
37 /// cut buffer by tokens
39 char* Score::get_next_token(char* from
, char* to
)
48 while(*from
!=0 && *from
!=';' && *from
!='\n' && *from
!='\r' && *from
!=0)
50 while(to
!=orig_to
&& *to
==' ')
60 //===========================================================================
61 /// global save(FILE*)
62 /// save score_line to file
64 void Score::save(FILE* fp
)
66 fprintf(fp
,"%d;%-12s;%s;%8d;%5d;%2d;%d;%d;%d;%d\n",
67 squares
,player
,date_time
,score
,lines
,level
,
68 mixed
? 1 : 0, rotation
? 1 : 0,play_time
,
72 //===========================================================================
73 /// global load(FILE*)
74 /// load score_line from file
76 bool Score::load(FILE* fp
)
80 char token
[L_MAXPATH
];
83 if(fgets(buf
,L_MAXPATH
-1,fp
)==0)
85 if(buf
[0]=='#') //Skipping comments
89 ptr
=get_next_token(ptr
,token
);
92 ptr
=get_next_token(ptr
,token
);
93 strncpy(player
,token
,PLAYER_NAME_LEN
-1);
94 player
[PLAYER_NAME_LEN
-1]=0;
96 ptr
=get_next_token(ptr
,token
);
97 strncpy(date_time
,token
,L_DATE
-1);
98 date_time
[L_DATE
-1]=0;
100 ptr
=get_next_token(ptr
,token
);
103 ptr
=get_next_token(ptr
,token
);
106 ptr
=get_next_token(ptr
,token
);
109 ptr
=get_next_token(ptr
,token
);
110 mixed
= atoi(token
) ? true : false;
112 ptr
=get_next_token(ptr
,token
);
113 rotation
= atoi(token
) ? true : false;
115 ptr
=get_next_token(ptr
,token
);
116 play_time
=atoi(token
);
118 ptr
=get_next_token(ptr
,token
);
119 next_piece
= atoi(token
) ? true : false;
122 dbgprintf(("scores: %d;%s;%s;%8d;%5d;%2d;%d;%d\n",
123 squares
,player
,date_time
,score
,lines
,level
,
124 mixed
? 1 : 0, rotation
? 1 : 0));
128 //===========================================================================
129 /// global set_current_time()
130 /// set current date and time to date (format YYYY-MM-DD HH:MI)
132 void Score::set_current_time()
136 time_str
=localtime(&start_t
);
137 strftime(date_time
,L_DATE
,"%Y-%m-%d",time_str
);
140 //===========================================================================
141 /// global calc_play_time()
142 /// calculate playing time from start_t till now
144 void Score::calc_play_time()
148 play_time
=now_t
- start_t
;
151 //===========================================================================
152 /// global set_player(char* pl)
155 void Score::set_player(char* name
)
158 strncpy(player
,name
,PLAYER_NAME_LEN
);
160 strcpy(player
,"unknown");
161 player
[PLAYER_NAME_LEN
-1]=0;
164 //////////////////////////ScoreTable struct methods/////////////////////////
168 //===========================================================================
169 /// global ScoreTable()
170 /// constructor of scoretable
172 ScoreTable::ScoreTable()
175 for(i
=0;i
<TOP_NINE
;i
++)
182 //===========================================================================
183 /// global ~ScoreTable()
184 /// destructor of scoretable
186 ScoreTable::~ScoreTable()
189 for(i
=0;i
<TOP_NINE
;i
++)
193 //===========================================================================
194 /// global sort_by_score()
195 /// sort table by score
197 void ScoreTable::sort_by_score()
202 for(i
=0;i
<TOP_NINE
;i
++)
205 for(j
=0;j
<TOP_NINE
-1;j
++)
206 for(i
=j
+1;i
<TOP_NINE
;i
++)
207 if(table
[j
]->score
<table
[i
]->score
)
218 //===========================================================================
219 /// global save(FILE* fp)
220 /// save table to give file descr
222 void ScoreTable::save(FILE* fp
)
225 for(i
=0;i
<TOP_NINE
;i
++)
229 //===========================================================================
230 /// global set_squares(int isquare)
231 /// set squares to given number
233 void ScoreTable::set_squares(int isquares
)
236 for(i
=0;i
<TOP_NINE
;i
++)
237 table
[i
]->squares
=isquares
;
240 //===========================================================================
241 /// global set_and_get(int idx,Score*)
242 /// set new Score and get old one. If new == 0 just return old
244 Score
* ScoreTable::set_and_get(int idx
, Score
* new_one
)
247 if(idx
<0 || idx
>=TOP_NINE
)
254 table
[idx
]->place
=idx
+1;
262 //////////////////////////////WellTopNine class///////////////////////////////
266 //===========================================================================
267 /// global WellTopNine()
268 /// constructor - fill table
270 WellTopNine::WellTopNine() : WellObject()
276 geo
=get_geo_by_name("top_nine_elements");
278 for(i
=0;i
<MAX_SQUARES
;i
++)
279 tables
[i
].set_squares(i
+1);
280 for(i
=0;i
<TOP_NINE
;i
++)
282 text_lines
[i
]=default_well_engine
->new_well_image_font(imFont1
,
285 text_lines
[i
]->set_screen_region(TOPNINE_TXT_X
,
286 TOPNINE_TXT_Y
+ i
*TOPNINE_TXT_STEPY
,
287 TOPNINE_TXT_L
,TOPNINE_TXT_H
);
289 key_exit
=default_well_engine
->new_well_key("top_key_exit");
290 key_exit
->set_object_on_press(ObjectCaller(this,&WellObject::hide_by_call
));
293 //===========================================================================
294 /// global save_scores()
295 /// save score table to file
297 bool WellTopNine::save_scores()
300 char fname
[L_MAXPATH
];
302 if(!find_full_path_for_file(SCOREFILE
,fname
,ReadWrite
))
305 sprintf(fname
, "%s/.xwelltris",getenv("HOME"));
308 strcat(fname
,SCOREFILE
);
312 FILE* fp
=fopen(fname
,"w");
315 fprintf(stderr
,"PANIC: can't save scores file: %s - %s\n",
316 fname
,strerror(errno
));
319 for(i
=0;i
<MAX_SQUARES
;i
++)
325 //===========================================================================
326 /// global load_scores()
327 /// load score table from file
329 bool WellTopNine::load_scores()
332 int idx
[MAX_SQUARES
];
333 char fname
[L_MAXPATH
];
334 Score
*pscore
=new Score
;
336 find_full_path_for_file(SCOREFILE
,fname
,ReadWrite
);
338 FILE* fp
=fopen(fname
,"r");
342 fprintf(stderr
,"ERROR: can't load scores file: %s - %s\n",
343 fname
,strerror(errno
));
347 for(i
=0;i
<MAX_SQUARES
;i
++)
350 while(pscore
->load(fp
))
353 pscore
=tables
[i
].set_and_get(idx
[i
],pscore
);
358 for(i
=0;i
<MAX_SQUARES
;i
++)
359 tables
[i
].sort_by_score();
365 //===========================================================================
366 /// global try_add_to_table()
367 /// trying to add given player score to table of winners
368 /// return looser pointer (the same one if you are a looser :)
370 Score
* WellTopNine::try_add_to_table(Score
* new_one
)
375 page
=new_one
->squares
-1;
376 current_score
=new_one
;
377 if(tables
[page
].table
[TOP_NINE
-1]->score
< new_one
->score
)
379 pscore
=tables
[page
].table
[TOP_NINE
-1];
380 tables
[page
].table
[TOP_NINE
-1]=new_one
;
381 tables
[page
].sort_by_score();
387 //===========================================================================
388 /// global find_challenger(Score* me)
389 /// Find challenger, whose score > than mine
390 // return 0 if you are the first
392 Score
* WellTopNine::find_challenger(Score
* me
)
399 pcur
= &tables
[me
->squares
-1];
400 for(i
=TOP_NINE
-1;i
>=0;i
--)
401 if(pcur
->table
[i
]->score
> me
->score
)
402 return pcur
->table
[i
];
407 //===========================================================================
408 /// global process_event(wEvent)
409 /// stub that process events (empty here)
411 bool WellTopNine::process_event(wEvent
)
416 //===========================================================================
418 /// stub that show object (empty)
420 void WellTopNine::show()
423 default_well_engine
->set_main_background_image(imScoreBG
);
424 default_well_engine
->add_object(this);
430 //===========================================================================
432 /// stub that hide object (empty)
434 void WellTopNine::hide()
437 default_well_engine
->del_object(this);
439 object_on_exit
.call(wEvent(aEmpty
,this));
442 //===========================================================================
443 /// global redraw_table()
444 /// redraw all data for score table according to current_score and page
446 void WellTopNine::redraw_table()
452 for(i
=0;i
<TOP_NINE
;i
++)
454 pscore
=tables
[page
].table
[i
];
455 sprintf(buf
,"%d.%-15s %s %7d %4d %2d",
456 pscore
->place
,pscore
->player
,pscore
->date_time
,
457 pscore
->score
,pscore
->lines
,pscore
->level
);
458 if(pscore
!=current_score
)
460 text_lines
[i
]->set_font(FONT3_L
,FONT3_H
,
462 text_lines
[i
]->set_screen_region(TOPNINE_TXT_X
,
463 TOPNINE_TXT_Y
+ i
*TOPNINE_TXT_STEPY
,
464 TOPNINE_TXT_L
,TOPNINE_TXT_H
);
467 dbgprintf(("Highlighting item %d\n",i
));
468 text_lines
[i
]->set_font(FONT5_L
,FONT5_H
,
470 text_lines
[i
]->set_screen_region(TOPNINE_TXT_X
,
471 TOPNINE_TXT_Y
+ i
*TOPNINE_TXT_STEPY
,
472 TOPNINE_TXT_L
,TOPNINE_TXT_H
);
474 text_lines
[i
]->set_text(buf
);
475 text_lines
[i
]->draw_text();
478 geo
[j
].toy
=TOPNINE_TXT_Y
+ i
*TOPNINE_TXT_STEPY
- 2;
481 default_well_engine
->screen_copy(&geo
[0]);
482 if(pscore
->next_piece
)
483 default_well_engine
->screen_copy(&geo
[1]);
485 default_well_engine
->screen_copy(&geo
[2]);