Author Topic: Survivor Code  (Read 5409 times)

Offline AmIMeYet

  • Full Member
  • ***
  • Posts: 128
Survivor Code
« Reply #25 on: April 06, 2010, 10:37:36 AM »
Ahh, that explains it! XD
I'm using the SVN's SDK, and that splits that code into DllMain.cpp and LevelMain.cpp

Offline Sirbomber

  • Hero Member
  • *****
  • Posts: 3237
Survivor Code
« Reply #26 on: April 06, 2010, 12:57:44 PM »
Hooman really needs to learn when not to mess with the SDK, especially because he only made that change to shave 2 seconds off the compile time...
"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 TH300

  • Hero Member
  • *****
  • Posts: 1404
    • http://op3game.net
Survivor Code
« Reply #27 on: April 06, 2010, 03:53:13 PM »
I think he did it to give it a better structure. I personally like how the actual mission code is separated from the boiler plate.

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4954
Survivor Code
« Reply #28 on: April 06, 2010, 10:00:49 PM »
I did it both for structure and for speed. There is no reason for someone just learning to write missions to even look at DllMain. That's just a required technical detail. You only ever muck with that with fairly advanced custom mods, usually involving code overwriting. So, the power to modify it is there for those rare cases when it's needed, and otherwise it stays out of people's way.

Plus, I'd learned a few tips around that time about getting C++ code to compile faster by improving it's organization. That's probably what got me thinking about the structural issue. Besides, shaving 2 seconds off compile time isn't exactly insignificant. That extra compile time slows down the testing/debugging cycle by a perceivable time period, which has negative psychological effects on wanting to do what you're doing. The slow compile times was one of my biggest annoyances about C++ when I first started learning it. Most C++ compilers are notorious for being slow, but then, the grammar for C++ is a huge pain for compilers to deal with.
 

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #29 on: April 07, 2010, 10:10:04 AM »
Quote
Are you using the SVN's updated SDK, Flashy?
I downloaded it here
Praise the mighty light towers!!!

Offline AmIMeYet

  • Full Member
  • ***
  • Posts: 128
Survivor Code
« Reply #30 on: April 07, 2010, 10:21:35 AM »
Okay.
Did you get it to work yet?
If not, then using this should work for your original problem:
Quote
Code: [Select]
// Replace the BOOL APIENTRY Dllmain section with this:
// -----------------------------------------------------------------------
// Blight Helper Code
// -----------------------------------------------------------------------

int GetHitPoints(Unit &unit)
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;
}

// 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();
            }
        }
    }
}

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;
}
« Last Edit: April 07, 2010, 10:22:28 AM by AmIMeYet »

Offline Flashy

  • Sr. Member
  • ****
  • Posts: 391
Survivor Code
« Reply #31 on: April 07, 2010, 11:49:14 AM »
Quote
Okay.
Did you get it to work yet?
If not, then using this should work for your original problem:
[After adding this, it worked!]
OMG!
It works!!!!
Thank you

PS: I can't believe it!
« Last Edit: April 07, 2010, 11:54:31 AM by Flashy »
Praise the mighty light towers!!!