Author Topic: Outpost 2 Coding 101: Week 2  (Read 12773 times)

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« on: January 28, 2010, 09:46:01 AM »
Week 2
Game Options - Morale Makes The World Go 'Round
Before we try making more units, let's make those game options do stuff, okay?

Morale
There are a few ways to change morale settings.
Code: [Select]
TethysGame::FreeMoraleLevel(player);	// Set morale to unsteady
TethysGame::ForceMoraleGreat(player); // Set morale to Excellent (99)
TethysGame::ForceMoraleGood(player); // Set morale to Good (75)
TethysGame::ForceMoraleOK(player); // Set morale to Fair (50)
TethysGame::ForceMoralePoor(player); // Set morale to Poor (25)
TethysGame::ForceMoraleRotten(player); // Set morale to Terrible (0)

Now, let's change morale settings based on whether or not the Morale Steady option was enabled
We'll start by freeing morale (in case MS is off), then check if the option was enabled, and force morale good if it was.
Code: [Select]
TethysGame::FreeMoraleLevel(-1);  // -1 = all players
if (TethysGame::UsesMorale() == 0)  // Check if Morale Steady is enabled
{
TethysGame::ForceMoraleGood(-1); // Set morale to 75 for all players if so
}

That wasn't too bad, was it?  Two things to keep in mind:
1) If you want to set morale for individual players, rather than all of them at once, you need to do it twice.
2) You CANNOT change the morale level after the game starts or OP2 will assume you're cheating and tell you so.
We're done with morale.

Day/Night
Daylight settings are pretty much the same in that we check if a setting is enabled and act accordingly.
Here's what you'll need for adjusting daylight:
Code: [Select]
TethysGame::SetDaylightEverywhere(0 or 1);	// Enables/disables permaday.  0 is off.  1 is on.
TethysGame::SetDaylightMoves(0 or 1); // Enables/disables movement of the "band" of daylight.  0 = off; 1 = on.
TethysGame::UsesDayNight(); // Use this to check if Day/Night is enabled
GameMap::SetInitialLightLevel(value); // Use this to set the starting position of the daylight "band"

So, how do we use this?  Pretty much the same way we use morale.
Code: [Select]
TethysGame::SetDaylightEverywhere(0);  // Assume day/night is on; disable permanoon
TethysGame::SetDaylightMoves(1);  // Enable movement of the band of light
if (TethysGame::UsesDayNight() == 0)  // Now check if Day/Night is enabled
{
TethysGame::SetDaylightEverywhere(1); // Enable permanoon if D/N is off
}

A really neat thing I like to do is randomly determine where the daylight strip starts everytime the mission is played.
Figure out the length of the map (if you're using La Corrida, it's 128) and do this:
Code: [Select]
GameMap::SetInitialLightLevel( TethysGame::GetRand(128) );	// Set random light level

Note: TethysGame::GetRand(value) returns a random number between 0 and (value - 1).

You would insert this line of code before that if statement above.

Initial Vehicles
We will not be covering Initial Vehicles at this time as we'll need to use a few things the tutorial hasn't covered yet.  Be patient.

Disasters
We will discuss disasters when we talk about triggers.  Again, be patient.

Keep in mind that you can be creative with the usage of these settings.  For example, I once used the "Morale Steady" option to enable the production of combat units in a special mission!

Okay, now onto base design.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #1 on: January 28, 2010, 09:49:19 AM »
Building a Full Base: All Your Base Are Belong To Us

Okay, so you've got a couple of structures and some vehicles.  That's really not enough for a base though...

