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-2023 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 org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27 
33 public class TreasureTreeNode extends DefaultMutableTreeNode {
34 
38  private static final long serialVersionUID = 1L;
39 
43  @NotNull
44  private final TreasureObj content;
45 
50  public TreasureTreeNode(@NotNull final TreasureObj content) {
51  this.content = content;
52  }
53 
58  @NotNull
59  public MutableTreeNode getClone(final boolean processSecondLinking, @Nullable final List<TreasureTreeNode> needSecondLink) {
60  // clone this object
61  final TreasureTreeNode clone = new TreasureTreeNode(content);
62 
63  // also clone all children nodes and link them properly
64  for (int i = 0; i < getChildCount(); i++) {
65  final TreasureTreeNode treasureTreeNode = (TreasureTreeNode) getChildAt(i);
66  clone.add(treasureTreeNode.getClone(processSecondLinking, needSecondLink));
67  }
68 
69  // if this is a list without children it will need second linking
70  if (!processSecondLinking && content.isTreasureList() && !content.getName().equalsIgnoreCase("NONE") && !content.hasLoop()) {
71  // this is a list, let's see if there are children
72  boolean hasChildren = false; // true when this list has real children
73  for (int i = 0; i < getChildCount(); i++) {
74  final TreasureTreeNode treasureTreeNode = (TreasureTreeNode) getChildAt(i);
75  if (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 (int i = 0; i < getChildCount(); i++) {
122  final TreasureTreeNode treasureTreeNode = (TreasureTreeNode) getChildAt(i);
123  sumChances += treasureTreeNode.content.initChance();
124  }
125 
126  final double corrector = 100.0 / sumChances;
127 
128  // now apply the correcting factor to all chances
129  for (int i = 0; i < getChildCount(); i++) {
130  final TreasureTreeNode treasureTreeNode = (TreasureTreeNode) getChildAt(i);
131  treasureTreeNode.content.correctChance(corrector);
132  }
133  }
134 
135 }
net.sf.gridarta.model.treasurelist.TreasureObj.toString
String toString()
Definition: TreasureObj.java:104
net.sf.gridarta.model.treasurelist.TreasureTreeNode
Subclass: Nodes in the CFTreasureListTree.
Definition: TreasureTreeNode.java:33
net.sf.gridarta.model.treasurelist.TreasureTreeNode.content
final TreasureObj content
The treasure object of this node.
Definition: TreasureTreeNode.java:44
net.sf.gridarta.model.treasurelist.TreasureObj.getName
String getName()
Returns the name of this treasure object.
Definition: TreasureObj.java:225
net.sf.gridarta.model.treasurelist.TreasureListTreasureObj
A TreasureObj representing a "treasure" entry.
Definition: TreasureListTreasureObj.java:28
net.sf.gridarta.model.treasurelist.TreasureObj
Subclass: UserObject (= content object) for nodes in the CFTreasureListTree These can be either treas...
Definition: TreasureObj.java:32
net.sf.gridarta.model.treasurelist.TreasureObj.isRealChild
final boolean isRealChild
Set for all except yes or no nodes.
Definition: TreasureObj.java:61
net.sf.gridarta.model.treasurelist.TreasureObj.hasLoop
boolean hasLoop
Set if thi list contains itself.
Definition: TreasureObj.java:84
net.sf.gridarta.model.treasurelist.TreasureListTreasureObjType.ONE
ONE
A "treasureone" list.
Definition: TreasureListTreasureObjType.java:36
net.sf.gridarta.model.treasurelist.TreasureTreeNode.getClone
MutableTreeNode getClone(final boolean processSecondLinking, @Nullable final List< TreasureTreeNode > needSecondLink)
Return a new cloned instance of this object.
Definition: TreasureTreeNode.java:59
net.sf.gridarta.model.treasurelist.TreasureTreeNode.recalculateChances
void recalculateChances()
Recalculate the chances of objects in a treasureone list.
Definition: TreasureTreeNode.java:108
net.sf.gridarta.model.treasurelist.TreasureTreeNode.toString
String toString()
@noinspection RefusedBequest
Definition: TreasureTreeNode.java:95
net.sf.gridarta.model.treasurelist.TreasureListTreasureObj.getListType
TreasureListTreasureObjType getListType()
Returns the list type.
Definition: TreasureListTreasureObj.java:57
net.sf.gridarta.model.treasurelist.TreasureTreeNode.serialVersionUID
static final long serialVersionUID
Serial Version UID.
Definition: TreasureTreeNode.java:38
net.sf.gridarta.model.treasurelist.TreasureTreeNode.TreasureTreeNode
TreasureTreeNode(@NotNull final TreasureObj content)
Construct tree node with specified content object.
Definition: TreasureTreeNode.java:50
net.sf.gridarta.model.treasurelist.TreasureListTreasureObjType
Type of TreasureListTreasureObj instances.
Definition: TreasureListTreasureObjType.java:26
net.sf.gridarta.model.treasurelist.TreasureObj.isTreasureList
final boolean isTreasureList
Whether this treasure object is a "treasure" or "treasureone" object.
Definition: TreasureObj.java:55
net.sf.gridarta.model.treasurelist.TreasureObj.correctChance
void correctChance(final double corrector)
Updates the chance attribute by a corrector factor.
Definition: TreasureObj.java:158
net.sf.gridarta.model.treasurelist.TreasureTreeNode.getTreasureObj
TreasureObj getTreasureObj()
Definition: TreasureTreeNode.java:100
net.sf.gridarta.model.treasurelist.TreasureObj.initChance
int initChance()
Initializes the chance attribute.
Definition: TreasureObj.java:147