| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- #include "Entity.h"
- #include <Mat4.h>
- #include <Text.h>
- #include "BlockType.h"
- #include "Dimension.h"
- #include "EntityType.h"
- #include "Game.h"
- #include "ItemSkill.h"
- #include "ItemStack.h"
- #include "ItemType.h"
- ActionTarget::ActionTarget(Framework::Vec3<int> blockPos, Direction blockSide)
- : blockPos(blockPos),
- targetBlockSide(blockSide),
- entityId(-1)
- {}
- ActionTarget::ActionTarget(int entityId)
- : entityId(entityId)
- {}
- bool ActionTarget::isBlock(
- Framework::Vec3<int> blockPos, Direction blockSide) const
- {
- return this->entityId == -1 && this->blockPos == blockPos
- && (this->targetBlockSide == targetBlockSide
- || blockSide == NO_DIRECTION);
- }
- bool ActionTarget::isEntity(int entityId) const
- {
- return this->entityId == entityId;
- }
- bool ActionTarget::useItemSkillOnTarget(
- Entity* zActor, ItemSkill* zItemSkill, Item* zUsedItem)
- {
- if (entityId >= 0)
- {
- Entity* target = Game::INSTANCE->zEntity(entityId);
- if (target)
- {
- return zItemSkill->use(zActor, zUsedItem, target);
- }
- }
- else
- {
- Block* block = Game::INSTANCE->zRealBlockInstance(
- blockPos, zActor->getDimensionId());
- if (block)
- {
- return zItemSkill->use(zActor, zUsedItem, block);
- }
- }
- return 0;
- }
- bool ActionTarget::interactItemSkillOnTarget(
- Entity* zActor, ItemSkill* zItemSkill, Item* zUsedItem)
- {
- if (zItemSkill)
- {
- if (entityId >= 0)
- {
- Entity* target = Game::INSTANCE->zEntity(entityId);
- if (target) return zItemSkill->interact(zActor, zUsedItem, target);
- }
- else
- {
- Block* block = Game::INSTANCE->zRealBlockInstance(
- blockPos, zActor->getDimensionId());
- if (block) return zItemSkill->interact(zActor, zUsedItem, block);
- }
- }
- else
- {
- bool itemChanged = 0;
- if (entityId >= 0)
- {
- Block* block = Game::INSTANCE->zRealBlockInstance(
- blockPos, zActor->getDimensionId());
- if (block) block->interact(zUsedItem, zActor, itemChanged);
- }
- else
- {
- Block* block = Game::INSTANCE->zRealBlockInstance(
- blockPos, zActor->getDimensionId());
- if (block) block->interact(zUsedItem, zActor, itemChanged);
- }
- return itemChanged;
- }
- return 0;
- }
- bool ActionTarget::placeBlock(Entity* zActor, Item* zItem)
- {
- if (zActor->getStamina() > 0.2f)
- {
- if (zItem->canBePlacedAt(zActor->getDimensionId(),
- blockPos + getDirection(targetBlockSide)))
- {
- Block* block = zItem->zPlacedBlockType()->createBlockAt(
- blockPos + getDirection(targetBlockSide),
- zActor->getDimensionId(),
- zItem);
- if (block)
- {
- Game::INSTANCE->zDimension(zActor->getDimensionId())
- ->placeBlock(block->getPos(), block);
- zItem->onPlaced();
- zActor->setStamina(zActor->getStamina() - 0.2f);
- return 1;
- }
- }
- }
- return 0;
- }
- void ActionTarget::toMessage(
- const ActionTarget* zTarget, int dimensionId, NetworkMessage* zMsg)
- {
- if (zTarget)
- {
- if (zTarget->entityId >= 0)
- {
- Entity* zEntity = Game::INSTANCE->zEntity(zTarget->entityId);
- if (zEntity)
- {
- Framework::XML::Element* targetUIML = zEntity->getTargetUIML();
- Framework::Text targetUIMLText
- = targetUIML ? targetUIML->toString() : Framework::Text();
- targetUIML->release();
- char* message = new char[8 + targetUIMLText.getLength()];
- message[0] = 3;
- message[1] = 1;
- *(int*)(message + 2) = zTarget->entityId;
- *(short*)(message + 6) = (short)targetUIMLText.getLength();
- memcpy(message + 8,
- targetUIMLText.getText(),
- targetUIMLText.getLength());
- zMsg->setMessage(message, 8 + targetUIMLText.getLength());
- }
- else
- {
- char* message = new char[2];
- message[0] = 3;
- message[1] = 0;
- zMsg->setMessage(message, 2);
- }
- }
- else
- {
- Framework::XML::Element* targetUIML = 0;
- auto block
- = Game::INSTANCE->zBlockAt(zTarget->blockPos, dimensionId, 0);
- if (block.isA())
- {
- targetUIML = block.getA()->getTargetUIML();
- }
- else if (block.isB())
- {
- targetUIML
- = Game::INSTANCE->zBlockType(block.getB())->getTargetUIML();
- }
- Framework::Text targetUIMLText
- = targetUIML ? targetUIML->toString() : Framework::Text();
- char* message = new char[18 + targetUIMLText.getLength() + 2];
- message[0] = 3;
- message[1] = 2;
- *(int*)(message + 2) = zTarget->blockPos.x;
- *(int*)(message + 6) = zTarget->blockPos.y;
- *(int*)(message + 10) = zTarget->blockPos.z;
- *(int*)(message + 14) = zTarget->targetBlockSide;
- short len = (short)targetUIMLText.getLength();
- *(short*)(message + 18) = len;
- memcpy(message + 20, targetUIMLText.getText(), len);
- zMsg->setMessage(message, 18 + len + 2);
- }
- }
- else
- {
- char* message = new char[2];
- message[0] = 3;
- message[1] = 0;
- zMsg->setMessage(message, 2);
- }
- }
- void ActionTarget::save(ActionTarget* zTarget, Framework::StreamWriter* zWriter)
- {
- if (zTarget)
- {
- if (zTarget->entityId >= 0)
- {
- char b = 1;
- zWriter->schreibe(&b, 1);
- zWriter->schreibe((char*)&zTarget->entityId, 4);
- }
- else
- {
- char b = 2;
- zWriter->schreibe(&b, 1);
- zWriter->schreibe((char*)&zTarget->blockPos.x, 4);
- zWriter->schreibe((char*)&zTarget->blockPos.y, 4);
- zWriter->schreibe((char*)&zTarget->blockPos.z, 4);
- zWriter->schreibe((char*)&zTarget->targetBlockSide, 4);
- }
- }
- else
- {
- char b = 0;
- zWriter->schreibe(&b, 1);
- }
- }
- ActionTarget* ActionTarget::load(Framework::StreamReader* zReader)
- {
- char b;
- zReader->lese(&b, 1);
- if (b == 1)
- {
- int id;
- zReader->lese((char*)&id, 4);
- return new ActionTarget(id);
- }
- else if (b == 2)
- {
- Framework::Vec3<int> pos;
- Direction side;
- zReader->lese((char*)&pos.x, 4);
- zReader->lese((char*)&pos.y, 4);
- zReader->lese((char*)&pos.z, 4);
- zReader->lese((char*)&side, 4);
- return new ActionTarget(pos, side);
- }
- return 0;
- }
- Entity::Entity(
- int typeId, Framework::Vec3<float> location, int dimensionId, int entityId)
- : Inventory(location, dimensionId, true),
- chatSecurityLevel(0),
- lastChunkCenter(0, 0),
- lastSavedChunkCenter(Framework::Maybe<Framework::Punkt>::empty()),
- lastDimensionId(-1),
- speed(0, 0, 0),
- faceDir(1, 0, 0),
- target(0),
- typeId(typeId),
- removed(0),
- gravityMultiplier(1.f),
- jumpSpeed(0.f),
- id(entityId),
- placeBlockCooldown(0)
- {}
- void Entity::onDeath(Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill)
- {
- if (!removed)
- {
- for (DropConfig* config : zType()->getDropConfigs())
- {
- config->onObjectDestroyed(zActor, zUsedItem, zUsedSkill, this);
- }
- Dimension* dim = Game::INSTANCE->zDimension(dimensionId);
- if (dim)
- {
- Chunk* chunk = dim->zChunk(lastChunkCenter);
- if (chunk)
- {
- chunk->onEntityLeaves(this, 0);
- }
- dim->removeEntity(id);
- }
- removed = 1;
- }
- }
- bool Entity::useItem(int typeId, ItemStack* zStack, bool left)
- {
- if (left)
- {
- if (!zStack || !zStack->zItem() || zStack->zItem()->isUsable())
- {
- cs.lock();
- if (target)
- {
- ItemSkill* selected = zSkill(typeId);
- if (!selected)
- {
- selected = Game::INSTANCE->zItemType(typeId)
- ->createDefaultItemSkill();
- selected->setItemTypeId(typeId);
- if (selected) skills.add(selected);
- }
- if (!selected)
- {
- selected = zSkill(ItemTypeEnum::PLAYER_HAND);
- selected->setItemTypeId(ItemTypeEnum::PLAYER_HAND);
- }
- bool result = target->useItemSkillOnTarget(this,
- selected,
- !zStack || zStack->getSize() > 1 ? 0
- : (Item*)zStack->zItem());
- cs.unlock();
- return result;
- }
- cs.unlock();
- }
- else
- {
- useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
- }
- }
- else
- {
- if (zStack && zStack->zItem() && zStack->zItem()->isPlaceable()
- && zStack->getSize() > 0)
- { // place item
- cs.lock();
- if (target)
- {
- if (placeBlockCooldown <= 0)
- {
- Item* item = zStack->extractFromStack();
- bool result = target->placeBlock(this, item);
- if (item->getHp() > 0)
- {
- if (!zStack->addToStack(
- dynamic_cast<Item*>(item->getThis())))
- {
- ItemStack* newStack = new ItemStack(item, 1);
- addItems(newStack, NO_DIRECTION, 0);
- if (newStack->getSize())
- {
- Game::INSTANCE->spawnItem(
- location, dimensionId, newStack);
- }
- }
- else
- {
- item->release();
- }
- }
- else
- {
- item->release();
- }
- if (result)
- {
- placeBlockCooldown = 15;
- }
- cs.unlock();
- return result;
- }
- else
- {
- cs.unlock();
- return 0;
- }
- }
- cs.unlock();
- }
- if (zStack && zStack->zItem() && zStack->zItem()->isEatable()
- && zStack->getSize() > 0)
- { // eat item
- if (zStack->getSize() == 1)
- {
- return ((Item*)zStack->zItem())->applyFoodEffects(this);
- }
- else
- {
- if (zStack->zItem()->canApplyFoodEffectsFully(this))
- {
- Item* item = zStack->extractFromStack();
- item->applyFoodEffects(this);
- item->release();
- return 1;
- }
- }
- }
- if (!zStack || !zStack->zItem() || zStack->zItem()->isUsable())
- {
- cs.lock();
- if (target)
- {
- ItemSkill* selected = zSkill(typeId);
- if (!selected)
- {
- selected = Game::INSTANCE->zItemType(typeId)
- ->createDefaultItemSkill();
- selected->setItemTypeId(typeId);
- if (selected) skills.add(selected);
- }
- if (!selected)
- {
- selected = zSkill(ItemTypeEnum::PLAYER_HAND);
- selected->setItemTypeId(ItemTypeEnum::PLAYER_HAND);
- }
- bool result = target->interactItemSkillOnTarget(this,
- selected,
- !zStack || zStack->getSize() > 1 ? 0
- : (Item*)zStack->zItem());
- cs.unlock();
- return result;
- }
- cs.unlock();
- }
- else
- {
- useItem(ItemTypeEnum::PLAYER_HAND, 0, left);
- }
- }
- return 0;
- }
- void Entity::onTargetChange() {}
- bool Entity::interact(Item* zItem, Entity* zActor)
- {
- return false;
- }
- void Entity::addMovementFrame(MovementFrame& frame)
- {
- cs.lock();
- movements.add(frame);
- cs.unlock();
- Dimension* dim = Game::INSTANCE->zDimension(lastDimensionId);
- if (dim)
- {
- Chunk* chunk = dim->zChunk(lastChunkCenter);
- if (chunk)
- {
- NetworkMessage* message = new NetworkMessage();
- message->addressEntity(this);
- char* msg = new char[37];
- msg[0] = 0;
- *(float*)(msg + 1) = frame.direction.x;
- *(float*)(msg + 5) = frame.direction.y;
- *(float*)(msg + 9) = frame.direction.z;
- *(float*)(msg + 13) = frame.targetPosition.x;
- *(float*)(msg + 17) = frame.targetPosition.y;
- *(float*)(msg + 21) = frame.targetPosition.z;
- *(int*)(msg + 25) = frame.movementFlags;
- *(double*)(msg + 29) = frame.duration;
- message->setMessage(msg, 37);
- chunk->notifyObservers(message);
- }
- }
- faceDir = frame.direction;
- }
- void Entity::calculateTarget(Framework::Vec3<float> basePos,
- Framework::Vec3<float> direction,
- const Item* zItem)
- {
- Framework::Vec3<float> headPosition = basePos + faceOffset;
- int px = (int)floor(headPosition.x);
- int py = (int)floor(headPosition.y);
- int pz = (int)floor(headPosition.z);
- direction.normalize();
- Direction dir = BOTTOM;
- bool found = false;
- bool changed = false;
- while (true)
- {
- if (getDefaultBlock(
- Game::INSTANCE->zBlockAt(
- Framework::Vec3<int>{px, py, pz}, dimensionId, 0))
- ->isInteractable(zItem))
- {
- found = true;
- if (!target || !target->isBlock({px, py, pz}, dir))
- {
- changed = true;
- }
- break;
- }
- // collision to neighbor of current block
- if (direction.x > 0)
- {
- float xt = ((float)px + 1.f - headPosition.x) / direction.x;
- Framework::Vec3<float> tmp = headPosition + direction * xt;
- if (xt <= targetDistanceLimit && tmp.y >= (float)py
- && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
- && tmp.z < (float)pz + 1.f)
- {
- dir = WEST;
- px++;
- continue;
- }
- }
- if (direction.x < 0)
- {
- float xt = ((float)px - headPosition.x) / direction.x;
- Framework::Vec3<float> tmp = headPosition + direction * xt;
- if (xt <= targetDistanceLimit && tmp.y >= (float)py
- && tmp.y < (float)py + 1.f && tmp.z >= (float)pz
- && tmp.z < (float)pz + 1.f)
- {
- dir = EAST;
- px--;
- continue;
- }
- }
- if (direction.y > 0)
- {
- float yt = ((float)py + 1.f - headPosition.y) / direction.y;
- Framework::Vec3<float> tmp = headPosition + direction * yt;
- if (yt <= targetDistanceLimit && tmp.x >= (float)px
- && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
- && tmp.z < (float)pz + 1.f)
- {
- dir = NORTH;
- py++;
- continue;
- }
- }
- if (direction.y < 0)
- {
- float yt = ((float)py - headPosition.y) / direction.y;
- Framework::Vec3<float> tmp = headPosition + direction * yt;
- if (yt <= targetDistanceLimit && tmp.x >= (float)px
- && tmp.x < (float)px + 1.f && tmp.z >= (float)pz
- && tmp.z < (float)pz + 1.f)
- {
- dir = SOUTH;
- py--;
- continue;
- }
- }
- if (direction.z > 0)
- {
- float zt = ((float)pz + 1.f - headPosition.z) / direction.z;
- Framework::Vec3<float> tmp = headPosition + direction * zt;
- if (zt <= targetDistanceLimit && tmp.x >= (float)px
- && tmp.x < (float)px + 1.f && tmp.y >= (float)py
- && tmp.y < (float)py + 1.f)
- {
- dir = BOTTOM;
- pz++;
- continue;
- }
- }
- if (direction.z < 0)
- {
- float zt = ((float)pz - headPosition.z) / direction.z;
- Framework::Vec3<float> tmp = headPosition + direction * zt;
- if (zt <= targetDistanceLimit && tmp.x >= (float)px
- && tmp.x < (float)px + 1.f && tmp.y >= (float)py
- && tmp.y < (float)py + 1)
- {
- dir = TOP;
- pz--;
- continue;
- }
- }
- if (target)
- {
- changed = true;
- }
- break;
- }
- float distSq = Framework::Vec3<float>((float)px, (float)py, (float)pz)
- .abstandSq(headPosition);
- Entity* zte = Game::INSTANCE->zDimension(dimensionId)
- ->zTarget(headPosition, direction, distSq);
- if (zte)
- {
- if (!target || !target->isEntity(zte->getId()))
- {
- cs.lock();
- delete target;
- target = new ActionTarget(zte->getId());
- cs.unlock();
- onTargetChange();
- }
- }
- else if (changed)
- {
- if (target && !found)
- {
- cs.lock();
- delete target;
- target = 0;
- cs.unlock();
- onTargetChange();
- }
- else
- {
- cs.lock();
- delete target;
- target = new ActionTarget({px, py, pz}, dir);
- cs.unlock();
- onTargetChange();
- }
- }
- }
- void Entity::notifyStatusBarObservers(NetworkMessage* msg)
- {
- statusBarObservable.notifyObservers(msg);
- }
- ItemSkill* Entity::zSkill(int itemType)
- {
- for (ItemSkill* skill : skills)
- {
- if (skill->getItemTypeId() == itemType)
- {
- return skill;
- }
- }
- return 0;
- }
- void Entity::prepareTick(const Dimension* zDimension) {}
- void Entity::tick(const Dimension* zDimension)
- {
- if (removed) return;
- if (placeBlockCooldown > 0)
- {
- placeBlockCooldown--;
- }
- placeBlockCooldown--;
- if (time.isMeasuring())
- {
- time.messungEnde();
- if (movements.getEintragAnzahl() > 0)
- {
- MovementFrame currentFrame = movements.get(0);
- double seconds = time.getSekunden();
- while (seconds > 0)
- {
- if (currentFrame.duration <= 0)
- {
- cs.lock();
- movements.remove(0);
- cs.unlock();
- if (movements.getEintragAnzahl() > 0)
- currentFrame = movements.get(0);
- else
- break;
- }
- double t = MIN(currentFrame.duration, seconds);
- // TODO: add collision detection to reduce cheating capability
- location += (currentFrame.targetPosition - location)
- * (float)(t / currentFrame.duration);
- currentFrame.duration -= t;
- seconds -= t;
- if (currentFrame.duration <= 0)
- {
- location = currentFrame.targetPosition;
- }
- }
- if (currentFrame.duration > 0) movements.set(currentFrame, 0);
- if (getStamina() <= getMaxStamina() - 0.0025f)
- {
- if (getThirst() > 0 && getHunger() > 0)
- {
- setStamina(getStamina() + 0.0025f);
- setHunger(getHunger() - 0.0005f);
- setThirst(getThirst() - 0.0015f);
- }
- }
- }
- else
- {
- if (getStamina() <= getMaxStamina() - 0.005f)
- {
- if (getThirst() > 0 && getHunger() > 0)
- {
- setStamina(getStamina() + 0.005f);
- setHunger(getHunger() - 0.001f);
- setThirst(getThirst() - 0.003f);
- }
- }
- }
- }
- time.messungStart();
- Framework::Punkt chunkCenter
- = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
- if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
- {
- Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
- Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
- Chunk* zCurrentChunk
- = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
- Chunk* zLastChunk
- = lastDimension ? lastDimension->zChunk(lastChunkCenter) : 0;
- if (lastDimensionId != -1)
- {
- if (zLastChunk)
- {
- zLastChunk->onEntityLeaves(
- this, lastDimensionId == dimensionId ? zCurrentChunk : 0);
- }
- }
- if (zCurrentChunk)
- {
- zCurrentChunk->onEntityEnters(
- this, lastDimensionId == dimensionId ? zLastChunk : 0);
- }
- lastDimensionId = dimensionId;
- lastChunkCenter = chunkCenter;
- }
- }
- void Entity::api(Framework::StreamReader* zRequest,
- NetworkMessage* zResponse,
- Entity* zSource)
- {
- char type;
- zRequest->lese(&type, 1);
- switch (type)
- {
- case 0: // request status bar state
- {
- char len;
- zRequest->lese(&len, 1);
- char* guiId = new char[(int)len + 1];
- zRequest->lese(guiId, len);
- guiId[(int)len] = 0;
- int processor;
- zRequest->lese((char*)&processor, 4);
- zResponse->addressUIElement(guiId, processor);
- statusBarObservable.addObserver(zSource, guiId, processor);
- char* msg = new char[33];
- msg[0] = 0;
- *(float*)(msg + 1) = getMaxHP();
- *(float*)(msg + 5) = getCurrentHP();
- *(float*)(msg + 9) = getMaxStamina();
- *(float*)(msg + 13) = getStamina();
- *(float*)(msg + 17) = getMaxHunger();
- *(float*)(msg + 21) = getHunger();
- *(float*)(msg + 25) = getMaxThirst();
- *(float*)(msg + 29) = getThirst();
- zResponse->setMessage(msg, 33);
- delete[] guiId;
- break;
- }
- case 1: // remove status bar observer
- {
- char len;
- zRequest->lese(&len, 1);
- char* guiId = new char[(int)len + 1];
- zRequest->lese(guiId, len);
- guiId[(int)len] = 0;
- int processor;
- zRequest->lese((char*)&processor, 4);
- statusBarObservable.removeObserver(zSource, guiId, processor);
- delete[] guiId;
- break;
- }
- case 2: // TODO: component request
- break;
- }
- }
- void Entity::onFall(float collisionSpeed)
- {
- if (collisionSpeed > 10)
- {
- setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 10.f) / 2.5f);
- }
- }
- Framework::XML::Element* Entity::getTargetUIML() const
- {
- return new Framework::XML::Element(
- Framework::Text(
- "<targetInfo><text id=\"type\" width=\"auto\" height=\"auto\">")
- + Game::INSTANCE->zEntityType(typeId)->getName()
- + "</text></targetInfo>");
- }
- void Entity::setChatSecurityLevel(int level)
- {
- chatSecurityLevel = level;
- }
- void Entity::setPosition(Framework::Vec3<float> pos)
- {
- location = pos;
- }
- void Entity::takeDamage(
- Entity* zSource, Item* zUsedItem, ItemSkill* zUsedSkill, float damage)
- {
- currentHP -= damage;
- if (currentHP <= 0)
- {
- currentHP = 0;
- onDeath(zSource, zUsedItem, zUsedSkill);
- }
- }
- void Entity::setHP(
- Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp)
- {
- currentHP = MIN(MAX(hp, 0), maxHP);
- NetworkMessage* msg = new NetworkMessage();
- char* message = new char[9];
- message[0] = 1;
- *(float*)(message + 1) = getMaxHP();
- *(float*)(message + 5) = getCurrentHP();
- msg->setMessage(message, 9);
- notifyStatusBarObservers(msg);
- if (currentHP == 0)
- {
- onDeath(zActor, zUsedItem, zUsedSkill);
- }
- }
- void Entity::setStamina(float stamina)
- {
- this->stamina = MIN(MAX(stamina, 0), maxStamina);
- NetworkMessage* msg = new NetworkMessage();
- char* message = new char[9];
- message[0] = 2;
- *(float*)(message + 1) = getMaxStamina();
- *(float*)(message + 5) = getStamina();
- msg->setMessage(message, 9);
- notifyStatusBarObservers(msg);
- }
- void Entity::setHunger(float hunger)
- {
- this->hunger = MIN(MAX(hunger, 0), maxHunger);
- NetworkMessage* msg = new NetworkMessage();
- char* message = new char[9];
- message[0] = 3;
- *(float*)(message + 1) = getMaxHunger();
- *(float*)(message + 5) = getHunger();
- msg->setMessage(message, 9);
- notifyStatusBarObservers(msg);
- }
- void Entity::setThirst(float thirst)
- {
- this->thirst = MIN(MAX(thirst, 0), maxThirst);
- NetworkMessage* msg = new NetworkMessage();
- char* message = new char[9];
- message[0] = 4;
- *(float*)(message + 1) = getMaxThirst();
- *(float*)(message + 5) = getThirst();
- msg->setMessage(message, 9);
- notifyStatusBarObservers(msg);
- }
- void Entity::setGravityMultiplier(float multiplier)
- {
- gravityMultiplier = multiplier;
- }
- float Entity::getMaxHP() const
- {
- return maxHP;
- }
- float Entity::getCurrentHP() const
- {
- return currentHP;
- }
- float Entity::getStamina() const
- {
- return stamina;
- }
- float Entity::getMaxStamina() const
- {
- return maxStamina;
- }
- float Entity::getHunger() const
- {
- return hunger;
- }
- float Entity::getMaxHunger() const
- {
- return maxHunger;
- }
- float Entity::getThirst() const
- {
- return thirst;
- }
- float Entity::getMaxThirst() const
- {
- return maxThirst;
- }
- Framework::Vec3<float> Entity::getSpeed() const
- {
- return speed;
- }
- Framework::Vec3<float> Entity::getFaceDir() const
- {
- return faceDir;
- }
- Framework::Vec3<float> Entity::getPosition() const
- {
- return location;
- }
- float Entity::getGravityMultiplier() const
- {
- return gravityMultiplier;
- }
- float Entity::getJumpSpeed() const
- {
- return jumpSpeed;
- }
- void Entity::setJumpSpeed(float speed)
- {
- jumpSpeed = speed;
- }
- bool Entity::isRemoved() const
- {
- return removed;
- }
- const EntityType* Entity::zType() const
- {
- return Game::INSTANCE->zEntityType(typeId);
- }
- const ActionTarget* Entity::zTarget() const
- {
- return target;
- }
- int Entity::getId() const
- {
- return id;
- }
- bool Entity::hasDefaultModel() const
- {
- return 1;
- }
- ModelInfo* Entity::zSpecialModel() const
- {
- return 0;
- }
- float Entity::getMaxSpeed() const
- {
- return maxMovementSpeed;
- }
- bool Entity::isMoving() const
- {
- return movements.getEintragAnzahl() > 0;
- }
- int Entity::getChatSecurityLevel() const
- {
- return chatSecurityLevel;
- }
- Framework::Maybe<Framework::Punkt> Entity::getLastSavedChunkCenter() const
- {
- return lastSavedChunkCenter;
- }
- void Entity::setLastSavedChunkCenter(Framework::Punkt pos)
- {
- lastSavedChunkCenter = Framework::Maybe<Framework::Punkt>::of(pos);
- }
- void Entity::setRemoved()
- {
- removed = true;
- }
- double Entity::getHitDistance(
- Framework::Vec3<float> rayOrigin, Framework::Vec3<float> rayDirection) const
- {
- Framework::Mat4<float> rotMat
- = Framework::Mat4<float>::rotationX(-rotation.x)
- * Framework::Mat4<float>::rotationY(-rotation.y)
- * Framework::Mat4<float>::rotationZ(-rotation.z);
- Framework::Vec3<float> rotatedRayOrigin = rotMat * rayOrigin;
- Framework::Vec3<float> rotatedRayDirection = rotMat * rayDirection;
- rotatedRayDirection.normalize();
- if (rotatedRayDirection.x != 0)
- {
- float d;
- if (rotatedRayDirection.x > 0)
- {
- float border = getPosition().x - boundingBox.x;
- d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
- }
- else if (rotatedRayDirection.x < 0)
- {
- float border = getPosition().x + boundingBox.x;
- d = (border - rotatedRayOrigin.x) / rotatedRayDirection.x;
- }
- if (d > 0)
- {
- Framework::Vec3<float> hitPoint
- = rotatedRayOrigin + rotatedRayDirection * d;
- if (hitPoint.y >= getPosition().y - boundingBox.y
- && hitPoint.y <= getPosition().y + boundingBox.y
- && hitPoint.z >= getPosition().z - boundingBox.z
- && hitPoint.z <= getPosition().z + boundingBox.z)
- {
- return d;
- }
- }
- }
- if (rotatedRayDirection.y != 0)
- {
- float d;
- if (rotatedRayDirection.y > 0)
- {
- float border = getPosition().y - boundingBox.y;
- d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
- }
- else if (rotatedRayDirection.y < 0)
- {
- float border = getPosition().y + boundingBox.y;
- d = (border - rotatedRayOrigin.y) / rotatedRayDirection.y;
- }
- if (d > 0)
- {
- Framework::Vec3<float> hitPoint
- = rotatedRayOrigin + rotatedRayDirection * d;
- if (hitPoint.x >= getPosition().x - boundingBox.x
- && hitPoint.x <= getPosition().x + boundingBox.x
- && hitPoint.z >= getPosition().z - boundingBox.z
- && hitPoint.z <= getPosition().z + boundingBox.z)
- {
- return d;
- }
- }
- }
- if (rotatedRayDirection.z != 0)
- {
- float d;
- if (rotatedRayDirection.z > 0)
- {
- float border = getPosition().z - boundingBox.z;
- d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
- }
- else if (rotatedRayDirection.z < 0)
- {
- float border = getPosition().z + boundingBox.z;
- d = (border - rotatedRayOrigin.z) / rotatedRayDirection.z;
- }
- if (d > 0)
- {
- Framework::Vec3<float> hitPoint
- = rotatedRayOrigin + rotatedRayDirection * d;
- if (hitPoint.x >= getPosition().x - boundingBox.x
- && hitPoint.x <= getPosition().x + boundingBox.x
- && hitPoint.y >= getPosition().y - boundingBox.y
- && hitPoint.y <= getPosition().y + boundingBox.y)
- {
- return d;
- }
- }
- }
- return NAN;
- }
|