Author Topic: Survivor Code  (Read 5410 times)

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« on: March 31, 2010, 11:27:40 AM »
Can anyone give me the survivor code for the destruction of infected buildings? And can you explain how to use it?
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #1 on: March 31, 2010, 11:55:47 AM »
Yesno I will won't.  :P

It comes in two parts.

Code: [Select]

// Replace the BOOL APIENTRY Dllmain section with this:
// -----------------------------------------------------------------------
// Blight Helper Code
// -----------------------------------------------------------------------

unsigned char *setTileLoc = (unsigned char *)0x00476D32;
unsigned char oldSetTile[] =
{
0x25, 0xE0, 0xFF, 0x00, 0x00,   // and eax, 0FFE0h
0xC1, 0xE8, 0x05                // shr eax, 5
};
unsigned char newSetTile[] =
{
    0x90, 0x90, 0x90, 0x90, 0x90,   // nop * 5
    0x90, 0x90, 0x90                // nop * 3
};
struct mapCell
{
    int cellType:5;
    int tileIndex:11;
    int unitID:11;
    int lava:1;
    int lavaPossible:1;
    int expand:1;
    int microbe:1;
    int wallBuilding:1;
};

BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    DWORD oldAttr;
if (fdwReason == DLL_PROCESS_ATTACH)
{
  DisableThreadLibraryCalls(hinstDLL);
  VirtualProtect(setTileLoc, sizeof(newSetTile), PAGE_EXECUTE_READWRITE, &oldAttr);
  memcpy(setTileLoc, newSetTile, sizeof(newSetTile));
}
else if (fdwReason == DLL_PROCESS_DETACH)
{
     memcpy(setTileLoc, oldSetTile, sizeof(newSetTile));
}

    return TRUE;
}

Now, you have two choices for killing infected buildings.  You can either destroy them as they become infected, or gradually damage infected buildings and destroy them when their HP drops below zero.

Code: [Select]
//Kill structures as they become infected
for ( int i = 0; i < TethysGame::NoPlayers(); i++ )
    {
        Unit curUnit;
        PlayerBuildingEnum unitEnum(i, mapNone);
        while (unitEnum.GetNext(curUnit) )
        {
            mapCell cell;
            int tile = GameMap::GetTile(curUnit.Location() );
            memcpy(&cell, &tile, 4);
            if (cell.expand || cell.microbe)
            {
                curUnit.DoDeath();
            }
        }
    }

OR

Code: [Select]
// Gradually damage infected buildings over time and then kill them
// NOTE: Requires IUnit add-on
void AIProc()
{
    for (int i = 0; i < TethysGame::NoPlayers(); i++)
    {
        //Blighted Building Kill Code
        IUnit curUnit;
        PlayerBuildingEnum unitEnum(i, mapNone);
        while (unitEnum.GetNext(curUnit))
        {
            mapCell cell;
            int tile = GameMap::GetTile(curUnit.Location());
            memcpy(&cell, &tile, 4);
            if (cell.expand || cell.microbe) {
                curUnit.SetDamage(curUnit.GetDamage()+7+TethysGame::GetRand(5));
                if (curUnit.GetDamage() >= GetHitPoints(curUnit))
                    curUnit.DoDeath();
            }
        }
    }
}

You can learn more about IUnit here.

Hope that helps!  May I ask what you want it for, though?
"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 Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #2 on: March 31, 2010, 12:05:29 PM »
Thanks. I think about creating a new survivor map.
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #3 on: March 31, 2010, 12:13:08 PM »
Thought as much.  If you need help feel free to ask.
"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 Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #4 on: April 02, 2010, 08:04:03 AM »
I have a little problem:
I've downloaded the IUnit expansion and the "Kill structures as they become infected" code works, but the "Gradually damage infected buildings over time and then kill them" code doesn't work for me, the compiler can't find the command "GetHitPoints"
« Last Edit: April 02, 2010, 08:04:16 AM by Flashy »
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #5 on: April 02, 2010, 11:43:30 AM »
Only the "gradual" method requires IUnit.  Make sure you follow the installation instructions correctly, and make sure to add #include "IUnit.h" to the top of your code file.
"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 Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #6 on: April 02, 2010, 01:36:09 PM »
I added IUnit.h. But maybe I made a mistake. How do I include "op2extra.lib" into the project?
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #7 on: April 03, 2010, 09:03:59 AM »
If you're using CodeBlocks:

