Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating Random variable for access throughout code?

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating Random variable for access throughout code?

    Hi all, i am trying to create a couple of random variables that compute a value once everytime the code is run, but then i can use this value at various points through the code. Its for a game called Il-2 1946.

    here is the full code

    // Decompiled by DJ v3.10.10.93 Copyright 2007 Atanas Neshkov  Date: 15/06/2019 23:00:16
    // Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
    // Decompiler options: packimports(3) 
    // Source File Name:   RocketX4H.java
     
    package com.maddox.il2.objects.weapons;
     
    import com.maddox.JGP.*;
    import com.maddox.il2.ai.*;
    import com.maddox.il2.ai.air.NearestTargets;
    import com.maddox.il2.engine.*;
    import com.maddox.il2.fm.*;
    import com.maddox.il2.game.*;
    import com.maddox.il2.objects.air.Aircraft;
    import com.maddox.rts.*;
    import java.io.IOException;
    import java.io.PrintStream;
    import java.util.Random;
     
    // Referenced classes of package com.maddox.il2.objects.weapons:
    //            Rocket, Ballistics, RocketX4
     
    public class RocketX4H extends Rocket
    {
        class Master extends ActorNet
            implements NetUpdate
        {
     
            public void msgNetNewChannel(NetChannel netchannel)
            {
                if(!Actor.isValid(actor()))
                    return;
                if(netchannel.isMirrored(this))
                    return;
                try
                {
                    if(netchannel.userState == 0)
                    {
                        NetMsgSpawn netmsgspawn = actor().netReplicate(netchannel);
                        if(netmsgspawn != null)
                        {
                            postTo(netchannel, netmsgspawn);
                            actor().netFirstUpdate(netchannel);
                        }
                    }
                }
                catch(Exception exception)
                {
                    NetObj.printDebug(exception);
                }
            }
     
            public boolean netInput(NetMsgInput netmsginput)
                throws IOException
            {
                return false;
            }
     
            public void netUpdate()
            {
                try
                {
                    out.unLockAndClear();
                    pos.getAbs(RocketX4H.p, RocketX4H.or);
                    out.writeFloat((float)RocketX4H.p.x);
                    out.writeFloat((float)RocketX4H.p.y);
                    out.writeFloat((float)RocketX4H.p.z);
                    RocketX4H.or.wrap();
                    int i = (int)((RocketX4H.or.getYaw() * 32000F) / 180F);
                    int j = (int)((RocketX4H.or.tangage() * 32000F) / 90F);
                    out.writeShort(i);
                    out.writeShort(j);
                    post(Time.current(), out);
                }
                catch(Exception exception)
                {
                    NetObj.printDebug(exception);
                }
            }
     
            NetMsgFiltered out;
     
            public Master(Actor actor)
            {
                super(actor);
                out = new NetMsgFiltered();
            }
        }
     
        class Mirror extends ActorNet
        {
     
            public void msgNetNewChannel(NetChannel netchannel)
            {
                if(!Actor.isValid(actor()))
                    return;
                if(netchannel.isMirrored(this))
                    return;
                try
                {
                    if(netchannel.userState == 0)
                    {
                        NetMsgSpawn netmsgspawn = actor().netReplicate(netchannel);
                        if(netmsgspawn != null)
                        {
                            postTo(netchannel, netmsgspawn);
                            actor().netFirstUpdate(netchannel);
                        }
                    }
                }
                catch(Exception exception)
                {
                    NetObj.printDebug(exception);
                }
            }
     
            public boolean netInput(NetMsgInput netmsginput)
                throws IOException
            {
                if(netmsginput.isGuaranted())
                    return false;
                if(isMirrored())
                {
                    out.unLockAndSet(netmsginput, 0);
                    postReal(Message.currentTime(true), out);
                }
                RocketX4H.p.x = netmsginput.readFloat();
                RocketX4H.p.y = netmsginput.readFloat();
                RocketX4H.p.z = netmsginput.readFloat();
                int i = netmsginput.readShort();
                int j = netmsginput.readShort();
                float f = -(((float)i * 180F) / 32000F);
                float f1 = ((float)j * 90F) / 32000F;
                RocketX4H.or.set(f, f1, 0.0F);
                pos.setAbs(RocketX4H.p, RocketX4H.or);
                return true;
            }
     
            NetMsgFiltered out;
     
            public Mirror(Actor actor, NetChannel netchannel, int i)
            {
                super(actor, netchannel, i);
                out = new NetMsgFiltered();
            }
        }
     
        static class SPAWN
            implements NetSpawn
        {
     
            public void netSpawn(int i, NetMsgInput netmsginput)
            {
                NetObj netobj = netmsginput.readNetObj();
                if(netobj == null)
                    return;
                try
                {
                    Actor actor = (Actor)netobj.superObj();
                    Point3d point3d = new Point3d(netmsginput.readFloat(), netmsginput.readFloat(), netmsginput.readFloat());
                    Orient orient = new Orient(netmsginput.readFloat(), netmsginput.readFloat(), 0.0F);
                    float f = netmsginput.readFloat();
                    new RocketX4(actor, netmsginput.channel(), i, point3d, orient, f);
                }
                catch(Exception exception)
                {
                    System.out.println(exception.getMessage());
                    exception.printStackTrace();
                }
            }
     
            SPAWN()
            {
            }
        }
     
     
        public boolean interpolateStep()
        {
            float f = Time.tickLenFs();
            float f1 = (float)getSpeed(null);
            pos.getAbs(p, or);
            v.set(1.0D, 0.0D, 0.0D);
            or.transform(v);
            v.scale(f1);
            setSpeed(v);
            float f2 = 0.0F;
            if(tStart + 1500L > Time.current())
                v.z -= 10D;
            else
            if(tStart + timeFire > Time.current())
                f2 = (P * (350F - f1)) / 350F;
            HUD.training("!!" + f2 + " # " + getSpeed(null));
            Ballistics.update(this, M, S, f2, false);
            p.x += v.x * (double)f;
            p.y += v.y * (double)f;
            p.z += v.z * (double)f;
            Random random = new Random();
            int i = random.nextInt(20);
            if(isNet() && isNetMirror())
            {
                pos.setAbs(p, or);
                return false;
            }
            if(Time.current() > tStart + 350L)
                if(!Actor.isValid(victim))
                {
                    victim = NearestTargets.getEnemy(8, -1, p, 750D, getOwner().getArmy());
                } else
                {
                    victim.pos.getAbs(pT);
                    pT.sub(p);
                    or.transformInv(pT);
                    if(pT.x > 0.0D && i <= 6)
                    {
                        if(pT.y > 0.10000000000000001D)
                            deltaAzimuth = -1F;
                        if(pT.y < -0.10000000000000001D)
                            deltaAzimuth = 1.0F;
                        if(pT.z < -0.10000000000000001D)
                            deltaTangage = -1F;
                        if(pT.z > 0.10000000000000001D)
                            deltaTangage = 1.0F;
                        or.increment(10F * f * deltaAzimuth, 10F * f * deltaTangage, 0.0F);
                        deltaAzimuth = deltaTangage = 0.0F;
                    } else
                    if(p.distance(victim.pos.getAbsPoint()) > 30D || i > 6)
                    {
                        victim = null;
                    } else
                    {
                        pos.setAbs(p, or);
                        doExplosionAir();
                        postDestroy();
                        collide(false);
                        drawing(false);
                        return false;
                    }
                }
            pos.setAbs(p, or);
            return false;
        }
     
        public RocketX4H()
        {
           Random random = new Random();
            float f = 17F + random.nextFloat() * (21F - 17F);
            float f1 = 4900F + random.nextFloat() * (5500F -  4900F);
            victim = null;
            Property.set(getClass(), "timeLife", f);
            Property.set(getClass(), "timeFire", 17F);
            Property.set(getClass(), "force", f1);
            fm = null;
            tStart = 0L;
            prevd = 1000F;
            deltaAzimuth = 0.0F;
            deltaTangage = 0.0F;
            victim = null;
        }
     
        public RocketX4H(Actor actor, NetChannel netchannel, int i, Point3d point3d, Orient orient, float f)
        {
            Property.set(getClass(), "timeLife", f);
            Property.set(getClass(), "timeFire", 17F);
            Property.set(getClass(), "force", 5300F);
            fm = null;
            victim = null;
            tStart = 0L;
            prevd = 1000F;
            net = new Mirror(this, netchannel, i);
            pos.setAbs(point3d, orient);
            pos.reset();
            pos.setBase(actor, null, true);
            doStart(-1F);
            v.set(1.0D, 0.0D, 0.0D);
            orient.transform(v);
            v.scale(f);
            setSpeed(v);
            collide(false);
        }
     
        public void start(float f, int i)
        {
            Actor actor = pos.base();
            if(Actor.isValid(actor) && (actor instanceof Aircraft))
            {
                if(actor.isNetMirror())
                {
                    destroy();
                    return;
                }
                net = new Master(this);
            }
            doStart(f);
        }
     
        private void doStart(float f)
        {
            super.start(-1F, 0);
            fm = ((Aircraft)getOwner()).FM;
            or.set(fm.Or);
            or.setYPR(or.getYaw(), or.getPitch() - 5F, 0.0F);
            pos.setAbs(or);
            speed.set(fm.Vwld);
            tStart = Time.current();
            if(Config.isUSE_RENDER())
            {
                fl1 = Eff3DActor.New(this, findHook("_NavLightR"), null, 1.0F, "3DO/Effects/Fireworks/FlareRed.eff", -1F);
                fl2 = Eff3DActor.New(this, findHook("_NavLightG"), null, 1.0F, "3DO/Effects/Fireworks/FlareGreen.eff", -1F);
                flame.drawing(false);
            }
            if(getOwner() == World.getPlayerAircraft() && ((RealFlightModel)fm).isRealMode())
                victim = Selector.look(true, false, Main3D.cur3D()._camera3D[Main3D.cur3D().getRenderIndx()], World.getPlayerAircraft().getArmy(), -1, World.getPlayerAircraft(), false);
        }
     
        public void destroy()
        {
            if(isNet() && isNetMirror())
                doExplosionAir();
            if(Config.isUSE_RENDER())
            {
                Eff3DActor.finish(fl1);
                Eff3DActor.finish(fl2);
            }
            super.destroy();
        }
     
        protected void doExplosion(Actor actor, String s)
        {
            pos.getTime(Time.current(), p);
            MsgExplosion.send(actor, s, p, getOwner(), 45F, 1.0F, 1, 400F);
            super.doExplosion(actor, s);
        }
     
        protected void doExplosionAir()
        {
            pos.getTime(Time.current(), p);
            MsgExplosion.send(null, null, p, getOwner(), 45F, 1.0F, 1, 400F);
            super.doExplosionAir();
        }
     
        public NetMsgSpawn netReplicate(NetChannel netchannel)
            throws IOException
        {
            NetMsgSpawn netmsgspawn = super.netReplicate(netchannel);
            netmsgspawn.writeNetObj(getOwner().net);
            Point3d point3d = pos.getAbsPoint();
            netmsgspawn.writeFloat((float)point3d.x);
            netmsgspawn.writeFloat((float)point3d.y);
            netmsgspawn.writeFloat((float)point3d.z);
            Orient orient = pos.getAbsOrient();
            netmsgspawn.writeFloat(orient.azimut());
            netmsgspawn.writeFloat(orient.tangage());
            float f = (float)getSpeed(null);
            netmsgspawn.writeFloat(f);
            return netmsgspawn;
        }
     
        protected void init(float f, float f1, float f2, float f3, float f4, float f5)
        {
            if(Actor.isValid(getOwner()) && World.getPlayerAircraft() == getOwner())
                setName("_rocket_");
            super.getSpeed(speed);
            if(World.cur().diffCur.Wind_N_Turbulence)
            {
                Point3d point3d = new Point3d();
                Vector3d vector3d = new Vector3d();
                pos.getAbs(point3d);
                World.wind().getVectorWeapon(point3d, vector3d);
                speed.add(-vector3d.x, -vector3d.y, 0.0D);
            }
            S = (float)((3.1415926535897931D * (double)f * (double)f) / 4D);
            M = f1;
            if(f3 > 0.0F)
                DM = (f1 - f2) / (f3 / Time.tickConstLenFs());
            else
                DM = 0.0F;
            P = f4;
            timeFire = (long)((double)(f3 * 1000F) + 0.5D);
            timeLife = (long)((double)(f5 * 1000F) + 0.5D);
        }
     
     
     
        private float S;
        private float DM;
        private float P;
        private FlightModel fm;
        private Eff3DActor fl1;
        private Eff3DActor fl2;
        private static Orient or = new Orient();
        private static Point3d p = new Point3d();
        private static Point3d pT = new Point3d();
        private static Vector3d v = new Vector3d();
        private long tStart;
        private float prevd;
        private float deltaAzimuth;
        private float deltaTangage;
        private Actor victim;
     
     
     
        static 
        {
            Class class1 = com.maddox.il2.objects.weapons.RocketX4H.class;
            Spawn.add(class1, new SPAWN());
            Property.set(class1, "mesh", "3do/arms/X-4/mono.sim");
            Property.set(class1, "sprite", "3DO/Effects/Tracers/GuidedRocket/Black.eff");
            Property.set(class1, "flame", "3do/effects/rocket/mono.sim");
            Property.set(class1, "smoke", "3DO/Effects/Tracers/GuidedRocket/White.eff");
            Property.set(class1, "emitColor", new Color3f(1.0F, 1.0F, 0.5F));
            Property.set(class1, "emitLen", 50F);
            Property.set(class1, "emitMax", 1.0F);
            Property.set(class1, "sound", "weapon.rocket_132");
            Property.set(class1, "radius", 30F);
            Property.set(class1, "timeLife", 20F);
            Property.set(class1, "timeFire", 17F);
            Property.set(class1, "force", 5300F);
            Property.set(class1, "power", 1.0F);
            Property.set(class1, "powerType", 0);
            Property.set(class1, "kalibr", 0.22F);
            Property.set(class1, "massa", 40F);
            Property.set(class1, "massaEnd", 45F);
        }
     
     
    }


    Now this part of the code has entries for "time life" and "force" as here highlighted in red, used twice throughout the code and declared in the static properties at the end of the code. My attempt of declaring random variables f and f1 is also shown in green. the problem is that where i have declared the variables they are only recognised within that section of code, i cannot compile as it says the variable f and f1 are not declared in the second part of code. so looking for a way to get around this please. Thanks




    public RocketX4H()
    {
    Random random = new Random();
    float f = 17F + random.nextFloat() * (21F - 17F);
    float f1 = 4900F + random.nextFloat() * (5500F - 4900F);

    victim = null;
    Property.set(getClass(), "timeLife", f);
    Property.set(getClass(), "timeFire", 17F);
    Property.set(getClass(), "force", f1);
    fm = null;
    tStart = 0L;
    prevd = 1000F;
    deltaAzimuth = 0.0F;
    deltaTangage = 0.0F;
    victim = null;
    }

    public RocketX4H(Actor actor, NetChannel netchannel, int i, Point3d point3d, Orient orient, float f)
    {
    Property.set(getClass(), "timeLife", f);
    Property.set(getClass(), "timeFire", 17F);
    Property.set(getClass(), "force", 5300F);
    fm = null;
    victim = null;
    tStart = 0L;
    prevd = 1000F;
    net = new Mirror(this, netchannel, i);
    pos.setAbs(point3d, orient);
    pos.reset();
    pos.setBase(actor, null, true);
    doStart(-1F);
    v.set(1.0D, 0.0D, 0.0D);
    orient.transform(v);
    v.scale(f);
    setSpeed(v);
    collide(false);
    }

    static
    {
    Class class1 = com.maddox.il2.objects.weapons.RocketX4H.class;
    Spawn.add(class1, new SPAWN());
    Property.set(class1, "mesh", "3do/arms/X-4/mono.sim");
    Property.set(class1, "sprite", "3DO/Effects/Tracers/GuidedRocket/Black.eff");
    Property.set(class1, "flame", "3do/effects/rocket/mono.sim");
    Property.set(class1, "smoke", "3DO/Effects/Tracers/GuidedRocket/White.eff");
    Property.set(class1, "emitColor", new Color3f(1.0F, 1.0F, 0.5F));
    Property.set(class1, "emitLen", 50F);
    Property.set(class1, "emitMax", 1.0F);
    Property.set(class1, "sound", "weapon.rocket_132");
    Property.set(class1, "radius", 30F);
    Property.set(class1, "timeLife", 20F);
    Property.set(class1, "timeFire", 17F);
    Property.set(class1, "force", 5300F);
    Property.set(class1, "power", 1.0F);
    Property.set(class1, "powerType", 0);
    Property.set(class1, "kalibr", 0.22F);
    Property.set(class1, "massa", 40F);
    Property.set(class1, "massaEnd", 45F);
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,404
    Thanks
    53
    Thanked 2,399 Times in 2,352 Posts

    Default Re: Creating Random variable for access throughout code?

    Define the variables where they will be in scope (perhaps give them initial values), then assign them the desired values inside the constructor or method
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Creating a random access file
    By ineedahero in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 24th, 2013, 08:27 PM
  2. Random Access File
    By Wish.. in forum Java Theory & Questions
    Replies: 2
    Last Post: March 12th, 2012, 02:40 PM
  3. Creating a new class and dont know how to generate a random number math code
    By beatlebaby70 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 15th, 2011, 03:03 PM
  4. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 12th, 2009, 01:51 AM
  5. Random Access File
    By silver_unicorn in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: August 10th, 2009, 10:44 PM