Galaxy Mapper: for those who play Galaxy From: truscott@gfx.engga.uwo.ca (Ashley Truscott) Date: Wed, 16 Jun 1993 18:32:31 +0000 /* *** This is my first X program (and almost my first C) so don't send any *** flames. Any suggestions or improvements can be sent to me *** (since I'm not quite finished yet) at truscott@gfx.engga.uwo.ca *** *** To compile type 'cc galaxy.c -o galaxy -lX11 -lm' *** *** To run type 'galaxy <name of turn file> <name of home planet file>' *** The default is 'galaxy turn homeworld.gamma' *** *** What this program does it to show the galaxy map for the game *** galaxy. What it does for the game player is that it will show *** who owns what planets (colour coded, the player's name is not *** up yet), and when you click you mouse by a planet it will tell *** you the name of the planet and it's S points. Also, if you click *** on another planet, it will give you the S points again as well as *** the distance (In light years) to the last planet that you clicked on. *** *** Enjoy. *** *** Ashley A. Truscott */ /* X include files */ #include <X11/Xlib.h> #include <X11/Xutil.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <math.h> /* Declaratins */ char bye[] = {"Exit"}; struct axises { int first; int x_axis; int y_axis; }; struct axises points[1000]; /* array of RGB values */ struct game { char planet[30]; float x_pos; float y_pos; float s_pos; }; struct game *location; struct players { char name[30]; char people[50]; }; struct players *numb; main (int argc,char **argv) { /* Declarations */ Display *mydisplay; Window mywindow; Window exitwindow; Window rootdumb; Window mapwindow; Colormap cmap; GC mygc; GC exitgc; GC mapgc; XEvent myevent; KeySym mykey; XSizeHints myhint; XSizeHints exithint; XSizeHints maphint; int myscreen; unsigned long myforeground, mybackground; int i,j,k,l,x,xmax,ymax,count,t,s,stat,steps,place,r; int xdumb, ydumb, xloc, yloc; unsigned int widthdumb, heightdumb, bwidthdumb, depthdumb, keydumb; unsigned long pixels[3]; int flags = {DoRed|DoGreen|DoBlue}; char text[10]; int done,ha[30]; float deltax,deltay,distance,damn; char *sbFont; Font font; Status status; char line[100]; long index, ind; char ground1[80], ground2[80]; float zen1, zen2, qyv1, qyv2; int pip,large,play; char temp[100], file1[20], file2[20]; FILE *stream; FILE *infile; r = 0; ha[0] = 0; strcpy(ground1,""); strcpy(ground2,""); strcpy(file1,"turn"); strcpy(file2,"homeworld.gamma"); zen1=0.0; zen2=0.0; qyv1=0.0; qyv2=0.0; /* Find Files*/ if (argc > 1) { strcpy(file1,argv[1]); strcpy(file2,argv[2]); } stream = fopen(file1,"rt"); infile = fopen(file2,"rt"); /* Initialization */ mydisplay = XOpenDisplay(""); myscreen = DefaultScreen (mydisplay); /* Default pixel values */ mybackground = WhitePixel (mydisplay, myscreen); myforeground = BlackPixel (mydisplay, myscreen); /* Default program-specified window position and size */ myhint.x = 200; myhint.y = 300; myhint.width = 900; myhint.height = 660; myhint.flags = PPosition | PSize; exithint.width = 40; exithint.height = 15; exithint.flags = PPosition | PSize; maphint.x = 30; maphint.y = 30; maphint.width = 600; maphint.height = 600; maphint.flags = PPosition | PSize; /* Load Data */ x=0; while (fscanf(infile, "%d %g %g ", &j, &deltax, &deltay) != EOF) { points[x].first = j; points[x].x_axis = deltax*3.0; points[x].y_axis = deltay*3.0; x++; } t = 0; stat = 0; do { fgets(line,99,stream); if (strstr(line,"Status of Players") != NULL) stat = 1; } while (stat == 0); ind = ftell(stream); stat = 0; do { fgets(line,99,stream); if (strstr(line,"Your Ship Types") != NULL) stat = 1; t++; } while (stat == 0); play = t-1; numb = (struct players *) calloc (t+2, sizeof(struct players)); fseek(stream, ind, SEEK_SET); fgets(line,99,stream); fgets(line,99,stream); strcpy(numb[0].name,"Your "); strcpy(numb[1].name,"Unidentified "); strcpy(numb[2].name,"Uninhabited "); large=0; for (l=3; l<play; l++) { fgets(line,99,stream); for (s=0; s<100; s++) if(isspace(line[s]) != 0) { pip=s; break; } if (large < pip) large=pip; } fseek(stream, ind, SEEK_SET); fgets(line,99,stream); fgets(line,99,stream); for (l=3; l<play; l++) { fgets(line,99,stream); strncpy(numb[l].name,line,large); } rewind(stream); t = 0; stat = 0; do { fgets(line,99,stream); if (strstr(line,"Your Planets") != NULL) stat = 1; } while (stat == 0); index = ftell(stream); stat = 0; do { fgets(line,99,stream); if (strstr(line,"Your Group") != NULL) stat = 1; t++; } while (stat == 0); location = (struct game *) calloc (t, sizeof(struct game)); l = 0; for (i=0; i<=play; i++) { stat = 0; for(t=0; t<30; t++) { if (isspace(numb[i].name[t]) != 0) { strncpy(line,numb[i].name,t); line[t]=0; break; } } sprintf(numb[i].people,"%s Planets",line); rewind(stream); do { fgets(line,99,stream); if (strstr(line,numb[i].people) != NULL) stat = 1; if (feof(stream) != 0) stat = 2; } while (stat == 0); if (stat == 1) { r++; index = ftell(stream); fgets(line,99,stream); fgets(line,99,stream); large = 0; pip=0; while (1) { fgets(line,99,stream); if(isspace(line[0]) != 0) break; for (s=0; s<100; s++) if(isspace(line[s]) != 0) { pip=s; break; } if (large < pip) large=pip; } fseek(stream, index, SEEK_SET); fgets(line,99,stream); fgets(line,99,stream); sprintf(temp,"%%%ds %%f %%f %%f",large); while (1) { location[l].s_pos = 0.0; fgets(line,99,stream); if(isspace(line[0]) != 0) break; sscanf(line,temp, &location[l].planet, &location[l].x_pos, &location[l].y_pos, &location[l].s_pos); if (l != 0) ha[r]=l + 1; else ha[r]=0; l++; } } } /* Window creation */ mywindow = XCreateSimpleWindow (mydisplay, DefaultRootWindow (mydisplay), myhint.x, myhint.y, myhint.width, myhint.height, 5, myforeground, mybackground); XSetStandardProperties (mydisplay, mywindow, "Hi There!", "Not Now!", None, argv, argc, &myhint); exitwindow = XCreateSimpleWindow(mydisplay, mywindow, myhint.width-50, 5, exithint.width, exithint.height, 2, myforeground, mybackground); XSetStandardProperties (mydisplay, exitwindow, "", "", None, argv, argc, &exithint); mapwindow = XCreateSimpleWindow(mydisplay, mywindow, maphint.x, maphint.y, maphint.width, maphint.height, 2, myforeground, mybackground); XSetStandardProperties (mydisplay, mapwindow, "", "", None, argv, argc, &maphint); /* Colour */ cmap = DefaultColormap (mydisplay, DefaultScreen (mydisplay)); status = XAllocColorCells (mydisplay, cmap, False, NULL, 0, pixels, 3); XStoreNamedColor (mydisplay, cmap, "Red", pixels[0], flags); XStoreNamedColor (mydisplay, cmap, "Blue", pixels[1], flags); XStoreNamedColor (mydisplay, cmap, "Green", pixels[2], flags); /* GC creation and initialization */ mygc = XCreateGC (mydisplay, mywindow, 0, 0); XSetBackground (mydisplay, mygc, mybackground); XSetForeground (mydisplay, mygc, myforeground); exitgc = XCreateGC (mydisplay, exitwindow, 0, 0); XSetBackground (mydisplay, exitgc, 1); XSetForeground (mydisplay, exitgc, 0); XSetWindowBackground (mydisplay, exitwindow, 1); mapgc = XCreateGC (mydisplay, mapwindow, 0, 0); XSetBackground (mydisplay, mapgc, mybackground); XSetForeground (mydisplay, mapgc, myforeground); /* Input event selection */ XSelectInput (mydisplay, mywindow, ButtonPressMask | KeyPressMask | ExposureMask); XSelectInput (mydisplay, exitwindow, ButtonPressMask | KeyPressMask | ExposureMask); XSelectInput (mydisplay, mapwindow, ButtonPressMask | KeyPressMask | ExposureMask); /* Font Type */ sbFont = "-*-times-*-r-*-*-17-*-*-*-*-*-*-*"; font = XLoadFont(mydisplay,sbFont); XSetFont (mydisplay,mygc,font); XSetFont (mydisplay,exitgc,font); XSetFont (mydisplay,mapgc,font); /* Window mapping */ XMapRaised (mydisplay, mywindow); XMapRaised (mydisplay, exitwindow); XMapRaised (mydisplay, mapwindow); /* main event-reading loop */ done = 0; while (done == 0) { /* Read the next event */ XNextEvent (mydisplay, &myevent); switch (myevent.type) { /* Repaint window on expose events */ case Expose: if (myevent.xexpose.count == 0) { status = XGetGeometry (mydisplay, mywindow, &rootdumb, &xdumb, &ydumb, &widthdumb, &heightdumb, &bwidthdumb, &depthdumb); XMoveWindow (mydisplay, exitwindow, widthdumb-50, 5); XSetForeground (mydisplay, mapgc, 0); XDrawImageString (myevent.xexpose.display, exitwindow, exitgc, 5, 13, bye, strlen (bye) ); for (count=0; count<x; count++) { XDrawArc (mydisplay, mapwindow, mapgc, points[count].x_axis-15, -15+points[count].y_axis, 30,30,0,360*64); XDrawArc (mydisplay, mapwindow, mapgc, points[count].x_axis-30, -30+points[count].y_axis, 60,60,0,360*64); XDrawArc (mydisplay, mapwindow, mapgc, points[count].x_axis-45, -45+points[count].y_axis, 90,90,0,360*64); XDrawArc (mydisplay, mapwindow, mapgc, points[count].x_axis-60, -60+points[count].y_axis, 120,120,0,360*64); } XDrawLine (mydisplay, mapwindow, mygc, 0, 300, 600,300); XDrawLine (mydisplay, mapwindow, mygc, 300, 0, 300,600); for (j=0; j<=r; j++) { XSetForeground (mydisplay, mapgc, j+1); for (s=ha[j]; s<(ha[j+1]); s++) XFillArc (mydisplay, mapwindow, mapgc, (int)(location[s].x_pos*3.0)-2, (int)(location[s].y_pos*3.0)-2, 4, 4, 0,360*64); } } break; /* Process keyboard mapping changes */ case MappingNotify: XRefreshKeyboardMapping (&myevent); break; /* Process mouse-button presses */ case ButtonPress: if (myevent.xbutton.window == exitwindow) done = 1; if (myevent.xbutton.window == mapwindow) { XClearWindow (mydisplay, mywindow); XQueryPointer (mydisplay, mapwindow, &rootdumb, &rootdumb, &xdumb, &ydumb, &xdumb, &ydumb, &keydumb); xloc = xdumb/3; yloc = ydumb/3; damn=10000.0; for (x=0; x<l; x++) { distance = sqrt( pow((xloc-location[x].x_pos),2.0) + pow((yloc-location[x].y_pos),2.0)); if (damn > distance) { damn = distance; large = x; } } strcpy(ground2,ground1); strcpy(ground1,location[large].planet); sprintf(temp," S = %4.1f\n", location[large].s_pos); strcat(ground1,temp); qyv1=zen1; qyv2=zen2; strcpy(temp,""); zen1=location[large].x_pos; zen2=location[large].y_pos; distance = sqrt( pow((zen1-qyv1),2.0) + pow((zen2-qyv2),2.0) ); sprintf(temp,"Distance = %3.1f\n",distance); XDrawImageString (mydisplay, mywindow, mygc, 660, 75, ground1, strlen(ground1) ); XDrawImageString (mydisplay, mywindow, mygc, 660, 50, ground2, strlen(ground2) ); XDrawImageString (mydisplay, mywindow, mygc, 660, 100, temp, strlen(temp) ); } break; /* Process keyboard input */ case KeyPress: /* i = XLookupString (&myevent, text, 10, &mykey, 0); if (i == 1 && text[0] == 'q') done = 1;*/ break; } /* switch (myevent.type) */ } /* while (done == 0 */ /* Termination */ free (numb); free (location); fclose(stream); XUnloadFont (mydisplay,font); XFreeGC (mydisplay, mapgc); XFreeGC (mydisplay, exitgc); XFreeGC (mydisplay, mygc); XDestroyWindow (mydisplay, mapwindow); XDestroyWindow (mydisplay, exitwindow); XDestroyWindow (mydisplay, mywindow); XCloseDisplay (mydisplay); fclose(infile); exit (0); } Referenced By Up