City processing

darkpanda

Dark Prince
Joined
Oct 28, 2007
Messages
823
Hi everyone,

Today I reached a major milestone in porting CIV.EXE's routine that I called "cityProcess", which is to be able to render the city screen fully and almost correctly (still show visited huts in red like in JCivED, a minor issue with fog of war on the city squares, also not showing yet "integrated" units stored in city bytes 27 and 28).

For me, this is quite a milestone, as this represents covering 70% of this routine, which at nearly 26,500 bytes is the biggest in CIV.EXE. It was translated into about 2,000 lines of Java code, that I make available for whomever is interested in seeing the processing (and rendering) code in action: http://pastebin.com/3dAKuMTb (WARNING! it's not very clean...)

Eventually, this will be put into JCivED, probably offering city previews, if not a full interactive interface to tweak cities just "as if" you were in CIV itself...

70% is also the step in this routine where all the data processing and rendering is done, and where code for user interaction starts (such as fiddling around with the city specialists, exploited city squares, selling buildings, unfortifying units, etc.)

Anyhow, I also started this thread to share discussions about the various formulae and game rules present in this routine.

And to conclude, a self-satisfaction screenshot for sample preview: Untitled.png
 
Pretty amazing work as always!

I like your approach of translating routines almost byte by byte. This should make it possible to make an exact replica of the game at some point. Perhaps sometime in 2020 when you have mapped 100% of the code? :D

It's really interesting to go through the logic. Like for instance the spawning of diplomats near the player. I didn't think the AI produced diplomats at all. I know they never steal my techs so this must mean the AI spawns the diplomat next to a city of mine, and then deletes the unit when it processes it (as per your previous work on unit processing).

If this is true then you should always try to give your opponents writing as soon as possible so they start wasting their shields on diplomats :)
 
I hope my remake will be done before 2020. I was thinking more along the lines of 2016, to celebrate the 25th anniversary of the game. :)
Really good to have these insights into the game... I am largely dependent on darkpanda's work, the rest I need to figure out by observation which is not always accurate.
 
Pretty amazing work as always!

I like your approach of translating routines almost byte by byte. This should make it possible to make an exact replica of the game at some point. Perhaps sometime in 2020 when you have mapped 100% of the code? :D

Thanks! As a matter of fact, when dealing with so much code and spending so much time on it, there's no other way than keeping a close track to the source assembly... For debugging the Java code, it's a must - I nearly never got any ported code right the first time, always needed to test portions of it and fix the various errors here and there. It's a tedious task.

Also I only work on this project sparsely, hence the long timespans :) And inbetween, I take some time "off" this task to try simplifying the task itself, such as trying to automate Java code generation directly from assembly - sadly we're not there yet...

It's really interesting to go through the logic. Like for instance the spawning of diplomats near the player. I didn't think the AI produced diplomats at all. I know they never steal my techs so this must mean the AI spawns the diplomat next to a city of mine, and then deletes the unit when it processes it (as per your previous work on unit processing).

If this is true then you should always try to give your opponents writing as soon as possible so they start wasting their shields on diplomats

CIV.EXE is full of things like that - as you saw it too in the unit processing thread.

I don't take the time to report on every finding I make, far from it, but it's clear that when you know the internal workings, there is room for gameplay optimization (or "taking advantage of the limited AI" some would say).
In the unit processing, for example, it's possible to trick the AI by maintaining small feebly defended cities nearby their own cities/units, since a lot of unit movement decisions are roughly based on "what does the closest enemy city look like?".
Also, "bigger is better" and AI is naturally bent on willing to conquer bigger continents rather than smaller, provided they are not too far away.

But I am sure I will discover more when delving into AI.
 
I thought I would check in and say, thank you again, darkpanda!

darkpanda said:
Anyhow, I also started this thread to share discussions about the various formulae and game rules present in this routine.

In that spirit I will chime in on the light-bulbs formula. I did not know the exact formula for taking Newton's College into account but now I see it is (effectively) like this:

Base: 1x lightbulbs
With library, without university, without Newton's: 1.5x lightbulbs
With library, without university, with Newton's: 1.83x lightbulbs
With library, with university, without Newton's: 2.0x lightbulbs
With library, with university, with Newton's: 2.66x lightbulbs

Then doubled on top of that if Copernicus is built; I also wasn't sure that the scientists get their contribution taken into account before all the multipliers but it appears they do indeed benefit from the compounding.

It's not terribly surprising but, as I mentioned, I never knew /exactly/.

Thanks again!
 
Also.... Is there any chance we can get a preview of the 'adjustCitizenHappiness' function? ;)

jarvisc
 
I thought I would check in and say, thank you again, darkpanda!



In that spirit I will chime in on the light-bulbs formula. I did not know the exact formula for taking Newton's College into account but now I see it is (effectively) like this:

Base: 1x lightbulbs
With library, without university, without Newton's: 1.5x lightbulbs
With library, without university, with Newton's: 1.83x lightbulbs
With library, with university, without Newton's: 2.0x lightbulbs
With library, with university, with Newton's: 2.66x lightbulbs

Then doubled on top of that if Copernicus is built; I also wasn't sure that the scientists get their contribution taken into account before all the multipliers but it appears they do indeed benefit from the compounding.

It's not terribly surprising but, as I mentioned, I never knew /exactly/.

Thanks again!

What about SETI Program?
 
Also.... Is there any chance we can get a preview of the 'adjustCitizenHappiness' function? ;)

jarvisc

Here it is:

Code:
	//seg007_6CE6:
	public static int adjustCitizenHappiness(City city, int specialistCount) {
		//seg007_6CE6:
		city_happyCitizenCount_dseg_7062 = rangeBound(city_happyCitizenCount_dseg_7062, 0, city.actualSize());
		
		while(true) {
			//seg007_6D3A:
			city_unhappyCitizenCount_dseg_7064 = rangeBound(city_unhappyCitizenCount_dseg_7064, 0, city.actualSize());

			if(rangeBound(city.actualSize()-specialistCount,0,99)>=city_happyCitizenCount_dseg_7062+city_unhappyCitizenCount_dseg_7064) return rangeBound(city.actualSize()-specialistCount,0,99);

			city_happyCitizenCount_dseg_7062--;
			city_happyCitizenCount_dseg_7062 = rangeBound(city_happyCitizenCount_dseg_7062, 0, city.actualSize());
			
			city_unhappyCitizenCount_dseg_7064--;
		}
	}


Also, the "rangeBound(a, b, c)" function is essentially as follows:

Code:
public static int rangeBound(int val, int min, int max) {
        return Math.max(Math.min(val, max), min);
}

Hope this helps...

As a matter of fact, I might as well put this all on pastebin: currently, the code ported from CIV.EXE is spread in 2 classes: CityProcess, which only contains the same-name routine, and CivLogc, which contains all the rest (only what has been rev-eng'd so far), including both routines above.
Here is CivLogic.java: http://pastebin.com/7jCMukuD
 
Top Bottom