name: Salamangreat
card_text: When your "Salamangreat" monster is targeted for an attack by an opponent's monster: You can send this card from your hand to the GY, then target those 2 monsters that would battle; gain LP equal to the ATK of that opponent's monster, also your targeted monster cannot be destroyed by battle this turn. When your "Salamangreat" monster destroys an opponent's monster by battle and sends it to the GY, while this card is in your GY: You can add this card to your hand. You can only use this effect of "Salamangreat Raccoon" once per turn.
external_ids: local SETCARD_ID_0 = 0x119 --Salamangreat
  
lua: local SETCARD_ID_0 = 0x119 --Salamangreat
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--recover
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_RECOVER)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_BE_BATTLE_TARGET)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCondition(s.reccon)
  	e1:SetCost(s.reccost)
  	e1:SetTarget(s.rectg)
  	e1:SetOperation(s.recop)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.reccon(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttackTarget()
  	return at:IsControler(tp) and at:IsFaceup() and at:IsSetCard(SETCARD_ID_0)
  end
  function s.reccost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGraveAsCost()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  function s.rectg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.GetAttacker():IsOnField() and Duel.GetAttacker():IsCanBeEffectTarget(e) and Duel.GetAttackTarget():IsOnField() and Duel.GetAttackTarget():IsCanBeEffectTarget(e)
  	end
  	local g = Group.FromCards(Duel.GetAttacker(), Duel.GetAttackTarget())
  	Duel.SetTargetCard(g)
  	e:SetLabelObject(Duel.GetAttacker())
  	Duel.SetOperationInfo(0, CATEGORY_RECOVER, nil, 0, 1 - tp, Duel.GetAttacker():GetAttack())
  end
  function s.recop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local hc = e:GetLabelObject()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc = g:GetFirst()
  	if tc == hc then
  		tc = g:GetNext()
  	end
  	if hc:IsFaceup() and hc:IsRelateToEffect(e) then
  		Duel.Recover(tp, hc:GetAttack(), REASON_EFFECT)
  	end
  	if tc:IsRelateToEffect(e) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  		e1:SetValue(1)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	local bc = tc:GetBattleTarget()
  	return eg:GetCount() == 1 and tc:IsLocation(LOCATION_GRAVE) and tc:IsReason(REASON_BATTLE) and bc:IsRelateToBattle() and bc:IsControler(tp) and bc:IsSetCard(SETCARD_ID_0)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  end
  
  
source_text: Salamangreat
  When your "Salamangreat" monster is targeted for an attack by an opponent's monster: You can send this card from your hand to the GY, then target those 2 monsters that would battle; gain LP equal to the ATK of that opponent's monster, also your targeted monster cannot be destroyed by battle this turn. When your "Salamangreat" monster destroys an opponent's monster by battle and sends it to the GY, while this card is in your GY: You can add this card to your hand. You can only use this effect of "Salamangreat Raccoon" once per turn.
target_text: -- Salamangreat
  local SETCARD_ID_0 = 0x119 --Salamangreat
  local s, id, o = GetID()
  function s.initial_effect(c)
  	--recover
  	local e1 = Effect.CreateEffect(c)
  	e1:SetDescription(aux.Stringid(id, 0))
  	e1:SetCategory(CATEGORY_RECOVER)
  	e1:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e1:SetCode(EVENT_BE_BATTLE_TARGET)
  	e1:SetProperty(EFFECT_FLAG_CARD_TARGET)
  	e1:SetRange(LOCATION_HAND)
  	e1:SetCondition(s.reccon)
  	e1:SetCost(s.reccost)
  	e1:SetTarget(s.rectg)
  	e1:SetOperation(s.recop)
  	c:RegisterEffect(e1)
  	--tohand
  	local e2 = Effect.CreateEffect(c)
  	e2:SetDescription(aux.Stringid(id, 1))
  	e2:SetCategory(CATEGORY_TOHAND)
  	e2:SetType(EFFECT_TYPE_FIELD + EFFECT_TYPE_TRIGGER_O)
  	e2:SetCode(EVENT_BATTLE_DESTROYED)
  	e2:SetRange(LOCATION_GRAVE)
  	e2:SetCountLimit(1, id)
  	e2:SetCondition(s.thcon)
  	e2:SetTarget(s.thtg)
  	e2:SetOperation(s.thop)
  	c:RegisterEffect(e2)
  end
  function s.reccon(e, tp, eg, ep, ev, re, r, rp)
  	local at = Duel.GetAttackTarget()
  	return at:IsControler(tp) and at:IsFaceup() and at:IsSetCard(SETCARD_ID_0)
  end
  function s.reccost(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToGraveAsCost()
  	end
  	Duel.SendtoGrave(e:GetHandler(), REASON_COST)
  end
  function s.rectg(e, tp, eg, ep, ev, re, r, rp, chk, chkc)
  	if chkc then
  		return false
  	end
  	if chk == 0 then
  		return Duel.GetAttacker():IsOnField() and Duel.GetAttacker():IsCanBeEffectTarget(e) and Duel.GetAttackTarget():IsOnField() and Duel.GetAttackTarget():IsCanBeEffectTarget(e)
  	end
  	local g = Group.FromCards(Duel.GetAttacker(), Duel.GetAttackTarget())
  	Duel.SetTargetCard(g)
  	e:SetLabelObject(Duel.GetAttacker())
  	Duel.SetOperationInfo(0, CATEGORY_RECOVER, nil, 0, 1 - tp, Duel.GetAttacker():GetAttack())
  end
  function s.recop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	local hc = e:GetLabelObject()
  	local g = Duel.GetChainInfo(0, CHAININFO_TARGET_CARDS)
  	local tc = g:GetFirst()
  	if tc == hc then
  		tc = g:GetNext()
  	end
  	if hc:IsFaceup() and hc:IsRelateToEffect(e) then
  		Duel.Recover(tp, hc:GetAttack(), REASON_EFFECT)
  	end
  	if tc:IsRelateToEffect(e) then
  		local e1 = Effect.CreateEffect(e:GetHandler())
  		e1:SetType(EFFECT_TYPE_SINGLE)
  		e1:SetCode(EFFECT_INDESTRUCTABLE_BATTLE)
  		e1:SetValue(1)
  		e1:SetReset(RESET_EVENT + RESETS_STANDARD + RESET_PHASE + PHASE_END)
  		tc:RegisterEffect(e1)
  	end
  end
  function s.thcon(e, tp, eg, ep, ev, re, r, rp)
  	local tc = eg:GetFirst()
  	local bc = tc:GetBattleTarget()
  	return eg:GetCount() == 1 and tc:IsLocation(LOCATION_GRAVE) and tc:IsReason(REASON_BATTLE) and bc:IsRelateToBattle() and bc:IsControler(tp) and bc:IsSetCard(SETCARD_ID_0)
  end
  function s.thtg(e, tp, eg, ep, ev, re, r, rp, chk)
  	if chk == 0 then
  		return e:GetHandler():IsAbleToHand()
  	end
  	Duel.SetOperationInfo(0, CATEGORY_TOHAND, e:GetHandler(), 1, 0, 0)
  end
  function s.thop(e, tp, eg, ep, ev, re, r, rp)
  	local c = e:GetHandler()
  	if c:IsRelateToEffect(e) then
  		Duel.SendtoHand(c, nil, REASON_EFFECT)
  	end
  end
  
  