Example #1
0
// init Game object: only called once during startup
bool Game::init()
{
int i;

	memset(&game, 0, sizeof(game));
	
	// set default properties
	memset(objprop, 0, sizeof(objprop));
	for(i=0;i<OBJ_LAST;i++)
	{
		objprop[i].shaketime = 16;
		#ifdef DEBUG	// big red "NO" sprite points out unimplemented objects
			objprop[i].sprite = SPR_UNIMPLEMENTED_OBJECT;
		#else
			objprop[i].sprite = SPR_NULL;
		#endif
	}
	
	AssignSprites();		// auto-generated function to assign sprites to objects
	AssignExtraSprites();	// assign rest of sprites (to be replaced at some point)
	
	if (ai_init()) return 1;			// setup function pointers to AI routines
	
	if (initslopetable()) return 1;
	if (initmapfirsttime()) return 1;
	
	// create the player object--note that the player is NOT destroyed on map change
	if (game.createplayer()) return 1;
	
	return 0;
}
Example #2
0
int joyai_open(void)
{
    if (ai_init() == -1) {
        return -1;
    }

    ai_attach();

    return 0;
}
Example #3
0
int joyai_open(void)
{
    if (ai_init() == -1) {
        return -1;
    }

    /* HACK: try to load the ini file, so we don't fall back to the defaults */
    resources_load(NULL);

    ai_attach();

    return 0;
}
void tta_init(struct tic_tac_ai* ai, int size, int dimension, 
	bool player_1, bool player_2){
	// the player 1 and player 2 bools determine whether we want AI or not
	ttc_init(&ai->game,size,dimension); // init inside tic tac toe struct
	if (player_1 == true){
		//printf("\nInit player %i as AI", 1);
		ai_init(&ai->ai_1,1,size,dimension);
		ai->is_ai[0] = 1;
	}
	else {
		//printf("\nInit player %i not as AI", 1);
		ai->is_ai[0] = 0;
	}
	if (player_2 == true){
		//printf("\nInit player %i as AI", -1);
		ai_init(&ai->ai_2,-1,size,dimension);
		ai->is_ai[1] = 1;
	}
	else {
		//printf("\nInit player %i not as AI", -1);
		ai->is_ai[1] = 0;
	}
}
Example #5
0
int main(int argc, char** argv)
{
  GameState state;
  
  ai_process_args(argc,argv);

  AIEngineParams defaults = {};
  defaults.num_players = 2;
  defaults.max_search_level = 9;
  ai_init(&defaults);

  assert(num_players == 2);
  init_game(&state);
  play_game(&state);
  ai_print_endgame_results(&state);
  
  return 0;
}
Example #6
0
int
main(int argc, char **argv)
{
#ifdef USE_ASM
	as_mallocation_t asm_array[MAX_NUM_MALLOCATIONS];

	// Zero-out the statically-allocated array of memory allocation locations.
	memset(asm_array, 0, sizeof(asm_array));

	// Set the ASMalloc callback user data.
	g_my_cb_udata = asm_array;

	// This must come first to allow initialization of the ASMalloc library.
	asm_init();
#endif // defined(USE_ASM)

#ifdef USE_JEM
	// Initialize the JEMalloc interface.
	jem_init(true);
#endif

	// Initialize ref-counting system.
	cf_rc_init(NULL);

	// Initialize fault management framework.
	cf_fault_init();

	// Setup signal handlers.
	as_signal_setup();

	// Initialize the Jansson JSON API.
	as_json_init();

	int i;
	int cmd_optidx;
	const char *config_file = DEFAULT_CONFIG_FILE;
	bool run_in_foreground = false;
	bool cold_start_cmd = false;
	uint32_t instance = 0;

	// Parse command line options.
	while (-1 != (i = getopt_long(argc, argv, "", cmd_opts, &cmd_optidx))) {
		switch (i) {
		case 'h':
			// printf() since we want stdout and don't want cf_fault's prefix.
			printf("%s\n", HELP);
			return 0;
		case 'v':
			// printf() since we want stdout and don't want cf_fault's prefix.
			printf("%s build %s\n", aerospike_build_type, aerospike_build_id);
			return 0;
		case 'f':
			config_file = cf_strdup(optarg);
			cf_assert(config_file, AS_AS, CF_CRITICAL, "config filename cf_strdup failed");
			break;
		case 'd':
			run_in_foreground = true;
			break;
		case 'c':
			cold_start_cmd = true;
			break;
		case 'n':
			instance = (uint32_t)strtol(optarg, NULL, 0);
			break;
		default:
			// fprintf() since we don't want cf_fault's prefix.
			fprintf(stderr, "%s\n", USAGE);
			return 1;
		}
	}

	// Set all fields in the global runtime configuration instance. This parses
	// the configuration file, and creates as_namespace objects. (Return value
	// is a shortcut pointer to the global runtime configuration instance.)
	as_config *c = as_config_init(config_file);

#ifdef USE_ASM
	g_asm_hook_enabled = g_asm_cb_enabled = c->asmalloc_enabled;

	long initial_tid = syscall(SYS_gettid);
#endif

#ifdef MEM_COUNT
	// [Note: This should ideally be at the very start of the "main()" function,
	//        but we need to wait until after the config file has been parsed in
	//        order to support run-time configurability.]
	mem_count_init(c->memory_accounting ? MEM_COUNT_ENABLE : MEM_COUNT_DISABLE);
#endif

	// Perform privilege separation as necessary. If configured user & group
	// don't have root privileges, all resources created or reopened past this
	// point must be set up so that they are accessible without root privileges.
	// If not, the process will self-terminate with (hopefully!) a log message
	// indicating which resource is not set up properly.
	if (0 != c->uid && 0 == geteuid()) {
		// To see this log, change NO_SINKS_LIMIT in fault.c:
		cf_info(AS_AS, "privsep to %d %d", c->uid, c->gid);
		cf_process_privsep(c->uid, c->gid);
	}

	//
	// All resources such as files, devices, and shared memory must be created
	// or reopened below this line! (The configuration file is the only thing
	// that must be opened above, in order to parse the user & group.)
	//==========================================================================

	// Activate log sinks. Up to this point, 'cf_' log output goes to stderr,
	// filtered according to NO_SINKS_LIMIT in fault.c. After this point, 'cf_'
	// log output will appear in all log file sinks specified in configuration,
	// with specified filtering. If console sink is specified in configuration,
	// 'cf_' log output will continue going to stderr, but filtering will switch
	// from NO_SINKS_LIMIT to that specified in console sink configuration.
	if (0 != cf_fault_sink_activate_all_held()) {
		// Specifics of failure are logged in cf_fault_sink_activate_all_held().
		cf_crash_nostack(AS_AS, "can't open log sink(s)");
	}

	// Daemonize asd if specified. After daemonization, output to stderr will no
	// longer appear in terminal. Instead, check /tmp/aerospike-console.<pid>
	// for console output.
	if (! run_in_foreground && c->run_as_daemon) {
		// Don't close any open files when daemonizing. At this point only log
		// sink files are open - instruct cf_process_daemonize() to ignore them.
		int open_fds[CF_FAULT_SINKS_MAX];
		int num_open_fds = cf_fault_sink_get_fd_list(open_fds);

		cf_process_daemonize(open_fds, num_open_fds);
	}

#ifdef USE_ASM
	// Log the main thread's Linux Task ID (pre- and post-fork) to the console.
	fprintf(stderr, "Initial main thread tid: %lu\n", initial_tid);

	if (! run_in_foreground && c->run_as_daemon) {
		fprintf(stderr, "Post-daemonize main thread tid: %lu\n",
				syscall(SYS_gettid));
	}
#endif

	// Log which build this is - should be the first line in the log file.
	cf_info(AS_AS, "<><><><><><><><><><>  %s build %s  <><><><><><><><><><>",
			aerospike_build_type, aerospike_build_id);

	// Includes echoing the configuration file to log.
	as_config_post_process(c, config_file);

	// If we allocated a non-default config file name, free it.
	if (config_file != DEFAULT_CONFIG_FILE) {
		cf_free((void*)config_file);
	}

	// Write the pid file, if specified.
	write_pidfile(c->pidfile);

	// Check that required directories are set up properly.
	validate_directory(c->work_directory, "work");
	validate_directory(c->mod_lua.system_path, "Lua system");
	validate_directory(c->mod_lua.user_path, "Lua user");
	validate_smd_directory();

	// Initialize subsystems. At this point we're allocating local resources,
	// starting worker threads, etc. (But no communication with other server
	// nodes or clients yet.)

	as_smd_init();				// System Metadata first - others depend on it
	ai_init();					// before as_storage_init() populates indexes
	as_sindex_thr_init();		// defrag secondary index (ok during population)

	// Initialize namespaces. Each namespace decides here whether it will do a
	// warm or cold start. Index arenas, partition structures and index tree
	// structures are initialized. Secondary index system metadata is restored.
	as_namespaces_init(cold_start_cmd, instance);

	// Initialize the storage system. For cold starts, this includes reading
	// all the objects off the drives. This may block for a long time. The
	// defrag subsystem starts operating at the end of this call.
	as_storage_init();

	// Populate all secondary indexes. This may block for a long time.
	as_sindex_boot_populateall();

	cf_info(AS_AS, "initializing services...");

	as_netio_init();
	as_security_init();			// security features
	as_tsvc_init();				// all transaction handling
	as_hb_init();				// inter-node heartbeat
	as_fabric_init();			// inter-node communications
	as_info_init();				// info transaction handling
	as_paxos_init();			// cluster consensus algorithm
	as_migrate_init();			// move data between nodes
	as_proxy_init();			// do work on behalf of others
	as_write_init();			// write service
	as_query_init();			// query transaction handling
	as_udf_init();				// apply user-defined functions
	as_scan_init();				// scan a namespace or set
	as_batch_init();			// batch transaction handling
	as_batch_direct_init();		// low priority transaction handling        
	as_xdr_init();				// cross data-center replication
	as_mon_init();				// monitor

	// Wait for enough available storage. We've been defragging all along, but
	// here we wait until it's enough. This may block for a long time.
	as_storage_wait_for_defrag();

	// Start subsystems. At this point we may begin communicating with other
	// cluster nodes, and ultimately with clients.

	as_smd_start(c->smd);		// enables receiving paxos state change events
	as_fabric_start();			// may send & receive fabric messages
	as_hb_start();				// start inter-node heatbeat
	as_paxos_start();			// blocks until cluster membership is obtained
	as_nsup_start();			// may send delete transactions to other nodes
	as_demarshal_start();		// server will now receive client transactions
	as_info_port_start();		// server will now receive info transactions
	info_debug_ticker_start();	// only after everything else is started

	// Log a service-ready message.
	cf_info(AS_AS, "service ready: soon there will be cake!");

	//--------------------------------------------
	// Startup is done. This thread will now wait
	// quietly for a shutdown signal.
	//

	// Stop this thread from finishing. Intentionally deadlocking on a mutex is
	// a remarkably efficient way to do this.
	pthread_mutex_init(&g_NONSTOP, NULL);
	pthread_mutex_lock(&g_NONSTOP);
	g_startup_complete = true;
	pthread_mutex_lock(&g_NONSTOP);

	// When the service is running, you are here (deadlocked) - the signals that
	// stop the service (yes, these signals always occur in this thread) will
	// unlock the mutex, allowing us to continue.

	g_shutdown_started = true;
	pthread_mutex_unlock(&g_NONSTOP);
	pthread_mutex_destroy(&g_NONSTOP);

	//--------------------------------------------
	// Received a shutdown signal.
	//

	as_storage_shutdown();
	as_xdr_shutdown();
	as_smd_shutdown(c->smd);

	cf_info(AS_AS, "finished clean shutdown - exiting");

	// If shutdown was totally clean (all threads joined) we could just return,
	// but for now we exit to make sure all threads die.
#ifdef DOPROFILE
	exit(0); // exit(0) so profile build actually dumps gmon.out
#else
	_exit(0);
#endif

	return 0;
}
Example #7
0
static BOOL
socks5_cmd_bind (conn_t * conn, chain_t * chain)
{
	ANCONN remote;
	SOCKET top;
	PI_SA sout;
	PI_SA locallistener;
	sl_t soutlen;
	SOCKADDR *addr;
	sl_t addrlen;
	fd_set fds;
	struct timeval tv;
#ifdef WITH_DEBUG
	char szDebug[100];
#endif

	if (!ai_getSockaddr (&conn->dest, &addr, &addrlen))
		return FALSE;
#ifdef WITH_DEBUG
	sprintf (szDebug, "Socket family: %x", addr->sa_family);
	DEBUG_LOG (szDebug);
#endif
	remote = an_new_connection ();
	conn_setupchain (conn, remote, chain);
#ifdef WITH_DEBUG
	{
		char *dest;
		dest = ai_getString (&conn->dest);
		sprintf (szDebug, "Binding to %s", dest);
		DEBUG_LOG (szDebug);
		free (dest);
	}
#endif
	/* FIXME: This hack listens on all interfaces on any port.
	 * Is this really what the spec had in mind? */
	if ((remote == NULL) ||
		(an_bind_tosockaddr (remote, (SOCKADDR *) addr, addrlen) !=
		 AN_ERROR_SUCCESS) || (an_listen (remote) != AN_ERROR_SUCCESS)) {
#ifdef WITH_DEBUG
		DEBUG_LOG ("Bind failed");
#endif
		an_close (remote);
		an_destroy (remote);
		socks5_sendResponse (conn, 0x01, NULL);
		free (addr);
		return FALSE;
	}
	soutlen = sizeof (locallistener);
	socks5_notifyclient (conn, remote, (SOCKADDR *) & locallistener, soutlen);
	/* Wait to see if a connection is arriving in a reasonable time */

	top = conn->s;
	FD_ZERO (&fds);
	FD_SET (conn->s, &fds);
	top = AN_FD_SET (remote, &fds, top);
	tv.tv_sec = config_getMaxbindwait (conn->conf);
	tv.tv_usec = 0;
	select (top + 1, &fds, NULL, NULL, &tv);

	if (AN_FD_ISSET (remote, &fds)) {
		addrinfo_t ai;
		int correct;
		char *whoArrived;
		char *whoShould;
		/* Now to wait for incoming connection */
		memset (&sout, 0, sizeof (sout));
		((SOCKADDR *) & sout)->sa_family = addr->sa_family;
		free (addr);
		soutlen = sizeof (sout);
		if (an_accept (remote, (SOCKADDR *) & sout, soutlen) !=
			AN_ERROR_SUCCESS) {
			/* Whoa, accept() failed.  Better tell client */
			an_close (remote);
			an_destroy (remote);
#ifdef WITH_DEBUG
			DEBUG_LOG ("an_accept failed");
#endif
			socks5_sendResponse (conn, 0x01, NULL);
			return FALSE;
		}
		ai_init (&ai, ((SOCKADDR *) & sout));
		whoArrived = ai_getAddressString (&ai);
		whoShould = ai_getAddressString (&conn->dest);
		ai_close (&ai);
		correct = strcmp (whoArrived, whoShould);
		free (whoArrived);
		free (whoShould);
		if (correct != 0) {
			/* Somebody connected who shouldn't have. */
#ifdef WITH_DEBUG
			DEBUG_LOG ("Connection not from the right host");
#endif
			socks5_sendResponse (conn, 0x01, NULL);
			an_close (remote);
			an_destroy (remote);
			return FALSE;
		}

		/* Tell client who just arrived */
		socks5_notifyclient (conn, remote, (SOCKADDR *) & sout, soutlen);

		log_log (conn, LOG_EVT_LOG, LOG_TYPE_CONNECTIONESTABLISHED, NULL);

		/* As with connect, start forwarding data. */
		conn_forwardData (conn, remote);
	} else {
#ifdef WITH_DEBUG
		DEBUG_LOG ("select returned but without a connection");
#endif
		free (addr);
		/* Either outside connection timed out or inside
		 * tried something */
		socks5_sendResponse (conn, 0x01, NULL);
		an_close (remote);
		an_destroy (remote);
		return FALSE;
	}

	return TRUE;
}
Example #8
0
// Creates and initializes a device.
struct cen64_device *device_create(struct cen64_device *device,
  const struct rom_file *ddipl, const struct rom_file *ddrom,
  const struct rom_file *pifrom, const struct rom_file *cart,
  const struct save_file *eeprom, const struct save_file *sram,
  const struct save_file *flashram, const struct controller *controller,
  bool no_audio, bool no_video) {

  // Initialize the bus.
  device->bus.ai = &device->ai;
  device->bus.dd = &device->dd;
  device->bus.pi = &device->pi;
  device->bus.ri = &device->ri;
  device->bus.si = &device->si;
  device->bus.vi = &device->vi;

  device->bus.rdp = &device->rdp;
  device->bus.rsp = &device->rsp;
  device->bus.vr4300 = &device->vr4300;

  // Initialize the bus.
  if (bus_init(&device->bus)) {
    debug("create_device: Failed to initialize the bus.\n");
    return NULL;
  }

  // Initialize the AI.
  if (ai_init(&device->ai, &device->bus, no_audio)) {
    debug("create_device: Failed to initialize the AI.\n");
    return NULL;
  }

  // Initialize the DD.
  if (dd_init(&device->dd, &device->bus,
    ddipl->ptr, ddrom->ptr, ddrom->size)) {
    debug("create_device: Failed to initialize the DD.\n");
    return NULL;
  }

  // Initialize the PI.
  if (pi_init(&device->pi, &device->bus, cart->ptr, cart->size, sram, flashram)) {
    debug("create_device: Failed to initialize the PI.\n");
    return NULL;
  }

  // Initialize the RI.
  if (ri_init(&device->ri, &device->bus)) {
    debug("create_device: Failed to initialize the RI.\n");
    return NULL;
  }

  // Initialize the SI.
  if (si_init(&device->si, &device->bus, pifrom->ptr,
    cart->ptr, ddipl->ptr != NULL, eeprom->ptr, eeprom->size,
    controller)) {
    debug("create_device: Failed to initialize the SI.\n");
    return NULL;
  }

  // Initialize the VI.
  if (vi_init(&device->vi, &device->bus, no_video)) {
    debug("create_device: Failed to initialize the VI.\n");
    return NULL;
  }

  // Initialize the RDP.
  if (rdp_init(&device->rdp, &device->bus)) {
    debug("create_device: Failed to initialize the RDP.\n");
    return NULL;
  }

  // Initialize the RSP.
  if (rsp_init(&device->rsp, &device->bus)) {
    debug("create_device: Failed to initialize the RSP.\n");
    return NULL;
  }

  // Initialize the VR4300.
  if (vr4300_init(&device->vr4300, &device->bus)) {
    debug("create_device: Failed to initialize the VR4300.\n");
    return NULL;
  }

  return device;
}