ID:138887
 
Code:
(Spell)

turf
Enter(atom/a)
if(istype(a,/obj/projectile))
if(projectile_wall)
if(istype(a,/obj/projectile/spell/beam))return 0
else a:Done()
else .=..()
else .=..()

mob
var/list/spells=list()
player/proc
removeSustain()
if(sustain_mana)for(var/spell/Sp in spells)if(Sp.cast_sustain)
var/sustained/S=hasSustained(Sp.spell_ID)
if(S)
Sp.button.overlays-=image('icons/interface/button/hotbar.dmi',"Use",HUD_LAYER+0.02)
S.Done()
if(client)client.HUD.updateSustain(sustain_mana,calcManaMax())
spell
var
upgrade=0
obj/hud/spellbutton/button
button_state=""
purchased=0

tmp
cooldown=0
ready=1
channel_time=0
cast_time=0
can_queue=1
channel=0
busy_walk=1
busy_cast=1
cast_animate=""//cast_dir
spell_ID=""
Descr=""

cast_mana=0
cast_sustain=0

melee_dmg=0
spell_dmg=0

dot_dmg=0
dot_duration=0
dot_tick=0
dot_stack=0

tile_dmg=0
tile_duration=0
tile_tick=0
tile_stack=0

aoe_dmg=0
aoe_range=0

beam_knockback=0

proj_speed=0

chain_bounce=0
chain_loss=0

targeted=0

spell_range=0
dmg_color=""
New()
..()
buildButton()
proc
Fire(mob/player/m)
if(cast_sustain) //sustain spells take no mana to cancel
var/sustained/S=m.hasSustained(spell_ID)
if(S)
button.overlays-=image('icons/interface/button/hotbar.dmi',"Use",HUD_LAYER+0.02)
S.Done()
if(m.client)m.client.HUD.updateSustain(m.sustain_mana,m.calcManaMax())
return 1
else
if(m.mana>=cast_mana&&m.mana_max-(m.sustain_mana+cast_sustain)>=0)
m.sustain_mana+=cast_sustain
if(!targeted)m.useMana(cast_mana)
if(m.client)m.client.HUD.updateSustain(m.sustain_mana,m.calcManaMax())
button.overlays+=image('icons/interface/button/hotbar.dmi',"Use",HUD_LAYER+0.02)
Use(m)
return 1
else
if(targeted&&!m.lockon_target)return
if(m.mana>=cast_mana) //normal spell use
if(!targeted)m.useMana(cast_mana)
if(m.run)if(!m.spell_run)m.run=0
Use(m)
return 1
Use(mob/player/m)
Channel(mob/player/m)
Spell_Cooldown()
ready=0
if(cooldown)
var/image/I=image(button.icon,"Cooldown")
button.overlays+=I
spawn(cooldown)
ready=1
button.overlays-=I
else
sleep(1)
ready=1


CreatureCooldown()
ready=0
spawn(cooldown)ready=1

removeSpell(mob/player/m)
if(!m.isBusy())
if(ready)
if(cast_sustain)
var/sustained/S=m.hasSustained(spell_ID)
if(S)
S.Done()
if(m.client)m.client.HUD.updateSustain(m.sustain_mana,m.calcManaMax())
m.spells-=src
//button=null//garbage
return 1
else m.spells-=src
return 1

buildButton()
button=new/obj/hud/spellbutton
button.icon_state=button_state
button.name=button_state
//if(upgrade)button.underlays+=image(button.icon,"Upgrade")
//else
button.underlays+=image(button.icon,"Empty")
button.spell=src

updateButton(position,mob/player/m)
var/newpos=""
switch(position)
if(0)
newpos=""
if(m.HotBar1==src)m.HotBar1=null
if(m.HotBar2==src)m.HotBar2=null
if(m.HotBar3==src)m.HotBar3=null
if(m.HotBar4==src)m.HotBar4=null
if(m.HotBar5==src)m.HotBar5=null
if(m.HotBar6==src)m.HotBar6=null
if(m.HotBar7==src)m.HotBar7=null
if(m.HotBar8==src)m.HotBar8=null
if(m.HotBar9==src)m.HotBar9=null
if(m.HotBar10==src)m.HotBar10=null
if(m.HotBar11==src)m.HotBar11=null
if(m.HotBar12==src)m.HotBar12=null
if(1)
newpos="Center-6:16,SOUTH"
m.HotBar1=src
if(2)
newpos="Center-5:16,SOUTH"
m.HotBar2=src
if(3)
newpos="Center-4:16,SOUTH"
m.HotBar3=src
if(4)
newpos="Center-3:16,SOUTH"
m.HotBar4=src
if(5)
newpos="Center-2:16,SOUTH"
m.HotBar5=src
if(6)
newpos="Center-1:16,SOUTH"
m.HotBar6=src
if(7)
newpos="Center:16,SOUTH"
m.HotBar7=src
if(8)
newpos="Center+1:16,SOUTH"
m.HotBar8=src
if(9)
newpos="Center+2:16,SOUTH"
m.HotBar9=src
if(10)
newpos="Center+3:16,SOUTH"
m.HotBar10=src
if(11)
newpos="Center+4:16,SOUTH"
m.HotBar11=src
if(12)
newpos="Center+5:16,SOUTH"
m.HotBar12=src
if(button.screen_loc!=newpos)
for(var/spell/s in m.spells)
if(s.button.screen_loc==newpos)s.delScreen(m)
if(button.screen_loc)m.client.screen-=button
button.screen_loc=newpos
button.button=position
addScreen(m)

