Gridarta Editor
TreasureTreeNode.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.model.treasurelist;
21 
22 import java.util.List;
23 import javax.swing.tree.DefaultMutableTreeNode;
24 import javax.swing.tree.MutableTreeNode;
25 import javax.swing.tree.TreeNode;
26 import net.sf.japi.util.EnumerationIterator;
27 import org.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29 
35 public class TreasureTreeNode extends DefaultMutableTreeNode {
36 
40  private static final long serialVersionUID = 1L;
41 
45  @NotNull
46  private final TreasureObj content;
47 
52  public TreasureTreeNode(@NotNull final TreasureObj content) {
53  this.content = content;
54  }
55 
60  @NotNull
61  public MutableTreeNode getClone(final boolean processSecondLinking, @Nullable final List<TreasureTreeNode> needSecondLink) {
62  // clone this object
63  final TreasureTreeNode clone = new TreasureTreeNode(content);
64 
65  // also clone all children nodes and link them properly
66  for (final TreeNode treasureTreeNode : new EnumerationIterator<TreeNode>(children())) {
67  clone.add(((TreasureTreeNode) treasureTreeNode).getClone(processSecondLinking, needSecondLink));
68  }
69 
70  // if this is a list without children it will need second linking
71  if (!processSecondLinking && content.isTreasureList() && !content.getName().equalsIgnoreCase("NONE") && !content.hasLoop()) {
72  // this is a list, let's see if there are children
73  boolean hasChildren = false; // true when this list has real children
74  for (final TreeNode treasureTreeNode : new EnumerationIterator<TreeNode>(children())) {
75  if (((TreasureTreeNode) treasureTreeNode).content.isRealChild()) {
76  hasChildren = true; // found a real child
77  break;
78  }
79  }
80  if (!hasChildren) {
81  // this is a list with nothing but YES/NO in it, needs linking
82  assert needSecondLink != null;
83  needSecondLink.add(clone);
84  }
85  }
86 
87  return clone;
88  }
89 
93  @NotNull
94  @Override
95  public String toString() {
96  return content.toString();
97  }
98 
99  @NotNull
101  return content;
102  }
103 
108  public void recalculateChances() {
109  if (!(content instanceof TreasureListTreasureObj)) {
110  return;
111  }
112 
113  final TreasureListTreasureObj treasureListTreasureObj = (TreasureListTreasureObj) content;
114  if (treasureListTreasureObj.getListType() != TreasureListTreasureObjType.ONE) {
115  return;
116  }
117 
118  int sumChances = 0;
119 
120  // calculate the sum of all chances
121  for (final TreeNode treasureTreeNode : new EnumerationIterator<TreeNode>(children())) {
122  final TreasureObj treasureObj = ((TreasureTreeNode) treasureTreeNode).content;
123  sumChances += treasureObj.initChance();
124  }
125 
126  final double corrector = 100.0 / (double) sumChances;
127 
128  // now apply the correcting factor to all chances
129  for (final TreeNode treasureTreeNode : new EnumerationIterator<TreeNode>(children())) {
130  final TreasureObj treasureObj = ((TreasureTreeNode) treasureTreeNode).content;
131  treasureObj.correctChance(corrector);
132  }
133  }
134 
135 }
MutableTreeNode getClone(final boolean processSecondLinking, @Nullable final List< TreasureTreeNode > needSecondLink)
Return a new cloned instance of this object.
boolean hasLoop
Set if thi list contains itself.
String getName()
Returns the name of this treasure object.
static final long serialVersionUID
Serial Version UID.
final boolean isTreasureList
Whether this treasure object is a "treasure" or "treasureone" object.
TreasureListTreasureObjType getListType()
Returns the list type.
void recalculateChances()
Recalculate the chances of objects in a treasureone list.
TreasureTreeNode(@NotNull final TreasureObj content)
Construct tree node with specified content object.
void correctChance(final double corrector)
Updates the chance attribute by a corrector factor.
Subclass: Nodes in the CFTreasureListTree.
Subclass: UserObject (= content object) for nodes in the CFTreasureListTree These can be either treas...
int initChance()
Initializes the chance attribute.
final TreasureObj content
The treasure object of this node.