InitAI passes invalid callback

InitAI passes invalid callback

Here is where ideas can be collected for the skirmish AI in development

Moderators: hoijui, Moderators

Post Reply
wishkah
Posts: 3
Joined: 20 Jul 2006, 23:45

InitAI passes invalid callback

Post by wishkah »

Hi,

I'm trying to get my own AI together, and after a lot of research I got Dev-C++ to compile a DLL that TASpring accepts as valid AI DLL. But now the problem is that when the engine calls my InitAI function, it passes an invalid pointer as first parameter (points to 0x1), and of course I can't do anything as a result... the relevant parts of my code are:

Code: Select all

std::set<IGlobalAI*> ais;

extern "C" __declspec(dllexport) int GetGlobalAiVersion()
{
    logf("GetGlobalAiVersion returning %i\n", GLOBAL_AI_INTERFACE_VERSION);
	return GLOBAL_AI_INTERFACE_VERSION;
}

extern "C" __declspec(dllexport) void GetAiName(char* name)
{
    logf("GetAiName returning %s\n", name);
	strcpy(name, AI_NAME);
}

extern "C" __declspec(dllexport) IGlobalAI * GetNewAI()
{
	CAIInterface * ai = new CAIInterface();
	ais.insert(ai);
    logf("GetNewAI returning AI object at (0x%x)\n", ai);
	return ai;
}

extern "C" __declspec(dllexport) void ReleaseAI(IGlobalAI* i)
{
    logf("ReleaseAI deleting AI object at (0x%x)\n", i);
	delete (CAIInterface*) i;
	ais.erase(i);
}

/////////////////////////////////////////////////////////////////////////////

CAIInterface::CAIInterface(){

}

CAIInterface::~CAIInterface(){

}

void CAIInterface::InitAI(IGlobalAICallback * gcb, int team){
    logf("CAIInterface instance at (0x%x)\n", this);
    logf("InitAI with global callback at (0x%x)\n", gcb);
	IAICallback * cb = gcb->GetAICallback();
    logf("InitAI: got ai callback (0x%x)\n", cb);
    good = true;
    
    g = new Globals(gcb, cb);
    ch = new ConflictHandler(g);
    uh = new UnitHandler(g);
}
The file that the logf function logs to contains:

Code: Select all

GetGlobalAiVersion returning 15
GetNewAI returning AI object at (0x4af3238)
CAIInterface instance at (0x835a278)
InitAI with global callback at (0x1)
Then it crashes (or rather the engine complains about an uncaught AI exception). edit: Obviously this is caused when I try to dereference the invalid pointer (cb = gcb->GetAICallback();)

Any help appreciated.

edit2: I've compared most of my stuff with the EmptyAI example code and I couldn't find a difference, yet all other AI's work (NTAI seems to be doing stuff the same way I do as well, actually I copied a lot from there, so I just don't understand why it's not working with my dll). Are there known runtime incompatabilities with GCC/mingw compiled binaries running under win32?
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Strange as your dll itnerface looks nothign like mine.

Infact ti looks more like the prelinux prot code.

Code: Select all

#include "float3.h"
#include "GlobalAI.h"
#include "ExternalAI/aibase.h"

DLL_EXPORT int GetGlobalAiVersion(){
	return GLOBAL_AI_INTERFACE_VERSION;
}
DLL_EXPORT void GetAiName(char* name){
	strcpy(name,AI_NAME);
}
DLL_EXPORT IGlobalAI* GetNewAI(){
	return new CNTai();
}
DLL_EXPORT void ReleaseAI(IGlobalAI* i){
	delete i;
}
That is all you need, renaming CNTai where appropriate.

However i believe part of your problem si Devc++ itself. You need to either recompile spring with devc++ or compile your AI with msvc2003 or above. The latter is preferable as it means that anything you do with devc++ wont work with the MSVC build the spring devs release or any of the other AI devs.

You may want to get the Visual studio 2005 express edition. Microsoft are giving away free copies for download but its a year long offer that will probably expire soon.

btw what exactly is the definition of the logf function/macro? Have you considered using the Log class of NTai + the NLOG and CLOG macros?

Alsoa re you working off of svn NTai or RC18/20 source?

btw have you cosndiered creating the ConflictHandler and UnitHandler classes inside the Globals class, then doing what NTai does and simply calling functions of globals for engine calls, and putting exception handling around the calls? It'd allow you to change the data into a different format say for a messaging system of your own design and distancing yourself form an itnerface you have no control of..[/code]
wishkah
Posts: 3
Joined: 20 Jul 2006, 23:45

Post by wishkah »

AF wrote:Strange as your dll itnerface looks nothign like mine.

Infact ti looks more like the prelinux prot code.

Code: Select all

#include "float3.h"
#include "GlobalAI.h"
#include "ExternalAI/aibase.h"

DLL_EXPORT int GetGlobalAiVersion(){
	return GLOBAL_AI_INTERFACE_VERSION;
}
DLL_EXPORT void GetAiName(char* name){
	strcpy(name,AI_NAME);
}
DLL_EXPORT IGlobalAI* GetNewAI(){
	return new CNTai();
}
DLL_EXPORT void ReleaseAI(IGlobalAI* i){
	delete i;
}
That is all you need, renaming CNTai where appropriate.
The DLL_EXPORT macro didn't work, the resulting DLL didn't export anything. Using the mentioned function type worked, and I never looked back :)...

You may want to get the Visual studio 2005 express edition. Microsoft are giving away free copies for download but its a year long offer that will probably expire soon.
Wow, I wasn't aware that MS is on a generous spree and gives away software that costs a donkey load of money... thanks for the tip, this will make things easier.
btw what exactly is the definition of the logf function/macro? Have you considered using the Log class of NTai + the NLOG and CLOG macros?

Alsoa re you working off of svn NTai or RC18/20 source?

btw have you cosndiered creating the ConflictHandler and UnitHandler classes inside the Globals class, then doing what NTai does and simply calling functions of globals for engine calls, and putting exception handling around the calls? It'd allow you to change the data into a different format say for a messaging system of your own design and distancing yourself form an itnerface you have no control of..[/code]
Valid criticism. To my defense: I was still in the "hacking around" phase, didn't produce anything serious. So far I was just trying to get a feeling for the engine.
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

ok, teh DLL_EXPORT macro should work as long as you have the VS project setup correctly and the fodlers in the usual places.

EmptyAI.vcproj isnt exactly a good project to base anything on either.

look at aibase.h too for the definitions of DLL_EXPORT
Post Reply

Return to “AI”