File: levelstate.c

package info (click to toggle)
gplanarity 17906-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 732 kB
  • sloc: ansic: 8,776; makefile: 131; perl: 17; sed: 2
file content (426 lines) | stat: -rw-r--r-- 9,124 bytes parent folder | download | duplicates (4)
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 *
 *  gPlanarity: 
 *     The geeky little puzzle game with a big noodly crunch!
 *    
 *     gPlanarity copyright (C) 2005 Monty <monty@xiph.org>
 *     Original Flash game by John Tantalo <john.tantalo@case.edu>
 *     Original game concept by Mary Radcliffe
 *
 *  gPlanarity is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  gPlanarity is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with Postfish; see the file COPYING.  If not, write to the
 *  Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * 
 */

#define _GNU_SOURCE
#include <gtk/gtk.h>
#include <gtk/gtkmain.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "graph.h"
#include "timer.h"
#include "levelstate.h"
#include "gameboard.h"
#include "dialog_pause.h"
#include "dialog_finish.h"
#include "dialog_level.h"
#include "main.h"
#include "graph_generate.h"

#define CHUNK 64
#define SAVENAME "levelstate"

typedef struct levelstate{
  struct levelstate *prev;
  struct levelstate *next;

  graphmeta gm;

  int in_progress;
  long highscore;
  
} levelstate;

static levelstate *head=0;
static levelstate *tail=0;
static levelstate *curr=0;
static levelstate *pool=0;
static int graph_dirty = 1;

static int aboutflag = 0;
static int pauseflag = 0;
static int finishflag = 0;
static int selectflag = 0;

static int level_limit = 1;
 
static levelstate *new_level(){
  levelstate *ret;
  int num=0;

  if(pool==0){
    int i;
    pool = calloc(CHUNK,sizeof(*pool));
    for(i=0;i<CHUNK-1;i++) /* last addition's ->next points to nothing */
      pool[i].next=pool+i+1;
  }

  ret=pool;
  if(tail)
    num=tail->gm.num+1;

  if(generate_get_meta(num, &ret->gm)) return 0;

  pool=ret->next;

  ret->next=0;
  ret->prev=tail;
    
  if(tail){
    ret->prev->next=ret;
  }else{
    head=ret;
  }
  tail=ret;

  ret->highscore=0;
  ret->in_progress=0;

  /* write out a 'fresh' board icon if it doesn't already exist */
  if(!gameboard_icon_exists(ret->gm.id,"1")){
    // generate a graph we can use to make the icon
    graph g;
    memset(&g,0,sizeof(g));
    g.width=gameboard->g.orig_width;
    g.height=gameboard->g.orig_height;
    g.orig_width=gameboard->g.orig_width;
    g.orig_height=gameboard->g.orig_height;

    generate_board(&g,num);
    impress_location(&g);

    gameboard_write_icon(ret->gm.id,"1",gameboard,&g,1,1);
   
    // releases the temporary graph memory
    graph_release(&g);
  }

  
  return ret;
}

static levelstate *ensure_level_num(int level){

  if(level<0)return 0;

  if(!tail)new_level();

  if(level <= tail->gm.num){
    // find it in the existing levels
    levelstate *l=tail;
    while(l){
      if(level == l->gm.num) return l;
      l=l->prev;
    }
    return 0;
  }else{
    // make new levels to fill 
      
    while(tail->gm.num<level)
      new_level();

    return tail;
  }
}

static levelstate *ensure_level(char *name){
  int level=generate_find_number(name);
  return ensure_level_num(level);
}

int levelstate_write(){
  FILE *f;
  char *name=alloca(strlen(statedir)+strlen(SAVENAME)+1);
  name[0]=0;
  strcat(name,statedir);
  strcat(name,SAVENAME);

  if(!graph_dirty && (curr->in_progress || gameboard->finish_dialog_active)){
    gameboard_write(curr->gm.id,gameboard);
    gameboard_write_icon(curr->gm.id,"2",gameboard,&gameboard->g,
			 !gameboard->hide_lines,gameboard->show_intersections);

  }

  f = fopen(name,"wb");
  if(f==NULL){
    fprintf(stderr,_("ERROR:  Could not save game state file \"%s\":\n\t%s\n"),
	    curr->gm.id,strerror(errno));
    return errno;
  }

  fprintf(f,"current %d : %s\n",(int)strlen(curr->gm.id),curr->gm.id);

  {
    levelstate *l=head;
    while(l){
      fprintf(f,"level %ld %d %d : %s\n",
	      l->highscore,l->in_progress,
	      (int)strlen(l->gm.id),l->gm.id);
      l=l->next;
    }
  }

  if(gameboard->about_dialog_active)
    fprintf(f,"about 1\n");
  if(gameboard->pause_dialog_active)
    fprintf(f,"pause 1\n");
  if(gameboard->finish_dialog_active)
    fprintf(f,"finish 1\n");
  if(gameboard->level_dialog_active)
    fprintf(f,"select 1\n");
	  
  fclose(f);
  return 0;
}