Before we really get into it, here's some odds and ends you may want.
Code: [Select]
Player[#].GoEden();  	// Forces a player to play as Eden
Player[#].GoPlymouth();   // Forces a player to play as Plymouth
Player[#].SetColorNumber(0 - 5); // Forces a player to play using the specified color
0 = blue
1 = red
2 = green
3 = yellow
4 = cyan
5 = magenta
6 = black (unused)
These should probably only be used in single player games or for multiplayer AIs since humans
generally pick their colony and color for a reason.


We are now going to cover:
-Creating Tubes and Walls
-Creating Mines
-Setting up Initial Resources
-Resource Settings

Let's start nice and easy.  Tubes and walls are pretty simple.
Code: [Select]
TethysGame::CreateWallOrTube(X, Y, ?, type)
X/Y are the x and y position of the wall/tube (obviously).  Don't forget the offsets!
? is unknown, to my knowledge.  Leave it at -1.
Type is the type of wall/tube you're making.
Code: [Select]
mapTube  // Tube
mapWall  // Concrete Wall
mapLavaWall // Plymouth Lava Wall
mapMicrobeWall // Eden Microbe Wall

Of course, you don't want to have to do these things one at a time, right?  Luckily, we can create lines at a time:
Code: [Select]
CreateTubeOrWallLine(xStart, yStart, xEnd, yEnd, type);
Again, this should be pretty self-explanitory.  On to mines.

Mining beacons.  The bane of my existence.  The problem isn't that they're hard.  The problem is there are so many of the darned things!  Finding good locations and placing them takes forever!

Anyways, here's the code:
Code: [Select]
TethysGame::CreateBeacon(Beacon Type, x, y, Ore Type, Bar Level, Variant);
You may be confused by the presence of a "Beacon Type" and an "Ore Type" setting.  Don't worry, you won't be for long.
Beacon Type: Is this a mining beacon, a magma vent, or a fumarole?  Valid options are mapMiningBeacon, mapMagmaVent, and mapFumarole.
Ore Type: Sets whether the mine is common or rare ore.  0 = common, 1 = rare. -1 = random.
Bar level: Sets mine's yield.  0 = 3 bar, 1 = 2 bar, 2 = 1 bar. -1 = random.
Variant: Not all mines are the same.  There are 3 variants of each type of mine.  I usually just set this to random though, so each game is a bit different. 0 = low-yield variant, 1 = regular-yield variant, 2 = high-yield variant.  -1 = random

IMPORTANT NOTE: For magma vents and fumaroles set ore type, bar level, and variant to -1.
For aesthetic reasons, Magma Vents should only be placed on dark gray terrain.  Convention dictates that fumaroles aren't found on sandy terrain, but this isn't a rule.

Here are some examples:
Code: [Select]
TethysGame::CreateBeacon(mapMiningBeacon, 93+31, 12-1, 0, 0, -1);
TethysGame::CreateBeacon(mapMiningBeacon, 14+31, 119-1, 1, 2, -1);
TethysGame::CreateBeacon(mapFumarole, 64+31, 4-1, -1, -1, -1);

Okay, it's time to put it all to work!  Go make a full base!  I want it to have at least:
1 Command Center
1 Structure Factory
1 Agridome
1 Tokamak
1 Common Ore Smelter
1 Mining Beacon (Common Ore)
1 Common Ore Mine (place directly above the mining beacon.  Beacon must be placed first!)
1 Robo-Miner
1 Robo-Surveyor
1 Earthworker
1 Robo-Dozer
3 Cargo Trucks
3 ConVecs
1 Guard Post
Some concrete walls

Make sure everything that needs to be is connected to the CC via tubes.  Come back when you're done.

Okay, we've got ourselves a base, but it still needs something.  People, food, ore, and technology, maybe?
Code: [Select]
Player[#].SetKids(amount);  // Sets number of children
Player[#].SetWorkers(amount);  // Sets number of workers
Player[#].SetScientists(amount); // Sets number of scientists
Player[#].SetFoodStored(amount); // Sets amount of food stored
Player[#].SetOre(amount);  // Sets amount of common metal stored (must have storage capacity)
Player[#].SetRareOre(amount);  // Sets amount of rare metal stored (must have storage capacity)
Player[#].MarkResearchComplete(techID); // Marks specified technology as completed

Most of that is pretty self-explanitory, though you may be wondering how you can find out what a tech's tech ID is.  Open up the techtree you're using in a text editor (notepad, for example).  It'll be the 5 digit number next to the tech's name.  Make sure not include any leading zeroes, however (example: If a tech's ID is 01234, you would write MarkResearchComplete(1234) )!

What about resource settings or difficulty level?  Just do this:
Code: [Select]
    short i;
    for (i = 0; i < TethysGame::NoPlayers(); i++)
    {
        switch (Player[i].Difficulty() )
        {
            case 0:
                Player[i].SetKids(22);
                Player[i].SetWorkers(30);
                Player[i].SetScientists(19);
                Player[i].SetOre(4500);
                Player[i].SetFoodStored(1900);
                break;

            case 1:
                Player[i].SetKids(19);
                Player[i].SetWorkers(26);
                Player[i].SetScientists(16);
                Player[i].SetOre(3000);
                Player[i].SetFoodStored(1500);
                break;

            case 2:
                Player[i].SetKids(17);
                Player[i].SetWorkers(22);
                Player[i].SetScientists(14);
                Player[i].SetOre(3000);
                Player[i].SetFoodStored(1200);
                break;
        }
}
That will iterate through all players and set their starting resources based on their Resource Setting.  These are just some recommended settings.  Feel free to modify the values as you see fit.

Looking good!  Again, feel free to ask question and post your results.  If anyone notices any errors, feel free to point them out!

Until next time, good luck!

Next week: Basic Triggers/Intro to Disasters
« Last Edit: January 28, 2010, 10:40:56 AM by Sirbomber »
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #2 on: January 28, 2010, 10:19:04 AM »
Bonus Lesson: An OP2 DLL By Any Other Name...

I forgot to mention something in the Week 1 Tutorial, so I'm posting it here.  The filename of your DLL is very important, as it tells OP2 where your map should go in the gametype sorting list.

Note: All filenames must be no longer than seven letters or bad things will happen!

Here's how filename sorting works:
eXX.dll = Eden Campaign Mission #XX
pXX.dll = Plymouth Campaign Mission #XX
tXX.dll = Tutorial #XX
c(your name here).dll = Colony Game
ademoX.dll = Auto-Demo #X
mlX(YNH).dll = Multiplayer, Last One Standing, X Players
mfX(YNH).dll = Multiplayer, Space Race, X Players
mmX(YNH).dll = Multiplayer, Midas, X Players
mrX(YNH).dll = Multiplayer, Resource Race, X Players
muX(YNH).dll = Multiplayer, Land Rush, X Players
In case you didn't figure it out, (YNH) = (Your Name Here).

Here are some naming conventions and examples:
Colony Games: c(Colony)(Type/Goal).dll
Examples: CEP.dll = Eden Population colony game, CPS2.dll = Plymouth Starship 2 colony game
Multiplayer Games: m(type)(players)_(up to 3 more characters here)
Examples: ml4_05.dll = 4P LoS, mf4_ccf.dll = 4P Space Race, mu6_12.dll = 6P Land Rush
« Last Edit: January 29, 2010, 05:26:49 PM by Sirbomber »
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline AmIMeYet

  • Full Member
  • ***
  • Posts: 128
Outpost 2 Coding 101: Week 2
« Reply #3 on: January 28, 2010, 10:38:46 AM »
Bar level != variant ?
Then what's the difference? Aren't both the yield ( = max extractable ore) of the mine?

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #4 on: January 28, 2010, 10:48:08 AM »
Oops!  I mixed up the variants.  0 = Low Variant, 1 = Normal Variant, 2 = High Variant.

Anyways, lemme go into some more detail here I guess.  Mines are divided into 3 main types: 1 bar (low yield), 2 bar (medium yield), and 3 bar (high yield).  But those are then further divided into 3 sub-types each.

Check out mines.txt in sheets.vol or this post for more info.  
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline AmIMeYet

  • Full Member
  • ***
  • Posts: 128
Outpost 2 Coding 101: Week 2
« Reply #5 on: January 28, 2010, 11:19:10 AM »
Ah, okay.. thanks

Offline CK9

  • Administrator
  • Hero Member
  • *****
  • Posts: 6226
    • http://www.outpost2.net/~ck9
Outpost 2 Coding 101: Week 2
« Reply #6 on: January 28, 2010, 11:51:38 AM »
Okay, about unit placement:

I know when I did my map-merge project that became a multiplayer map, Mcshay had me send him the map.dat file the mapper generates.  Does the file generate the code lines for placing the units, allowiny you to cut+paste them in?

BTW, I pinned your topics :P
CK9 in outpost
Iamck in runescape (yes, I still play...sometimes...)
srentiln in minecraft (I like legos, and I like computer games...it was only a matter of time...) and youtube...
xdarkinsidex on deviantart

yup, I have too many screen names

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #7 on: January 28, 2010, 12:49:14 PM »
Sorta.  The dat file is loaded by OP2Mapper.  If you want code, use the Mapper's Generate Code feature.  Personally, I don't like using it, but feel free.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline AmIMeYet

  • Full Member
  • ***
  • Posts: 128
Outpost 2 Coding 101: Week 2
« Reply #8 on: January 28, 2010, 12:50:07 PM »
Quote
Does the file generate the code lines for placing the units, allowiny you to cut+paste them in?
AFAIK, the mapper has that function, but the code it outputs is a bit unmanageable & impractical. It *will* work though.
Never tried it myself, but anyway..

Edit: Curse you, posting time!
« Last Edit: January 28, 2010, 12:50:26 PM by AmIMeYet »

Offline CK9

  • Administrator
  • Hero Member
  • *****
  • Posts: 6226
    • http://www.outpost2.net/~ck9
Outpost 2 Coding 101: Week 2
« Reply #9 on: January 28, 2010, 01:43:13 PM »
okay, and the coordinates (for structures) are the center, a corner of the footprint, or a corner of the buldozed area?
« Last Edit: January 28, 2010, 01:43:28 PM by CK9 »
CK9 in outpost
Iamck in runescape (yes, I still play...sometimes...)
srentiln in minecraft (I like legos, and I like computer games...it was only a matter of time...) and youtube...
xdarkinsidex on deviantart

yup, I have too many screen names

Offline Hidiot

  • Hero Member
  • *****
  • Posts: 1018
Outpost 2 Coding 101: Week 2
« Reply #10 on: January 28, 2010, 02:19:18 PM »
They're actually the center of the building, not corner. For a 3x2 building (such as the CC), the center will be considered  (2,1), ignoring the dozed outilne and for a 2x2 building, the center will be (2,2).

It might be that ConVecs use the bottom right dozed tile as reference when using the game's functions (RecordBuilding and DoBuild).

It's been a long time since I've written Outpost 2 code, though, so don't take my words for granted.
"Nothing from nowhere, I'm no one at all"

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #11 on: January 28, 2010, 02:25:20 PM »
A structure's "origin" (center) is the tile under the structure where the tubes would intersect.



For structures without tubes, the origin is the bottom-right corner (I think?).
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Moley

  • Jr. Member
  • **
  • Posts: 95
Outpost 2 Coding 101: Week 2
« Reply #12 on: January 29, 2010, 05:12:56 PM »
in your examples you have CES2 for ply star 2
I HATE SPELLING!!!!!!
if i spell something or screw up grammer,
ignore it or tell me if you dont understand what i typed.

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #13 on: January 29, 2010, 05:27:09 PM »
Fixed.  It now says CPS2.dll.  Thanks.
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Angellus Mortis

  • Full Member
  • ***
  • Posts: 138
Outpost 2 Coding 101: Week 2
« Reply #14 on: February 07, 2010, 08:14:01 AM »
Quote
Code: [Select]
Player[#].GoEden();  	// Forces a player to play as Eden
Player[#].GoPlymouth();   // Forces a player to play as Plymouth
Player[#].SetColorNumber(0 - 5); // Forces a player to play using the specified color
0 = blue
1 = red
2 = green
3 = yellow
4 = cyan
5 = magenta
6 = black (unused)
These should probably only be used in single player games or for multiplayer AIs since humans
generally pick their colony and color for a reason.

This is something that is really easy to do that Hidiot thought of and I use now. A random color for the player each time they start the game for single player games.
Code: [Select]
Player[#].SetColorNumber(TethysGame::GetRand(5));

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #15 on: February 07, 2010, 09:08:34 AM »
That should be
Code: [Select]
Player[#].SetColorNumber(TethysGame::GetRand(6));

Otherwise you'll never get Magenta, because GetRand(x) generates a random number from 0 through (x-1).

Personally, I don't recommend this (what if you have an AI or two and all players end up with the same color?  Good luck figuring out who's who!).
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline Hidiot

  • Hero Member
  • *****
  • Posts: 1018
Outpost 2 Coding 101: Week 2
« Reply #16 on: February 07, 2010, 11:36:03 AM »
I only made single-player maps, and single player doesn't let you choose your color.
When I gave a damn about avoiding the same color, I put in a check against it, not exactly hard.
If I ever used 5 instead of 6, it was probably just an overlooked mistake.

Sirbomber, you helped make the 666 mod, so what's wrong with figuring out who's who?
"Nothing from nowhere, I'm no one at all"

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #17 on: February 07, 2010, 11:50:57 AM »
Because DM666Z is made for your displeasure.  :P  
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials

Offline jcj94

  • Sr. Member
  • ****
  • Posts: 407
    • http://techfusion-279.com
Outpost 2 Coding 101: Week 2
« Reply #18 on: March 20, 2011, 09:55:33 PM »
On the whole "mapper merging code"

Its a pain, BUT that's how I do my maps.  I do run into a lot of problems, but I cutout the problem code and put the good code in... Now that I've done two (personal) maps for my enjoyment.  Basicly you with an invicible CC watching as the AI keeps trying to build as the blight destroys everything BUT your CC and one meteor per building just to see how much damage a meteor does per structure.  

 The mapper like to use
 Player[1].MarkResearchComplete(3303);
for research and something allong the lines of:
if (TethysGame::NoPlayers() > 2)
for using starting bases.

This is great, UNLESS you have an AI, in which case it takes over the other base's units.  It does nothing with them but It does get annoying.

 

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3238
Outpost 2 Coding 101: Week 2
« Reply #19 on: March 20, 2011, 10:20:24 PM »
For the love of God, STOP NECROING POSTS.  This is over a year old...
"As usual, colonist opinion is split between those who think the plague is a good idea, and those who are dying from it." - Outpost Evening Star

Outpost 2 Coding 101 Tutorials