Board index
Programs with AI or Rules Enforcement
Magic: The Gathering - Duels of the Planeswalkers
Programming Talk



Functions for mana cost retrieval
Moderator: CCGHQ Admins
7 posts
• Page 1 of 1
Functions for mana cost retrieval
by thefiremind » 18 Jul 2012, 19:27
I just inserted the following functions in my GENERAL_FUNCTIONS.LOL file:
As I wrote in the file, I think they could generate wrong results with hybrid or phyrexian mana. You must also be careful not to use those functions while you are setting another filter, otherwise you would clear it and screw what you are doing (same as CountCardsInHand and similar).
- Code: Select all
ManaCost = function(object, colour)
-- returns the amount of mana of the selected colour in object's cost
local filter = Object():GetFilter()
filter:Clear()
filter:NotTargetted()
filter:SetCardInstance(object)
return filter:ChromaCount(colour)
end
ManaCostString = function(object, colour)
-- same as ManaCost, but it generates a string (useful for TapLand, CanAfford, etc.)
local amount = ManaCost(object, colour)
if amount > 0 then
local colour_table = {"{W}", "{U}", "{B}", "{R}", "{G}"}
local string = ""
for i=1,amount do
string = string..colour_table[colour]
end
return string
else
return "{0}"
end
end
FullManaCostString = function(object)
-- returns the full mana cost string (I think it won't work properly with hybrid or phyrexian mana, for obvious reasons)
local cmc = object:GetConvertedManaCost()
if cmc > 0 then
local string = ""
local coloured_mana = 0
for i=1,5 do
local amount = ManaCost(object, i)
coloured_mana = coloured_mana + amount
if amount > 0 then
string = string..ManaCostString(object, i)
end
end
if cmc > coloured_mana then
string = "{"..(cmc-coloured_mana).."}"..string
end
return string
else
return "{0}"
end
end
As I wrote in the file, I think they could generate wrong results with hybrid or phyrexian mana. You must also be careful not to use those functions while you are setting another filter, otherwise you would clear it and screw what you are doing (same as CountCardsInHand and similar).
< Former DotP 2012/2013/2014 modder >
Currently busy with life...
Currently busy with life...
-
thefiremind - Programmer
- Posts: 3515
- Joined: 07 Nov 2011, 10:55
- Has thanked: 118 times
- Been thanked: 722 times
Re: Functions for mana cost retrieval
by sadlyblue » 19 Jul 2012, 22:16
I'm thinking it could be useful for Snapcaster Mage, also.
Re: Functions for mana cost retrieval
by thefiremind » 31 Jul 2012, 21:03
I expanded my collection of functions related to mana costs, edited some function names so that they are more coherent, and moved the functions to a separate MANA_FUNCTIONS.LOL file. This is the result:
).
Example: the good old Crimson Hellkite:
- Code: Select all
ColourToString = function(colour)
-- converts colour constants (i.e. COLOUR_RED) to mana strings (i.e. "{R}")
local mana_table = {"{W}", "{U}", "{B}", "{R}", "{G}"}
return mana_table[colour]
end
ManaString = function(colour, amount)
-- generates a mana string of the selected colour and amount
local string = ""
if amount == 0 then
return "{0}"
end
for i=1,amount do
string = string..ColourToString(colour)
end
return string
end
GetCostOfColour = function(object, colour)
-- returns the amount of mana of the selected colour in object's cost
local filter = Object():GetFilter()
filter:Clear()
filter:NotTargetted()
filter:SetCardInstance(object)
return filter:ChromaCount(colour)
end
GetCostStringOfColour = function(object, colour)
-- same as GetCostOfColour, but it generates a string (useful for TapLand, CanAfford, etc.)
local amount = GetCostOfColour(object, colour)
return ManaString(colour, amount)
end
GetCostString = function(object)
-- returns the full mana cost string but it doesn't work with hybrid or phyrexian mana, for obvious reasons
local cmc = object:GetConvertedManaCost()
if cmc > 0 then
local string = ""
local coloured_mana = 0
for i=1,5 do
local amount = GetCostOfColour(object, i)
coloured_mana = coloured_mana + amount
if amount > 0 then
string = string..GetCostStringOfColour(object, i)
end
end
if cmc > coloured_mana then
string = "{"..(cmc-coloured_mana).."}"..string
end
return string
else
return "{0}"
end
end
GetTotalManaOfColour = function(player, colour)
-- returns the maximum amount of mana of the selected colour that player can afford
local total = player:GetTotalMana()
local string = ""
for i=0,total do
string = string..ColourToString(colour)
if player:CanAfford(string) == 0 then
return i
end
end
return 0
end

