Skip to content

Commit

Permalink
add bit reading/writing helpers
Browse files Browse the repository at this point in the history
also fix monster schedule to int conversion
  • Loading branch information
wootguy committed Nov 8, 2024
1 parent b30af61 commit 0992003
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
2 changes: 2 additions & 0 deletions dlls/monster/CBaseMonster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7023,6 +7023,8 @@ int CBaseMonster::GetScheduleTableIdx() {
return i;
}
}

return -1;
}

//=========================================================
Expand Down
10 changes: 5 additions & 5 deletions dlls/monster/monsters.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ EXPORT BOOL IsFacing(entvars_t* pevTest, const Vector& reference);
#define bits_MEMORY_MOVE_FAILED ( 1 << 5 )// Movement has already failed
#define bits_MEMORY_FLINCHED ( 1 << 6 )// Has already flinched
#define bits_MEMORY_KILLED ( 1 << 7 )// HACKHACK -- remember that I've already called my Killed()
#define bits_MEMORY_CUSTOM4 ( 1 << 12 ) // Monster-specific memory
#define bits_MEMORY_CUSTOM3 ( 1 << 13 ) // Monster-specific memory
#define bits_MEMORY_CUSTOM2 ( 1 << 14 ) // Monster-specific memory
#define bits_MEMORY_CUSTOM1 ( 1 << 15 ) // Monster-specific memory
#define bits_MEMORY_CUSTOM4 ( 1 << 8 ) // Monster-specific memory
#define bits_MEMORY_CUSTOM3 ( 1 << 9 ) // Monster-specific memory
#define bits_MEMORY_CUSTOM2 ( 1 << 10 ) // Monster-specific memory
#define bits_MEMORY_CUSTOM1 ( 1 << 11 ) // Monster-specific memory

// trigger conditions for scripted AI
// these MUST match the CHOICES interface in halflife.fgd for the base monster
Expand Down Expand Up @@ -197,7 +197,7 @@ class EXPORT CGib : public CBaseEntity
if (baseIdx != -1) { \
return baseIdx; \
} \
for (int i = 0; i < ARRAYSIZE(m_scheduleList); i++) { \
for (int i = 0; i < (int)ARRAYSIZE(m_scheduleList); i++) { \
if (m_scheduleList[i] == m_pSchedule) { \
return baseClass::GetScheduleTableSize() + i; \
} \
Expand Down
50 changes: 50 additions & 0 deletions dlls/mstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ float mstream::readBitCoord() {
return 0;
}

bool mstream::endBitReading() {
if (currentBit) {
pos++;
currentBit = 0;

if (pos >= end) {
eomFlag = true;
pos = end;
}

return true;
}

return false;
}

uint64_t mstream::write( void * src, uint64_t bytes )
{
if (eomFlag)
Expand Down Expand Up @@ -160,6 +176,22 @@ uint8_t mstream::writeBits(uint32_t value, uint8_t bitCount) {
return bitCount;
}

bool mstream::endBitWriting() {
if (currentBit == 0 || currentBit == 8) {
return false;
}

while (currentBit != 8 && writeBit(0));

currentBit = 0;
pos++;

if (pos + 1 >= end) {
eomFlag = true;
return true;
}
}

bool mstream::writeBitCoord(const float f) {
int signbit = f <= -0.125;
int intval = abs((int32)f);
Expand Down Expand Up @@ -214,6 +246,20 @@ void mstream::seek( uint64_t to )
}
}

void mstream::seekBits(uint64_t to) {
pos = start + (to >> 3);
currentBit = to & 0x7;
eomFlag = false;
if (pos < start) {
pos = start;
eomFlag = true;
}
if (pos >= end) {
pos = end;
eomFlag = true;
}
}

void mstream::seek( uint64_t to, int whence )
{
switch(whence)
Expand Down Expand Up @@ -254,6 +300,10 @@ uint64_t mstream::tell()
return pos - start;
}

uint64_t mstream::tellBits() {
return (pos - start) * 8 + currentBit;
}

char * mstream::getBuffer()
{
return (char*)start;
Expand Down
14 changes: 14 additions & 0 deletions dlls/mstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class EXPORT mstream

float readBitCoord();

// increment to the next byte if current bit is not 0
// true if position was incremented
bool endBitReading();

// returns number of bytes that could be written into the buffer
uint64_t write(void * src, uint64_t bytes);

Expand All @@ -41,6 +45,10 @@ class EXPORT mstream
// write bitCount bits from value. Returns bits written
uint8_t writeBits(uint32_t value, uint8_t bitCount);

// write zeroes into the remaining bits at the current byte, then increment the position to the next byte
// true if position was incremented
bool endBitWriting();

// write Half-Life vector as bits (used in sound message)
bool writeBitVec3Coord(const float* fa);

Expand All @@ -50,6 +58,9 @@ class EXPORT mstream
// returns the offset in the buffer
uint64_t tell();

// returns the offset in the buffer, accurate to the bit
uint64_t tellBits();

// returns the size of the buffer
uint64_t size();

Expand All @@ -63,6 +74,9 @@ class EXPORT mstream
// resets eom flag if set to a valid position
void seek(uint64_t to);

// position is given in bits
void seekBits(uint64_t to);

// like seek but implements SEEK_CUR/SET/END functionality
void seek(uint64_t to, int whence);

Expand Down

0 comments on commit 0992003

Please sign in to comment.