| 09-28-2004, 03:35 AM | #1 |
DnD RC3 is my latest release in the drag and drop project. It is also probably some of the hardest to understand code in Jass 2. I figure since I don't have a documentation included or an issues board or whatever. I'll use this thread to talk about problems and how my code implementation is. Now the triggers that mess with the bag are really quite ugly. I would say that if anything bars people from understanding how drag and drop works its the triggers. I hate them immensely and I can't really think of a coercive and clean way to do it. But for psuedo code sake here are the basic Ideas of how the triggers work in relation to the bag: Code:
State 0 is the normative state (Not in the bag) State -2 is the in the bag state. AN ORDER WAS ISSUED: A hero has a bag and he does an order on a bag: ----Is he trying to pick up another bag? -------Stop him, and give him an error message. ----Did he move the bag in his inventory? -------Record the new position of this bag. ----Did he just use the bag? -------Set the bags Safety Switch, swap the hero's inv, and open the bag A hero is in State 0 and he is trying to either move something (in his inv) or use something ----Is he moving the item onto the bag? -------Did adding the item to the bag pass? ------------Yes, remove the item! ------------No, remove the item, give him an error and remake it. A hero is in State 2 and he is trying to either move something or use something ----Is he trying to use an item with more than one charge? -------Set its life to 74 ----Is he trying to use an item with one charge? -------Set its life to 73 ----Did he hit next page? -------Show the next page. ----Did he hit cancel? -------Set the state to 74, clear the inv, swap the inv, set the page to 0, set state to 0, set bags hp to 75. ----Is he trying to move an item to any slot but 6? -------Reprint the current page. ----Is he trying to move an item on to cancel? -------Is his normative inventory count 6? ------------No. Remove the item from the bags list, remove in reality, Create a new item for the unit in the normative inv, set the charges on the cancel button up by one, and set the page back by one, and next page. -----------YES. Give him an error, redisplay the current page. AN ITEM WAS PICKED UP: Is the item in question a bag? ---Set it invul then ask some questions ---Is the unit picking it up a hero? Is his state 0 and does he not possess a bag? ------Yes,is the bag in question, not a bag object? -----------Yes. make it a bag. -------Set the bag on the hero object, record its slot. ------No,He's a hero, but he has a bag and his state is still 0 -----------Drop the new bag, and call him an idiot for trying to own two. ------No, he's not a hero, but he could be one and he isn't hidden. -----------Make him a hero. (the constructor will take care of the rest) AN ITEM WAS USED: The state is -2. ---Did he use the cancel button? -------No, the dropped items life is 73 ------------Remove the used item from the bag, redisplay the current page. -------No, the dropped items life is 74 ------------Set on the bags array the charges on the bag to itself -1. Set the items life to 75 AN ITEM WAS DROPPED: Is the guy dropping items not a Hero? ---Yeah, so lets not even bother proceeding The state is -2, is the dropped item life greater than 0? ---The dropped item wasn't cancel ------Remove it from the bags lists, wait a timer of zero, and redisplay the page. ---It was cancel ------Remove the cancel thing wait a timer of zero, and re add the cancel button. The state is 0, is the dropped items life 75 and the bag? ---Set the hero's bag to null. I also am not so happy with my bag in code. Obviously this can be some difficulty in understanding it from the jass point of view. So heres is a C-like langauge overview: Code:
Public Class Bag{
//Private vars to the class
private int m_Length;
private int m_LastUsedIx;
private int[] m_ItemIds;
private int[] m_Charges;
private int m_Page;
//Constructer
public Bag(int length) extends itemBag { //though in the real code you pass it an item.
super();//we keep pretending that we actually construct the bag.
m_Length=length;
m_ItemIds=new Int[m_Length];
m_Charges=new Int[m_Charges];
m_LastUsedIx=-1;
m_Page=0;
for(int i=0;i<m_Length;i++){
m_ItemIds[i]=-1;
m_Charges[i]=-1;
}
//Properties
public int Length(){ return m_LastUsedIx;}
public int getItemId(int index){ return m_ItemIds[index];}
public int GetCharge(int index){ return m_Charges[index];}
public int getPage(){return m_Page;}
public void setItemId(int index,int value){ m_ItemIds[index]=value;}
public void setCharge(int index,int value){ m_Charges[index]=value;}
public void setPage(int val){ m_Page=val;}
//Functions
public boolean Add(item toAdd){
if(m_LastUsedIx+1!=m_Length){
m_LastUsedIx++;
m_ItemIds[m_LastUsedIx]=toAdd.TypeId();
m_Charges[m_LastUsedIx]=toAdd.Charge();
super.m_Charges++; //increment the bags charge counter by one
return true;
}
return false;
}
public void Remove(int index)(
for(int i=index;i<m_Length-1;i++){
m_ItemIds[i]=m_ItemIds[i+1];
m_Charges[i]=m_Charges[i+1];
}
m_LastUsedIx--;
m_ItemIds[m_Length-1]=-1;
m_Charges[m_Length-1]=-1;
super.m_Charges--; //decrement the bags charge counter.
}
public boolean Exists(){returns true;}//neccessary for the way the code is written.
}For instance, as I wrote it out in C like code, I just realized that the m_LastUsedIx variable is totally unneccessary since it has the same value as the bag item's Charges-1. But there is more to it then that. I have two arrays of values. Logic dictates that I probably should just store one item array. But there are difficulties with that. I would have to do another rewrite of the underlying triggers. I would also not be able to merely clear the inventory between pages, I would have to drop and hide the items. It complicates the things but it does add ease of functionality and can make remove more flexible in that it returns the object it removed. I don't know. Anyway whats your thoughts. |
| 09-28-2004, 01:23 PM | #2 |
well, i tried to read it, but it was a slow process, because i have to go back to the header whenever i see anew function. and i wasnt as good at jass then i when i tried to decipher it. I'm also just getting used to the functionality of the return bug. I never really got through the header.... but yeah, this helps |
