Beispiel #1
0
int save_request(char *request, size_t len, int wSock, int clSock) {
	LOG_INFO("Consumer: Save_Request\n");
	
	int res;
	
	pthread_mutex_lock(&savedReq_map_mutex);
	res = put_hashmap(&savedRequests, request, len, &wSock, sizeof(int));
	if (res == OK) {
		res = put_hashmap(&cl_req_map, &clSock, sizeof(int), request, len);
	}
	pthread_mutex_unlock(&savedReq_map_mutex);
	
	return res;
}
Beispiel #2
0
int map_client_with_webserver(int wbSock, int clSock) {
	LOG_INFO("Consumer: Map_Client_With_Webserver\n");
	int res;
	
	if (contains_appropriate_clients(wbSock) != FOUND) {
		Queue queue;	
		init_queue(&queue);
		
		if (push_queue(&queue, &clSock) != OK) {
			return QUEUE_ERROR;
		}
		
		pthread_mutex_lock(&wb_cl_map_mutex);	
		res = put_hashmap(&wb_cl_map, &wbSock, sizeof(int), &queue, sizeof(Queue));
		pthread_mutex_unlock(&wb_cl_map_mutex);
	} else {
		Queue *queue = find_appropriate_clients(wbSock);
		
		pthread_mutex_lock(&wb_cl_map_mutex);	
		res = push_queue(queue, &clSock);
		pthread_mutex_unlock(&wb_cl_map_mutex);
	}
	
	return res;
}
Beispiel #3
0
struct aitask* ai_initTask(struct entity* entity, uint16_t mutex, int32_t (*ai_tick)(struct world* world, struct entity* entity, struct aitask* ai), int (*ai_should)(struct world* world, struct entity* entity, struct aitask* ai), void* data) {
	struct aitask* ai = xmalloc(sizeof(struct aitask));
	ai->ai_running = 0;
	ai->ai_waiting = 0;
	ai->mutex = mutex;
	ai->ai_should = ai_should;
	ai->ai_tick = ai_tick;
	ai->ai_cancel = &ai_stdcancel;
	ai->data = data;
	put_hashmap(entity->ai->tasks, (uint64_t) ai, ai);
	return ai;
}
Beispiel #4
0
int ai_shouldnearestattackabletarget(struct world* world, struct entity* entity, struct aitask* ai) {
	struct ai_nearestattackabletarget_data* data = ai->data;
	if (data->chance > 0 && rand() % data->chance != 0) return 0;
	double cd = data->targetDist * data->targetDist;
	struct entity* ce = NULL;
	BEGIN_HASHMAP_ITERATION(world->entities)
	struct entity* ie = value;
	if (!hasFlag(getEntityInfo(ie->type), "livingbase") || ie == entity) continue;
	double dsq = entity_distsq(entity, value);
	if (ie->type == ENT_PLAYER) {
		struct player* pl = ie->data.player.player;
		if (pl->gamemode == 1 || pl->gamemode == 3 || pl->invulnerable) continue;
		int sk = hasFlag(getEntityInfo(entity->type), "skeleton");
		int zo = hasFlag(getEntityInfo(entity->type), "zombie");
		int cr = hasFlag(getEntityInfo(entity->type), "creeper");
		if (sk || zo || cr) {
			struct slot* hs = inventory_get(pl, pl->inventory, 5);
			if (hs != NULL) {
				if (sk && hs->damage == 0) dsq *= 2.;
				else if (zo && hs->damage == 2) dsq *= 2.;
				else if (cr && hs->damage == 4) dsq *= 2.;
			}
		}
	}
	if (dsq < cd) {
		//TODO check teams, sight
		cd = dsq;
		ce = value;
	}
	END_HASHMAP_ITERATION(world->entities)
	if (entity->attacking != NULL && ce != entity->attacking) put_hashmap(entity->attacking->attackers, entity->id, NULL);
	if (ce != NULL && entity->attacking != ce) {
		put_hashmap(ce->attackers, entity->id, entity);
	}
	entity->attacking = ce;
	return 0;
}
Beispiel #5
0
void init_plugins() {
    plugin_pool = mempool_new();
	plugins = hashmap_new(16, plugin_pool);
	DIR* dir = opendir("plugins/");
	char lp[PATH_MAX];
	uint32_t lua_count = 0;
	char** lua_paths = NULL;
	if (dir != NULL) {
		struct dirent* de = NULL;
		while ((de = readdir(dir)) != NULL) {
			snprintf(lp, PATH_MAX, "plugins/%s", de->d_name);
			if (str_suffixes(de->d_name, ".so")) { // BASIN C
				struct plugin* pl = xcalloc(sizeof(struct plugin));
				pl->hnd = dlopen(lp, RTLD_GLOBAL | RTLD_NOW);
				if (pl->hnd == NULL) {
					printf("Error loading plugin! %s\n", dlerror());
					free(pl);
					continue;
				}
				pl->filename = xstrdup(de->d_name, 0);
				void (*init)(struct plugin* plugin) = dlsym(pl->hnd, "init_plugin");
				(*init)(pl);
				pl->type = PLUGIN_BASIN;
				put_hashmap(plugins, next_plugin_id++, pl);
			} else if (str_suffixes(de->d_name, ".jar")) { // BUKKIT
				struct plugin* pl = xcalloc(sizeof(struct plugin));
				pl->type = PLUGIN_BUKKIT;
				char* cp = xmalloc(strlen(lp) + strlen("-Djava.class.path=bukkit.jar:basinBukkit.jar:") + 1);
				strcpy(cp, "-Djava.class.path=bukkit.jar:basinBukkit.jar:"); // len checks done above
				strcat(cp, lp);
				JavaVMOption jvmopt[1];
				jvmopt[0].optionString = cp;

				JavaVMInitArgs vmArgs;
				vmArgs.version = JNI_VERSION_1_2;
				vmArgs.nOptions = 1;
				vmArgs.options = jvmopt;
				vmArgs.ignoreUnrecognized = JNI_TRUE;
				long flag = JNI_CreateJavaVM(&pl->javaVM, (void**) &pl->jniEnv, &vmArgs);
				if (flag == JNI_ERR) {
					printf("Error creating Java VM, Bukkit plugins will not be loaded.\n");
					free(pl);
					free(cp);
					continue;
				}
				jclass loadHelper = (*pl->jniEnv)->FindClass(pl->jniEnv, "org.basin.bukkit.LoadHelper");
				if (loadHelper == NULL) {
					(*pl->jniEnv)->ExceptionDescribe(pl->jniEnv);
					(*pl->javaVM)->DestroyJavaVM(pl->javaVM);
					free(pl);
					free(cp);
					continue;
				}
				jmethodID loadMainYaml = (*pl->jniEnv)->GetStaticMethodID(pl->jniEnv, loadHelper, "loadMainYaml", "()V");
				(*pl->jniEnv)->CallStaticVoidMethod(pl->jniEnv, loadHelper, loadMainYaml);
				if ((*pl->jniEnv)->ExceptionCheck(pl->jniEnv)) {
					(*pl->jniEnv)->ExceptionDescribe(pl->jniEnv);
					(*pl->jniEnv)->ExceptionClear(pl->jniEnv);
				}
				pl->filename = xstrdup(de->d_name, 0);
				put_hashmap(plugins, next_plugin_id++, pl);
			} else if (endsWith_nocase(de->d_name, ".zip")) { // LUA
				lua_paths = xrealloc(lua_paths, (lua_count + 1) * sizeof(char*));
				lua_paths[lua_count++] = xstrdup(lp, 0);
			}
		}
		closedir(dir);
	}
	if (lua_count > 0) {
		//TODO: create LUA VM
	}
}