Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New methods to handle summon creatures #4814

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ramon-bernardo
Copy link
Contributor

@ramon-bernardo ramon-bernardo commented Oct 24, 2024

Pull Request Prelude

Introduced methods to enhance the Creature class, allowing for better handling of creature ownership and master relationships.

Changes Proposed

  • Removed the static methods Combat::isPlayerCombat and Player::lastHitIsPlayer.
  • New getter methods for retrieving the respective owners: getPlayerOwned(), getNpcOwned() and getMonsterOwned(). Methods to determine if the creature is owned, either directly or through its master.
  • Methods like isPlayerSummon(), isNpcSummon() and isMonsterSummon() to check the type of creature master.

Usage

c++

// Check if the creature is a summon.
if (creature->isSummon()) {
    std::cout << "This creature is a summon." << std::endl;
}

// Retrieve the player that owns this creature.
if (Player* owner = creature->getPlayerOwned()) {
    std::cout << "This creature is owned by player: " << owner->getName() << std::endl;
}

lua

if creature:isSummon() then
    print("This creature is a summon.")
end

local owner = creature:getPlayerOwned()
if owner then
    print("This creature is owned by player: " .. owner:getName())
end

@ghost ghost requested a review from ranisalt October 24, 2024 00:47
@ramon-bernardo ramon-bernardo changed the title New methods to handle creatures with a master New methods to handle summon creatures Oct 24, 2024
src/combat.cpp Fixed Show fixed Hide fixed
src/creature.cpp Fixed Show fixed Hide fixed
src/monster.cpp Fixed Show fixed Hide fixed
src/player.cpp Fixed Show fixed Hide fixed
src/tile.cpp Fixed Show fixed Hide fixed
@MillhioreBT
Copy link
Contributor

These changes are supposed to clean up the code and make it more readable, but it is quite the opposite, in fact it adds 200 more lines of code that are not necessary. (Especially in C++), in Lua it would be as easy as making a function that calls another and that's it and it would be acceptable

Basically what we would be avoiding with these changes is simply not checking null or nil of the master and then knowing if the master is one type or another.

I think everyone agrees that it is nicer to write it as you would normally do:

local master = creature:getMaster()

if not master then
	return
end

if master:isPlayer() then
	return
end
local master = creature:getMaster()
if master and master:isPlayer() then
	return
end

or in any case if we wish we can create our custom methods

function Creature.getPlayerOwned(self)
	local owner = self:getMaster()
	if owner and owner:isPlayer() then
		return owner
	end
end

@ramon-bernardo
Copy link
Contributor Author

@MillhioreBT

Perhaps we could improve the function names; 'Owned' seems confusing at first. The validation appears to check if something is 'controlled by a player,' whether it's directly a player or a creature summoned by.

LOC here doesn't equate to quality, as it's luascript (15k LOL)

@ramon-bernardo
Copy link
Contributor Author

However, I agree that much of luascript could be impl in lua (Creature)

@ArturKnopik ArturKnopik added the enhancement Increase or improvement in quality, value, or extent label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Increase or improvement in quality, value, or extent
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants