www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/10/31/16:40:11

From: "Oliver Batchelor" <batchg AT ihug DOT co DOT nz>
Newsgroups: comp.os.msdos.djgpp
Subject: Random numbers, 'or ' and 'and' ;
Date: 31 Oct 1998 21:22:33 GMT
Organization: The Internet Group (Christchurch)
Lines: 416
Message-ID: <01be0514$a99ee440$2e884fd1@default>
NNTP-Posting-Host: newsch.ihug.co.nz
X-Newsreader: Microsoft Internet News 4.70.1155
Cache-Post-Path: newsch.es.co.nz!unknown AT p46-max9 DOT chc DOT ihug DOT co DOT nz
X-Cache: nntpcache 2.3.2.1 (see http://www.nntpcache.org/)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

I am writing a rts game.  full code below. I am going quite well despite
having a few problems, i drew 106 different tiles for water, grass and dirt
and joins between them, i use rotate sprite and use angles for units rather
than directions. my problems are :

1: I cannot find out how to use and  or  or  in an if statement

i tried all of these but they dont work.. I would really appreciate it if
someone told me what whas wrong

if(a and b) blah;
if((a) and (b)) blah;
if(a & b) blah;
if((a) & (b)) blah;

if(a or b) blah;
if((a) or (b)) blah;
if(a || b) blah;
if((a) || (b)) blah;

2: I cant seem to find a procedure that makes random numbers.

Here is the code, it is a bit slopy with not many comments and is hard to
follow and is very un optimised i think :<

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include "audio.h"
#include "allegro.h"
#include "good.h"

  float pi;


   BITMAP *page1, *page2, *introbmp;
   BITMAP *active_page;
   char msg[50], ch;
   int i, a,b,x,y,c,scrolx,scroly, sector;
   int map[100][100];
   DATAFILE *tiledata, *gooddata;
   int maxmapy, maxmapx;
   FILE *mapfile;
   AUDIOINFO info;
   LPAUDIOMODULE lpModule;
   BOOL stopped;
   unsigned char audioon, quit;
   float d1, d2;
   float fract, dx, dy, ang, fa, fb, fc;
   /*unit info*/
   float uspeed[100];
   float uturnspeed[100];
   float ux[100];
   float uy[100];
   float udir[100];
   float udestx[100];
   float udesty[100];
   float udestdir[100];
   BOOL selected[100];
   BOOL umoving[100];
   BOOL uturning[100];
   BOOL uimage[100];
   BOOL ucanstop[100];
   int unitno;
   /*mouse rectangle*/
   int sx, sy, ex, ey;
   BOOL selecting;
   BOOL someselected;


float calcang(float x1, float y1, float x2, float y2)  /*Calculate the
angle
                                             between the line x1, y1, x2,
y2
                                             and the line y = x1 (straight
up)*/
{
               float ang;

               dx=x1- x2;
               dy=y1-y2;
               b=0;

               if((dx > -0.5) & (dx < 0.5)) {
               b=1;
               if(dy > 0) ang=0; else ang=128;
               }
                                             /*atan 0 or atan a huge number
                                              at the angels 0 and 64 dont
work
                                              (out of 256) */
               if((dy > -0.5)& (dy < 0.5)) {
               b=1;
               if(dx > 0) ang=192; else ang=64;
               }

               if(!b)
               { if(dx < 0)
                    {if(dy > 0) sector=0; else sector=1;}
                    else {if(dy>0)sector=3; else sector=2;}

                    if(dx <0)dx=-dx;
                    if(dy <0)dy=-dy;

                    fract=dy/dx;
                    if(fract <0) fract=-fract;
                    if((sector==0)) fract = 1/fract;
                    if((sector==2)) fract = 1/fract;

                    ang=fixtof(fatan(ftofix(fract)))+sector*64;


               }
        return ang;
}

void calcangdist(float a1, float a2) /*return values in d1, d2 */
{
         if(a1 > a2) {
         d1=255-a1+a2;
         d2=a1-a2;
         } else

         if(a1 < a2) {
         d2=255-a2+a1;
         d1=a2-a1;
         }
         return;
}



int main()         /*Start of main prog, set vars */
{
    maxmapy=100;
    maxmapx=100;
    scroly=0;

    audioon=1;

    scrolx=0;
    i=0;
    unitno=20;
    someselected=0;
    pi=3.14159;

    for (a=0; a<unitno; a++) {
    ux[a]=a*50+20;
    uy[a]=a*50+20;
    udir[a]=a*20;
    selected[a]=0;
    uspeed[a]=2+a/2;
    uturnspeed[a]=8-a/2;
    uturning[a]=0;
    umoving[a]=0;
    ucanstop[a]=0;
    uimage[a]=1;
    if(a < 10) { uimage[a]=2; ucanstop[a]=1; }
    }


    if (audioon) {
                          /* initialise mod playing */
    AInitialize();

    /* open audio device */
    info.nDeviceId = AUDIO_DEVICE_MAPPER;
    info.wFormat = AUDIO_FORMAT_16BITS | AUDIO_FORMAT_STEREO;
    info.nSampleRate = 44100;
    AOpenAudio(&info);

    /* load module file */
    ALoadModuleFile("mods/war-rem.s3m", &lpModule, 0);
    /* open voices and play module */
    AOpenVoices(lpModule->nTracks);
    APlayModule(lpModule);
    }

    allegro_init();
/*    install_timer();  */
    install_keyboard();           /*initialise keyboard mouse grahpics
etc.. */
    install_mouse();


    mapfile=fopen("demo.dat", "rb");
    fread(map, sizeof(map),1,mapfile);
    fclose(mapfile);

    set_color_depth(16);
    set_gfx_mode(GFX_AUTODETECT, 320, 240, 336, 512);

    tiledata = load_datafile("land.dat");
     if (!tiledata) {
      allegro_exit();
      printf("Error loading land.dat!\n\n");
      quit=1;
   }

   gooddata = load_datafile("good.dat");
     if (!tiledata) {
      allegro_exit();
      printf("Error loading good.dat!\n\n");
      quit=1;
    }

     page1 = create_sub_bitmap(screen, 0, 0, SCREEN_W, SCREEN_H);
     page2 = create_sub_bitmap(screen, 0, SCREEN_H, SCREEN_W, SCREEN_H);
     active_page = page1;


     while (!key[KEY_ESC])        /*Start of main loop !!! */
     {
     if(audioon)AUpdateAudio();
     clear(active_page);

      for (x=0+scrolx; x<10+scrolx; x++) {
      for (y=0+scroly; y<7+scroly; y++) {

     draw_compiled_sprite(active_page, tiledata[map[x][y]].dat,
(x-scrolx)*32, (y-scroly)*32);

      }}

      for(a=0; a<unitno; a++) {
      if(ux[a] > (scrolx-1)*32)
      if(ux[a] < (scrolx+11)*32)
      if(uy[a] > (scroly-1)*32)
      if(uy[a] < (scroly+8)*32)
      
      rotate_sprite(active_page, gooddata[uimage[a]].dat, ux[a]-scrolx*32,
uy[a]-scroly*32, ftofix(udir[a]+i));
      if(selected[a]) rect(active_page, ux[a]-scrolx*32, uy[a]-scroly*32,
ux[a]+32-scrolx*32, uy[a]+32-scroly*32, 255);
      }



      for(a=0; a<unitno; a++)
         if(uturning[a]) {
         fa=udir[a]-udestdir[a];
         if(fa < 0) fa=-fa;
         if(fa < uturnspeed[a]) { uturning[a]=0; udir[a]=udestdir[a];}
         else {

         calcangdist(udestdir[a], udir[a]);

         if(d1 < d2) udir[a]-=uturnspeed[a];
         if(d1 > d2) udir[a]+=uturnspeed[a];


         if(udir[a] > 255) udir[a]-=256;
         if(udir[a] < 0) udir[a]+=256;

      }}

      if(audioon)AUpdateAudio();

      for(a=0; a<unitno; a++)
         if(umoving[a]) {

                ux[a]+=fixtof(fsin(ftofix(udir[a])))*uspeed[a];
                uy[a]-=fixtof(fcos(ftofix(udir[a])))*uspeed[a];
                udestdir[a]=calcang(ux[a]+16, uy[a]+16, udestx[a],
udesty[a]);
                uturning[a]=1;

                if(ucanstop[a]) {
                fa=ux[a]+16-udestx[a];
                fb=uy[a]+16-udesty[a];

                if(fa <0) fa=-fa;
                if(fb <0) fb=-fb;

                if(fa < 20) if (fb < 20) {

                fa=fa*fa;
                fb=fb*fb;
                fa=fa+fb;

                fb=fixtof(fsqrt(ftofix(fa)));

                if(fb<uspeed[a]) {
                umoving[a]=0; uturning[a]=0; ux[a]=udestx[a]-16;
uy[a]=udesty[a]-16;}
                }}

                if(key[KEY_L]) line(active_page, ux[a]+16-scrolx*32,
uy[a]+16-scroly*32, udestx[a]-scrolx*32, udesty[a]-scroly*32, 255);
               /* sprintf(msg, "dist = %f", fb);
                textout(screen, font, msg, SCREEN_W/2, SCREEN_H/2+20, 255);
                */

         }

      if (mouse_b & 1) {
         if(!selecting) {
                if(!key[KEY_I]) {
                someselected=0;
                for(a=0; a<unitno; a++)
                selected[a]=0; }
                sx=mouse_x;
                sy=mouse_y;
                selecting=1;
                }
         ex=mouse_x;
         ey=mouse_y;

         rect(active_page, sx, sy, ex, ey, 255);
         }

     if(selecting)
         if(!mouse_b & 1) {
         selecting=0;

         if(sx > ex) { a=ex; ex=sx; sx=a; }
         if(sy > ey) { a=ey; ey=sy; sy=a; }
         for(a=0; a < unitno; a++) {
         if(ux[a]+32 > sx+scrolx*32)
         if(ux[a] < ex+scrolx*32)
         if(uy[a]+32 > sy+scroly*32)
         if(uy[a] < ey+scroly*32)
         selected[a] = 1;
         someselected=1;
         }
         }


     if(mouse_b & 2)
     if(someselected)
        for(a=0;a<unitno;a++)
            if(selected[a]) {

               udestx[a]=mouse_x+32*scrolx;   /*unit rotation and angle
calcs*/
               udesty[a]=mouse_y+32*scroly;

               umoving[a]=1;
               uturning[a]=1;

               udestdir[a]=calcang(ux[a]+16, uy[a]+16, udestx[a],
udesty[a]);

            }

                                                 /*Whatches*/
      /*sprintf(msg, "fract = %f", dx);
      textout(screen, font, msg, SCREEN_W/2, SCREEN_H/2+20, 255);*/


     draw_sprite(active_page, gooddata[MOUSE_BMP].dat, mouse_x, mouse_y);


     if(!selecting) {
     if(mouse_y == 239) {
     scroly++;
      if(scroly > maxmapy-7) scroly=maxmapy-7;
     }

     if(mouse_y == 0) {
     scroly--;
      if(scroly < 0 ) scroly=0;
     }

     if(mouse_x==319) {
     scrolx++;
      if(scrolx > maxmapx-10) scrolx=maxmapx-10;
     }

     if(mouse_x==0) {
     scrolx--;
      if(scrolx < 0 ) scrolx=0;
     }}

     if (active_page == page1) {
	 scroll_screen(0, 0);
	 active_page = page2;
      }
      else {
	 scroll_screen(0, SCREEN_H);
	 active_page = page1;
      }

}                                         /*END of main loop*/

   clear_keybuf();


   destroy_bitmap(page1);         /*Clean up, close down of graphics sound
etc.. */
   destroy_bitmap(page2);
   unload_datafile(tiledata);

   mapfile=fopen("map.dat", "wb");
   fwrite(map, sizeof(map),1,mapfile);
   fclose(mapfile);

    if(audioon) {
    AStopModule();
    ACloseVoices();

    /* release module file */
    AFreeModuleFile(lpModule);

    /* close audio device */
    ACloseAudio();
    }


   return 0;
}



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019