set opt(chan)			Channel/UnderwaterChannel
set opt(prop)			Propagation/UnderwaterPropagation
set opt(netif)			Phy/UnderwaterPhy
set opt(mac)			Mac/UnderwaterMac/BroadcastMac
set opt(ifq)			Queue/DropTail
set opt(ll)				LL
set opt(energy)         EnergyModel
set opt(txpower)       2
set opt(rxpower)        0.75
set opt(initialenergy)  1000
set opt(idlepower)      0.01
set opt(ant)            Antenna/OmniAntenna  
set opt(filters)        GradientFilter   
set opt(max_pkts)		50000
set opt(interval_)	          1;# [lindex $argv 0] 
set opt(pkt_len)	          100;  #[lindex $argv 2] ;# pkt length of cbr
set opt(bit_rate)                         17.8e3 ;#[lindex $argv 1];#1.0e4    ;#bandwidth of the phy link
set opt(encoding_efficiency)          1
set opt(ifqlen)		              50	;# max packet in ifq
set opt(nn)	                		30
set opt(x)	                		1000	;# X dimension of the topography
set opt(y)	                        	1000  ;# Y dimension of the topography
set opt(z)                      		1000
set opt(seed)	                		0
set opt(stop)	                		100 ;#[lindex $argv 3] ;#150	;# simulation time
set opt(tr)	                		"dbr.tr"	;# trace file
set opt(nam)                            	"dbr.nam"  ;# nam file
set opt(datafile)	                    "dbr.data"
set opt(adhocRouting)                 	DBR
set start_time				1
if { $argc > 0 } {
set opt(adhocRouting) [lindex $argv 0]
  set opt(tr) [lindex $argv 0].tr
  set opt(nam)   [lindex $argv 0].nam
set opt(datafile) [lindex $argv 0].data
  set opt(stop) [lindex $argv 1]
set opt(initialenergy) [lindex $argv 2]
set opt(nn) [lindex $argv 3]
set opt(interval_) [lindex $argv 4]
}
# ==================================================================

LL set mindelay_		50us
LL set delay_			25us
Antenna/OmniAntenna set X_ 0
Antenna/OmniAntenna set Y_ 0
Antenna/OmniAntenna set Z_ 0.05
Antenna/OmniAntenna set Gt_ 1.0
Antenna/OmniAntenna set Gr_ 1.0

Mac/UnderwaterMac set bit_rate_  $opt(bit_rate)
Mac/UnderwaterMac set encoding_efficiency_  $opt(encoding_efficiency)
Node/MobileNode/UnderwaterSensorNode set position_update_interval_ 1.0

Phy/UnderwaterPhy set CPThresh_ 10 
Phy/UnderwaterPhy set CSThresh_ 0  
Phy/UnderwaterPhy set RXThresh_ 0   
Phy/UnderwaterPhy set Pt_ 0.2818
Phy/UnderwaterPhy set freq_ 35  ;#frequency range in khz 
Phy/UnderwaterPhy set K_ 2.0   ;#spherical spreading

# ==================================================================
# Main Program
# =================================================================

#
# Initialize Global Variables
set ns_ [new Simulator]
set topo  [new Topography]

$topo load_cubicgrid $opt(x) $opt(y) $opt(z)
set tracefd	[open $opt(tr) w]
$ns_ trace-all $tracefd
#set nf [open $opt(nam) w]
#$ns_ namtrace-all-wireless $nf $opt(x) $opt(y)
set total_number [expr $opt(nn)-1]
set god_ [create-god $opt(nn)]

set chan_1_ [new $opt(chan)]

$ns_ at 0.0 "$god_  set_filename $opt(datafile)"
global defaultRNG

$defaultRNG seed $opt(seed)

$ns_ node-config -adhocRouting $opt(adhocRouting)  \
		 -llType $opt(ll) \
		 -macType $opt(mac) \
		 -ifqType $opt(ifq) \
		 -ifqLen $opt(ifqlen) \
		 -antType $opt(ant) \
		 -propType $opt(prop) \
		 -phyType $opt(netif) \
		 -agentTrace ON \
         -routerTrace ON \
         -macTrace ON \
         -movementTrace OFF \
         -topoInstance $topo\
         -energyModel $opt(energy)\
         -txpower $opt(txpower)\
         -rxpower $opt(rxpower)\
         -initialEnergy $opt(initialenergy)\
         -idlePower $opt(idlepower)\
         -channel $chan_1_


