Author Topic: Random Land Rush  (Read 1818 times)

Offline JFB

  • Newbie
  • *
  • Posts: 1
Random Land Rush
« on: August 09, 2006, 02:11:13 PM »
Hello everyone,

I'm trying to play around with a completely random Land Rush Setup.  I have this so far.

Quote
#include <basebuilder.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

struct VehicleInfo vehicle0[] =
{
    {5, 4, mapCargoTruck, mapNone, 3},
    {-3, 2, mapCargoTruck, mapNone, 5},
    {1, 6, mapCargoTruck, mapNone, 2},
    {2, -5, mapCargoTruck, mapNone, 1},
    {-1, -2, mapCargoTruck, mapNone, 3},
    {7, -3, mapConVec, mapAgridome, 4},
    {-4, 1, mapConVec, mapCommandCenter, 6},
    {3, -3, mapConVec, mapCommonOreSmelter, 7},
    {4, 5, mapConVec, mapStructureFactory, 1},
    {-6, 3, mapConVec, mapTokamak, 4}
};

struct BaseInfo base[] =
{
    {0, 0, 0, 0, 0, 0, 0, 0, 10, vehicle0}
};

int InitProc()
{
    srand(time(NULL)*100000);

    CreateBase(0, rand()%512, rand()%216+20, base[0]);

    return 1; // return 1 if OK; 0 on failure
}

Other code is just standard stuff that is irrelevant.  By using srand, I have managed to create mining beacons that randomly place across the map at different locations each time the map is loaded.  My problem is twofold.  I cannot seem to figure out how to center the view on the vehicles when they appear (probably somewhere on the forum, but I couldn't find it).  I also cannot modify the cargo in the trucks to fill them with common metals.

Using

Quote
{5, 4, mapCargoTruck, mapCommonMetalsCargo, 3},

gives a truck with a whopping 6 metals in it.  (As an aside, mapCommonMetalsCargo originally pointed to Rare Metals.  I had to change it in MapIdEnum.h)

Basebuilder.h doesn't seem to assign a class to the vehicles, so I can't use SetTruckCargo to change the cargo in the trucks.

I'm sorry if my searching for two days didn't find an already posted answer to this problem, but if anyone has an idea (or solution to my newb problem), please help.

Thank you.
« Last Edit: August 09, 2006, 02:12:10 PM by JFB »

Offline Mcshay

  • Administrator
  • Sr. Member
  • *****
  • Posts: 404
Random Land Rush
« Reply #1 on: August 09, 2006, 02:34:41 PM »
Hmm, this is programed a bit differently than when i do my landrush maps. I usualy make all the base handling code by hand, since BaseBuilder.h can be not good.

Anyway, you can center the view using:

Code: [Select]
Unit CV;
PlayerVehicleEnum CVfinder(0);
CVfinder.GetNext(CV);
Player[0].CenterViewOn(CV.Location().x, CV.Location().y);

And fill a cargo truck by creating all empty trucks and using:

Code: [Select]
Truck_Cargo Cargoes[5] = {truckFood, truckCommonOre, truckCommonOre, truckCommonOre, truckCommonOre};
Unit CT;
PlayerVehicleEnum CTfinder(0);
for(int i = 0; CTfinder.GetNext(CT) == 1; i++) //Or i < 5 as the condition    CT.SetTruckCargo(Cargoes[i], 1000);

Also, you'll need to have code to check for impassible terrain where you create your units. Or you could cheat and make an array of possible base locations and randomly pick one of those.

Note: the above two code sections only work for player 1 (Player[0]) just stick it in a loop to fix that.

There is also the function:

Code: [Select]
TethysGame::GetRand(int Range)//returns rand int between 0 and Range - 1

To get a random number from op2.
« Last Edit: August 09, 2006, 02:37:00 PM by Mcshay »

Offline BlackBox

  • Administrator
  • Hero Member
  • *****
  • Posts: 3093
Random Land Rush
« Reply #2 on: August 09, 2006, 02:51:26 PM »
Yeah, the cargo truck is an oddity, you have to use one of the Truck_Cargo types to fill it. Type cast the value. (Also undo your edit in MapIdEnum.h, this will cause problems otherwise).

Never, ever use srand() and rand() in a mission. Use TethysGame::GetRand() instead. (No need to seed it -- that is done automatically for you).

The game uses a different random number generator. The reason you should be using GetRand is because the seed is transmitted through the network (that way all players start with the same seed, and random numbers are generated correctly in all sessions -- for example, if you use the random numbers to place mines, the mines will end up in the same (correct) places if you use TethysGame::GetRand(). It will still be random, but the result will be the same on all the computers.

If you have any other questions, feel free to ask!

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4955
Random Land Rush
« Reply #3 on: August 23, 2006, 06:40:32 PM »
Yeah, my first thought before even reading your question was to use TethysGame::GetRand().

Also, when setting the truck cargo, there is also the amount of cargo that needs to be set. With your current setup, there is no way to specify how much of that cargo the truck holds. You can try using the Unit::SetTruckCargo function which will set both the cargo type and amount for you. The only problem is, there is no easy way to use this function without changing around how you create your units in the first place. You might want to look at abandoning the whole BaseBuilder.h setup, and just use direct calls to TethysGame::CreateUnit yourself. Just look in BaseBuilder.h if you want to see how those functions were called from there, or figure them out yourself from the comments in the SDK or other posts on here somewhere.

If you aren't creating too many units at once, it should be fairly easy to make the calls directly. (Or just copy and paste a lot for more units). It's easiest to set the cargo directly after creating the unit.
 

Offline Chandler

  • Full Member
  • ***
  • Posts: 138
Random Land Rush
« Reply #4 on: August 23, 2006, 09:33:42 PM »
Or just use a for loop perhaps?

So for this CreateUnit(type, cargo)

for (i = 0; i++; i < 5) {
    CreateUnit(type(i), cargo(i))
}

where type and cargo are as follows:
index    type    cargo
0         convec   CC
1         convec   Agri
2         convec   SF
etc
 
Chandler

Offline BlackBox

  • Administrator
  • Hero Member
  • *****
  • Posts: 3093
Random Land Rush
« Reply #5 on: August 24, 2006, 07:58:00 AM »
Well, that code isn't entirely correct. However, you still need to address things like setting cargo truck cargo.

You can't just put the truck cargo as the param to TethysGame::CreateUnit since it doesn't let you set the amount.

Like Hooman said, your best bet is to do away with the BaseBuilder code and just create your units manually, setting the truck cargo to mapNone when you call TethysGame::CreateUnit and then call TethysGame::SetTruckCargo which takes both a cargo as well as the amount.

Oh, one last consideration. When you call SetTruckCargo you have to pass one of the values in the Truck_Cargo enum rather than a map_id. (See enums.h for the values in Truck_Cargo). Cast the value to a map_id. (If you just pass one of the map_id values that 'seems' to be correct you'll end up getting something totally unexpected in your truck, but if you use the Truck_Cargo values you'll get the cargo as expected).