Converting a VBscript plugin

Posted by Rakon on Thu 21 Dec 2006 08:27 AM — 8 posts, 37,837 views.

USA #0
Greetings. Currently Im running a MUD 'system' in python, for an IRE game. Though I really like python and am more famlier with its language, I've been trying to get a VBscript plugin written for python, integrated into my script, using python.

I don't really know how to go about doing the correct functions in Python, though it works fine in the plugin.

The question is, anyone able to help me convert the following plugin into a Python state, or give me an idea on how to go about writing the functions for it to work??

The plugin: http://www.gammon.com.au/mushclient/plugins/Health_Bar.xml

Thanks
-Rakon
Australia Forum Administrator #1
I'm not sure what the problem is. The main difference would be simply syntactic differences, which you should be able to do easily enough if you are familiar with Python (which I am not). Can you post an example of what you are attempting, and in what way it doesn't work?
USA #2
Hello again.

The issue is, while I have converted the plugin to python, using the syntax's I believe to be correct in what I desire for it to do. (Behave the same way as ked's plugin, in VB).

It(his) performs the basic math, and allocates a number in assigning how many 'g's to place like a bar. It is able to have half the 'g's of ten, one colour, and the rest another, depending on the math's count.

When I do this SAME thing in python, it doesn't change part of the total of 'g's to another colour, it either has them all one colour, all the other defined colour, or not showing at all (black).

Ked's(VBscript)

sub DoGauge (sPrompt, iCurrent, iMax, sGoodColour, sBadColour)
dim pc, count

'
'  Do prompt in black Arial
'
  InfoColour "black"
  InfoFont "Arial", 10, 0
  Info sPrompt

'
'  Use Webdings for gauge (black square)
'

  InfoFont "Webdings", 10, 0

  pc = CInt ((CInt (iCurrent) / CInt (iMax)) * 10)

'
'  Below 20% warn by using different colour
'

  if pc < 3 then
    InfoColour sBadColour
  else
    InfoColour sGoodColour
  end if 
 
'
'  Draw active part of gauge
'
  if pc < 1 then
	InfoColour "black"
  end if 
  for count = 0 to pc
    Info "g"
  next  

'
'  Draw rest of gauge in grey (ie. unfilled bit)
'

  InfoColour "dimgray"
  while count <= 10
    count = count + 1
    Info "g"
  wend

end sub


Mine(Python):

def DoGauge(Prompt, Current, Max, HColour, LColour):
	world.InfoColour("Orange")
	world.InfoFont("Arial", 10, 0)
	world.Info(Prompt)
	
	world.InfoFont("Webdings", 10, 0)
	count = int(Current) / int(Max) * 12

	

	if count < 4:
		world.InfoColour(LColour)
	else:
		world.InfoColour(HColour)
	

		
	for i in range(0,count):
		world.Info("g")
		
	while count <= 11:
		world.InfoColour("dimgrey")
		count += 1
		world.Info("g")		


As you can see, it essentially the same, just different syntax's. What I am confused to is WHY my version only colours them all, or not at all. While HIS will change a few based off the math. Any ideas??
USA #3
Well, yours multiplies by 12 instead of his 10; is that intentional? You also use "dimgrey" instead of his "dimgray". Finally, you do not convert the result of the division to an integer; I don't know if Python distinguishes between a number and an integer. It's possible that the int function returns the floored version, not an actual integer that behaves like integers with respect to division etc. I'd apply int to the result of the division and see what happens.
Australia Forum Administrator #4
Quote:

You also use "dimgrey" instead of his "dimgray".


InfoColour does nothing if not given a valid colour name, so this may well be your problem.
USA #5
The InfoColour is still not the problem, though thanks for pointing it out. Simple logic 'spelling > me'.

The '12' I have for the count is just because I want two extra blocks, 'g's to show up.

However even now, when I have only changed the value, 'dimgrey' to 'dimgray', I'm still having the same issue.

**EDIT** RTFM before commenting something doesn't work.

In python:

count = round(round(Current) / round(Max) * 12)


Is the equiv of the VB:

pc = CInt ((CInt (iCurrent) / CInt (iMax)) * 12)


The CInt function rounds to a nearest 'even' number, where as Int would just change the value to be an integer.
Thanks for the help.
--Rakon
Amended on Sat 06 Jan 2007 12:47 PM by Rakon
USA #6
Actually I don't think that round and cint do the same thing. cint truncates a number, that is, takes the floor. 3.9 becomes 3. round, well, rounds the number. By 'even' did you mean whole or really even as in the opposite of odd?

In any case I'm glad that was the problem. You were converting only part of the expression, and as I said in my earlier post you really want to do the whole thing to get the values you need.
#7
Python has an integer division operator "/" and a float division operator "/". Do not confuse the two.


    count = 12 * int(Current) / int(Max)


Should achieve what you are looking for... though personally I'd roll with:


    count = int(12.0 * Current / Max)


As I recollect it, the / (and * &c..) are left-associative and the result is of the same type as the left operand (the right operand is cast to the correct type before the operator function is called). So in this case int(Current) / int(Max) gives you 0 which is then multiplied by 12 and amply explains why the bar is completely grey.