addScreen(mob/player/m)
m.client.screen+=button
m.client.HUD.spellbuttons+=button
delScreen(mob/player/m)
m.client.screen-=button
m.client.HUD.spellbuttons-=button
button.screen_loc=""
mob/player/proc
getButton(button,wipe)
var/X
switch(button)
if(1)
X=HotBar1
if(wipe)HotBar1=null
if(2)
X=HotBar2
if(wipe)HotBar2=null
if(3)
X=HotBar3
if(wipe)HotBar3=null
if(4)
X=HotBar4
if(wipe)HotBar4=null
if(5)
X=HotBar5
if(wipe)HotBar5=null
if(6)
X=HotBar6
if(wipe)HotBar6=null
if(7)
X=HotBar7
if(wipe)HotBar7=null
if(8)
X=HotBar8
if(wipe)HotBar8=null
if(9)
X=HotBar9
if(wipe)HotBar9=null
if(10)
X=HotBar10
if(wipe)HotBar10=null
if(11)
X=HotBar11
if(wipe)HotBar11=null
if(12)
X=HotBar12
if(wipe)HotBar12=null
return X
swapButton(pos1,pos2,spell/S)
if(pos2)
var/spell
S1=getButton(pos1,1)
S2=getButton(pos2,1)
if(S1)S1.updateButton(pos2,src)
if(S2)S2.updateButton(pos1,src)
else
if(S)
var/spell/S1=getButton(pos1,1)
if(S1)S1.updateButton(pos2,src)
S.updateButton(pos1,src)
return

useMana(take_mana)
//if(!infmana)
mana-=take_mana
if(client)client.HUD.updateMana(mana,mana_max)
Cast(spell/S)
if(cast)if(cast==S&&S.channel)
S.Use(src) //channeled spells need to cancel themselves
else
if(S.ready)
if(isBusy())Command_Queue_Add(S)
else
if(canCast())S.Fire(src)
else Command_Queue_Add(S)
else Command_Queue_Add(S)



mob/var/In=""
mob/player
proc
learnSpell(ID)
if(spell_points>=1)
if(!spells)spells=list()
var/spell/N
for(var/spell/S in spells)
if(S.button_state==ID)
N=S
if(!N)
N=newSpell(ID)
switch(alert("Are you sure you wish to purchase [ID]?","Purchase Spell","Yes","No"))
if("Yes")
spells+=N
addSpell2Hotbar(N)
spell_points--
SaveCharacter(ckey)
return 1
if("No")
for(var/obj/hud/talents/T)
if(T.selected==1)
T.selected=0
return

upgradeSpell(ID)
if(spell_points>=1)
if(!spells)spells=list()
var/spell/N
for(var/spell/S in spells)
if(S.button_state==ID&&!S.upgrade)
N=S
if(N)
switch(alert("Are you sure you wish to upgrade [ID]?","Upgrade Spell","Yes","No"))
if("Yes")
if(N.removeSpell(src))
N.updateButton(0,src)
N=newSpell("[ID]2")
spells+=N
addSpell2Hotbar(N)
spell_points--
SaveCharacter(ckey)
return 1
if("No")
for(var/obj/hud/talents/T)
if(T.selected==1)
T.selected=0
return
N.updateButton(0,src)

remSpellHotbar(spell/S)
for(var/spell/sa in spells)
if(HotBar1==sa)HotBar1=null

