Codebase list gplanarity / cd2e967e-cece-4b7a-b2eb-0accc5988312/main gameboard_logic.c
cd2e967e-cece-4b7a-b2eb-0accc5988312/main

Tree @cd2e967e-cece-4b7a-b2eb-0accc5988312/main (Download .tar.gz)

gameboard_logic.c @cd2e967e-cece-4b7a-b2eb-0accc5988312/mainraw · history · blame

  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
427
428
/*
 *
 *  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 <gdk/gdk.h>
#include <gdk/gdkx.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "graph.h"
#include "timer.h"
#include "gameboard.h"
#include "levelstate.h"
#include "dialog_finish.h"
#include "dialog_pause.h"
#include "dialog_level.h"
#include "main.h"

/* game state helpers ************************************************************************************/

void prepare_reenter_game(Gameboard *g){
  g->lit_vertex=0;
  g->grabbed_vertex=0;
  g->delayed_background=0;
  
  g->group_drag=0;
  g->button_grabbed=0;
  g->selection_grab=0;
  g->selection_active=num_selected_verticies(&g->g);
  
  update_full(g);
  update_score(g);
}

void reenter_game(Gameboard *g){
  deploy_buttonbar(g);
  unpause_timer();
  set_in_progress();
}

void enter_game(Gameboard *g){
  set_timer(0);
  prepare_reenter_game(g);
  reenter_game(g);
}

static void animate_verticies(Gameboard *g, 
			      int *x_target,
			      int *y_target,
			      float *delta){
  int flag=1,count=0;
  vertex *v=g->g.verticies;
  float *tx=alloca(g->g.vertex_num * sizeof(*tx));
  float *ty=alloca(g->g.vertex_num * sizeof(*ty));

  // hide intersections now; deactivating the verticies below will hide them anyway
  g->show_intersections = 0;
  g->realtime_background = 1;
  update_full(g);

  // deactivate all the verticies so they can be moved en-masse
  // without graph metadata updates at each move
  v=g->g.verticies;
  while(v){
    deactivate_vertex(&g->g,v);
    tx[count]=v->x;
    ty[count++]=v->y;
    v=v->next;
  }

  // animate
  while(flag){
    count=0;
    flag=0;
    
    v=g->g.verticies;
    while(v){
      if(v->x!=x_target[count] || v->y!=y_target[count]){
	
	float xd = x_target[count] - tx[count];
	float yd = y_target[count] - ty[count];
	float m = hypot(xd,yd);

	tx[count]+= xd/m*delta[count];
	ty[count]+= yd/m*delta[count];
	
	invalidate_vertex(g,v);
	invalidate_edges(GTK_WIDGET(g),v,0,0);

	v->x = rint(tx[count]);
	v->y = rint(ty[count]);

	if( (v->x > x_target[count] && xd > 0) ||
	    (v->x < x_target[count] && xd < 0) ||
	    (v->y > y_target[count] && yd > 0) ||
	    (v->y < y_target[count] && yd < 0) ){
	  v->x = x_target[count];
	  v->y = y_target[count];
	}else
	  flag=1;

	invalidate_vertex(g,v);
	invalidate_edges(GTK_WIDGET(g),v,0,0);

      }	    
      count++;
      v=v->next;
    }

    gdk_window_process_all_updates();
    gdk_flush();
    
  }

  // reactivate all the verticies 
  activate_verticies(&g->g);

  // update the score 
  update_score(g);

  // it's a reset; show lines is default. This also has the side
  // effect of forcing a full board redraw and expose
  g->realtime_background=0;
  update_full(g);

  // possible
  if(g->g.active_intersections <= g->g.objective){
    deploy_check(g);
  }else{
    undeploy_check(g);
  }

}


static void scale(Gameboard *g,double scale){
  int x=g->g.width/2;
  int y=g->g.height/2;
  int sel = num_selected_verticies(&g->g);
  vertex *v;
  int *tx=alloca(g->g.vertex_num * sizeof(*tx));
  int *ty=alloca(g->g.vertex_num * sizeof(*ty));
  float *tm=alloca(g->g.vertex_num * sizeof(*ty));
  int i=0;
  int minx=0;
  int maxx=g->g.width-1;
  int miny=0;
  int maxy=g->g.height-1;
  int okflag=0;

  // if selected, expand from center of selected mass
  if(sel){
    double xac=0;
    double yac=0;

    v=g->g.verticies;
    while(v){
      if(v->selected){
	xac+=v->x;
	yac+=v->y;
      }
      v=v->next;
    }
    x = xac / sel;
    y = yac / sel;
  }

  while(!okflag){
    okflag=1;
    i=0;
    v=g->g.verticies;
    while(v){
      
      if(!sel || v->selected){
	float nx = rint((v->x - x)*scale+x);
	float ny = rint((v->y - y)*scale+y);
	float m = hypot(nx - v->x,ny-v->y)/5;
	
	if(nx<minx){
	  scale = (float)(minx-x) / (v->x-x);
	  okflag=0;
	}

	if(nx>maxx){
	  scale = (float)(maxx-x) / (v->x - x);
	  okflag=0;
	}

	if(ny<miny){
	  scale = (float)(miny-y) / (v->y-y);
	  okflag=0;
	}

	if(ny>maxy){
	  scale = (float)(maxy-y) / (v->y - y);
	  okflag=0;
	}
	if(m<1)m=1;
      
	tx[i] = rint(nx);
	ty[i] = rint(ny);
	tm[i++] = m;
      }else{
	tx[i] = v->x;
	ty[i] = v->y;
	tm[i++] = 0;
      }
      v=v->next;
    }
  }
  
  if(scale>=.99999 && scale<=1.00001){
    ungrab_verticies(&g->g);
    v=g->g.verticies;
    while(v){
      if(v->x<2 || v->x>=g->g.width-3)v->grabbed=1;
      if(v->y<2 || v->y>=g->g.height-3)v->grabbed=1;
      v=v->next;
    }
    fade_marked(g);
    ungrab_verticies(&g->g);
  }else{
    animate_verticies(g,tx,ty,tm);
  }
}

