The most difficult piece of code I've ever attempted

Craig_Sutter

Deity
Joined
Aug 13, 2002
Messages
2,753
Location
Calgary, Canada
This will be a running thread while I develop the code and seek advice.

I intend for it to be a progress log, but I will be asking questions. However, I will only do so as a last resort... when I am at a dead end or a cross-roads in decision making. I will try not to impost upon the modding community too much and will try to author as much of the code as I can myself, given my limited skills.

The purpose of the code is to randomize a set of attacks by viking civilizations in my scenario. Currently, I have a prearranged set of unit spawns and DoW's on pre-selected target cities and fixed dates. I would like to randomize the spawns, the dates and the targets within a certain set of parameters.

The intent is that a random number will generate attacks upon one of the rectangular areas. The attacker will be selected in a weighted but random number based upon the attacked area using historical attack patterns to adjust likelihood of particular attacker being selected. Then a city will be selected within that area with the target being weighted depending upon if the owner is a city state or major civ, coastal or inland, previously attacked as a target. The attacker will then spawn units near that target with a typical attack task force being spawned. There will be a possibility of a secondary spawn by that same attacker in an adjacent area upon which a target will be selected there as well. DoW's will ensue... provided the target is not in a peace treaty with the attacker in the case of a major civ, or is not an allied city state... in which case, the target selection will have to reboot and go through the selection process again.

The code I have developed thus far is contained in a file named "GrandArmy.lua". It saves founded city plots and assigns coastal, adjacent to coastal designations within 5 tables based upon the city location on a map (each area is rectangle encompassing a geographical area upon which viking attacks will take place.

Here is the initial code. I will post modified versions as I go on to the final version. I had a lot of help from LeeS if I recall correctly. Forgive me if I am mistaken in my attribution... I am old and memory fades :)

Code:
--saves city list when cities are founded.  Keeps information on city plot, coastal or inland acjacent to a coast tile
		
tNorthscotland = {}
tUmbria = {}
tAnglia = {}
tSouthengland= {}
tIrishsea = {}
tSouthireland = {}

function citylist(iPlayer, iX, iY)
	local plot = Map.GetPlot(iX, iY);
	local iplot = plot:GetIndex()
	local sCitySortResult = citysort(iX, iY)

	if sCitySortResult ~= "inland" then	
		if iX>=20 and iX<=83 and iY>=64 and iY<=76 then
			tNorthscotland[iplot] = sCitySortResult
		end

		if iX>=47 and iX<=54 and iY>=35 and iY<=48 then
			tUmbria[iplot] = sCitySortResult
		end

		if iX>=45 and iX<=50 and iY>=20 and iY<=34 then
			tAnglia[iplot] = sCitySortResult
		end

		if iX>=27 and iX<=44 and iY>=16 and iY<=28 then
			tSouthengland[iplot] = sCitySortResult
		end

		if iX>=33 and iX<=46 and iY>=47 and iY<=63 then
			tIrishsea[iplot] = sCitySortResult
		end

		if iX>=10 and iX<=32 and iY>=48 and iY<= 62 then
			tSouthireland[iplot] = sCitySortResult
		end
	end
end
GameEvents.PlayerCityFounded.Add(citylist)

directions = {DirectionTypes.DIRECTION_NORTHEAST, DirectionTypes.DIRECTION_EAST, DirectionTypes.DIRECTION_SOUTHEAST,
              DirectionTypes.DIRECTION_SOUTHWEST, DirectionTypes.DIRECTION_WEST, DirectionTypes.DIRECTION_NORTHWEST}


function citysort(iX, iY)
	local CityPlot = Map.GetPlot(iX, iY);
	if CityPlot:IsCoastalLand() then
		-- If the city is on a coastal tile
		return "coastal"
	else
		-- Or, if the city is adjacent to a coastal tile
		for loop, direction in ipairs(directions) do
			local pPlot = Map.PlotDirection(pCityPlot:GetX(), pCityPlot:GetY(), direction)

			if (pPlot ~= nil and pPlot:IsCoastalLand()) then
				return "adjcoast"
			end
		end
	end
	return "inland"
end

I will post questions in further posts as they come up.
 
My first question is about persistent data. I need to save the above and other information over saved games. I am led to believe TableSaverLoader is one way to do so...

Is this the best method?

I am having difficulty figuring out exactly how I use the .lua file in question. How do I save and recall data? Can someone give me a very simplified example of doing so? Perhaps using the tables I created in my previous file? I assume this is testable by a simple recall of the tables creates and "GetName (XXX)" function of some sort.

Added:

I think the original code needs this added... replacing the original variables.

Code:
gT = {}
tNorthscotland = {}
tUmbria = {}
tAnglia = {}
tSouthengland= {}
tIrishsea = {}
tSouthireland = {}

gT = {	tNorthscotland = tNorthscotland,
tUmbria = tUmbria,
tAnglia = tAnglia,
tSouthengland = tSouthengland,
tIrishsea = tIrishsea,
tSouthireland = tSouthireland }
 
quick question that has little to do with the bulk of the code but I noticed this line here:

local iplot = plot:GetIndex()

I've attempted to access the `GetIndex()` function of the `Plot` object but the program keeps yelling `nil value` at me.

Is `GetIndex` your own method, or does it already exist?
 
It's fine, thanks. Plots have a :GetX( ) and :GetY( ) method so combining those with the Map.GetGridSize( ) and some math can extrapolate the plot index.
 
Is `GetIndex` your own method, or does it already exist?
It seems that Plot:GetIndex() is a method added by VMC/CP DLL.

There is however
Code:
Plot:GetPlotIndex()
in the base game though (which I expect to do the exact same thing)
 
Top Bottom