From bd145c3ebfc082286486a8700b9ce20bfd7383e2 Mon Sep 17 00:00:00 2001 From: fusion32 Date: Sat, 7 Jun 2025 01:44:53 -0300 Subject: implement TFindCreatures and KillStatistics --- reference/game.c | 472 ----------------------------------------------------- reference/types.hh | 13 -- src/containers.hh | 11 ++ src/cr.hh | 26 +++ src/crmain.cc | 145 +++++++++++++++- src/stubs.hh | 2 +- 6 files changed, 180 insertions(+), 489 deletions(-) diff --git a/reference/game.c b/reference/game.c index 65f96be..d50249a 100644 --- a/reference/game.c +++ b/reference/game.c @@ -29623,478 +29623,6 @@ void __static_initialization_and_destruction_0(int __initialize_p,int __priority return; } -// DWARF original prototype: void TFindCreatures(TFindCreatures * this, int radx, int rady, int x, -// int y, int Mask) - -void __thiscall -TFindCreatures::TFindCreatures(TFindCreatures *this,int radx,int rady,int x,int y,int Mask) - -{ - uint uVar1; - uint uVar2; - - uVar1 = x - radx; - uVar2 = y - rady; - this->endx = x + radx; - this->startx = uVar1; - this->starty = uVar2; - this->endy = y + rady; - if (0x7fffffff < uVar1) { - uVar1 = uVar1 + 0xf; - } - this->blockx = ((int)uVar1 >> 4) + -1; - if (0x7fffffff < uVar2) { - uVar2 = uVar2 + 0xf; - } - this->blocky = (int)uVar2 >> 4; - this->ActID = 0; - this->SkipID = 0; - this->Mask = Mask; - this->finished = false; - return; -} - - - -// DWARF original prototype: void TFindCreatures(TFindCreatures * this, int radx, int rady, int x, -// int y, int Mask) - -void __thiscall -TFindCreatures::TFindCreatures(TFindCreatures *this,int radx,int rady,int x,int y,int Mask) - -{ - uint uVar1; - uint uVar2; - - uVar1 = x - radx; - uVar2 = y - rady; - this->endy = y + rady; - this->startx = uVar1; - if (0x7fffffff < uVar1) { - uVar1 = uVar1 + 0xf; - } - this->endx = x + radx; - this->blockx = ((int)uVar1 >> 4) + -1; - this->starty = uVar2; - if (0x7fffffff < uVar2) { - uVar2 = uVar2 + 0xf; - } - this->blocky = (int)uVar2 >> 4; - this->ActID = 0; - this->SkipID = 0; - this->Mask = Mask; - this->finished = false; - return; -} - - - -// DWARF original prototype: void TFindCreatures(TFindCreatures * this, int radx, int rady, ulong -// CreatureID, int Mask) - -void __thiscall -TFindCreatures::TFindCreatures(TFindCreatures *this,int radx,int rady,ulong CreatureID,int Mask) - -{ - int iVar1; - int iVar2; - TCreature *pTVar3; - uint uVar4; - uint uVar5; - - pTVar3 = GetCreature(CreatureID); - if (pTVar3 == (TCreature *)0x0) { - error("TFindCreatures::TFindCreatures: Kreatur existiert nicht.\n"); - this->finished = true; - } - else { - iVar1 = pTVar3->posx; - iVar2 = pTVar3->posy; - uVar4 = iVar1 - radx; - this->startx = uVar4; - uVar5 = iVar2 - rady; - this->ActID = 0; - this->endy = iVar2 + rady; - if (0x7fffffff < uVar4) { - uVar4 = uVar4 + 0xf; - } - this->blockx = ((int)uVar4 >> 4) + -1; - this->finished = false; - this->endx = iVar1 + radx; - this->Mask = Mask; - this->starty = uVar5; - if (0x7fffffff < uVar5) { - uVar5 = uVar5 + 0xf; - } - this->blocky = (int)uVar5 >> 4; - this->SkipID = CreatureID; - } - return; -} - - - -// DWARF original prototype: void TFindCreatures(TFindCreatures * this, int radx, int rady, ulong -// CreatureID, int Mask) - -void __thiscall -TFindCreatures::TFindCreatures(TFindCreatures *this,int radx,int rady,ulong CreatureID,int Mask) - -{ - int iVar1; - int iVar2; - TCreature *pTVar3; - uint uVar4; - uint uVar5; - - pTVar3 = GetCreature(CreatureID); - if (pTVar3 == (TCreature *)0x0) { - error("TFindCreatures::TFindCreatures: Kreatur existiert nicht.\n"); - this->finished = true; - } - else { - iVar1 = pTVar3->posx; - iVar2 = pTVar3->posy; - uVar4 = iVar1 - radx; - this->startx = uVar4; - uVar5 = iVar2 - rady; - this->ActID = 0; - this->endy = iVar2 + rady; - if (0x7fffffff < uVar4) { - uVar4 = uVar4 + 0xf; - } - this->blockx = ((int)uVar4 >> 4) + -1; - this->finished = false; - this->endx = iVar1 + radx; - this->Mask = Mask; - this->starty = uVar5; - if (0x7fffffff < uVar5) { - uVar5 = uVar5 + 0xf; - } - this->blocky = (int)uVar5 >> 4; - this->SkipID = CreatureID; - } - return; -} - - - -// DWARF original prototype: void TFindCreatures(TFindCreatures * this, int radx, int rady, Object * -// Obj, int Mask) - -void __thiscall -TFindCreatures::TFindCreatures(TFindCreatures *this,int radx,int rady,Object *Obj,int Mask) - -{ - bool bVar1; - uint uVar2; - uint uVar3; - int local_38; - int x; - int y; - int z; - - bVar1 = Object::exists(Obj); - if (bVar1) { - z = Obj->ObjectID; - GetObjectCoordinates((Object *)&z,&local_38,&x,&y); - uVar3 = x - rady; - this->endx = local_38 + radx; - uVar2 = local_38 - radx; - this->SkipID = 0; - this->startx = uVar2; - this->endy = x + rady; - if (0x7fffffff < uVar2) { - uVar2 = uVar2 + 0xf; - } - this->blockx = ((int)uVar2 >> 4) + -1; - this->starty = uVar3; - if (0x7fffffff < uVar3) { - uVar3 = uVar3 + 0xf; - } - this->blocky = (int)uVar3 >> 4; - this->ActID = 0; - this->Mask = Mask; - this->finished = false; - } - else { - error(&DAT_08101a80); - this->finished = true; - } - return; -} - - - -// DWARF original prototype: void TFindCreatures(TFindCreatures * this, int radx, int rady, Object * -// Obj, int Mask) - -void __thiscall -TFindCreatures::TFindCreatures(TFindCreatures *this,int radx,int rady,Object *Obj,int Mask) - -{ - bool bVar1; - uint uVar2; - uint uVar3; - int local_38; - int x; - int y; - int z; - - bVar1 = Object::exists(Obj); - if (bVar1) { - z = Obj->ObjectID; - GetObjectCoordinates((Object *)&z,&local_38,&x,&y); - uVar3 = x - rady; - this->endx = local_38 + radx; - uVar2 = local_38 - radx; - this->SkipID = 0; - this->startx = uVar2; - this->endy = x + rady; - if (0x7fffffff < uVar2) { - uVar2 = uVar2 + 0xf; - } - this->blockx = ((int)uVar2 >> 4) + -1; - this->starty = uVar3; - if (0x7fffffff < uVar3) { - uVar3 = uVar3 + 0xf; - } - this->blocky = (int)uVar3 >> 4; - this->ActID = 0; - this->Mask = Mask; - this->finished = false; - } - else { - error(&DAT_08101a80); - this->finished = true; - } - return; -} - - - -// DWARF original prototype: void initSearch(TFindCreatures * this, int radx, int rady, int x, int -// y, int mask) - -void __thiscall -TFindCreatures::initSearch(TFindCreatures *this,int radx,int rady,int x,int y,int mask) - -{ - uint uVar1; - uint uVar2; - - uVar1 = x - radx; - uVar2 = y - rady; - this->endy = y + rady; - this->startx = uVar1; - if (0x7fffffff < uVar1) { - uVar1 = uVar1 + 0xf; - } - this->endx = x + radx; - this->blockx = ((int)uVar1 >> 4) + -1; - this->starty = uVar2; - if (0x7fffffff < uVar2) { - uVar2 = uVar2 + 0xf; - } - this->blocky = (int)uVar2 >> 4; - this->ActID = 0; - this->SkipID = 0; - this->Mask = mask; - this->finished = false; - return; -} - - - -// DWARF original prototype: ulong getNext(TFindCreatures * this) - -ulong __thiscall TFindCreatures::getNext(TFindCreatures *this) - -{ - CreatureType CVar1; - bool bVar2; - matrix<> *pmVar3; - int iVar4; - uint uVar5; - TCreature *pTVar6; - int iVar7; - ulong *puVar8; - ulong uVar9; - TCreature *cr; - - if (this->finished == false) { - do { - do { - do { - while( true ) { - uVar9 = this->ActID; - while (uVar9 == 0) { - uVar5 = this->endx; - iVar4 = this->blockx + 1; - if (0x7fffffff < uVar5) { - uVar5 = uVar5 + 0xf; - } - this->blockx = iVar4; - if ((int)uVar5 >> 4 < iVar4) { - uVar5 = this->startx; - if (0x7fffffff < uVar5) { - uVar5 = uVar5 + 0xf; - } - iVar4 = this->blocky + 1; - this->blockx = (int)uVar5 >> 4; - uVar5 = this->endy; - this->blocky = iVar4; - if (0x7fffffff < uVar5) { - uVar5 = uVar5 + 0xf; - } - if ((int)uVar5 >> 4 < iVar4) { - this->finished = true; - goto LAB_0809ec9e; - } - } - pmVar3 = FirstChainCreature; - iVar4 = this->blockx; - if ((((SBORROW4(iVar4,SectorXMin * 2) == iVar4 + SectorXMin * -2 < 0) && - (iVar4 <= SectorXMax * 2 + 1)) && - (iVar7 = this->blocky, - SBORROW4(iVar7,SectorYMin * 2) == iVar7 + SectorYMin * -2 < 0)) && - (iVar7 <= SectorYMax * 2 + 1)) { - iVar7 = iVar7 - FirstChainCreature->ymin; - iVar4 = iVar4 - FirstChainCreature->xmin; - if (((iVar4 < 0) || (FirstChainCreature->dx <= iVar4)) || - ((iVar7 < 0 || (FirstChainCreature->dy <= iVar7)))) { - error("matrix::operator(): Ungueltiger Index %d/%d.\n", - iVar4 + FirstChainCreature->xmin, - iVar7 + FirstChainCreature->ymin); - puVar8 = pmVar3->entry; - } - else { - puVar8 = FirstChainCreature->entry + - iVar4 + iVar7 * FirstChainCreature->dx; - } - this->ActID = *puVar8; - } - else { - this->ActID = 0; - } - uVar9 = this->ActID; - } - pTVar6 = GetCreature(uVar9); - if (pTVar6 != (TCreature *)0x0) break; - error("TFindCreatures::getNext: Kreatur existiert nicht.\n"); - this->ActID = 0; - } - this->ActID = pTVar6->NextChainCreature; - uVar9 = pTVar6->ID; - } while (uVar9 == this->SkipID); - bVar2 = false; - if ((this->startx <= pTVar6->posx) && (pTVar6->posx <= this->endx)) { - bVar2 = true; - } - } while (!bVar2); - bVar2 = false; - if ((this->starty <= pTVar6->posy) && (pTVar6->posy <= this->endy)) { - bVar2 = true; - } - } while ((((!bVar2) || ((CVar1 = pTVar6->Type, CVar1 == PLAYER && ((this->Mask & 1) == 0)))) - || ((CVar1 == NPC && ((this->Mask & 2) == 0)))) || - ((CVar1 == MONSTER && ((this->Mask & 4) == 0)))); - } - else { -LAB_0809ec9e: - uVar9 = 0; - } - return uVar9; -} - - - -// WARNING: Unknown calling convention -- yet parameter storage is locked - -void InitKillStatistics(void) - -{ - int i; - int iVar1; - - iVar1 = 0; - do { - KilledCreatures[iVar1] = 0; - KilledPlayers[iVar1] = 0; - iVar1 = iVar1 + 1; - } while (iVar1 < 0x200); - return; -} - - - -// WARNING: Variable defined which should be unmapped: NumberOfRaces -// WARNING: Unknown calling convention -- yet parameter storage is locked - -void WriteKillStatistics(void) - -{ - int i_1; - char *RaceNames; - int *KilledPlayers; - int *KilledCreatures; - char *Text; - char *pcVar1; - int i; - int iVar2; - char *__s; - int local_24; - int *Creatures; - int *Players; - char *Names; - int NumberOfRaces; - - Names = (char *)0x0; - RaceNames = (char *)operator_new__(0x3c00); - KilledPlayers = (int *)operator_new__(0x800); - KilledCreatures = (int *)operator_new__(0x800); - local_24 = 0; - iVar2 = 0; - pcVar1 = RaceNames; - __s = RaceNames; - do { - if ((::KilledCreatures[iVar2] != 0) || (::KilledPlayers[iVar2] != 0)) { - if (iVar2 < 1) { - builtin_strncpy(pcVar1,"(fire/poison/energy)",0x15); - } - else { - sprintf(__s,"%s %s",local_24 + 0x81c477e,RaceData[0].Name + local_24); - Text = Plural(__s,0); - Capitals(Text); - } - pcVar1 = pcVar1 + 0x1e; - __s = __s + 0x1e; - KilledPlayers[(int)Names] = ::KilledPlayers[iVar2]; - KilledCreatures[(int)Names] = ::KilledCreatures[iVar2]; - Names = Names + 1; - } - local_24 = local_24 + 0x150; - iVar2 = iVar2 + 1; - } while (iVar2 < 0x200); - KillStatisticsOrder((int)Names,RaceNames,KilledPlayers,KilledCreatures); - iVar2 = 0; - do { - ::KilledCreatures[iVar2] = 0; - ::KilledPlayers[iVar2] = 0; - iVar2 = iVar2 + 1; - } while (iVar2 < 0x200); - return; -} - -// WARNING: Unknown calling convention -- yet parameter storage is locked - -void ExitKillStatistics(void) - -{ - WriteKillStatistics(); - return; -} - char * GetRaceName(int Race) { diff --git a/reference/types.hh b/reference/types.hh index 42caab1..c711831 100644 --- a/reference/types.hh +++ b/reference/types.hh @@ -284,19 +284,6 @@ struct TMoveUseDatabase { int NumberOfRules; }; -struct TFindCreatures { - int startx; - int starty; - int endx; - int endy; - int blockx; - int blocky; - ulong ActID; - ulong SkipID; - int Mask; - bool finished; -}; - struct TReaderThreadOrder { enum TReaderThreadOrderType OrderType; int SectorX; diff --git a/src/containers.hh b/src/containers.hh index fafe5b7..ba7e3cd 100644 --- a/src/containers.hh +++ b/src/containers.hh @@ -239,6 +239,17 @@ struct matrix{ delete[] this->entry; } + // NOTE(fusion): Same as `at` but returns NULL on out of bounds coordinates. + T *boundedAt(int x, int y){ + int xoffset = x - this->xmin; + int yoffset = y - this->ymin; + if(xoffset < 0 || xoffset >= this->dx || yoffset < 0 || yoffset >= this->dy){ + return NULL; + }else{ + return &this->entry[yoffset * this->dx + xoffset]; + } + } + T *at(int x, int y){ int xoffset = x - this->xmin; int yoffset = y - this->ymin; diff --git a/src/cr.hh b/src/cr.hh index 8f8e0de..a81b59b 100644 --- a/src/cr.hh +++ b/src/cr.hh @@ -378,6 +378,29 @@ struct TCombat{ int LearningPoints; }; +// TFindCreatures +// ============================================================================= +struct TFindCreatures { + TFindCreatures(int RadiusX, int RadiusY, int CenterX, int CenterY, int Mask); + TFindCreatures(int RadiusX, int RadiusY, uint32 CreatureID, int Mask); + TFindCreatures(int RadiusX, int RadiusY, Object Obj, int Mask); + void initSearch(int RadiusX, int RadiusY, int CenterX, int CenterY, int Mask); + uint32 getNext(void); + + // DATA + // ================= + int startx; + int starty; + int endx; + int endy; + int blockx; + int blocky; + uint32 ActID; + uint32 SkipID; + int Mask; + bool finished; +}; + // TCreature // ============================================================================= struct TToDoEntry { @@ -713,6 +736,9 @@ void ProcessSkills(void); void MoveCreatures(int Delay); void AddKillStatistics(int AttackerRace, int DefenderRace); +void WriteKillStatistics(void); +void InitKillStatistics(void); +void ExitKillStatistics(void); bool IsRaceValid(int Race); int GetRaceByName(const char *RaceName); diff --git a/src/crmain.cc b/src/crmain.cc index 49348d8..6a9602d 100644 --- a/src/crmain.cc +++ b/src/crmain.cc @@ -18,6 +18,99 @@ static TCreature *HashList[1000]; static matrix *FirstChainCreature; static vector CreatureList(0, 10000, 1000); +// TFindCreatures +// ============================================================================= +TFindCreatures::TFindCreatures(int RadiusX, int RadiusY, int CenterX, int CenterY, int Mask){ + this->initSearch(RadiusX, RadiusY, CenterX, CenterY, Mask); +} + +TFindCreatures::TFindCreatures(int RadiusX, int RadiusY, uint32 CreatureID, int Mask){ + TCreature *Creature = GetCreature(CreatureID); + if(Creature == NULL){ + error("TFindCreatures::TFindCreatures: Kreatur existiert nicht.\n"); + this->finished = true; + return; + } + + this->initSearch(RadiusX, RadiusY, Creature->posx, Creature->posy, Mask); + this->SkipID = Creature->ID; +} + +TFindCreatures::TFindCreatures(int RadiusX, int RadiusY, Object Obj, int Mask){ + if(!Obj.exists()){ + error("TFindCreatures::TFindCreatures: Übergebenes Objekt existiert nicht.\n"); + this->finished = true; + return; + } + + int ObjX, ObjY, ObjZ; + GetObjectCoordinates(Obj, &ObjX, &ObjY, &ObjZ); + this->initSearch(RadiusX, RadiusY, ObjX, ObjY, Mask); +} + +void TFindCreatures::initSearch(int RadiusX, int RadiusY, int CenterX, int CenterY, int Mask){ + this->startx = CenterX - RadiusX; + this->starty = CenterY - RadiusY; + this->endx = CenterX + RadiusX; + this->endy = CenterY + RadiusY; + // NOTE(fusion): See `TFindCreatures::getNext` for an explanation on the -1. + this->blockx = (this->startx / 16) - 1; + this->blocky = (this->starty / 16); + this->ActID = 0; + this->SkipID = 0; + this->Mask = Mask; + this->finished = false; +} + +uint32 TFindCreatures::getNext(void){ + if(this->finished){ + return 0; + } + + int StartBlockX = this->startx / 16; + int EndBlockX = this->endx / 16; + int EndBlockY = this->endy / 16; + while(true){ + while(this->ActID == 0){ + this->blockx += 1; + if(this->blockx > EndBlockX){ + this->blockx = StartBlockX; + this->blocky += 1; + if(this->blocky > EndBlockY){ + this->finished = true; + return 0; + } + } + + uint32 *FirstID = FirstChainCreature->boundedAt(this->blockx, this->blocky); + if(FirstID != NULL){ + this->ActID = *FirstID; + }else{ + this->ActID = 0; + } + } + + TCreature *Creature = GetCreature(this->ActID); + if(Creature == NULL){ + error("TFindCreatures::getNext: Kreatur existiert nicht.\n"); + this->ActID = 0; + continue; + } + + this->ActID = Creature->NextChainCreature; + if(Creature->ID == this->SkipID + || Creature->posx < this->startx || Creature->posx > this->endx + || Creature->posy < this->starty || Creature->posy > this->endy + || (Creature->Type == PLAYER && (this->Mask & 0x01) == 0) + || (Creature->Type == NPC && (this->Mask & 0x02) == 0) + || (Creature->Type == MONSTER && (this->Mask & 0x04) == 0)){ + continue; + } + + return Creature->ID; + } +} + // TCreature // ============================================================================= TCreature::TCreature(void) : @@ -793,7 +886,7 @@ void TCreature::CreatureMoveStimulus(uint32 CreatureID, int Type){ } // TODO(fusion): Find out what `Type` is here. - if(Type != 2 // STIMULUS_TYPE?? + if(Type != 2 // STIMULUS_TYPE_?? || !this->LockToDo || this->ActToDo >= this->NrToDo || this->ToDoList.at(this->ActToDo)->Code != TDAttack){ @@ -1022,8 +1115,6 @@ void MoveCreatures(int Delay){ // Kill Statistics // ============================================================================= -void InitKillStatistics(void);//TODO -void ExitKillStatistics(void);//TODO void AddKillStatistics(int AttackerRace, int DefenderRace){ // NOTE(fusion): I think the race name can be "human" only for players, // which means we're probably tracking how many creatures are killed by @@ -1040,6 +1131,54 @@ void AddKillStatistics(int AttackerRace, int DefenderRace){ } } +void WriteKillStatistics(void){ + // TODO(fusion): Using the same names with local and global variables is + // trash. I'd personally have all globals with a `g_` prefix but I'm trying + // to not change the original code too much. + + // TODO(fusion): The way we manage race names here is a disaster but could + // make sense if there is a constant `MAX_RACE_NAME` somewhere. I've also + // seen the length of 30 often used with name strings so it could also be + // a general constant `MAX_NAME`. + + int NumberOfRaces = 0; + char *RaceNames = new char[MAX_RACES * 30]; + int *KilledPlayers = new int[MAX_RACES]; + int *KilledCreatures = new int[MAX_RACES]; + for(int Race = 0; Race < MAX_RACES; Race += 1){ + if(::KilledCreatures[Race] == 0 && ::KilledPlayers[Race] == 0){ + continue; + } + + char *Name = &RaceNames[NumberOfRaces * 30]; + if(Race == 0){ + strcpy(Name, "(fire/poison/energy)"); + }else{ + sprintf(Name, "%s %s", RaceData[Race].Article, RaceData[Race].Name); + // TODO(fusion): The original function had a call to `Plural` and + // `Capitals` but didn't seem to put the results back into `Name`. + } + + KilledPlayers[NumberOfRaces] = ::KilledPlayers[Race]; + KilledCreatures[NumberOfRaces] = ::KilledCreatures[Race]; + NumberOfRaces += 1; + } + + KillStatisticsOrder(NumberOfRaces, RaceNames, KilledPlayers, KilledCreatures); + InitKillStatistics(); +} + +void InitKillStatistics(void){ + for(int i = 0; i < MAX_RACES; i += 1){ + KilledCreatures[i] = 0; + KilledPlayers[i] = 0; + } +} + +void ExitKillStatistics(void){ + WriteKillStatistics(); +} + // Race // ============================================================================= TRaceData::TRaceData(void) : diff --git a/src/stubs.hh b/src/stubs.hh index fa7f0d2..ba87f27 100644 --- a/src/stubs.hh +++ b/src/stubs.hh @@ -52,6 +52,7 @@ extern void GraphicalEffect(Object Obj, int Type); extern int IdentifyPlayer(const char *Name, bool ExactMatch, bool IgnoreGamemasters, TPlayer **Player); extern void InitLog(const char *ProtocolName); extern void KickGuest(uint16 HouseID, TPlayer *Host, TPlayer *Guest); +extern void KillStatisticsOrder(int NumberOfRaces, const char *RaceNames, int *KilledPlayers, int *KilledCreatures); extern bool LagDetected(void); extern void LoadMonsterRaid(const char *FileName, int Start, bool *Type, int *Date, int *Interval, int *Duration); @@ -97,6 +98,5 @@ extern void ShowNameDoor(Object Door, TPlayer *Player, char *Buffer); extern void TextualEffect(Object Obj, int Color, const char *Text, ...) ATTR_PRINTF(3, 4); extern bool ThrowPossible(int FromX, int FromY, int FromZ, int ToX, int ToY, int ToZ, int Power); -extern void WriteKillStatistics(void); #endif //TIBIA_STUBS_HH_ -- cgit v1.2.3