static void gtk_main_quit_wrapper(Gameboard *g){
  fade_cancel(g);
  gtk_main_quit();
}

static void level_wrapper(Gameboard *g){
  fade_cancel(g);
  level_dialog(g,0);
}

static void finish_wrapper(Gameboard *g){
  fade_cancel(g);
  finish_level_dialog(g);
}

/* toplevel main gameboard action entry points; when a button is
   clicked and decoded to a specific action or a game state change
   triggers an action, one of the below functions is the entry
   point **************************************************************************************************************/

void quit_action(Gameboard *g){
  pause_timer();
  undeploy_buttons(g,gtk_main_quit_wrapper);
}

void level_action(Gameboard *g){
  pause_timer();
  undeploy_buttons(g,level_wrapper);
}

void finish_action(Gameboard *g){
  if(g->g.active_intersections<=g->g.original_intersections){
    pause_timer();
    levelstate_finish();
    undeploy_buttons(g,finish_wrapper);
  }
}

void pause_action(Gameboard *g){
  pause_timer();
  undeploy_buttons(g,pause_dialog);
}

void about_action(Gameboard *g){
  pause_timer();
  undeploy_buttons(g,about_dialog);
}

void expand_action(Gameboard *g){
  scale(g,1.2);
}

void shrink_action(Gameboard *g){
  scale(g,.8);
}

void set_hide_lines(Gameboard *g, int state){
  g->hide_lines=state;
  update_full(g);
}

void toggle_hide_lines(Gameboard *g){
  g->hide_lines= !g->hide_lines;
  update_full(g);
}

void set_show_intersections(Gameboard *g, int state){
  if(g->show_intersections != state){
    g->show_intersections=state;
    expose_full(g);
  }
}

void toggle_show_intersections(Gameboard *g){
  g->show_intersections=!g->show_intersections;
  expose_full(g);
}

void reset_action(Gameboard *g){
  vertex *v=g->g.verticies;
  int *target_x = alloca(g->g.vertex_num * sizeof(*target_x));
  int *target_y = alloca(g->g.vertex_num * sizeof(*target_y));
  float *target_m = alloca(g->g.vertex_num * sizeof(*target_m));
  int i=0;
  int xd = (g->g.width-g->g.orig_width)>>1;
  int yd = (g->g.height-g->g.orig_height)>>1;

  while(v){
    target_x[i]=v->orig_x+xd;
    target_y[i]=v->orig_y+yd;
    target_m[i++]=RESET_DELTA;
    v=v->next;
  }

  animate_verticies(g,target_x,target_y,target_m);
  
  // reset timer
  set_timer(0);
  unpause_timer();
}


/***************** save/load gameboard the widget state we want to be persistent **************/

// there are only a few things; lines, intersections
int gameboard_write(char *basename, Gameboard *g){
  char *name;
  FILE *f;

  name=alloca(strlen(boarddir)+strlen(basename)+1);
  name[0]=0;
  strcat(name,boarddir);
  strcat(name,basename);
  
  f = fopen(name,"wb");
  if(f==NULL){
    fprintf(stderr,_("ERROR:  Could not save board state for \"%s\":\n\t%s\n"),
	    get_level_desc(),strerror(errno));
    return errno;
  }

  graph_write(&g->g,f);

  if(g->hide_lines)
    fprintf(f,"hide_lines 1\n");
  if(g->show_intersections)
    fprintf(f,"show_intersections 1\n");
  
  fclose(f);

  return 0;
}

int gameboard_read(char *basename, Gameboard *g){
  FILE *f;
  char *name;
  char *line=NULL;
  size_t n=0;

  name=alloca(strlen(boarddir)+strlen(basename)+3);
  name[0]=0;
  strcat(name,boarddir);
  strcat(name,basename);
  
  f = fopen(name,"rb");
  if(f==NULL){
    fprintf(stderr,_("ERROR:  Could not read saved board state for \"%s\":\n\t%s\n"),
	    get_level_desc(),strerror(errno));
    return errno;
  }
  
  graph_read(&g->g,f);

  // get local game state
  while(getline(&line,&n,f)>0){
    int i;
    
    if (sscanf(line,"hide_lines %d",&i)==1)
      if(i)
	g->hide_lines = 1;
    
    if (sscanf(line,"show_intersections %d",&i)==1)
      if(i)
	g->show_intersections = 1;
    
  }
  
  fclose (f);
  free(line);
  request_resize(g->g.width,g->g.height);
  activate_verticies(&g->g);

  return 0;
}