I need some help on this game...

This is a discussion on I need some help on this game... within the Game Programming forums, part of the General Programming Boards category; I saw an article on the new game "State of Emergency", and noticed how advanced the AI was (it's kinda ...

  1. #1
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    Smile I need some help on this game...

    I saw an article on the new game "State of Emergency", and noticed how advanced the AI was (it's kinda like GTA3, cept killing pedestrians is the point, and the crowds react to gunshots, people dying, etc.), it could control 500 people at a time. So I decided to try it with simple squares that react to a simlated gunshot. Space starts the simulation, and return stops it. The problem is, everyone moves similarly, and I tried added a random factor to it, but rand() relies on the system clock, which doesn't change much during a for loop. I also need some sort of collision system (simple rectangle bounding box) so they won't run into each other. Here's the Allegro code(sorry, but ftp isn't working quite right, so I hafta c/p):

    Code:
    #include <allegro.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #define RX 640
    #define RY 480
    #define STILL 0
    #define UP 1
    #define DOWN 2
    #define LEFT 3
    #define RIGHT 4
    
    void randomize() {
     srand((unsigned)time(NULL));
    }
    
    int rand(int num) {
     randomize();
     return rand()%num;
    }
    
    class square {
     public:
      int size;
      int x, y;
      int c;
      int speed, brave;
      int dir;
      bool run;
      void move(int mx, int my);
    };
    
    void square::move(int mx, int my) {
     x = x + mx;
     y = y + my;
    }
    
    int main() {
     int n, cury = 230, curx = 100, scare = 10;
     allegro_init();
     set_gfx_mode(GFX_AUTODETECT_WINDOWED, RX, RY, 0, 0);
     set_window_title("Crowd Control");
     set_color_depth(8);
     install_mouse();
     install_keyboard();
     set_palette(default_palette);
     BITMAP *screen2 = create_video_bitmap(RX, RY), *buffer = create_bitmap(RX, RY);
     fade_in(default_palette, 1);
     square player, crowd[10];
     player.x = 10;
     player.y = 230;
     player.c = 4;
     player.size = 10;
     player.dir = STILL;
     player.run = false;
     player.speed = 3;
     for (n=0;n<10;n++) {
      crowd[n].size = 10;
      curx += 30;
      crowd[n].x = curx;
      crowd[n].y = cury;
      crowd[n].c = 1;
      crowd[n].brave = 5;
      crowd[n].speed = 1;
      crowd[n].dir = STILL;
      crowd[n].run = false;
     }
     while(!key[KEY_ESC]) {
      clear_bitmap(buffer);
      if (key[KEY_UP]) {
       player.dir = UP;
       player.y -= player.speed;
      }
      if (key[KEY_DOWN]) {
       player.dir = UP;
       player.y += player.speed;
      }
      if (key[KEY_LEFT]) {
       player.dir = UP;
       player.x -= player.speed;
      }
      if (key[KEY_RIGHT]) {
       player.dir = UP;
       player.x += player.speed;
      }
      if (key[KEY_SPACE]) {
       for (n=0;n<10;n++) {
        crowd[n].run = true;
       }
      }
      if (key[KEY_ENTER]) {
       for (n=0;n<10;n++) {
        crowd[n].run = false;
       }
      }
      for (n=0;n<10;n++) {
       if (crowd[n].run == true) {
        if (scare > crowd[n].brave) {
         if (crowd[n].x > player.x || rand(3) == 1) {
          crowd[n].x = crowd[n].x + (rand(2) + crowd[n].speed);
         }
    	 if (crowd[n].x < player.x || rand(3) == 1) {
    	  crowd[n].x = crowd[n].x - (rand(2) + crowd[n].speed);
    	 }
    	 if (crowd[n].y > player.y || rand(3) == 1) {
    	  crowd[n].y = crowd[n].y + (rand(2) + crowd[n].speed);
    	 }
    	 if (crowd[n].y < player.y || rand(3) == 1) {
    	  crowd[n].y = crowd[n].y - (rand(2) + crowd[n].speed);
    	 }
        }
       }
      }
      rectfill(buffer,player.x,player.y,player.x+player.size,player.y+player.size,player.c);
      for (n=0;n<10;n++) {
       rectfill(buffer,crowd[n].x,crowd[n].y,crowd[n].x+crowd[n].size,crowd[n].y+crowd[n].size,crowd[n].c);
      }
      blit(buffer, screen2, 0, 0, 0, 0, RX, RY);
      show_video_bitmap(screen2);
     }
     fade_out(6);
     allegro_exit();
     return 0;
    }
    
    END_OF_MAIN();

    Thanks for any help,
    Scott Hand
    -Save the whales. Collect the whole set.

  2. #2
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    To start with rand() doesn't rely on the system clock. Your problem is srand() should only be called once in the program, not every time you need a random number.
    So in your main you should call randomize() once, and then your random function should look like this-
    Code:
    int rand(int num) {
     return rand()%num;
    }
    I'd also rename the function to something other than rand(), just to keep from getting confused.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    411
    These are the random functions i use

    #include <stdlib.h>

    int getrandi(int low, int high)
    {
    return low+rand()%(high-low+1);
    }

    float getrandf(float low, float high)
    {
    float range = high - low;
    float num = range * (float)rand()/(float)RAND_MAX;
    return( num + low );
    }

    double getrandd( double low, double high ) {
    double range = high - low;
    double num = range * (double)rand()/(double)RAND_MAX;
    return( num + low );
    }

    Just remember to call srand(); once at the programs startup like taylorguitarman said.

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    http://www.math.keio.ac.jp/~matumoto/emt.html

    one of the best random number generators you can get...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288

    Thanks

    I got rid of the extra randomize() and it works! I got the ftp problem fixed, so I uploaded the binary for all the trusting people to download. Note, if you don't trst me, you don't have to download it.
    http://personal.lig.bellsouth.net/~markhand/Crowds.exe

    Controls:

    Arrows: Move
    Insert: Speed up player
    Delete: Speed down player
    Home: Speed up CPU
    End: Speed down CPU
    Numpad+: Raise threat (more people run away)
    Numpad-: Lower threat (more people attack)
    It's kinda pointless, but pretty neat and fun to play with.

    Thanks a bunch for the help,
    Valar_King
    -Save the whales. Collect the whole set.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Needs allegro.dll.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    375
    Needs allegro.dll.
    valar_king, compile it as static linked to avoid this if you like.
    Allegro precompiled Installer for Dev-C++, MSVC, and Borland: http://galileo.spaceports.com/~springs/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. Open Source / Semi Open source game idea. Help needed
    By CaptainPatent in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 05-16-2007, 11:44 AM
  3. game engine advice?
    By stien in forum Game Programming
    Replies: 0
    Last Post: 01-23-2007, 03:46 PM
  4. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-25-2002, 12:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21