| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606 |
- #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),
- maxMovementSpeed(0.f),
- speed(0, 0, 0),
- faceDir(1, 0, 0),
- target(0),
- typeId(typeId),
- removed(0),
- gravityMultiplier(1.f),
- jumpSpeed(0.f),
- id(entityId),
- placeBlockCooldown(0.0),
- movementFlags(0),
- collisionMap(0),
- collisionMapLength(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 = 1.0;
- }
- 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::calculateTarget(const Item* zItem)
- {
- Framework::Vec3<float> headPosition = location + faceOffset;
- int px = (int)floor(headPosition.x);
- int py = (int)floor(headPosition.y);
- int pz = (int)floor(headPosition.z);
- Framework::Vec3<float> direction = faceDir;
- 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::calcBlockCollision(int& x, int& y, int& xl, int& yl)
- {
- Framework::Vec3<float> minXY = -boundingBox;
- Framework::Vec3<float> maxXY = boundingBox;
- Framework::Vec3<float> minXmaxY(minXY.x, maxXY.y, minXY.z);
- Framework::Vec3<float> maxXminY(maxXY.x, minXY.y, minXY.z);
- maxXY.z = minXY.z;
- minXY.rotateZ(rotation);
- maxXY.rotateZ(rotation);
- minXmaxY.rotateZ(rotation);
- maxXminY.rotateZ(rotation);
- minXY += location;
- maxXY += location;
- minXmaxY += location;
- maxXminY += location;
- Framework::Vec3<int> minXYI(
- (int)floor(minXY.x), (int)floor(minXY.y), (int)floor(minXY.z));
- Framework::Vec3<int> maxXYI(
- (int)floor(maxXY.x), (int)floor(maxXY.y), (int)floor(maxXY.z));
- Framework::Vec3<int> minXmaxYI(
- (int)floor(minXmaxY.x), (int)floor(minXmaxY.y), (int)floor(minXmaxY.z));
- Framework::Vec3<int> maxXminYI(
- (int)floor(maxXminY.x), (int)floor(maxXminY.y), (int)floor(maxXminY.z));
- x = minXYI.x;
- y = minXYI.y;
- if (x > minXmaxYI.x) x = minXmaxYI.x;
- if (x > maxXminYI.x) x = maxXminYI.x;
- if (x > maxXYI.x) x = maxXYI.x;
- if (y > minXmaxYI.y) y = minXmaxYI.y;
- if (y > maxXminYI.y) y = maxXminYI.y;
- if (y > maxXYI.y) y = maxXYI.y;
- xl = minXYI.x;
- if (xl < minXmaxYI.x) xl = minXmaxYI.x;
- if (xl < maxXminYI.x) xl = maxXminYI.x;
- if (xl < maxXYI.x) xl = maxXYI.x;
- yl = minXYI.y;
- if (yl < minXmaxYI.y) yl = minXmaxYI.y;
- if (yl < maxXminYI.y) yl = maxXminYI.y;
- if (yl < maxXYI.y) yl = maxXYI.y;
- xl = xl - x + 1;
- yl = yl - y + 1;
- if (collisionMapLength < xl * yl)
- {
- delete[] collisionMap;
- collisionMap = new bool[xl * yl];
- collisionMapLength = xl * yl;
- }
- memset(collisionMap, 0, xl * yl * sizeof(bool));
- int xi = minXYI.x - x;
- int yi = minXYI.y - y;
- collisionMap[xi + yi * xl] = 1;
- xi = maxXYI.x - x;
- yi = maxXYI.y - y;
- collisionMap[xi + yi * xl] = 1;
- xi = minXmaxYI.x - x;
- yi = minXmaxYI.y - y;
- collisionMap[xi + yi * xl] = 1;
- xi = maxXminYI.x - x;
- yi = maxXminYI.y - y;
- collisionMap[xi + yi * xl] = 1;
- auto dir = minXmaxY - minXY;
- float m = 0;
- if (abs(dir.x) >= abs(dir.y))
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.y / dir.x;
- }
- float yy = minXY.y;
- for (int xx = (int)floor(minXY.x); xx < (int)floor(minXmaxY.x); xx++)
- {
- int xi = xx - x;
- int yi = (int)floor(yy) - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- yy += m;
- }
- }
- else
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.x / dir.y;
- }
- float xx = minXY.x;
- for (int yy = (int)floor(minXY.y); yy < (int)floor(minXmaxY.y); yy++)
- {
- int xi = (int)floor(xx) - x;
- int yi = yy - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- xx += m;
- }
- }
- dir = maxXminY - minXY;
- m = 0;
- if (abs(dir.x) >= abs(dir.y))
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.y / dir.x;
- }
- float yy = minXY.y;
- for (int xx = (int)floor(minXY.x); xx < (int)floor(maxXminY.x); xx++)
- {
- int xi = xx - x;
- int yi = (int)floor(yy) - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- yy += m;
- }
- }
- else
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.x / dir.y;
- }
- float xx = minXY.x;
- for (int yy = (int)floor(minXY.y); yy < (int)floor(maxXminY.y); yy++)
- {
- int xi = (int)floor(xx) - x;
- int yi = yy - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- xx += m;
- }
- }
- dir = maxXY - maxXminY;
- m = 0;
- if (abs(dir.x) >= abs(dir.y))
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.y / dir.x;
- }
- float yy = maxXminY.y;
- for (int xx = (int)floor(maxXminY.x); xx < (int)floor(maxXY.x); xx++)
- {
- int xi = xx - x;
- int yi = (int)floor(yy) - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- yy += m;
- }
- }
- else
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.x / dir.y;
- }
- float xx = maxXminY.x;
- for (int yy = (int)floor(maxXminY.y); yy < (int)floor(maxXY.y); yy++)
- {
- int xi = (int)floor(xx) - x;
- int yi = yy - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- xx += m;
- }
- }
- dir = maxXY - minXmaxY;
- m = 0;
- if (abs(dir.x) >= abs(dir.y))
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.y / dir.x;
- }
- float yy = minXmaxY.y;
- for (int xx = (int)floor(minXmaxY.x); xx < (int)floor(maxXY.x); xx++)
- {
- int xi = xx - x;
- int yi = (int)floor(yy) - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- yy += m;
- }
- }
- else
- {
- if (dir.x != 0 && dir.y != 0)
- {
- m = dir.x / dir.y;
- }
- float xx = minXmaxY.x;
- for (int yy = (int)floor(minXmaxY.y); yy < (int)floor(maxXY.y); yy++)
- {
- int xi = (int)floor(xx) - x;
- int yi = yy - y;
- if (xi >= xl || yi >= yl)
- {
- continue;
- }
- collisionMap[xi + yi * xl] = 1;
- xx += m;
- }
- }
- for (int yy = 0; yy < yl; yy++)
- {
- int min = xl;
- int max = -1;
- for (int xx = 0; xx < xl; xx++)
- {
- if (collisionMap[xx + yy * xl])
- {
- if (xx < min) min = xx;
- if (xx > max) max = xx;
- }
- }
- for (int xx = min; xx <= max; xx++)
- {
- collisionMap[xx + yy * xl] = 1;
- }
- }
- }
- bool Entity::isCollidingWithBlock(const Dimension* zDimension)
- {
- int x, y, xl, yl;
- calcBlockCollision(x, y, xl, yl);
- bool result = isCollidingWithBlock(zDimension, x, y, xl, yl);
- return result;
- }
- bool Entity::isCollidingWithBlock(
- const Dimension* zDimension, int x, int y, int xl, int yl)
- {
- for (int yy = 0; yy < yl; yy++)
- {
- for (int xx = 0; xx < xl; xx++)
- {
- if (collisionMap[xx + yy * xl])
- {
- for (int zz = (int)(location.z - boundingBox.z);
- zz <= (int)(location.z + boundingBox.z);
- zz++)
- {
- const Block* block = zDimension->zBlockOrDefault(
- Framework::Vec3<int>(x + xx, y + yy, zz));
- if (!block->isPassable())
- {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- void Entity::prepareTick(const Dimension* zDimension, double seconds) {}
- void Entity::tick(const Dimension* zDimension, double seconds)
- {
- if (removed) return;
- if (placeBlockCooldown > 0)
- {
- placeBlockCooldown -= seconds;
- }
- bool changed = 0;
- float rotSpeed = 0.f;
- if (movementFlags & MovementFlags::ROTATE_LEFT)
- {
- rotSpeed += 3.f * (float)seconds;
- }
- if (movementFlags & MovementFlags::ROTATE_RIGHT)
- {
- rotSpeed -= 3.f * (float)seconds;
- }
- if (movementFlags & MovementFlags::ROTATE_TO_FACE)
- {
- float rot = Framework::Vec2<float>(0, -1).angle({faceDir.x, faceDir.y});
- rotSpeed = rot - rotation;
- while (rotSpeed >= 2.f * (float)PI)
- {
- rotSpeed -= 2.f * (float)PI;
- }
- while (rotSpeed <= -2.f * (float)PI)
- {
- rotSpeed += 2.f * (float)PI;
- }
- }
- if (rotSpeed != 0.f)
- {
- float last = rotation;
- rotation += rotSpeed;
- while (rotation >= 2.f * (float)PI)
- {
- rotation -= 2.f * (float)PI;
- }
- while (rotation <= -2.f * (float)PI)
- {
- rotation += 2.f * (float)PI;
- }
- if (last != rotation)
- {
- if (isCollidingWithBlock(zDimension))
- {
- rotation -= rotSpeed;
- }
- else
- {
- changed = 1;
- }
- }
- }
- Framework::Vec3<float> moveDir(0.f, 0.f, 0.f);
- if (movementFlags & MovementFlags::WALK_FORWARD)
- {
- moveDir.x += cosf(rotation - (float)PI / 2.f);
- moveDir.y += sinf(rotation - (float)PI / 2.f);
- }
- if (movementFlags & MovementFlags::WALK_BACKWARD)
- {
- moveDir.x -= cosf(rotation - (float)PI / 2.f);
- moveDir.y -= sinf(rotation - (float)PI / 2.f);
- }
- if (movementFlags & MovementFlags::WALK_LEFT)
- {
- moveDir.x += cosf(rotation + (float)PI);
- moveDir.y += sinf(rotation + (float)PI);
- }
- if (movementFlags & MovementFlags::WALK_RIGHT)
- {
- moveDir.x += cosf(rotation);
- moveDir.y += sinf(rotation);
- }
- if (movementFlags & MovementFlags::FLYING
- && movementFlags & MovementFlags::JUMPING)
- {
- moveDir.z += 1.f;
- movementFlags &= ~MovementFlags::GROUND_CONTACT;
- }
- if (movementFlags & MovementFlags::FLYING
- && movementFlags & MovementFlags::SNEAKING)
- {
- moveDir.z -= 1.f;
- }
- float staminaCost = 0.f;
- if (moveDir.x != 0.f || moveDir.y != 0.f)
- {
- moveDir.normalize();
- moveDir *= maxMovementSpeed * (float)seconds;
- if (movementFlags & MovementFlags::SPRINTING)
- {
- moveDir *= 2.f;
- }
- if (movementFlags & MovementFlags::SNEAKING
- && !(movementFlags & MovementFlags::FLYING))
- {
- moveDir *= 0.5f;
- }
- staminaCost = moveDir.getLength() / (maxMovementSpeed * 10);
- if (stamina < staminaCost)
- {
- staminaCost = stamina;
- moveDir *= stamina / staminaCost;
- }
- }
- if (!(movementFlags & MovementFlags::FLYING)
- && movementFlags & MovementFlags::JUMPING
- && movementFlags & MovementFlags::GROUND_CONTACT)
- {
- if (stamina > staminaCost + 0.02f)
- {
- staminaCost += 0.02f;
- speed.z += jumpSpeed;
- movementFlags &= ~MovementFlags::GROUND_CONTACT;
- }
- }
- if ((!(movementFlags & MovementFlags::FLYING)
- && (speed.x != 0.f || speed.y != 0.f || speed.z != 0.f
- || moveDir.x != 0.f || moveDir.y != 0.f || moveDir.z != 0.f))
- || !(movementFlags & MovementFlags::GROUND_CONTACT))
- {
- speed.z
- -= zDimension->getGravity() * gravityMultiplier * (float)seconds;
- }
- moveDir += speed * (float)seconds;
- bool staminaChanged = staminaCost != 0.f;
- stamina -= staminaCost;
- if (stamina <= maxStamina - (float)seconds)
- {
- if (getThirst() > 0 && getHunger() > 0)
- { // TODO: modify regen rate based on hunger/thirst
- stamina += (float)seconds;
- staminaChanged = 1;
- setHunger(hunger - (float)seconds / 5.f);
- setThirst(thirst - (float)seconds / 2.5f);
- }
- }
- if (staminaChanged)
- {
- setStamina(stamina); // notify observers
- }
- if (moveDir.x != 0.f || moveDir.y != 0.f || moveDir.z != 0.f)
- {
- while (abs(moveDir.x) > 1.f)
- {
- if (moveDir.x > 0.f)
- {
- location.x += 1.f;
- if (isCollidingWithBlock(zDimension))
- {
- moveDir.x = 0.f;
- speed.x = 0.f;
- location.x -= 1.f;
- }
- else
- {
- moveDir.x -= 1.f;
- changed = 1;
- }
- }
- else
- {
- location.x -= 1.f;
- if (isCollidingWithBlock(zDimension))
- {
- moveDir.x = 0.f;
- speed.x = 0.f;
- location.x += 1.f;
- }
- else
- {
- moveDir.x += 1.f;
- changed = 1;
- }
- }
- }
- if (moveDir.x != 0.f)
- {
- location.x += moveDir.x;
- if (isCollidingWithBlock(zDimension))
- {
- location.x -= moveDir.x;
- speed.x = 0.f;
- }
- else
- {
- changed = 1;
- }
- }
- while (abs(moveDir.y) > 1.f)
- {
- if (moveDir.y > 0.f)
- {
- location.y += 1.f;
- if (isCollidingWithBlock(zDimension))
- {
- moveDir.y = 0.f;
- location.y -= 1.f;
- speed.y = 0.f;
- }
- else
- {
- moveDir.y -= 1.f;
- changed = 1;
- }
- }
- else
- {
- location.y -= 1.f;
- if (isCollidingWithBlock(zDimension))
- {
- moveDir.y = 0.f;
- location.y += 1.f;
- speed.y = 0.f;
- }
- else
- {
- moveDir.y += 1.f;
- changed = 1;
- }
- }
- }
- if (moveDir.y != 0.f)
- {
- location.y += moveDir.y;
- if (isCollidingWithBlock(zDimension))
- {
- location.y -= moveDir.y;
- speed.y = 0.f;
- }
- else
- {
- changed = 1;
- }
- }
- while (abs(moveDir.z) > 1.f)
- {
- if (moveDir.z > 0.f)
- {
- location.z += 1.f;
- if (isCollidingWithBlock(zDimension))
- {
- moveDir.z = 0.f;
- location.z -= 1.f;
- speed.z = 0.f;
- }
- else
- {
- moveDir.z -= 1.f;
- changed = 1;
- }
- }
- else
- {
- location.z -= 1.f;
- if (isCollidingWithBlock(zDimension))
- {
- moveDir.z = 0.f;
- location.z += 1.f;
- onFall(speed.z);
- speed.z = 0.f;
- movementFlags |= MovementFlags::GROUND_CONTACT;
- }
- else
- {
- moveDir.z += 1.f;
- changed = 1;
- }
- }
- }
- if (moveDir.z != 0.f)
- {
- location.z += moveDir.z;
- if (isCollidingWithBlock(zDimension))
- {
- if (speed.z < 0.f)
- {
- onFall(speed.z);
- speed.z = 0.f;
- movementFlags |= MovementFlags::GROUND_CONTACT;
- }
- else
- {
- changed = 1;
- }
- location.z -= moveDir.z;
- }
- }
- }
- if (movementFlags & MovementFlags::GROUND_CONTACT)
- {
- if (speed.x != 0)
- {
- if (speed.x > (float)seconds)
- {
- speed.x -= (float)seconds;
- }
- else if (speed.x < -(float)seconds)
- {
- speed.x += (float)seconds;
- }
- else
- {
- speed.x = 0;
- }
- }
- if (speed.y != 0)
- {
- if (speed.y > (float)seconds)
- {
- speed.y -= (float)seconds;
- }
- else if (speed.y < -(float)seconds)
- {
- speed.y += (float)seconds;
- }
- else
- {
- speed.y = 0;
- }
- }
- }
- Framework::Punkt chunkCenter
- = Game::INSTANCE->getChunkCenter((int)location.x, (int)location.y);
- Chunk* zCurrentChunk = 0;
- if (dimensionId != lastDimensionId || chunkCenter != lastChunkCenter)
- {
- Dimension* lastDimension = Game::INSTANCE->zDimension(lastDimensionId);
- Dimension* currentDimension = Game::INSTANCE->zDimension(dimensionId);
- 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;
- }
- if (changed)
- {
- if (!zCurrentChunk)
- {
- Dimension* currentDimension
- = Game::INSTANCE->zDimension(dimensionId);
- zCurrentChunk
- = currentDimension ? currentDimension->zChunk(chunkCenter) : 0;
- }
- if (zCurrentChunk)
- {
- NetworkMessage* msg = new NetworkMessage();
- msg->sendEntityMovement(this, (float)seconds);
- zCurrentChunk->notifyObservers(msg);
- }
- }
- }
- 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 > 20)
- {
- setHP(this, 0, 0, getCurrentHP() - (collisionSpeed - 20.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 movementFlags > 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::Vec3<float> rotatedRayOrigin
- = Framework::Vec3<float>(rayOrigin).rotateZ(-rotation);
- Framework::Vec3<float> rotatedRayDirection
- = Framework::Vec3<float>(rayDirection).rotateZ(-rotation);
- 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;
- }
- float Entity::getRotation() const
- {
- return rotation;
- }
|