Changeset 3b415b5340c87a9f5080e64474a65dc3b6011b1a
- Timestamp:
- 06/10/07 17:24:25 (1 year ago)
- git-parent:
- Files:
-
- src/net/driftingsouls/ds2/server/ships/Ship.java (modified) (9 diffs)
- src/net/driftingsouls/ds2/server/ships/Ships.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/net/driftingsouls/ds2/server/ships/Ship.java
r4e34802 r3b415b5 21 21 import java.sql.Blob; 22 22 import java.util.ArrayList; 23 import java.util.Iterator; 23 24 import java.util.List; 24 25 … … 26 27 import javax.persistence.Entity; 27 28 import javax.persistence.FetchType; 29 import javax.persistence.GeneratedValue; 28 30 import javax.persistence.Id; 29 31 import javax.persistence.JoinColumn; … … 31 33 import javax.persistence.Table; 32 34 35 import net.driftingsouls.ds2.server.ContextCommon; 33 36 import net.driftingsouls.ds2.server.Offizier; 34 37 import net.driftingsouls.ds2.server.cargo.Cargo; … … 37 40 import net.driftingsouls.ds2.server.entities.User; 38 41 import net.driftingsouls.ds2.server.framework.Common; 42 import net.driftingsouls.ds2.server.framework.Configuration; 39 43 import net.driftingsouls.ds2.server.framework.ContextMap; 40 44 import net.driftingsouls.ds2.server.framework.Loggable; 41 45 import net.driftingsouls.ds2.server.framework.db.Database; 46 import net.driftingsouls.ds2.server.framework.db.SQLQuery; 42 47 import net.driftingsouls.ds2.server.framework.db.SQLResultRow; 48 import net.driftingsouls.ds2.server.tasks.Taskmanager; 43 49 44 50 import org.apache.commons.lang.StringUtils; 51 import org.apache.commons.lang.math.RandomUtils; 45 52 import org.hibernate.annotations.Generated; 46 53 import org.hibernate.annotations.GenerationTime; … … 55 62 @Table(name="ships") 56 63 public class Ship implements Loggable { 57 @Id 58 @Generated(GenerationTime.INSERT) 64 @Id @GeneratedValue 59 65 private int id; 60 66 @ManyToOne(fetch=FetchType.LAZY) … … 99 105 @Column(name="`lock`") 100 106 private String lock; 101 private Shortvisibility;107 private Integer visibility; 102 108 private String onmove; 103 109 private Byte respawn; … … 722 728 * @return Die Sichtbarkeitsdaten des Schiffes 723 729 */ 724 public ShortgetVisibility() {730 public Integer getVisibility() { 725 731 return visibility; 726 732 } … … 730 736 * @param visibility Die neuen Sichtbarkeitsdaten 731 737 */ 732 public void setVisibility( Shortvisibility) {738 public void setVisibility(Integer visibility) { 733 739 this.visibility = visibility; 734 740 } … … 853 859 854 860 /** 861 * Generiert ein Truemmerteil mit Loot fuer das Schiff unter Beruecksichtigung desjenigen, 862 * der es zerstoert hat. Wenn fuer das Schiff kein Loot existiert oder keiner generiert wurde (Zufall spielt eine 863 * Rolle!), dann wird kein Truemmerteil erzeugt. 864 * @param destroyer Die ID des Spielers, der es zerstoert hat 865 */ 866 public void generateLoot( int destroyer ) { 867 Database database = ContextMap.getContext().getDatabase(); 868 org.hibernate.Session db = ContextMap.getContext().getDB(); 869 870 ShipTypeData shiptype = this.getTypeData(); 871 872 int rnd = RandomUtils.nextInt(101); 873 874 // Gibts was zu looten? 875 if( rnd > shiptype.getChance4Loot() ) { 876 return; 877 } 878 879 // History analysieren (Alle Schiffe die erst kuerzlich uebergeben wurden, haben kein Loot) 880 // TODO: Das funktioniert im Moment nur, weil im Log nur Uebergaben stehen... 881 String[] history = StringUtils.split(this.history.trim(), '\n'); 882 if( history.length > 0 ) { 883 String lastHistory = history[history.length-1].trim(); 884 885 final int length = "Übergeben am [tick=".length(); 886 887 if( lastHistory.startsWith("Übergeben am [tick=") ) { 888 int endIndex = lastHistory.indexOf("] an ",length); 889 if( endIndex > -1 ) { 890 try { 891 int date = Integer.parseInt( 892 lastHistory.substring( 893 length, 894 endIndex 895 ) 896 ); 897 898 if( ContextMap.getContext().get(ContextCommon.class).getTick() - date < 49 ) { 899 return; 900 } 901 } 902 catch( StringIndexOutOfBoundsException e ) { 903 LOG.warn("[Ships.generateLoot] Fehler beim Parsen des Schiffshistoryeintrags '"+lastHistory+"' - "+length+", "+endIndex); 904 } 905 } 906 else { 907 LOG.warn("[Ships.generateLoot] Fehler beim Parsen des Schiffshistoryeintrags '"+lastHistory+"'"); 908 } 909 } 910 } 911 912 // Moeglichen Loot zusammensuchen 913 List<ShipLoot> loot = new ArrayList<ShipLoot>(); 914 int maxchance = 0; 915 916 List lootList = db.createQuery("from ShipLoot where owner=? and shiptype in (?,?) and targetuser in (0,?) and totalmax!=0") 917 .setInteger(0, this.owner.getID()) 918 .setInteger(1, this.type) 919 .setInteger(2, -this.id) 920 .setInteger(3, destroyer) 921 .list(); 922 923 for( Iterator iter=lootList.iterator(); iter.hasNext(); ) { 924 ShipLoot lootEntry = (ShipLoot)iter.next(); 925 926 maxchance += lootEntry.getChance(); 927 loot.add(lootEntry); 928 } 929 930 if( loot.size() == 0 ) { 931 return; 932 } 933 934 // Und nun den Loot generieren 935 Cargo cargo = new Cargo(); 936 937 for( int i=0; i <= Configuration.getIntSetting("CONFIG_TRUEMER_MAXITEMS"); i++ ) { 938 rnd = RandomUtils.nextInt(maxchance+1); 939 int currentchance = 0; 940 for( int j=0; j < loot.size(); j++ ) { 941 ShipLoot aloot = loot.get(j); 942 if( aloot.getChance() + currentchance > rnd ) { 943 if( aloot.getTotalMax() > 0 ) { 944 aloot.setTotalMax(aloot.getTotalMax()-1); 945 } 946 cargo.addResource( Resources.fromString(aloot.getResource()), aloot.getCount() ); 947 break; 948 } 949 950 currentchance += aloot.getChance(); 951 } 952 953 rnd = RandomUtils.nextInt(101); 954 955 // Gibts nichts mehr zu looten? 956 if( rnd > shiptype.getChance4Loot() ) { 957 break; 958 } 959 } 960 961 // Truemmer-Schiff hinzufuegen und entfernen-Task setzen 962 Ship truemmer = new Ship(); 963 truemmer.setOwner((User)db.get(User.class, -1)); 964 truemmer.setName("Trümmerteile"); 965 truemmer.setType(Configuration.getIntSetting("CONFIG_TRUEMMER")); 966 truemmer.setCargo(cargo); 967 truemmer.setX(this.x); 968 truemmer.setY(this.y); 969 truemmer.setSystem(this.system); 970 truemmer.setHull(Configuration.getIntSetting("CONFIG_TRUEMMER_HUELLE")); 971 truemmer.setVisibility(destroyer); 972 int id = (Integer)db.save(truemmer); 973 974 Taskmanager.getInstance().addTask(Taskmanager.Types.SHIP_DESTROY_COUNTDOWN, 21, Integer.toString(id), "", "" ); 975 976 return; 977 } 978 979 /** 855 980 * Gibt die Liste aller Flags zurueck, ueber die der angegebene 856 981 * Schiffstyp verfuegt src/net/driftingsouls/ds2/server/ships/Ships.java
r4e34802 r3b415b5 32 32 import net.driftingsouls.ds2.server.cargo.Cargo; 33 33 import net.driftingsouls.ds2.server.cargo.ItemCargoEntry; 34 import net.driftingsouls.ds2.server.cargo.Resources;35 34 import net.driftingsouls.ds2.server.cargo.modules.Module; 36 35 import net.driftingsouls.ds2.server.cargo.modules.Modules; … … 43 42 import net.driftingsouls.ds2.server.framework.CacheMap; 44 43 import net.driftingsouls.ds2.server.framework.Common; 45 import net.driftingsouls.ds2.server.framework.Configuration;46 44 import net.driftingsouls.ds2.server.framework.Context; 47 45 import net.driftingsouls.ds2.server.framework.ContextLocalMessage; … … 1934 1932 */ 1935 1933 public static void generateLoot( int shipid, int destroyer ) { 1936 Database db = ContextMap.getContext().getDatabase(); 1937 1938 SQLResultRow ship = db.first("SELECT id,owner,x,y,system,history,type,status FROM ships WHERE id>0 AND id="+shipid); 1939 1940 ShipTypeData shiptype = Ship.getShipType( ship ); 1941 1942 int rnd = RandomUtils.nextInt(101); 1943 1944 // Gibts was zu looten? 1945 if( rnd > shiptype.getChance4Loot() ) { 1946 return; 1947 } 1948 1949 // History analysieren (Alle Schiffe die erst kuerzlich uebergeben wurden, haben kein Loot) 1950 // TODO: Das funktioniert im Moment nur, weil im Log nur Uebergaben stehen... 1951 String[] history = StringUtils.split(ship.getString("history").trim(), '\n'); 1952 if( history.length > 0 ) { 1953 String lastHistory = history[history.length-1].trim(); 1954 1955 final int length = "Übergeben am [tick=".length(); 1956 1957 if( lastHistory.startsWith("Übergeben am [tick=") ) { 1958 int endIndex = lastHistory.indexOf("] an ",length); 1959 if( endIndex > -1 ) { 1960 try { 1961 int date = Integer.parseInt( 1962 lastHistory.substring( 1963 length, 1964 endIndex 1965 ) 1966 ); 1967 1968 if( ContextMap.getContext().get(ContextCommon.class).getTick() - date < 49 ) { 1969 return; 1970 } 1971 } 1972 catch( StringIndexOutOfBoundsException e ) { 1973 LOG.warn("[Ships.generateLoot] Fehler beim Parsen des Schiffshistoryeintrags '"+lastHistory+"' - "+length+", "+endIndex); 1974 } 1975 } 1976 else { 1977 LOG.warn("[Ships.generateLoot] Fehler beim Parsen des Schiffshistoryeintrags '"+lastHistory+"'"); 1978 } 1979 } 1980 } 1981 1982 // Moeglichen Loot zusammensuchen 1983 List<SQLResultRow> loot = new ArrayList<SQLResultRow>(); 1984 int maxchance = 0; 1985 1986 SQLQuery lootHandle = db.query("SELECT id,chance,resource,count,totalmax " , 1987 "FROM ship_loot " , 1988 "WHERE owner='",ship.getInt("owner"),"' AND shiptype IN ('",ship.getInt("type"),"','-",ship.getInt("id"),"') AND targetuser IN ('0','",destroyer,"') AND totalmax!=0"); 1989 1990 while( lootHandle.next() ) { 1991 maxchance += lootHandle.getInt("chance"); 1992 loot.add(lootHandle.getRow()); 1993 } 1994 lootHandle.free(); 1995 1996 if( loot.size() == 0 ) { 1997 return; 1998 } 1999 2000 // Und nun den Loot generieren 2001 Cargo cargo = new Cargo(); 2002 2003 for( int i=0; i <= Configuration.getIntSetting("CONFIG_TRUEMER_MAXITEMS"); i++ ) { 2004 rnd = RandomUtils.nextInt(maxchance+1); 2005 int currentchance = 0; 2006 for( int j=0; j < loot.size(); j++ ) { 2007 SQLResultRow aloot = loot.get(j); 2008 if( aloot.getInt("chance") + currentchance > rnd ) { 2009 if( aloot.getInt("totalmax") > 0 ) { 2010 aloot.put("totalmax", aloot.getInt("totalmax")-1); 2011 2012 db.update("UPDATE ship_loot SET totalmax='",aloot.getInt("totalmax"),"' WHERE id='",aloot.getInt("id"),"'"); 2013 } 2014 cargo.addResource( Resources.fromString(aloot.getString("resource")), aloot.getLong("count") ); 2015 break; 2016 } 2017 2018 currentchance += aloot.getInt("chance"); 2019 } 2020 2021 rnd = RandomUtils.nextInt(101); 2022 2023 // Gibts nichts mehr zu looten? 2024 if( rnd > shiptype.getChance4Loot() ) { 2025 break; 2026 } 2027 } 2028 2029 // Truemmer-Schiff hinzufuegen und entfernen-Task setzen 2030 db.update("INSERT INTO ships ", 2031 "(owner,name,type,cargo,x,y,system,hull,visibility) ", 2032 "VALUES ", 2033 "('-1','Trümmerteile',",Configuration.getIntSetting("CONFIG_TRUEMMER"),",'",cargo.save(),"','", 2034 ship.getInt("x"),"','",ship.getInt("y"),"','",ship.getInt("system"),"','", 2035 Configuration.getIntSetting("CONFIG_TRUEMMER_HUELLE"),"','",destroyer,"')"); 2036 2037 Taskmanager.getInstance().addTask(Taskmanager.Types.SHIP_DESTROY_COUNTDOWN, 21, Integer.toString(db.insertID()), "", "" ); 2038 2039 return; 1934 org.hibernate.Session db = ContextMap.getContext().getDB(); 1935 Ship ship = (Ship)db.get(Ship.class, shipid); 1936 1937 ship.generateLoot(destroyer); 2040 1938 } 2041 1939
