(UI of the site is a bit pats: there is a non-obvious arrow at the bottom of the page to view older posts, of which there are many.)
Re: CoffeeScript for warzone scripting.
Posted: 05 Nov 2012, 21:18
by milo christiansen
Duha wrote:What is you first favorite?
Go. Go is by far the best programming language I have ever used, too bad its no good for this kind of thing.
For scripting I prefer JavaScript so long as it is not complicated, once you start getting complicated in JavaScript it becomes pretty hard to work with.
For least favorite launguages: (in least favorite first order)
Python
Forth
Most BASICs
lua
Java
C
C++
Most other languages are somewhere after lua/C. As far as embeded scripting launguages go there are not many (if any) good choices, JavaScript is about the best (as long as you dont try to be too OO)
Re: CoffeeScript for warzone scripting.
Posted: 09 Nov 2012, 01:24
by Duha
aubergine wrote:Duha,
I think it's worth adding tutorial anyway - at the end of the day, if people want to use coffeescript and that results in more AIs or warzone mods, it's good for the community despite the ugly syntax
#python(2.7)
import os
from glob import glob
PATH = 'g:/Games/Warzone 2100-3.1_rc3/mp/multiplay/skirmish/'
MOD_NAME = 'api'
SCRIPTPATH = os.path.dirname(__file__)
separator ='\\n\\'
dir = os.path.join(PATH, MOD_NAME)
if not os.path.exists(dir):
os.mkdir(dir)
files = glob('%s/*.coffee' % SCRIPTPATH)
file_names = {os.path.basename(file).replace('.coffee', ''):file for file in files}
def add_to_loader(load, name):
load.extend([
'try {',
" include('multiplay/skirmish/%s/%s.js');" % (MOD_NAME, name),
" api.CoffeeScript.eval(%sCoffee);" % name,
' } catch (error) {',
' debug("Fail to eval script <%s.coffee>: " + error);' % name,
'}', ''
])
def create_loader(groups):
start = [
'function %s() {}; // Main container' % MOD_NAME,
"include('multiplay/skirmish/coffee-script.js');",
'api.CoffeeScript = CoffeeScript;',
'this.CoffeeScript = null;',
'',
]
middle,end = [], []
names = file_names.keys()
for key in groups['start']:
if key in names:
add_to_loader(start, key)
names.remove(key)
for key in groups['end'][::-1]:
if key in names:
add_to_loader(end, key)
names.remove(key)
for name in names:
add_to_loader(middle, name)
with open(os.path.join(PATH, 'load_%s.js' % MOD_NAME), 'w') as f:
f.write('\n'.join(start + middle + end))
def write_files():
for name, file in file_names.items():
with open(os.path.join(PATH, MOD_NAME, '%s.js' % name), 'w') as f:
lines = open(file).readlines()
lines = [line.rstrip('\n\r').replace("'", "\\'") for line in lines]
start = "%sCoffee = '%s" % (name, separator)
end = "'"
new_lines = [start] + ['%s%s' % (line, separator) for line in lines if line] + [end]
res = '\n'.join(new_lines)
f.write(res)
# set order here
groups = {'start': ['inspector'],
'end': [] # last element of list will be loaded last!
}
create_loader(groups)
write_files()