set node_(0) [$ns_  node 0]
$node_(0) set sinkStatus_ 1
$node_(0) set passive 1
$god_ new_node $node_(0)
$node_(0) set X_   0
$node_(0) set Y_   0
$node_(0) set Z_   0
set a_(0) [new Agent/Null]
$ns_ attach-agent $node_(0) $a_(0)



set topoFile [open "topo_X.dat" r]
set i 1
while { [gets $topoFile line] >= 0 } {
	set valId [lindex [split $line " "] 0];
	
	# ignore comments
	if { [string index $valId 0] == "#" } {
		continue;
	}

	set valx [lindex [split $line " "] 0];
	set valy [lindex [split $line " "] 1];
	set valz [lindex [split $line " "] 2];

	set node_($i) [ $ns_  node $i]
	$node_($i) set sinkStatus_ 1
	$god_ new_node $node_($i)
	$node_($i) set X_  $valx
	$node_($i) set Y_   $valy
	$node_($i) set Z_   $valz
	set a_($i) [new Agent/Null]
	$ns_ attach-agent $node_($i) $a_($i)
#$ns_ at $opt(stop) "$node_($i) reset"
puts "+++++++++++++++++++++++++++++++++++++++++++++++++++++++$i"
	#set $i [expr $i+1]
	incr i
}

close $topoFile


set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  0
	$node_($i) set Y_   0
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)

set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
$ns_ at $start_time "$cbr_($i) start"
incr i
set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  250
	$node_($i) set Y_   0
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)

set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
incr start_time
$ns_ at $start_time "$cbr_($i) start"
incr i
set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  0
	$node_($i) set Y_   250
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)

set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
incr start_time
$ns_ at $start_time "$cbr_($i) start"
incr i
set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  250
	$node_($i) set Y_   250
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)

set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
incr start_time
$ns_ at $start_time "$cbr_($i) start"
incr i
set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  -250
	$node_($i) set Y_   0
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)


set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
incr start_time
$ns_ at $start_time "$cbr_($i) start"
incr i
set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  0
	$node_($i) set Y_   -250
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)

set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
incr start_time
$ns_ at $start_time "$cbr_($i) start"
incr i
set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  -250
	$node_($i) set Y_   250
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)

set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
incr start_time
$ns_ at $start_time "$cbr_($i) start"
incr i
set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  250
	$node_($i) set Y_   -250
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)

set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)
incr start_time
$ns_ at $start_time "$cbr_($i) start"
incr i

set node_($i) [$ns_  node $i]
$node_($i) set sinkStatus_ 1
$node_($i) set passive 1
$god_ new_node $node_($i)
	$node_($i) set X_  -250
	$node_($i) set Y_  -250
	$node_($i) set Z_  -500
set a_($i) [new Agent/Null]
$ns_ attach-agent $node_($i) $a_($i)
set a_($i) [new Agent/UDP]
$ns_ attach-agent $node_($i) $a_($i)
$ns_ connect $a_($i) $a_(0)




set cbr_($i) [new Application/Traffic/CBR]
$cbr_($i) set packetSize $opt(pkt_len)   ;#80
$cbr_($i) set interval_ $opt(interval_)
$cbr_($i) set random 0
$cbr_($i) set maxpkts_  $opt(max_pkts)

$cbr_($i) attach-agent $a_($i)

set node_size 6
incr start_time
$ns_ at $start_time "$cbr_($total_number) start"

puts "+++++++AFTER ANNOUNCE++++++++++++++"



for {set p 0} {$p<=$total_number} {incr p} {
$ns_ initial_node_pos $node_($p) $node_size
}
$ns_ at $opt(stop).001 "puts \"==$$$$$$$$$$==$total_number\""
$ns_ at $opt(stop).001 "puts \"+++++++++++++++++++++++++++++++++++++++++++++++++++++++$i\""
$ns_ at $opt(stop).001  "$god_ compute_energy"
$ns_ at $opt(stop).001  "$ns_ nam-end-wireless $opt(stop)"
$ns_ at $opt(stop).001 "puts \"NS EXISTING...\"; $ns_ halt"

 puts $tracefd "vectorbased"
 puts $tracefd "M 0.0 nn $opt(nn) x $opt(x) y $opt(y) z $opt(z)"
 puts $tracefd "M 0.0 prop $opt(prop) ant $opt(ant)"
 puts "starting Simulation..."
 $ns_ run
 