1) Open your project file in CodeBlocks.
2) Right click the project icon on the left sidebar.
3) Click "Build Options...".
4) Click the "Linker Settings" tab.
5) Click the "Add" button under the "Link Libraries" window.
6) Find op2extra.lib and click "OK".

If you're using Visual C++, I dunno.  It's probably a similar process, but some things may have slightly different names.  Ask AmIMeYet I guess, since he wrote the MSVC tutorial.
"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 Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #8 on: April 03, 2010, 09:42:26 AM »
I'm using Visual C++. I'll ask him.
Praise the mighty light towers!!!

Offline AmIMeYet

  • Full Member
  • ***
  • Posts: 128
Survivor Code
« Reply #9 on: April 03, 2010, 09:56:32 AM »
I don't have VC++ installed anymore, but I think it's like this:

top menu bar:
Project > <projectname> Properties
and in that popup:
Configuration Properties > Linker > Input
there should be a field called 'Additional Dependencies', and add /path/to/op2extra.lib using some separation formatting (I think it's comma separation)

let me know what happens

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #10 on: April 03, 2010, 01:16:03 PM »
Strange. That is exacly what i did. I added op2extra.lib correcly and wrote
Code: [Select]
#include "..\Outpost2DLL\Outpost2DLL.h"
#include "..\OP2Helper\OP2Helper.h"
#include "..\IUnit\IUnit.h"
#include "..\IUnit\commands.h"
But Visual C++ can't find "GetHitPoints"
Praise the mighty light towers!!!

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #11 on: April 03, 2010, 02:35:34 PM »
I think it should be <IUnit.h> not "IUnit.h".

I dunno if that really matters though?
« Last Edit: April 03, 2010, 02:36:06 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 Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #12 on: April 03, 2010, 03:08:13 PM »
Quote
"..\IUnit\<IUnit.h>": No such file or directory
Quote
"<IUnit.h>": Invalid argument
Praise the mighty light towers!!!

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Survivor Code
« Reply #13 on: April 03, 2010, 03:12:39 PM »
The difference between #include "file" and #include <file> is the search path used. Typically standard API headers are included with <>, but your project may need some setup to do that. To use <> with a custom API, such as ours, you'll need to have a custom include path set for the folder containing the file you want to include.

In the version of the SDK in the SVN, I set such an additional include path in all the project files to "../../API/". Note that I used a relative path, and keep all the source files in a common directory stucture, so that it can be rerooted easily. This is kind of important if you want to share your project files with someone else, and they don't want to muck about with your project settings just to get the thing to compile.



Edit: Flashy, you don't use <> inside of "". You use them instead of quotes.
« Last Edit: April 03, 2010, 03:13:42 PM by Hooman »

Offline evecolonycamander

  • Hero Member
  • *****
  • Posts: 602
Survivor Code
« Reply #14 on: April 03, 2010, 03:29:52 PM »
Out of curiosity Who cares if the blight infected a building if its infected then soon you will be to and before then i don't think you are going to be trying to build any buildings in the mean time
''The blight cant get us up here!''
-famous last words
--------------o0o--------------
Outpost 2: EoM project status: Re-planning

Offline Hidiot

  • Hero Member
  • *****
  • Posts: 1018
Survivor Code
« Reply #15 on: April 03, 2010, 03:40:49 PM »
Custom maps, where blight grows relatively slowly and is able to eat up one or two bases before the game is over.

It's an important process to prevent annoying structures disabled messages and meaningless morale hits (realistically, who cares if an abandoned base is being eaten by the blight?)
"Nothing from nowhere, I'm no one at all"

Offline Kayedon

  • Sr. Member
  • ****
  • Posts: 378
Survivor Code
« Reply #16 on: April 03, 2010, 07:25:58 PM »
Quote
(realistically, who cares if an abandoned base is being eaten by the blight?)
"But I painted that room myself!"
"Trust me, I'm crazy."

Offline Hidiot

  • Hero Member
  • *****
  • Posts: 1018
Survivor Code
« Reply #17 on: April 04, 2010, 04:14:05 AM »
Quote
Quote
(realistically, who cares if an abandoned base is being eaten by the blight?)
"But I painted that room myself!"
That... was spammy...
"Nothing from nowhere, I'm no one at all"

Offline AmIMeYet

  • Full Member
  • ***
  • Posts: 128
Survivor Code
« Reply #18 on: April 04, 2010, 04:41:34 AM »
Flashy, I'll see if I can setup the SDK & MSVC again, and I'll try to recreate your steps

[size=8]Edit: For me, it breaks as early as mapCell. Is mapCell defined in IUnit?[/size]

Quote
[size=8]1>------ Build started: Project: OP2Script, Configuration: Release MinSize Win32 ------
1>Compiling...
1>LevelMain.cpp
1>.\LevelMain.cpp(131) : error C2065: 'mapCell' : undeclared identifier
1>.\LevelMain.cpp(131) : error C2146: syntax error : missing ';' before identifier 'cell'
1>.\LevelMain.cpp(131) : error C2065: 'cell' : undeclared identifier
1>.\LevelMain.cpp(133) : error C2065: 'cell' : undeclared identifier
1>.\LevelMain.cpp(133) : error C3861: 'memcpy': identifier not found
1>.\LevelMain.cpp(134) : error C2065: 'cell' : undeclared identifier
1>.\LevelMain.cpp(134) : error C2228: left of '.expand' must have class/struct/union
1>        type is ''unknown-type''
1>.\LevelMain.cpp(134) : error C2065: 'cell' : undeclared identifier
1>.\LevelMain.cpp(134) : error C2228: left of '.microbe' must have class/struct/union
1>        type is ''unknown-type''
1>.\LevelMain.cpp(136) : error C3861: 'GetHitPoints': identifier not found
1>Build log was saved at "file://c:\Program Files\Outpost2\SDK\Levels\Flashy\ReleaseMinSize-IntermediateFiles\BuildLog.htm"
1>OP2Script - 10 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========[/size]
[size=8]Edit 2: Wait, what?! That's part of Sirbomber's snippet! wtf
Edit 3: Adding that mapCell struct to LevelMain.cpp leaves me with this:[/size]
Quote
[size=8]1>------ Build started: Project: OP2Script, Configuration: Release MinSize Win32 ------
1>Compiling...
1>LevelMain.cpp
1>.\LevelMain.cpp(144) : error C3861: 'memcpy': identifier not found
1>.\LevelMain.cpp(147) : error C3861: 'GetHitPoints': identifier not found
1>Build log was saved at "file://c:\Program Files\Outpost2\SDK\Levels\Flashy\ReleaseMinSize-IntermediateFiles\BuildLog.htm"
1>OP2Script - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/size]
[size=8]
Edit 4: Got it to stop complaining. Now waiting for the blight to spread[/size]