Example: the good old Crimson Hellkite:
- Code: Select all
<ACTIVATED_ABILITY filter_zone="ZONE_IN_PLAY">
<COST type="TapSelf" />
<TARGET_DEFINITION id="0">
local filter = Object():GetFilter()
filter:Clear()
filter:AddCardType( CARD_TYPE_CREATURE )
filter:SetZone( ZONE_IN_PLAY )
filter:SetHint( HINT_ENEMY_ONLY, EffectController() )
</TARGET_DEFINITION>
<TARGET_DETERMINATION>
return AtLeastOneTargetFromDefinition(0)
</TARGET_DETERMINATION>
<PLAY_TIME_ACTION>
local player = EffectController()
local max_mana = GetTotalManaOfColour(player, COLOUR_RED)
player:BeginNewNumericalChoice()
player:AddNumericalChoiceAnswer(max_mana)
player:AskNumericalChoiceQuestion("NUM_LANDS")
</PLAY_TIME_ACTION>
<PLAY_TIME_ACTION>
EffectDC():Set_Int( 1, Object():GetNumericalChoiceResult() )
</PLAY_TIME_ACTION>
<PLAY_TIME_ACTION target_choosing="1">
local damage = EffectDC():Get_Int(1)
EffectController():SetCustomQueryInstructionValue(damage)
EffectController():ChooseTarget( 0, "CARD_QUERY_CHOOSE_DEAL_DAMAGE_AMOUNT", EffectDC():Make_Targets(0) )
</PLAY_TIME_ACTION>
<PLAY_TIME_ACTION>
local damage = EffectDC():Get_Int(1)
local mana_string = ManaString(COLOUR_RED, damage)
local player = EffectController()
if player:CanAfford(mana_string) then
player:TapLand(mana_string)
end
</PLAY_TIME_ACTION>
<RESOLUTION_TIME_ACTION>
local damage = EffectDC():Get_Int(1)
local target_creature = EffectDC():Get_Targets(0):Get_CardPtr(0)
local source = EffectSource()
if source == nil then
source = Object()
end
if target_creature ~= nil then
target_creature:DealDamage(damage, source)
end
</RESOLUTION_TIME_ACTION>
<SFX text="TARGET_FIREBALL_PLAY" />
<AI_AVAILABILITY type="in_response" />
<AI_AVAILABILITY step="begin_combat" turn="their_turn" />
<AI_AVAILABILITY step="main_1" turn="my_turn" />
<AI_AVAILABILITY step="declare_attackers" turn="their_turn" />
<AI_AVAILABILITY step="declare_blockers" />
<AI_AVAILABILITY step="end_of_turn" turn="their_turn" />
</ACTIVATED_ABILITY>
< Former DotP 2012/2013/2014 modder >
Currently busy with life...
Currently busy with life...
-
thefiremind - Programmer
- Posts: 3515
- Joined: 07 Nov 2011, 10:55
- Has thanked: 118 times
- Been thanked: 722 times
Re: Functions for mana cost retrieval
by thefiremind » 13 Aug 2012, 18:29
Why do you always make riddles? You should use a sphinx as avatar...nabeshin wrote:violation of the game rules

Which rules are you talking about?
< Former DotP 2012/2013/2014 modder >
Currently busy with life...
Currently busy with life...
-
thefiremind - Programmer
- Posts: 3515
- Joined: 07 Nov 2011, 10:55
- Has thanked: 118 times
- Been thanked: 722 times
Re: Functions for mana cost retrieval
by nabeshin » 13 Aug 2012, 18:35
I didn't see
in your code ( excuse me, I not riddler, just mood bad )

Re: Functions for mana cost retrieval
by thefiremind » 13 Aug 2012, 18:43
If I need to select only one type of mana I can't usenabeshin wrote:I didn't seein your code

EDIT: I think I finally solved the riddle... ehm... understood what you mean.




< Former DotP 2012/2013/2014 modder >
Currently busy with life...
Currently busy with life...
-
thefiremind - Programmer
- Posts: 3515
- Joined: 07 Nov 2011, 10:55
- Has thanked: 118 times
- Been thanked: 722 times
7 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 11 guests