Changeset 3b415b5340c87a9f5080e64474a65dc3b6011b1a

Show
Ignore:
Timestamp:
06/10/07 17:24:25 (1 year ago)
Author:
Christopher Jung <bktheg@web.de>
git-committer:
Christopher Jung <bktheg@web.de> 1181489065 +0200
git-parent:

[4e3480232150fec3f0d7484fb98198030b98b813]

git-author:
Christopher Jung <bktheg@web.de> 1181489065 +0200
Message:

generateLoot nach Ship verschoben

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • src/net/driftingsouls/ds2/server/ships/Ship.java

    r4e34802 r3b415b5  
    2121import java.sql.Blob; 
    2222import java.util.ArrayList; 
     23import java.util.Iterator; 
    2324import java.util.List; 
    2425 
     
    2627import javax.persistence.Entity; 
    2728import javax.persistence.FetchType; 
     29import javax.persistence.GeneratedValue; 
    2830import javax.persistence.Id; 
    2931import javax.persistence.JoinColumn; 
     
    3133import javax.persistence.Table; 
    3234 
     35import net.driftingsouls.ds2.server.ContextCommon; 
    3336import net.driftingsouls.ds2.server.Offizier; 
    3437import net.driftingsouls.ds2.server.cargo.Cargo; 
     
    3740import net.driftingsouls.ds2.server.entities.User; 
    3841import net.driftingsouls.ds2.server.framework.Common; 
     42import net.driftingsouls.ds2.server.framework.Configuration; 
    3943import net.driftingsouls.ds2.server.framework.ContextMap; 
    4044import net.driftingsouls.ds2.server.framework.Loggable; 
    4145import net.driftingsouls.ds2.server.framework.db.Database; 
     46import net.driftingsouls.ds2.server.framework.db.SQLQuery; 
    4247import net.driftingsouls.ds2.server.framework.db.SQLResultRow; 
     48import net.driftingsouls.ds2.server.tasks.Taskmanager; 
    4349 
    4450import org.apache.commons.lang.StringUtils; 
     51import org.apache.commons.lang.math.RandomUtils; 
    4552import org.hibernate.annotations.Generated; 
    4653import org.hibernate.annotations.GenerationTime; 
     
    5562@Table(name="ships") 
    5663public class Ship implements Loggable { 
    57         @Id 
    58         @Generated(GenerationTime.INSERT) 
     64        @Id @GeneratedValue 
    5965        private int id; 
    6066        @ManyToOne(fetch=FetchType.LAZY) 
     
    99105        @Column(name="`lock`") 
    100106        private String lock; 
    101         private Short visibility; 
     107        private Integer visibility; 
    102108        private String onmove; 
    103109        private Byte respawn; 
     
    722728         * @return Die Sichtbarkeitsdaten des Schiffes 
    723729         */ 
    724         public Short getVisibility() { 
     730        public Integer getVisibility() { 
    725731                return visibility; 
    726732        } 
     
    730736         * @param visibility Die neuen Sichtbarkeitsdaten 
    731737         */ 
    732         public void setVisibility(Short visibility) { 
     738        public void setVisibility(Integer visibility) { 
    733739                this.visibility = visibility; 
    734740        } 
     
    853859         
    854860        /** 
     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 = "&Uuml;bergeben am [tick=".length(); 
     886                         
     887                        if( lastHistory.startsWith("&Uuml;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&uuml;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        /** 
    855980         * Gibt die Liste aller Flags zurueck, ueber die der angegebene 
    856981         * Schiffstyp verfuegt 
  • src/net/driftingsouls/ds2/server/ships/Ships.java

    r4e34802 r3b415b5  
    3232import net.driftingsouls.ds2.server.cargo.Cargo; 
    3333import net.driftingsouls.ds2.server.cargo.ItemCargoEntry; 
    34 import net.driftingsouls.ds2.server.cargo.Resources; 
    3534import net.driftingsouls.ds2.server.cargo.modules.Module; 
    3635import net.driftingsouls.ds2.server.cargo.modules.Modules; 
     
    4342import net.driftingsouls.ds2.server.framework.CacheMap; 
    4443import net.driftingsouls.ds2.server.framework.Common; 
    45 import net.driftingsouls.ds2.server.framework.Configuration; 
    4644import net.driftingsouls.ds2.server.framework.Context; 
    4745import net.driftingsouls.ds2.server.framework.ContextLocalMessage; 
     
    19341932         */ 
    19351933        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 = "&Uuml;bergeben am [tick=".length(); 
    1956                          
    1957                         if( lastHistory.startsWith("&Uuml;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&uuml;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); 
    20401938        } 
    20411939