Skip to content

Commit

Permalink
Merge branch 'master' into 'master'
Browse files Browse the repository at this point in the history
An ability to specify attack type in controls.use value

See merge request OpenMW/openmw!4187
  • Loading branch information
psi29a committed Jul 5, 2024
2 parents 804b589 + 723c64a commit ef0bb02
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ message(STATUS "Configuring OpenMW...")
set(OPENMW_VERSION_MAJOR 0)
set(OPENMW_VERSION_MINOR 49)
set(OPENMW_VERSION_RELEASE 0)
set(OPENMW_LUA_API_REVISION 62)
set(OPENMW_LUA_API_REVISION 63)
set(OPENMW_POSTPROCESSING_API_REVISION 1)

set(OPENMW_VERSION_COMMITHASH "")
Expand Down
7 changes: 7 additions & 0 deletions apps/openmw/mwlua/localscripts.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "localscripts.hpp"

#include <components/esm3/loadcell.hpp>
#include <components/esm3/loadweap.hpp>
#include <components/misc/strings/lower.hpp>

#include "../mwbase/environment.hpp"
Expand All @@ -13,6 +14,7 @@
#include "../mwmechanics/aisequence.hpp"
#include "../mwmechanics/aitravel.hpp"
#include "../mwmechanics/aiwander.hpp"
#include "../mwmechanics/attacktype.hpp"
#include "../mwmechanics/creaturestats.hpp"
#include "../mwworld/class.hpp"
#include "../mwworld/ptr.hpp"
Expand Down Expand Up @@ -63,6 +65,11 @@ namespace MWLua
selfAPI["controls"] = sol::readonly_property([](SelfObject& self) { return &self.mControls; });
selfAPI["isActive"] = [](SelfObject& self) { return &self.mIsActive; };
selfAPI["enableAI"] = [](SelfObject& self, bool v) { self.mControls.mDisableAI = !v; };
selfAPI["ATTACK_TYPE"]
= LuaUtil::makeStrictReadOnly(context.mLua->tableFromPairs<std::string_view, MWMechanics::AttackType>(
{ { "NoAttack", MWMechanics::AttackType::NoAttack }, { "Any", MWMechanics::AttackType::Any },
{ "Chop", MWMechanics::AttackType::Chop }, { "Slash", MWMechanics::AttackType::Slash },
{ "Thrust", MWMechanics::AttackType::Thrust } }));

using AiPackage = MWMechanics::AiPackage;
sol::usertype<AiPackage> aiPackage = context.mLua->sol().new_usertype<AiPackage>("AiPackage");
Expand Down
24 changes: 23 additions & 1 deletion apps/openmw/mwmechanics/actors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "aifollow.hpp"
#include "aipursue.hpp"
#include "aiwander.hpp"
#include "attacktype.hpp"
#include "character.hpp"
#include "creaturestats.hpp"
#include "movement.hpp"
Expand Down Expand Up @@ -239,6 +240,23 @@ namespace MWMechanics