Edit 5: Okay, got it working now.

I had to put this in LevelMain.ccp:
Code: [Select]
#define WIN32_LEAN_AND_MEAN  // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <IUnit/IUnit.h> // The specific loading function for the SVN's SDK. Extracted op2extra.zip into /API/IUnit, and my project is in /Levels/Flashy
// Also, my Linker > Input > Additional Dependencies now reads: odbc32.lib odbccp32.lib ../../API/IUnit/op2extra.lib

struct mapCell
{
    int cellType:5;
    int tileIndex:11;
    int unitID:11;
    int lava:1;
    int lavaPossible:1;
    int expand:1;
    int microbe:1;
    int wallBuilding:1;
};
int GetHitPoints(Unit &unit)
{
    int unitObj = *((int*)0x54F848) + (120*unit.unitID);
    int hp = 0;
    __asm
    {
        mov ecx, unitObj
        mov eax, [ecx]
        call [eax]
        add eax, 8
        mov eax, [eax]
        mov hp, eax
    }
    return hp;
}
(be sure to read the IUnit related comments at the top!)
And DLLMain.ccp:
Code: [Select]
unsigned char *setTileLoc = (unsigned char *)0x00476D32;
unsigned char oldSetTile[] =
{
0x25, 0xE0, 0xFF, 0x00, 0x00,   // and eax, 0FFE0h
0xC1, 0xE8, 0x05                // shr eax, 5
};
unsigned char newSetTile[] =
{
    0x90, 0x90, 0x90, 0x90, 0x90,   // nop * 5
    0x90, 0x90, 0x90                // nop * 3
};

BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    DWORD oldAttr;
if (fdwReason == DLL_PROCESS_ATTACH)
{
  DisableThreadLibraryCalls(hinstDLL);


  // Modify the SetTile code for our purposes
  VirtualProtect(setTileLoc, sizeof(newSetTile), PAGE_EXECUTE_READWRITE, &oldAttr);
  memcpy(setTileLoc, newSetTile, sizeof(newSetTile));
}
else if (fdwReason == DLL_PROCESS_DETACH)
{
  // Put the old SetTile code back
  memcpy(setTileLoc, oldSetTile, sizeof(newSetTile));
}

    return TRUE;
}
Are you using the SVN's updated SDK, Flashy?
« Last Edit: April 04, 2010, 11:17:21 AM by AmIMeYet »

Offline Kayedon

  • Sr. Member
  • ****
  • Posts: 378
Survivor Code
« Reply #19 on: April 04, 2010, 01:48:52 PM »
Quote
Quote
Quote
(realistically, who cares if an abandoned base is being eaten by the blight?)
"But I painted that room myself!"
That... was spammy...
Maybe so, but I couldn't resist. I was giving a reason for people to care about an abandoned blight-infested base. My apologies. Feel free to delete it. And this. And yeah.
« Last Edit: April 04, 2010, 01:49:16 PM by Kayedon »
"Trust me, I'm crazy."

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #20 on: April 06, 2010, 07:34:19 AM »
Forgot something.  Stick this with the rest of the "Blight Helper Code":
Code: [Select]
int GetHitPoints(Unit &unit);

And stick this below AIProc somewhere:
Code: [Select]
int GetHitPoints(Unit &unit)
{
    int unitObj = *((int*)0x54F848) + (120*unit.unitID);
    int hp = 0;
    __asm
    {
        mov ecx, unitObj
        mov eax, [ecx]
        call [eax]
        add eax, 8
        mov eax, [eax]
        mov hp, eax
    }
    return hp;
}
"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
Survivor Code
« Reply #21 on: April 06, 2010, 08:17:00 AM »
Yeah, but what I don't get is why I had to specifically include windows.h in LevelMain :S
« Last Edit: April 06, 2010, 08:17:15 AM by AmIMeYet »

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #22 on: April 06, 2010, 08:20:07 AM »
Dunno, but it's part of the default template, so I'm sure it's there for a good reason...?
"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
Survivor Code
« Reply #23 on: April 06, 2010, 09:41:02 AM »
Quote
Dunno, but it's part of the default template, so I'm sure it's there for a good reason...?
Well that's the thing.. It's not in the default template. windows.h is only included in DllMain, but not LevelMain, and somehow this decay code requires it to be in LevelMain as well.

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #24 on: April 06, 2010, 10:21:06 AM »
LevelMain...?  Uhhh... Are we using the same templates here...?

Code: [Select]
OP2 Mission Skeleton Template (Most comments removed)

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <Outpost2DLL.h>
#include <OP2Helper.h>

char MapName[]   = "on6_01.map";
char LevelDesc[]  = "6 Player, LastOne, '<map name>' map";
char TechtreeName[]  = "MULTITEK.TXT";
SDescBlock DescBlock = { MultiLastOneStanding, 6, 12, 0 };

BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
 Â DisableThreadLibraryCalls(hinstDLL);
}

 Â   return TRUE;
}

int InitProc()
{
// **TODO**: Add your own code here.
return 1; // return 1 if OK; 0 on failure
}


void AIProc()
{
}

void __cdecl GetSaveRegions(struct BufferDesc &bufDesc)
{
bufDesc.bufferStart = 0; // Pointer to a buffer that needs to be saved
bufDesc.length = 0;   // sizeof(buffer)
}

int StatusProc()
{
return 0; // must return 0
}

SCRIPT_API void NoResponseToTrigger()
{
}

« Last Edit: April 06, 2010, 10:21:23 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