FfH2: Source Code

I wee'd too early. I'm really spoiled by high level languages. Something as simple as putting a message that says "I'm now considering Agriculture" into my log turned out to be too much for me.

So, this is what I want to do: in the beginning of the loop of bestTech, I want to log what tech is currently being considered. This is where the trouble started. There is no getter for getting the tech's name. Instead of writing that, I decided to go with the tech's quote, which should make it perfectly obvious what tech the AI is looking at as well. So merrily I went about logging the current tech's getQuote... when I ran into a problem of converting from one char type to another.

ONE CHAR TYPE TO ANOTHER? What the hell is wrong with C++!
There's a lot wrong with C++. Frankly, the primary positive thing about the language is its templates allow far more abstract programming than any other major computer language. Unfortunately, that feature is typically underused (as it is here), and weaker portions are used instead.


Anyhoo, I googled, and I've read things for hours, and my brain hurts and I can't figure this out, so I thought I'd ask for help again (maybe someone other than Kael? I would hate to take away time from Fire's completion).

This is my error:

CvPlayerAI.cpp:2072: error C2664: 'CvDLLUtilityIFaceBase::logMsg' : cannot convert parameter 2 from 'const wchar *' to 'const TCHAR *'
:: === Build finished: 1 errors, 0 warnings ===

Now I've googled, and I've found out that TCHAR is MS's magical way of saying "char if it's not a unicode environment, wchar_t if it is". (Right so far? :/) I'm really not sure what logMsg does expect. A simple ascii string would make most sense, of course. I've tried a LOT of things until I've at least found one way to get it to compile:

Code:
char temp;
temp = CHAR(GC.getTechInfo((TechTypes)iI).getQuote());
gDLL->logMsg("mylog.log", &temp);

However, this ends me up with broken characters in my log, so straight CHAR conversion doesn't seem to work.

Can someone tell me off the top of their head what the conversion I'm looking for is? I've wrestled with wcstombs, but it's stronger than me. And probably not what I'm looking for anyway. Help?

Let me go through this in detail (first with what is actually happening):

Code:
char temp;
This code reserves 1 byte (8 bits) for a single character. So this could store 'A', or '^'.

Code:
temp = CHAR(GC.getTechInfo((TechTypes)iI).getQuote());
GC.getTechInfo.getQuote returns a type wchar*, which is a pointer to a wide character. It returns a pointer to the first character in an array of 16-bit characters.
CHAR() is a function that performs conversion into a single (8-bit) character from a wchar (actually, I'm not certain of this; this function is a pain to look up via a web interface, which is what I have here). This conversion looses most (or all) of your information (and should return a warning).

Code:
gDLL->logMsg("mylog.log", &temp);
gDLL->logMsg accepts something of type char *, which is an old version of the generic concept of a string (essentially it's expecting a constant array of characters). It should see the first character at &temp, and then continue to read memory until is reaches a 0. Note that this memory is most likely execution code.

So this code successfully prints out some of the execution code (and one desired byte) as the log string. How do we get what you want? 2 options I can see, I'll spout the easier one. (Note that wcstombs can work, but it's kind of ugly.)

Asuming that the objective is to convert wchar * to char *, I'd probably use wstring.

So the code would look like this:
Code:
wchar wztemp[1024];
temp = GC.getTechInfo((TechTypes)iI).getQuote();
wstring wstemp = wztemp; // I'm not certain how well this conversion works. you may need to use this instead: wstring wstemp = (wchar *)wztemp;
gDLL->logMsg("mylog.log", wstemp.c_str());

Or you could remove the code in the middle; part of the conversion is already written:

Code:
wstring wstemp;
wstemp = GC.getTechInfo((TechTypes)iI).pyGetQuote();
gDLL->logMsg("mylog.log", wstemp.c_str());

Note that in either of these cases, is #include <string> is not before the given code, you'll need it. It appears that this is included everywhere here, so I don't think that this is an issue.


The final thing to note is that you can get the name; it's just called a description. For example, try:

Code:
wstring wstemp;
wstemp = GC.getTechInfo((TechTypes)iI).pyGetDescription();
gDLL->logMsg("mylog.log", wstemp.c_str());
 
I always use namespace std, so I forgot that this code isn't necessarily in that namespace.

So corrected versions of the last example above:

Code:
using namespace std;
wstring wstemp;
wstemp = GC.getTechInfo((TechTypes)iI).pyGetDescription();
gDLL->logMsg("mylog.log", wstemp.c_str());
or
Code:
std::wstring wstemp;
wstemp = GC.getTechInfo((TechTypes)iI).pyGetDescription();
gDLL->logMsg("mylog.log", wstemp.c_str());
The latter won't cause unpleasant side-effects; the former may.
 
Spoiler :
Hey xanaqui, thanks for the help! But I'm still getting an error on the gDLL line:

Code:
CvPlayerAI.cpp:2077: error C2664: 'CvDLLUtilityIFaceBase::logMsg' : cannot convert parameter 2 from 'const wchar_t *' to 'const TCHAR *'
:: === Build finished: 1 errors, 0 warnings ===

That's the gDLL->logMsg line. What do I have to do to the wstring to get it into an array of tchars?


Solved it. A pox on me for not looking into the classes that the classes I use inherit from. Every get*Info has a getType, which was precisely what I needed, and conveniently enough, getType returns tchar, precisely what gDLL->logMsg wants. The following code works:

Code:
gDLL->logMsg("mylog.log", GC.getTechInfo((TechTypes)iI).getType());

Thanks again!
 
Probably will take a while. Kael likes to wait a week or two before he releases the SDK. I won't be doing any AI work either until Fire starts to become a little more stable.
 
Any chance the Fire SDK will be released soon?

It will be out Friday. As Bringa mentioned I like to give it a week to get issues fixed.
 
Personally I would highly encourge modmakers to use this as a base for their mods. Its easy enough to backout changes you dont like (we always keep the origional lines in and commented out in the source code). Also Talchas's spell system opens up so many possibilities that I really coudln't imagine modding without it.

What kinds of possibilities does it open up?
 
What kinds of possibilities does it open up?

Giving special abilities to units. It could be used for wizards casting spells, snipers damaging units in adjacent tiles, exploding a hidden mine, or causing Bruce Bannor to change into the Hulk and back.
 
Source code up to 0.20g is linked in the first post.
 
I'm just curious...
When you find the source (for the code) do you have to make a decision, the fundamental one about existance? The continuation of the human species or the love of your life?

Cause I'm just thinkin - whoa..
-Qes

Actually, the realy big question at that moment, is whether you are truly the One, or in fact merely another Zero. ;)
 
Uploaded a new version in the first post.
 
Updated to the latest (0.21c) code.
 
Is there new source for 0.21d? If so, when will it be posted?

Sorry, it doesn't change much btu does have a few minor changes. It is linked in the first post.
 
New version linked in the first post.
 
Does someone have the source code for 0.20 with the latest patch? I would really appreciate it.
 
0.22 source code is uploaded and linked in the first post.
 
Back
Top Bottom