Unit category sorter class + functions - Page 3

Unit category sorter class + functions

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

Moderators: hoijui, Moderators

User avatar
sp2danny72
Posts: 60
Joined: 09 Jan 2005, 04:52

Post by sp2danny72 »

the line
#include "UnitDef.h"
solves it
this is not a problem with pointers, you just thought so because
you think pointers are hard. The error message says:
use of undefined type 'UnitDef'
and it means just that, that UnitDef hasnt been defined
(just declared) as in
struct UnitDef;
C++ (and C) syntax can be a bit terse and confusing for somone
who is migrating from a more verbose language, and I remember
having the exact same kind of problem, ie not reading the error
message properly and assuming it was something different...
Now code away... :P
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Hahaha oh gosh you are right :oops: ! Ill take your advice better from now on!
Sorry for being so stubborn!
You and Sub rule my (AI) world :lol:


Now to the last step of using classes:

If i want to call a method from the Test Function in a method from the CGlobalAI:

Code: Select all

void CGlobalAI::Update()
{
	int frame=cb->GetCurrentFrame();
	if(!(frame%600))
	{
		char c[200];
		float Score1 = Test::GetEnergyScore("ARMSOLAR");
		sprintf(c,"A solar plant would have a score of: %f",Score1);
		cb->SendTextMsg(c,0);
	}
}
gives me:


Code: Select all

i:\documents and settings\admin\my documents\visual studio 2005\taspring\testglobalai\globalai.cpp(115) : error C2352: 'Test::GetEnergyScore' : illegal call of non-static member function
        i:\documents and settings\admin\my documents\visual studio 2005\taspring\testglobalai\test.h(16) : see declaration of 'Test::GetEnergyScore'
Whats the correct syntax? Should I use a pointer?
Do i have to include any other files? am I a n00b?
submarine
AI Developer
Posts: 834
Joined: 31 Jan 2005, 20:04

Post by submarine »

no i think you should probably look at object oriented programming again:

the class is just some kind of description about how things are. there isn't anything created unless you create an instance of an object.

class Test{..int i; void f() { i = 2;}; ...}

unless you create an instance of Test, there is for example no memory allocated for i

int value = Test::i -> error


Test bla;
int value = bla.i; -> value is set to 2


there is the possibility of declaring functions to be "static" (which would solve your problem as well), but i think you should first read more about classes, objects and instances in a book...
submarine
AI Developer
Posts: 834
Joined: 31 Jan 2005, 20:04

Post by submarine »

oh i forgot:

Code: Select all

char c[200];

Test test; // dont know what your constructor needs..
float Score1 = test.GetEnergyScore("ARMSOLAR");
      
sprintf(c,"A solar plant would have a score of: %f",Score1);
     
 cb->SendTextMsg(c,0); 
would work as well, though i dont recommend it as every time the function is called an instance of Test has to be created on the heap; better declare Test test; as a var of you GlobalAI class
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Right, Ill read up for a few days more, make more sample programs etc...
I have all the basics but I still dont think in the OOP, C++ way!
You guys wont see me much for a few days, and when Im back, hopefully Ill be able to code properly without bugging you guys every 5 mins asking for help in simple stuff!

My AI can wait, Ill be a decent C++ programmer first!

thanks sub
User avatar
sp2danny72
Posts: 60
Joined: 09 Jan 2005, 04:52

Post by sp2danny72 »

submarine wrote:and why is your destructor declared "virtual"?
Well, there is a reason why all destructors should be virtual.
Consider:

Code: Select all

class A {
  public:
    A() {}
    ~A() {}
    virtual void foo() =0;
  protected:
    int a;
};

class B : public A {
  public:
    B() {}
    ~B() {}
    virtual void foo() { a=b=7; }
  private:
    int b;
};

main()
{
  A* p=new B();
  p->foo();
  delete p;
}
This code has a memory leak, of one int.
Also, the destructor of B wouldnt get called,
even if it actually did something.
Adding virtual to both destructors would solve that.
Therefore, it is considered good practice to use
virtual on all destructors, at least on non POD objects.
submarine
AI Developer
Posts: 834
Joined: 31 Jan 2005, 20:04

Post by submarine »

ok i see, completely forgot about that apsect of oop :)

all my aai classes are not derived (is it the right word?) from any base classes; but in case he wants to use that its better to declare constructors virtual..
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

I'd forgotten about that too and all ym classes are derived from base classes, maybe thats where my memory leak is coming from...

Krogothe, I'd prefer ti if you uploaded to fileunvierse instead of having to ask you to email it.
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Ill be taking a break from coding until i have learned enough c++...
I got the reply "read a book" a few times now so ill get the basics working before asking here, its easier that way
User avatar
jcnossen
Former Engine Dev
Posts: 2440
Joined: 05 Jun 2005, 19:13

Post by jcnossen »

Maybe you should just try making a small game or something first, it can be much more satisfying than programming an AI with an undocumented interface ;)
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

If anybody can remember, I was in exactly the same position as he is now, perhaps in an even worse position as I didnt have people looking voer any code I posted and telling me why ti didnt work adn what to do to get it working.