// also functions as the levelstate init; always called once upon startup
int levelstate_read(){
  char *line=NULL;
  size_t n=0;

  FILE *f;
  char *name=alloca(strlen(statedir)+strlen(SAVENAME)+1);
  name[0]=0;
  strcat(name,statedir);
  strcat(name,SAVENAME);

  if(!head)new_level();
  if(!curr)curr=head;

  f = fopen(name,"rb");
  if(f==NULL){
    if(errno!=ENOENT){ 
      fprintf(stderr,_("ERROR:  Could not read game state file \"%s\":\n\t%s\n"),
	      curr->gm.id,strerror(errno));
    }
    return errno;
  }
  
  // get all levels we've seen.
  while(getline(&line,&n,f)>0){
    long l;
    int i;
    unsigned int j;
    if (sscanf(line,"level %ld %d %d : ",&l,&i,&j)==3){
      char *name=strchr(line,':');
      // guard against bad edits
      if(name &&
	 (strlen(line) - (name - line + 2) >= j)){
	levelstate *le;
	name += 2;
	name[j]=0;
	le = ensure_level(name);
	if(le){
	  le->highscore=l;
	  le->in_progress=i;

	  if(le->highscore)
	    if(le->gm.unlock_plus + le->gm.num > level_limit)
	      level_limit = le->gm.unlock_plus + le->gm.num;
	}
      }
    }
  }

  rewind(f);

  // get current
  while(getline(&line,&n,f)>0){
    int i;
    if (sscanf(line,"current %d : ",&i)==1){
      char *name=strchr(line,':');
      // guard against bad edits
      if(name &&
	 (strlen(line) - (name - line + 2) >= (unsigned)i)){
	levelstate *le;
	name += 2;
	name[i]=0;
	le = ensure_level(name);
	if(le)
	  curr=le;
      }
    }

    if(sscanf(line,"about %d",&i)==1)
      if(i==1)
	aboutflag=1;
    
    if(sscanf(line,"pause %d",&i)==1)
      if(i==1)
	pauseflag=1;
    
    if(sscanf(line,"finish %d",&i)==1)
      if(i==1)
	finishflag=1;

    if(sscanf(line,"select %d",&i)==1)
      if(i==1)
	selectflag=1;
	  
  }

  while(curr->gm.num >= level_limit){
    levelstate_prev();
  }

  return 0;
}

void levelstate_resume(){

  levelstate_go();

  if(pauseflag){
    prepare_reenter_game(gameboard);
    pause_dialog(gameboard);
  }else if (aboutflag){
    prepare_reenter_game(gameboard);
    about_dialog(gameboard);
  }else if (finishflag){
    prepare_reenter_game(gameboard);
    finish_level_dialog(gameboard);
  }else if (selectflag){
    prepare_reenter_game(gameboard);
    level_dialog(gameboard,0);
  }else{
    prepare_reenter_game(gameboard);
    reenter_game(gameboard);
  }
  aboutflag=0;
  pauseflag=0;
  finishflag=0;
  selectflag=0;

}

void set_in_progress(){
  curr->in_progress=1;
}

long levelstate_total_hiscore(){
  long score=0;
  levelstate *l=head;

  while(l){
    score+=l->highscore;
    l=l->next;
  }
  return score;
}

long levelstate_get_hiscore(){
  if(!curr)return 0;
  return curr->highscore;
}

int levelstate_next(){
  if(!curr->next)
    new_level();

  if(curr->next){
    curr=curr->next;
    graph_dirty=1;
    return 1;
  }
  return 0;
}

int levelstate_prev(){
  if(curr->prev){
    curr=curr->prev;
    graph_dirty=1;
    return 1;
  }
  return 0;
}

int get_level_num(){
  return curr->gm.num;
}

char *get_level_desc(){
  return _(curr->gm.desc);
}

void levelstate_finish(){
  int score = graphscore_get_score(&gameboard->g) + 
    graphscore_get_bonus(&gameboard->g);
  curr->in_progress=0;
  if(score > curr->highscore)
    curr->highscore = score;

  if(curr->gm.unlock_plus + curr->gm.num > level_limit)
    level_limit = curr->gm.unlock_plus + curr->gm.num;

}

void levelstate_reset(){
  curr->in_progress=0;
  graph_dirty=1;
}

int levelstate_in_progress(){
  return curr->in_progress;
}

int levelstate_limit(){
  return level_limit;
}

/* commit to the currently selected level and set the game state to
   readiness using it */
void levelstate_go(){

  // we need to load the board if we're currently playing the board or sitting in the finish dialog right after
  if(curr->in_progress || finishflag){
    if(gameboard_read(curr->gm.id,gameboard)){
      /* not on disk or couldn't load it.  clear level state flags and get a fresh version */
      aboutflag=0;
      pauseflag=0;
      finishflag=0;
      selectflag=0;
      generate_board(&gameboard->g,curr->gm.num);
      activate_verticies(&gameboard->g);
      impress_location(&gameboard->g);
      set_timer(0);
    }
  }else{
    /* no board in progress; fetch a new board */
    generate_board(&gameboard->g,curr->gm.num);
    activate_verticies(&gameboard->g);
    impress_location(&gameboard->g);
    set_timer(0);
  }

  graph_dirty=0;
}

cairo_surface_t *levelstate_get_icon(int num){
  levelstate *l=ensure_level_num(num);
  if(l==0)return 0;
  return gameboard_read_icon(l->gm.id,(l->in_progress?"2":"1"),gameboard);
}