Version française
Home     About     Download     Resources     Contact us    
Browse thread
Ocaml and BzFlag
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nathan Cooprider <coop@c...>
Subject: Ocaml and BzFlag
I am attempting to write an AI for bzFlag (written in C++) using Ocaml.  
However, the garbage collector is giving me problems.  I can get 
information from C++ to Ocaml using a callback, but then the program 
dies after a while.  I have been fighting with this and the 
documentation for a few weeks, and I am out of ideas.  Here is the code 
which I believe is the problem:

/* C code to update my AI with new robot information */
void update_robot( player_t *p, int your_robot_num ){
    CAMLparam0();
    CAMLlocalN(pparam,17);

    pparam[0]=Val_int(p->team);
    pparam[1]=alloc(Double_wosize, Double_tag);
   
    pparam[1]=copy_double(p->old_position[0]);
    pparam[2]=copy_double(p->old_position[1]);
    pparam[3]=copy_double(p->old_position[2]);
    pparam[4]=copy_double(p->cur_position[0]);
    pparam[5]=copy_double(p->cur_position[1]);
    pparam[6]=copy_double(p->cur_position[2]);
    pparam[7]=copy_double(p->velocity[0]);
    pparam[8]=copy_double(p->velocity[1]);
    pparam[9]=copy_double(p->velocity[2]);
    pparam[10]=copy_double(p->angle);
    pparam[11]=copy_double(p->angvel);
    pparam[12]=Val_int(p->status);
    pparam[13]=Val_int(p->has_flag);
    pparam[14]=Val_int(p->shots_used);
    pparam[15]=Val_int(p->shots_avail);
    pparam[16]=Val_int(your_robot_num);
   
    /*
    printf("C cur_position %d %f %f %f \n",
       your_robot_num,
       Double_val(pparam[4]),
       Double_val(pparam[5]),
       Double_val(pparam[6]));
    fflush(stdout);
    */

    static value * update_players_closure = NULL;
    if (update_players_closure == NULL) {
    update_players_closure = caml_named_value("update_robot");
    }
    callbackN(*update_players_closure, 17, pparam);

    CAMLreturn0;
}

(* Ocaml code that is called from above C code *)
let update_robot team opx opy opz cpx cpy cpz
    vx vy vz angle angvel status has_flag s_used s_avail num =
  let tcpx = cpx
  and tcpy = cpy
  and tcpz = cpz in
 
  robot.cur_position <- Array.copy [|tcpx;tcpy;tcpz|] ;
  (* Gc.compact (); *)
  (*
   print_string "Ocaml cur_position ";
   print_float tcpx;
   print_char ' ';
   print_float tcpy;
   print_char ' ';
   print_float tcpz;
   print_newline ();
   flush stdout ;
    *)

  robot.old_position <- Array.copy [|opx;opy;opz|];
  robot.velocity <- Array.copy [|vx;vy;vz|];
  robot.angle <- angle;
  robot.angvel <- angvel;
  robot.status <- status;
  robot.has_flag <- has_flag;
  robot.shots_used <- s_used;
  robot.shots_avail <- s_avail;
  number := num
let _ = Callback.register "update_robot" update_robot

Any thoughts or ideas would be appreciated.  The commented print lines 
in both code print out equivalent numbers, so I think the callback is 
working fine.  Eventually, I read the robot.cur_position field to create 
a potential field, and that is where I think the program crashes. 

Please let me know if you have any ideas.  I appreciate it. 

Nathan