/* * Class: DoomGame * Method: getState * Signature: ()LGameState; */ JNIEXPORT jobject JNICALL Java_vizdoom_DoomGame_getState (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); jclass state = env->FindClass("vizdoom/GameState"); int rozmiar=game->getScreenSize(); std::vector<int> ourvector; GameState statec = game->getState(); ourvector=statec.gameVariables; jintArray jbuffer = env->NewIntArray(ourvector.size()); jint *oarr = env->GetIntArrayElements(jbuffer, NULL); for (int i=0;i<ourvector.size();i++){ oarr[i]=ourvector[i]; } env->ReleaseIntArrayElements(jbuffer, oarr, NULL); uint8_t *pointer; pointer=statec.imageBuffer; jintArray jbuffer2 = env->NewIntArray(rozmiar); oarr = env->GetIntArrayElements(jbuffer2, NULL); for (int i=0;i<rozmiar;i++){ oarr[i]=(int)*(pointer+i); } env->ReleaseIntArrayElements(jbuffer2, oarr, NULL); jmethodID constructor = env->GetMethodID(state, "<init>", "(I[I[I)V"); jobject result = env->NewObject(state, constructor, statec.number,jbuffer, jbuffer2); return result; }
/* * Class: DoomGame * Method: getScreenSize * Signature: ()I */ JNIEXPORT jint JNICALL Java_vizdoom_DoomGame_getScreenSize (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); int ret; ret=game->getScreenSize(); return (jint)ret; }
/* * Class: DoomGame * Method: advanceAction * Signature: (IZZ)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_advanceAction__IZZ (JNIEnv *env, jobject obj, jint int1, jboolean bol1, jboolean bol2){ try{ DoomGame* game = GetObject(env,obj); game->advanceAction(int1, bol1,bol2); } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: advanceAction * Signature: ()V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_advanceAction__ (JNIEnv *env, jobject obj){ try{ DoomGame* game = GetObject(env,obj); game->advanceAction(); } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: newEpisode * Signature: ()V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_newEpisode__ (JNIEnv *env, jobject obj){ try{ DoomGame* game = GetObject(env,obj); game->newEpisode(); } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: setDoomScenarioPath * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setDoomScenarioPath (JNIEnv *env, jobject obj, jstring jstr){ try{ DoomGame* game = GetObject(env,obj); JSTR_2_STR(jstr, str) game->setDoomScenarioPath(str); } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: isPlayerDead * Signature: ()Z */ JNIEXPORT jboolean JNICALL Java_vizdoom_DoomGame_isPlayerDead (JNIEnv *env, jobject obj){ try{ DoomGame* game = GetObject(env,obj); bool ret=game->isPlayerDead(); return (jboolean)ret; } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: replayEpisode * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_replayEpisode (JNIEnv *env, jobject obj, jstring jstr){ try{ DoomGame* game = GetObject(env,obj); JSTR_2_STR(jstr, str) game->replayEpisode(str); } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: loadConfig * Signature: (Ljava/lang/String;)Z */ JNIEXPORT jboolean JNICALL Java_vizdoom_DoomGame_loadConfig (JNIEnv *env, jobject obj, jstring jstr){ try{ DoomGame* game = GetObject(env,obj); JSTR_2_STR(jstr, str) return (jboolean) game->loadConfig(str); } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: getLastAction */ JNIEXPORT jintArray JNICALL Java_vizdoom_DoomGame_getLastAction (JNIEnv *env, jobject obj){ std::vector<int> ourvector; DoomGame* game = GetObject(env,obj); ourvector = game->getLastAction(); jintArray bob=env->NewIntArray(ourvector.size()); jint *oarr = env->GetIntArrayElements(bob, NULL); for (int i=0;i<ourvector.size();i++){ oarr[i]=ourvector.at(i); } env->ReleaseIntArrayElements(bob, oarr, NULL); return bob; }
/* * Class: DoomGame * Method: addAvailableGameVariable * Signature: (Lvizdoom/GameVariable;)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_addAvailableGameVariable (JNIEnv *env, jobject obj, jobject enumVal){ DoomGame* game = GetObject(env,obj); jclass jclassEnum = env->FindClass("vizdoom/GameVariable"); if(jclassEnum != 0){ jmethodID ordinal_ID = env->GetMethodID(jclassEnum, "ordinal", "()I"); if (ordinal_ID == 0){ return; } jint value = env->CallIntMethod(enumVal, ordinal_ID); GameVariable ret=static_cast<GameVariable>(value); game->addAvailableGameVariable(ret); env->DeleteLocalRef(jclassEnum); } }
/* * Class: DoomGame * Method: setButtonMaxValue * Signature: (Lvizdoom/Button;I)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setButtonMaxValue (JNIEnv *env, jobject obj, jobject enumVal, jint intval){ DoomGame* game = GetObject(env,obj); jclass jclassEnum = env->FindClass("vizdoom/Button"); if(jclassEnum != 0){ jmethodID ordinal_ID = env->GetMethodID(jclassEnum, "ordinal", "()I"); if (ordinal_ID == 0){ return; } jint value = env->CallIntMethod(enumVal, ordinal_ID); Button ret=static_cast<Button>(value); game->setButtonMaxValue(ret, intval); env->DeleteLocalRef(jclassEnum); } }
/* * Class: DoomGame * Method: setNextAction * Signature: (Ljava/util/ArrayList;)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setAction (JNIEnv *env, jobject obj, jintArray ourarray){ try { DoomGame* game = GetObject(env,obj); int NumElts = env->GetArrayLength(ourarray); jint *oarr = env->GetIntArrayElements(ourarray, NULL); std::vector<int> ourvector; for (int i=0;i<NumElts;i++){ ourvector.push_back(oarr[i]); } game->setAction(ourvector); } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: setScreenFormat * Signature: (Lvizdoom/ScreenFormat;)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setScreenFormat (JNIEnv *env, jobject obj, jobject enumVal){ DoomGame* game = GetObject(env,obj); jclass jclassEnum = env->FindClass("vizdoom/ScreenFormat"); if(jclassEnum != 0){ jmethodID ordinal_ID = env->GetMethodID(jclassEnum, "ordinal", "()I"); if (ordinal_ID == 0){ return; } jint value = env->CallIntMethod(enumVal, ordinal_ID); ScreenFormat ret=static_cast<ScreenFormat>(value); game->setScreenFormat(ret); env->DeleteLocalRef(jclassEnum); } }
/* * Class: DoomGame * Method: makeAction * Signature: ([II)F */ JNIEXPORT jdouble JNICALL Java_vizdoom_DoomGame_makeAction___3II (JNIEnv *env, jobject obj, jintArray ourarray, jint integ){ try { DoomGame* game = GetObject(env,obj); int NumElts = env->GetArrayLength(ourarray); jint *oarr = env->GetIntArrayElements(ourarray, NULL); std::vector<int> ourvector; for (int i=0;i<NumElts;i++){ ourvector.push_back(oarr[i]); } double ret = game->makeAction(ourvector, integ); return ret; } catch(...){ throwAsJavaException(env); } }
/* * Class: DoomGame * Method: getGameScreen * Signature: ()Ljava/util/ArrayList; */ JNIEXPORT jintArray JNICALL Java_vizdoom_DoomGame_getGameScreen (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); int rozmiar=game->getScreenSize(); std::vector<int> ourvector; uint8_t *pointer; pointer=game->getGameScreen(); jintArray jbuffer = env->NewIntArray(rozmiar); jint *oarr; oarr = env->GetIntArrayElements(jbuffer, NULL); for (int i = 0; i < rozmiar; i++){ oarr[i] = (int)*(pointer+i); } env->ReleaseIntArrayElements(jbuffer, oarr, NULL); return jbuffer; }
/* * Class: DoomGame * Method: getGameVariable * Signature: (Lvizdoom/GameVariable;)I */ JNIEXPORT jint JNICALL Java_vizdoom_DoomGame_getGameVariable (JNIEnv *env, jobject obj, jobject enumVal){ DoomGame* game = GetObject(env,obj); jclass jclassEnum = env->FindClass("vizdoom/GameVariable"); if(jclassEnum != 0){ jmethodID ordinal_ID = env->GetMethodID(jclassEnum, "ordinal", "()I"); if (ordinal_ID == 0){ return -1; } jint value = env->CallIntMethod(enumVal, ordinal_ID); GameVariable ret=static_cast<GameVariable>(value); int retint=game->getGameVariable(ret); // Delete local references created env->DeleteLocalRef(jclassEnum); return retint; } }
int main(){ std::cout << "\n\nSHAPING EXAMPLE\n\n"; DoomGame *game = new DoomGame(); // Health gathering scenario has scripted shaping reward. game->loadConfig("../../examples/config/health_gathering.cfg"); game->setScreenResolution(RES_640X480); game->init(); // Define some actions. std::vector<int> actions[3]; actions[0] = {1, 0, 0}; actions[1] = {0, 1, 0}; actions[2] = {0, 0, 1}; std::srand(time(0)); int episodes = 10; unsigned int sleepTime = 28; // Use this to remember last shaping reward value. double lastTotalShapingReward = 0; for (int i = 0; i < episodes; ++i) { std::cout << "Episode #" << i + 1 << "\n"; // Seed can be changed anytime. It will affect next episodes. // game->setSeed(seed); game->newEpisode(); lastTotalShapingReward = 0; while (!game->isEpisodeFinished()) { // Get the state GameStatePtr state = game->getState(); // Make random action and get reward double reward = game->makeAction(actions[std::rand() % 3]); // Retrieve the shaping reward int fixedShapingReward = game->getGameVariable(USER1); // Get value of scripted variable double shapingReward = doomFixedToDouble(shapingReward); // If value is in DoomFixed format project it to double shapingReward = shapingReward - lastTotalShapingReward; lastTotalShapingReward += shapingReward; std::cout << "State #" << state->number << "\n"; std::cout << "Health: " << state->gameVariables[0] << "\n"; std::cout << "Action reward: " << reward << "\n"; std::cout << "Action shaping reward: " << shapingReward << "\n"; std::cout << "=====================\n"; if(sleepTime) sleep(sleepTime); } std::cout << "Episode finished.\n"; std::cout << "Total reward: " << game->getTotalReward() << "\n"; std::cout << "************************\n"; } // It will be done automatically in destructor but after close You can init it again with different settings. game->close(); delete game; }
int main(){ std::cout << "\n\nSEED EXAMPLE\n\n"; DoomGame *game = new DoomGame(); // Choose scenario config file you wish to be watched by agent. // Don't load two configs cause the second will overwrite the first one. // Multiple config files are ok but combining these ones doesn't make much sense. game->loadConfig("../../examples/config/basic.cfg"); // game->loadConfig("../../examples/config/deadly_corridor.cfg"); // game->loadConfig("../../examples/config/deathmatch.cfg"); // game->loadConfig("../../examples/config/defend_the_center.cfg"); // game->loadConfig("../../examples/config/defend_the_line.cfg"); // game->loadConfig("../../examples/config/health_gathering.cfg"); // game->loadConfig("../../examples/config/my_way_home.cfg"); // game->loadConfig("../../examples/config/predict_position.cfg"); // game->loadConfig("../../examples/config/take_cover.cfg"); game->setScreenResolution(RES_640X480); unsigned int seed = 1234; // Sets the seed. It could be after init as well. game->setSeed(seed); game->init(); std::vector<int> actions[3]; int action0[] = {1, 0, 0}; actions[0] = std::vector<int>(action0, action0 + sizeof(action0) / sizeof(int)); int action1[] = {0, 1, 0}; actions[1] = std::vector<int>(action1, action1 + sizeof(action1) / sizeof(int)); int action2[] = {0, 0, 1}; actions[2] = std::vector<int>(action2, action2 + sizeof(action2) / sizeof(int)); std::srand(time(0)); // Run this many episodes int episodes = 10; for (int i = 0; i < episodes; ++i) { std::cout << "Episode #" << i + 1 << "\n"; // Seed can be changed anytime. It will affect next episodes. // game->setSeed(seed); game->newEpisode(); while (!game->isEpisodeFinished()) { // Get the state GameState s = game->getState(); // Make random action and get reward double r = game->makeAction(actions[std::rand() % 3]); std::cout << "State #" << s.number << "\n"; std::cout << "Action reward: " << r << "\n"; std::cout << "Seed: " << game->getSeed() << "\n"; std::cout << "=====================\n"; } std::cout << "Episode finished.\n"; std::cout << "Total reward: " << game->getTotalReward() << "\n"; std::cout << "************************\n"; } // It will be done automatically in destructor but after close You can init it again with different settings. game->close(); delete game; }
/* * Class: DoomGame * Method: setSeed * Signature: (I)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setSeed (JNIEnv *env, jobject obj, jint seed){ DoomGame* game = GetObject(env,obj); game->setSeed(seed); }
/* * Class: DoomGame * Method: setDoomConfigPath * Signature: (Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setDoomConfigPath (JNIEnv *env, jobject obj, jstring jstr){ DoomGame* game = GetObject(env,obj); JSTR_2_STR(jstr, str) game->setDoomConfigPath(str); }
/* * Class: DoomGame * Method: setDoomSkill * Signature: (I)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setDoomSkill (JNIEnv *env, jobject obj, jint skill){ DoomGame* game = GetObject(env,obj); game->setDoomSkill(skill); }
/* * Class: DoomGame * Method: clearCustomGameArgs * Signature: ()V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_clearGameArgs (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); game->clearGameArgs(); }
/* * Class: DoomGame * Method: getTotalReward * Signature: ()F */ JNIEXPORT jdouble JNICALL Java_vizdoom_DoomGame_getTotalReward (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); double ret=game->getTotalReward(); return ret; }
/* * Class: DoomGame * Method: setDeathPenalty * Signature: (F)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setDeathPenalty (JNIEnv *env, jobject obj, jdouble rew){ DoomGame* game = GetObject(env,obj); game->setDeathPenalty(rew); }
/* * Class: DoomGame * Method: getDeathPenalty * Signature: ()F */ JNIEXPORT jdouble JNICALL Java_vizdoom_DoomGame_getDeathPenalty (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); double ret = game->getDeathPenalty(); return ret; }
/* * Class: DoomGame * Method: setLivingReward * Signature: (F)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setLivingReward (JNIEnv *env, jobject obj, jdouble rew){ DoomGame* game = GetObject(env,obj); game->setLivingReward(rew); }
/* * Class: DoomGame * Method: setTicrate * Signature: (I)V */ JNIEXPORT void JNICALL Java_vizdoom_DoomGame_setTicrate (JNIEnv *env, jobject obj, jint ticrate){ DoomGame* game = GetObject(env,obj); game->setTicrate(ticrate); }
/* * Class: DoomGame * Method: getTicrate * Signature: ()I */ JNIEXPORT jint JNICALL Java_vizdoom_DoomGame_getTicrate (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); return game->getTicrate(); }
/* * Class: DoomGame * Method: getMode * Signature: ()Lvizdoom/Mode; */ JNIEXPORT jint JNICALL Java_vizdoom_DoomGame_getMod (JNIEnv *env, jobject obj){ DoomGame* game = GetObject(env,obj); Mode mode=game->getMode(); return (jint) mode; }