Skip to content

Commit

Permalink
Redirect debug logs to retroarch log system + properly support player…
Browse files Browse the repository at this point in the history
… override
  • Loading branch information
MatPoliquin committed Jul 9, 2024
1 parent 6953b2a commit a9eb210
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 17 deletions.
66 changes: 57 additions & 9 deletions ai/game_ai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@
#include <bitset>
#include <iostream>
#include <string>
#include <stdarg.h>

#ifdef _WIN32
#include <windows.h>
#else
#include <dlfcn.h>
#endif

#define GAME_AI_MAX_PLAYERS 2

#include "../../stable-retro-scripts/ef_lib/GameAI.h"


static creategameai_t CreateGameAI = nullptr;
static GameAI * ga = nullptr;
static volatile void * g_ram_ptr = nullptr;
static volatile int g_ram_size = 0;
static volatile signed short int g_buttons_bits = 0;
static volatile signed short int g_buttons_bits[GAME_AI_MAX_PLAYERS] = {0};
static volatile int g_frameCount = 0;
static volatile char game_ai_lib_path[1024];
static std::string g_game_name;
static retro_log_printf_t g_log = nullptr;

extern "C" signed short int game_ai_input(unsigned int port, unsigned int device, unsigned int idx, unsigned int id, signed short int result)
{
if(ga == nullptr)
return 0;

return g_buttons_bits;
if(port < GAME_AI_MAX_PLAYERS)
return g_buttons_bits[port];

return 0;
}

extern "C" void game_ai_init()
Expand Down Expand Up @@ -68,16 +75,43 @@ extern "C" void game_ai_init()
}
}

extern "C" void game_ai_load(const char * name, void * ram_ptr, int ram_size)
extern "C" void game_ai_load(const char * name, void * ram_ptr, int ram_size, retro_log_printf_t log)
{
g_game_name = "NHL941on1-Genesis";

g_ram_ptr = ram_ptr;
g_ram_size = ram_size;

g_log = log;
}

extern "C" void game_ai_debug_log(int level, const char *fmt, ...)
{
va_list vp;
va_start(vp, fmt);

if(g_log)
{
g_log((enum retro_log_level)level, fmt, vp);
}

va_end(vp);
}

extern "C" void game_ai_think()
void array_to_bits_16(volatile signed short & result, const bool b[16])
{
for(int bit=0; bit<=15; bit++){
result |= b[bit] ? (1 << bit) : 0;
}
}

extern "C" void game_ai_think(bool override_p1, bool override_p2, bool show_debug)
{
if(ga)
{
ga->SetShowDebug(show_debug);
}

if(ga == nullptr && g_ram_ptr != nullptr)
{
ga = CreateGameAI(g_game_name.c_str());
Expand All @@ -88,20 +122,34 @@ extern "C" void game_ai_think()
data_path += g_game_name;

ga->Init(data_path.c_str(), (void *) g_ram_ptr, g_ram_size);

ga->SetDebugLog(game_ai_debug_log);
}

if (g_frameCount >= 3)
{
if(ga)
{
bool b[16] = {0};
ga->Think(b);

g_buttons_bits=0;
g_buttons_bits[0]=0;
g_buttons_bits[1]=0;

if (override_p1)
{
ga->Think(b, 0);
array_to_bits_16(g_buttons_bits[0], b);
}

if (override_p2)
{
ga->Think(b, 1);
array_to_bits_16(g_buttons_bits[1], b);
}

for(int bit=0; bit<=15; bit++){
g_buttons_bits |= b[bit] ? (1 << bit) : 0;
}
/*for(int bit=0; bit<=15; bit++){
g_buttons_bits[1] |= b[bit] ? (1 << bit) : 0;
}*/
}

g_frameCount=0;
Expand Down
5 changes: 3 additions & 2 deletions ai/game_ai.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
#define EXTERNC
#endif

#include <libretro.h>

EXTERNC signed short int game_ai_input(unsigned int port, unsigned int device, unsigned int idx, unsigned int id, signed short int result);
EXTERNC void game_ai_init();
EXTERNC void game_ai_load(const char * name, void * ram_ptr, int ram_size);
EXTERNC void game_ai_think();
EXTERNC void game_ai_load(const char * name, void * ram_ptr, int ram_size, retro_log_printf_t log);
EXTERNC void game_ai_think(bool override_p1, bool override_p2, bool show_debug);
9 changes: 6 additions & 3 deletions input/input_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -6519,10 +6519,13 @@ int16_t input_driver_state_wrapper(unsigned port, unsigned device,
#endif

#ifdef HAVE_GAME_AI
if(settings->bools.game_ai_override_p2)
if(settings->bools.game_ai_override_p1 && port == 0)
{
if(port == 1)
result |= game_ai_input(port, device, idx, id, result);
result |= game_ai_input(port, device, idx, id, result);
}
if(settings->bools.game_ai_override_p2 && port == 1)
{
result |= game_ai_input(port, device, idx, id, result);
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion intl/msg_hash_us.h
Original file line number Diff line number Diff line change
Expand Up @@ -16407,7 +16407,7 @@ MSG_HASH(

MSG_HASH(
MENU_ENUM_LABEL_VALUE_GAME_AI_SHOW_DEBUG,
"Override p2"
"Show Debug"
)
MSG_HASH(
MENU_ENUM_SUBLABEL_GAME_AI_SHOW_DEBUG,
Expand Down
5 changes: 3 additions & 2 deletions runloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -7684,7 +7684,7 @@ bool core_load_game(retro_ctx_load_content_info_t *load_info)

#ifdef HAVE_GAME_AI
// load models
game_ai_load("test", runloop_st->current_core.retro_get_memory_data(RETRO_MEMORY_SYSTEM_RAM), runloop_st->current_core.retro_get_memory_size(RETRO_MEMORY_SYSTEM_RAM));
game_ai_load("test", runloop_st->current_core.retro_get_memory_data(RETRO_MEMORY_SYSTEM_RAM), runloop_st->current_core.retro_get_memory_size(RETRO_MEMORY_SYSTEM_RAM), libretro_log_cb);
#endif
return true;
}
Expand Down Expand Up @@ -7827,7 +7827,8 @@ void core_run(void)
current_core->retro_run();

#ifdef HAVE_GAME_AI
game_ai_think();
settings_t *settings = config_get_ptr();
game_ai_think(settings->bools.game_ai_override_p1, settings->bools.game_ai_override_p2, settings->bools.game_ai_show_debug);
#endif

if ( late_polling
Expand Down

0 comments on commit a9eb210

Please sign in to comment.