Aw world attributes send C example 1
Jump to navigation
Jump to search
This is a simple example of how to do world attribute zones in a world.
#include "Aw.h" #include "Reason.h" #include <stdio.h> #include <stdlib.h> void handle_avatar_add (void); void handle_avatar_change (void); int main(int argc, char *argv[]) { int rc; /* Check command line */ if (argc < 4) { printf ("Usage: %s number password world\n", argv[0]); return 1; } /* Initialize Active Worlds API */ rc = aw_init (AW_BUILD); if (rc != RC_SUCCESS) { printf ("Unable to initialize API (reason %d)\n", rc); return 1; } /* Install event handlers */ aw_event_set (AW_EVENT_AVATAR_ADD, handle_avatar_add); aw_event_set (AW_EVENT_AVATAR_CHANGE, handle_avatar_change); /* Create bot instance */ rc = aw_create (NULL, 0, NULL); if (rc != RC_SUCCESS) { printf ("Unable to create bot instance (reason %d)\n", rc); return 1; } /* Log bot into the universe */ aw_int_set (AW_LOGIN_OWNER, atoi (argv[1])); aw_string_set (AW_LOGIN_PRIVILEGE_PASSWORD, argv[2]); aw_string_set (AW_LOGIN_APPLICATION, "aw_world_attributes_send sample application #1"); aw_string_set (AW_LOGIN_NAME, "MyBot"); rc = aw_login (); if (rc != RC_SUCCESS) { printf ("Unable to login (reason %d)\n", rc); return 1; } /* Use global mode to allow zones anywhere in world */ aw_bool_set (AW_ENTER_GLOBAL, 1); /* Enter bot into the world */ rc = aw_enter (argv[3]); if (rc != RC_SUCCESS) { printf ("Unable to enter world (reason %d)\n", rc); return 1; } /* Main event loop */ while (aw_wait (-1) == RC_SUCCESS); /* Close everything down */ aw_destroy (); aw_term (); return 0; } /* Array of all sessions, a static array in this example; * You are advised to make this a dynamic array, e.g. <map> with stdc++ * and remove the array entry on world-exit (not shown in this example). */ static int zones[100000]; int zone_from_cm (int x, int z) { /* zone 1 */ if (x >= 10000 && x < 20000) /* 10W to 20W */ if (z >= 10000 && z < 20000) /* 10N to 20N */ return 1; /* zone 2 */ if (x >= -20000 && x < -10000) /* 10E to 20E */ if (z >= -20000 && z < -10000) /* 10S to 20S */ return 2; /* ... zone N */ /* zone 0 means the coordinates do not lie within any of the zones */ return 0; } void handle_avatar_change () { int session; /* session number of avatar */ int before; /* zone before moving */ int after; /* zone after moving */ int rc; session = aw_int (AW_AVATAR_SESSION); before = zones[session]; after = zone_from_cm (aw_int (AW_AVATAR_X), aw_int (AW_AVATAR_Z)); zones[session] = after; if (before == after) { /* avatar is still within the same zone */ return; } /* send world attributes that differ depending on zone */ switch (after) { case 0: /* default zone */ aw_float_set (AW_WORLD_GRAVITY, 1.0); break; case 1: /* zone 1 */ aw_float_set (AW_WORLD_GRAVITY, 0); break; case 2: /* zone 2 */ aw_float_set (AW_WORLD_GRAVITY, -1.0); break; /* ... zone N */ } rc = aw_world_attributes_send (session); if (rc != RC_SUCCESS) printf ("Unable to send attributes (reason %d)\n", rc); } void handle_avatar_add () { int session; /* session number of avatar */ session = aw_int (AW_AVATAR_SESSION); zones[session] = 0; /* default zone */ /* check if avatar has entered outside of the default zone */ handle_avatar_change(); }