set ns [new Simulator]
set topo [new Topography]
$topo load_flatgrid 600 600
$ns trace-all [open simple.tr w]
$ns namtrace-all-wireless [open out.nam w] 600 600 

proc finish {} {
        exec nam out.nam &
        puts "sai certo....."
        exit 0
}

create-god 15 

$ns node-config \
     -adhocRouting DSDV \
     -llType LL \
     -macType Mac/802_11 \
     -ifqLen 50 \
     -ifqType Queue/DropTail/PriQueue \
     -phyType Phy/WirelessPhy \
     -antType Antenna/OmniAntenna \
     -propType Propagation/TwoRayGround \
     -channel [new Channel/WirelessChannel] \
     -topoInstance $topo \
     -agentTrace ON \
     -routerTrace ON \
     -macTrace ON \
     -movementTrace OFF

#$ns node-config \
      –energyModel   EnergyModel \
      -initialEnergy 100.0 \
      -txPower    0.6 \
      -rxPower    0.2

#inicializacao
for {set i 0} {$i<15} {incr i} {
   set node_($i) [$ns node]
   set a [expr {int(rand()*600)}]
   set b [expr {int(rand()*600)}]
   $node_($i) set X_ $a
   $node_($i) set Y_ $b
   $node_($i) set Z_ 0.0 
   
   $ns initial_node_pos $node_($i) 20 
}

#rotas aleatorias
for {set i 0} {$i < 400} {incr i} {
   set a [expr {int(rand()*20)}] 
   set b [expr {int(rand()*15)}] 
   set c [expr {double(rand()*600)}] 
   set d [expr {double(rand()*600)}] 
   set e [expr {double(rand()*100+2)}] 
   $ns at $a "$node_($b) setdest $c $d $e"
   set a [expr {int(rand()*60)}] 
   set b [expr {int(rand()*15)}] 
   set c [expr {double(rand()*600)}] 
   set d [expr {double(rand()*600)}] 
   set e [expr {double(rand()*150+2)}] 
   $ns at $a "$node_($b) setdest $c $d $e"
   set a [expr {int(rand()*120+30)}] 
   set b [expr {int(rand()*15)}] 
   set c [expr {double(rand()*600)}] 
   set d [expr {double(rand()*600)}] 
   set e [expr {double(rand()*200+2)}]
   $ns at $a "$node_($b) setdest $c $d $e"
   set a [expr {int(rand()*150+30)}] 
   set b [expr {int(rand()*15)}] 
   set c [expr {double(rand()*600)}] 
   set d [expr {double(rand()*600)}] 
   set e [expr {double(rand()*300+30)}]
   $ns at $a "$node_($b) setdest $c $d $e"
} 

#comunicacao aleatoria
#proc random_tcp {} {
   for {set i 0} {$i < 30} {incr i} {
      set a [expr {int(rand()*15)}]
      set b [expr {int(rand()*15)}]  
      if {$a != $b} { 
         puts "$i"
         set tcp_($i) [new Agent/TCP]
         $tcp_($i) set class_ 2
         set sink_($i) [new Agent/TCPSink]
         $ns attach-agent $node_($a) $tcp_($i)
         $ns attach-agent $node_($b) $sink_($i)
         $ns connect $tcp_($i) $sink_($i)
         set ftp_($i) [new Application/FTP]
         $ftp_($i) attach-agent $tcp_($i)
         $ns at 3.0 "$ftp_($i) start"
      }
   }
#}

$ns at 100.0 "$ns nam-end-wireless 100.0"
$ns at 100.0 "finish"
$ns run