addSpell2Hotbar(spell/S)
if(!HotBar1)S.updateButton(1,src)
else
if(!HotBar2)S.updateButton(2,src)
else
if(!HotBar3)S.updateButton(3,src)
else
if(!HotBar4)S.updateButton(4,src)
else
if(!HotBar5)S.updateButton(5,src)
else
if(!HotBar6)S.updateButton(6,src)
else
if(!HotBar7)S.updateButton(7,src)
else
if(!HotBar8)S.updateButton(8,src)
else
if(!HotBar9)S.updateButton(9,src)
else
if(!HotBar10)S.updateButton(10,src)
else
if(!HotBar11)S.updateButton(11,src)
else if(!HotBar12)S.updateButton(12,src)
proc
newSpell(spell)switch(spell)
if("Fireball")return new/spell/Fireball
if("FireStream")return new/spell/FireStream
if("BlazingWave")return new/spell/BlazingWave
if("FlameShroud")return new/spell/FlameShroud
if("EarthBomb")return new/spell/EarthBomb
if("Spike")return new/spell/Spike
if("MudSlide")return new/spell/MudSlide
if("StoneFist")return new/spell/StoneFist
if("RockWall")return new/spell/RockWall
if("AirBlast")return new/spell/AirBlast
if("Flurry")return new/spell/Flurry
if("Whirlwind")return new/spell/Whirlwind
if("RazorWind")return new/spell/RazorWind
if("BubbleBullets")return new/spell/BubbleBullets
if("WaterWhip")return new/spell/WaterWhip
if("AquaBeam")return new/spell/AquaBeam
if("AquaticAegis")return new/spell/AquaticAegis
if("Smite")return new/spell/Smite
if("Benediction")return new/spell/Benediction
if("Heal")return new/spell/Heal
if("RighteousAura")return new/spell/RighteousAura
if("Torment")return new/spell/Torment
if("Corruption")return new/spell/Corruption
if("DrainLife")return new/spell/DrainLife
if("ShadowFlame")return new/spell/ShadowFlame
if("Shock")return new/spell/Shock
if("Bolt")return new/spell/Bolt
if("SparkBomb")return new/spell/SparkBomb
if("StaticCharge")return new/spell/StaticCharge


if("Fireball2")return new/spell/Fireball2
if("FireStream2")return new/spell/FireStream2
if("BlazingWave2")return new/spell/BlazingWave2
if("FlameShroud2")return new/spell/FlameShroud2
if("EarthBomb2")return new/spell/EarthBomb2
if("Spike2")return new/spell/Spike2
if("MudSlide2")return new/spell/MudSlide2
if("StoneFist2")return new/spell/StoneFist2
if("RockWall2")return new/spell/RockWall2
if("AirBlast2")return new/spell/AirBlast2
if("Flurry2")return new/spell/Flurry2
if("Whirlwind2")return new/spell/Whirlwind2
if("RazorWind2")return new/spell/RazorWind2
if("BubbleBullets2")return new/spell/BubbleBullets2
if("WaterWhip2")return new/spell/WaterWhip2
if("AquaBeam2")return new/spell/AquaBeam2
if("AquaticAegis2")return new/spell/AquaticAegis2
//if("Smite2")return new/spell/Smite2
//if("Benediction2")return new/spell/Benediction2
if("Heal2")return new/spell/Heal2
if("RighteousAura2")return new/spell/RighteousAura2
//if("Torment2")return new/spell/Torment2
//if("Corruption2")return new/spell/Corruption2
if("DrainLife2")return new/spell/DrainLife2
if("ShadowFlame2")return new/spell/ShadowFlame2
if("Shock2")return new/spell/Shock2
if("Bolt2")return new/spell/Bolt2
if("SparkBomb2")return new/spell/SparkBomb2
if("StaticCharge2")return new/spell/StaticCharge2

For spells when you click on them in the purchase menu

Click(location,control,params)
var/list/p=params2list(params)
if(p["right"])
var/spell/S
for(var/spell/sp in usr.spells)
if(sp.button_state==icon_state)
S=sp
break
if(S)usr.grabClick(S.button)
else usr.unClick()
else
if(p["left"])
if(usr.Clicking)usr.unClick()
else
if(selected==1) return
else
selected=1
usr.In = "[src.info]"
switch(alert("[usr.In]","Spell Description","Ok"))
if("Ok")
if(upgrade)
if(usr:upgradeSpell(icon_state))
overlays+=image(icon,"Use",layer=layer+0.01)
usr.client.HUD.updatePoints()
else
if(usr:learnSpell(icon_state))
overlays+=image(icon,"Use",layer=layer+0.01)
usr.client.HUD.updatePoints()
selected=0

How it saves and loads

F["spells"]<<spells

spells=list()
F["spells"]>>spells


Problem description: Everything works fine unless you purchase a skill, then logout and back in then try to buy the upgraded version. It then creates another copy on the hotbar instead of overwriting it. Any ideas what's causing this or what I can do to fix it? Sorry this is so long, I just wanted to post any code that might be relevant.

Remove the older one before adding new one
In response to Hassanjalil
That's the thing, if you upgrade the skill without reloging first it removes the old one and puts the new one in it's place. It's just when you buy a spell, logout, login and then upgrade that it bugs.
This:
proc/newSpell(spell)
var/path = text2path("new/spell/[spell]")
return new path


is the same as:
proc
newSpell(spell)switch(spell)
if("Fireball")return new/spell/Fireball
if("FireStream")return new/spell/FireStream
if("BlazingWave")return new/spell/BlazingWave
if("FlameShroud")return new/spell/FlameShroud
...
In response to Gooseheaded
I suggested the exact same thing to the original creator and he threw a hissy fit. Realistically, they should just be using the datum that they pulled spell_ID from and use new spell.type.