But look at where I am now, I'm sure krogothe can do the same, better even. He just needs determination.
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Hey didnt you know C++ either alantai?? how long ago was that?
Im still busy with the planning of my AI anyways, im trying to work out a way for it to share tasks, but every time i compile the plan (i can do it in my head) i find something wrong, eg when the comm is building something expensive etc...

I also had a massive paradigm shift while taking a break, Ill have to rethink a lot of my plans in order to come out with a challenging AI!

And yeah i wanna code something easier first to get used to it, but i dont find anything easy interesting... I want to make a NERO-style game, an AI and a macro program that has a bit of an AI... none of these are easy to do!
Id make a game, but Im only interested in making RTSs or MMORPGS so yeah ill just do crappy exercises if i get stuck!

On a happy note, Ive started over on the AI, and so far its working! Managed to create a class, structures, have all sorts of pointers working, dynamic arrays, never got stuck once yay!
I still need to figure out a way to build a build tree and move around then save data!

I dont know if you guys saw my basic plans on the babysteps thread. Each of those boxes corresponds to about 5-10 functions! What kind of program could i use to make a MASSIVE spider diagram (at least 10x the area of the one posted)? words too small and awkward, paint and photoshop take too long. Any ideas? EDIT: NEVERMIND, just got word to give me a 2'x2' page, and im writing in size 6 :lol:
daraknor
Posts: 40
Joined: 09 Nov 2005, 09:22

Post by daraknor »

http://dia-installer.sourceforge.net/
Dia is a diagramming tool similar to Visio but it is free. It works with UML and so on.

VS2003 has some UML modeling software, but 2005 I am not sure about. I know some developers at work claimed it removed some autodocumenting features. It is likely they are just moved or hidden :/
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Krogothe, I am sure that one rule I follow would do wonders in your case. From the looks of ti your going to have an AI that will ahev a highly complex structure from the start out.

1 line to do one thing, anymore than 3 or 4 and it's not good code. Thats why TAI 0.1 was such a mess compared to NTAI 0.1
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Yes, the AI will be incredibly complex, since its gonna work like a brain:
each module is only a few lines long (rarely over a dozen), and does a simple processing on the input received. All the information the AI will need to work will be:
1-map
2-mod
3-cheat interface (all units)

No cfg files for each map/mod...


It wont just "build a solar with unit x at Y when Z"

It would just have the task handler say"need energy" to the unit database that will say "with this mod you can build ARMSOLAR, ARMWIN etc" the building scorer which says "solar scored highest, build one" to the job handler which says "build a solar at Y or send another builder to the half-built solar at Z" to the job finder which will find the nearest appropriate unit to do that task. So although each module is independent and can be fed with dummy variables set by me, that takes time. And that is a simplified example. There are about 3-6 more modules in-between to work out allowed locations, check if reinforcing will work etc.
So for now im just writing each function and testing its outputs in-game, then i can start to piece them together.

The good part of this approach is that it will be very easy to spot bugs and fix them, since each module is independent but I wont have a working AI until its pretty much 100% done, unlike most other AIs that start out simple and get improvements built on top of them.

Now i need to come up with a name for my AI!
Id name it NSAI (youll see why when its done) but thats too close to NTAI
I might just naff the acronyms off and call it "Leon" lol
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Actually thats nto whats happening.

What we do is we improove our code replacing outdated bits with enw bits that do similair jobs.

For exampel in NTAI I switched the whole building system onto a system that sues units and tasks objects instead of shoving around strings.

I did this only so that i could then use that to jump ahead to what i had planned with Universal mod support.

Also I planend to ahev modules like you said in TAI, but when ti came to implementing those modules merged into larger chunks which handled all the seperate bits at once.

Your going to fidn that modules are goin to be doing similair things, and that it'd be in your itnerests to merge those modules and become mroe abstract in doing so. It'll help you a lot in the long run and it'll be easier to track bugs.

An example of this is the jackhammer I'm taking to the chaser clas sin NTAI over the next week to solve attack bug once and for all
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

WOOTZ0R!!!
My class is written and working! Its about 80% done, I just need to add jammers and change some constants to improve consistency...
All that pretty much thanks to sub, who let me use his class as a base!
took me 5 hours but I got a crash only once during the whole process, which was spotted and fixed straight away, which is pretty amazing considering i started on C++ last week and the class weights at 1,127 lines (excluding the header file)
Once i write the Construction handler class and the Task Manager, the AI will start working and its construction capabilites will be at very close to final!

Thanks to you all especially sub for getting me this far!
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

hmm I hope we'll be seeing a nice package of source code in the next 24 hours ^_^
User avatar
krogothe
AI Developer
Posts: 1050
Joined: 14 Nov 2005, 17:07

Post by krogothe »

Soon enough yes
Im off to cambridge tomorrow and will only be back on thurs (wed if im lucky).
If you cant wait, just ask sub for his sorter class, its extremely similar to mine (we have different categories, thats all)
User avatar
AF
AI Developer
Posts: 20687
Joined: 14 Sep 2004, 11:32

Post by AF »

Will do
Post Reply

Return to “AI”