namespace
{
std::string_view attackTypeName(AttackType attackType)
{
switch (attackType)
{
case AttackType::NoAttack:
case AttackType::Any:
return {};
case AttackType::Chop:
return "chop";
case AttackType::Slash:
return "slash";
case AttackType::Thrust:
return "thrust";
}
throw std::logic_error("Invalid attack type value: " + std::to_string(static_cast<int>(attackType)));
}

float getTimeToDestination(const AiPackage& package, const osg::Vec3f& position, float speed, float duration,
const osg::Vec3f& halfExtents)
{
Expand Down Expand Up @@ -363,7 +381,11 @@ namespace MWMechanics
mov.mSpeedFactor = osg::Vec2(controls.mMovement, controls.mSideMovement).length();
stats.setMovementFlag(MWMechanics::CreatureStats::Flag_Run, controls.mRun);
stats.setMovementFlag(MWMechanics::CreatureStats::Flag_Sneak, controls.mSneak);
stats.setAttackingOrSpell((controls.mUse & 1) == 1);

AttackType attackType = static_cast<AttackType>(controls.mUse);
stats.setAttackingOrSpell(attackType != AttackType::NoAttack);
stats.setAttackType(attackTypeName(attackType));

controls.mChanged = false;
}
// For the player we don't need to copy these values to Lua because mwinput doesn't change them.
Expand Down
16 changes: 16 additions & 0 deletions apps/openmw/mwmechanics/attacktype.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef OPENMW_MWMECHANICS_ATTACKTYPE_H
#define OPENMW_MWMECHANICS_ATTACKTYPE_H

namespace MWMechanics
{
enum class AttackType
{
NoAttack,
Any,
Chop,
Slash,
Thrust
};
}

#endif
12 changes: 11 additions & 1 deletion apps/openmw/mwmechanics/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,12 @@ namespace MWMechanics
}
}
else if (aiInactive)
mAttackType = getRandomAttackType();
{
mAttackType = getDesiredAttackType();
if (mAttackType == "")
mAttackType = getRandomAttackType();
}

// else if (mPtr != getPlayer()) use mAttackType set by AiCombat
startKey = mAttackType + ' ' + startKey;
stopKey = mAttackType + " max attack";
Expand Down Expand Up @@ -3003,6 +3008,11 @@ namespace MWMechanics
return mPtr.getClass().getCreatureStats(mPtr).getAttackingOrSpell();
}

std::string_view CharacterController::getDesiredAttackType() const
{
return mPtr.getClass().getCreatureStats(mPtr).getAttackType();
}

void CharacterController::setActive(int active) const
{
mAnimation->setActive(active);
Expand Down
2 changes: 2 additions & 0 deletions apps/openmw/mwmechanics/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ namespace MWMechanics
bool getAttackingOrSpell() const;
void setAttackingOrSpell(bool attackingOrSpell) const;

std::string_view getDesiredAttackType() const;

void prepareHit();

public:
Expand Down
4 changes: 4 additions & 0 deletions apps/openmw/mwmechanics/creaturestats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ namespace MWMechanics
protected:
int mLevel;
bool mAttackingOrSpell;
std::string mAttackType;

public:
CreatureStats();
Expand Down Expand Up @@ -130,6 +131,7 @@ namespace MWMechanics
const MagicEffects& getMagicEffects() const;

bool getAttackingOrSpell() const { return mAttackingOrSpell; }
std::string_view getAttackType() const { return mAttackType; }

int getLevel() const;

Expand All @@ -156,6 +158,8 @@ namespace MWMechanics

void setAttackingOrSpell(bool attackingOrSpell) { mAttackingOrSpell = attackingOrSpell; }

void setAttackType(std::string_view attackType) { mAttackType = attackType; }

void setLevel(int level);

void setAiSetting(AiSetting index, Stat<int> value);
Expand Down
10 changes: 9 additions & 1 deletion files/lua_api/openmw/self.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,15 @@
-- @field [parent=#ActorControls] #boolean run true - run, false - walk
-- @field [parent=#ActorControls] #boolean sneak If true - sneak
-- @field [parent=#ActorControls] #boolean jump If true - initiate a jump
-- @field [parent=#ActorControls] #number use if 1 - activates the readied weapon/spell. For weapons, keeping at 1 will charge the attack until set to 0.
-- @field [parent=#ActorControls] #ATTACK_TYPE use Activates the readied weapon/spell according to a provided value. For weapons, keeping this value modified will charge the attack until set to @{#ATTACK_TYPE.NoAttack}. If an @{#ATTACK_TYPE} not appropriate for a currently equipped weapon provided - an appropriate @{#ATTACK_TYPE} will be used instead.

---
-- @type ATTACK_TYPE
-- @field #number NoAttack
-- @field #number Any
-- @field #number Chop
-- @field #number Swing
-- @field #number Thrust

---
-- Enables or disables standard AI (enabled by default).
Expand Down

0 comments on commit ef0bb02

Please sign in to comment.