I understand, it's fine.
Ok, so, no, better doing everything in one state as you're doing, I'll help you.
So the first thing I notice is, your varset for var(13) will ALWAYS be 2, here is why:
Spoiler, click to toggle visibilty
ifelse(var(13)=0,1,(ifelse(var(13)=1,2,0))), is doing the following:
var(13) starts as 0, then it is set to 1, then it is set to 2, all of this in 1 tick, and will stay as 2 forever from now on. Read carefully my explanation about "ifelse" and read the code, you will understand.
Now try using this in your State 0:
;Stand
[Statedef 0]
type = S
physics = S
sprpriority = 0
[State 0, Anim]
type = changeanim
trigger1 = anim != var(10)
trigger1 = anim != 5
trigger2 = anim = 5 && !animtime
value = var(10)
You can delete everything else from state 0.
Although I think it will still have some issues but at least this will clean your code. Try it and tell me what happened.