Skip to content

Commit

Permalink
Defined bitmask, populated functions, added bit manipulation in utils
Browse files Browse the repository at this point in the history
  • Loading branch information
MicheleMichetti committed Jun 18, 2024
1 parent a7cba18 commit 34713af
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 7 deletions.
9 changes: 8 additions & 1 deletion game/include/Interaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ class Interaction {
uint8_t getType();
uint8_t getStatus();

//Interaction status reading
bool isInteraction();
void playInteraction();
bool isInteractionEnded();

//Interaction status manipulation
void enableInteraction();
void disableInteraction();
void playInteraction();
void endInteraction();
void increaseInteractionCounter();

void callFromFile();
};

} // namespace _interaction
54 changes: 53 additions & 1 deletion game/include/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

#include <bits/stdc++.h>
#include <stdint.h>

namespace utils {
Expand All @@ -25,4 +25,56 @@ enum InteractionType : uint8_t {
trigger = 0b01000000,
trainer = 0b10000000
};

enum InteractionStatus : uint8_t {
ENABLED = 0b00000001,
INTERACTION_NEVER_HAPPENED = 0b00000010,
IS_PLAYING = 0b00000100,
INTERACTION_ENDED = 0b00001000,
PLAYED_BIT1 = 0b00010000,
PLAYED_BIT2 = 0b00100000,
PLAYED_BIT3 = 0b01000000,
PLAYED_BIT4 = 0b10000000

};

//read n-th bit
template <class T>
bool readBit(T field, T n) {
return field & ((T)1<<n);
}
//TODO below have to be template func
// function to find the position of rightmost set bit
uint8_t getPosOfRightmostSetBit(uint8_t n) {
return std::log2(n & -n);
}

// function to toggle the last m bits
uint8_t toggleLastKBits(uint8_t n, uint8_t k) {
// calculating a number 'num' having 'm' bits and all are set
uint8_t num = (1 << k) - 1;

// toggle the last m bits and return the number
return (n ^ num);
}

// function to increment a number by one by manipulating the bits
uint8_t incrementByOne(uint8_t n) {
// get position of rightmost unset bit
// if all bits of 'n' are set, then the
// bit left to the MSB is the rightmost
// unset bit
uint8_t k = getPosOfRightmostSetBit(~n);

// kth bit of n is being set by this operation
n = ((1 << k) | n);

// from the right toggle all the bits before the k-th bit
if (k != 0) {
n = toggleLastKBits(n, k);
}

return n;
}

} // namespace utils
46 changes: 41 additions & 5 deletions game/src/Interaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,44 @@ uint32_t Interaction::getId() { return id_; }
uint8_t Interaction::getType() { return type_; }
uint8_t Interaction::getStatus() { return status_bit_mask_; }

bool Interaction::isInteraction() { return false; }
void Interaction::playInteraction() { return; }
bool Interaction::isInteractionEnded() { return false; }
void Interaction::enableInteraction() { return; }
void Interaction::disableInteraction() { return; }
bool Interaction::isInteraction() {
if( !(status_bit_mask_ & utils::InteractionStatus::ENABLED) ) {
return false;
}
if( !isInteractionEnded() ) {
return false;
}
return true;


}
bool Interaction::isInteractionEnded() {
if( status_bit_mask_ & utils::InteractionStatus::INTERACTION_ENDED ) {
return true;
}
return false;
}

void Interaction::enableInteraction() {
status_bit_mask_ |= utils::InteractionStatus::ENABLED;
}
void Interaction::disableInteraction() {
status_bit_mask_ & ~utils::InteractionStatus::ENABLED;
}
void Interaction::playInteraction() {
status_bit_mask_ |= utils::InteractionStatus::IS_PLAYING;
callFromFile();
increaseInteractionCounter();
}
void Interaction::endInteraction() {
status_bit_mask_ |= utils::InteractionStatus::INTERACTION_ENDED;
status_bit_mask_ & ~utils::InteractionStatus::IS_PLAYING;
}
void Interaction::increaseInteractionCounter() {
uint8_t counter = 0;
for(uint8_t bit_pos = 4; bit_pos < 8; ++bit_pos) {
counter |= utils::readBit<uint8_t>(status_bit_mask_,bit_pos);
}
counter = utils::incrementByOne(counter);
//TODO: copy counter to bitmask
}

0 comments on commit 34713af

Please sign in to comment.