If you want to randomize your map or make a stone sledge (like I wanted in my mod
) this function could come in useful. It places a specified amount of environment objects in an area. If it is not possible to place all the wanted environment objects in the area because of lack of space or obstacles like buildings, units or other environment objects, the function will only generate the possible amount of environment objects (not causing the program to freeze).
Export Function PlaceEnvironmentArea(environment,numbers_list,amount,area);
var area_list, area_list_x, area_list_y, a, rand_num, rand_hex_x, rand_hex_y;
area_list = AreaToList(area,0);
if area_list = 0 or amount = 0 then exit;
area_list_x = area_list;
area_list_y = area_list;
a = 0;
a = a + 1;
if not ValidHex(area_list_x[a], area_list_y[a]) or HexInfo(area_list_x[a], area_list_y[a]) <> 0 then
area_list_x = Delete(area_list_x, a);
area_list_y = Delete(area_list_y, a);
a = a - 1;
until a = area_list_x;
//hvis der skal placeres flere environments end der er plads til nedsættes antallet der skal placeres
if amount > area_list_x then amount = area_list_x;
rand_num = Rand(1,area_list_x);
rand_hex_x = area_list_x[rand_num];
rand_hex_y = area_list_y[rand_num];
if numbers_list = 0 then
if IsEnvironment(rand_hex_x,rand_hex_y) then amount = amount - 1;
area_list_x = Delete(area_list_x, rand_num);
area_list_y = Delete(area_list_y, rand_num);
until amount = 0;
"environment" must be an integer telling from which environment cathegory the objects should be chosen.
"numbers_list" must be a list of numbers (the numbers of the objects in the specified environment cathegory). The function will (randomly) place these objects only. If "numbers_list" if set to 0/false, the function will pick random objects from the whole environment cathegory.
"amount" is of course the amount if objects you want to place.
"area" - hmm... I wonder...
Note: The program may lagg a bit if the function must place > 50 objects.
EDIT1: Function improved so it's now about 60% faster than b4 (!)
EDIT2: Fixed function so it now places the right amount of objects if there isn't any units in the area
EDIT3: Minor function speed improvement